X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/dece16822124a86dd2f57b7d3b25ca1aa7d75600..9ffdf93f954bfe7bfe2269d361961d653caea146:/lib/controller/router/response.go diff --git a/lib/controller/router/response.go b/lib/controller/router/response.go index 4536380fd9..c0c599be8b 100644 --- a/lib/controller/router/response.go +++ b/lib/controller/router/response.go @@ -12,8 +12,8 @@ import ( "strings" "time" - "git.curoverse.com/arvados.git/sdk/go/arvados" - "git.curoverse.com/arvados.git/sdk/go/httpserver" + "git.arvados.org/arvados.git/sdk/go/arvados" + "git.arvados.org/arvados.git/sdk/go/httpserver" ) const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00" @@ -26,56 +26,107 @@ type responseOptions struct { func (rtr *router) responseOptions(opts interface{}) (responseOptions, error) { var rOpts responseOptions switch opts := opts.(type) { + case *arvados.CreateOptions: + rOpts.Select = opts.Select + case *arvados.UpdateOptions: + rOpts.Select = opts.Select case *arvados.GetOptions: rOpts.Select = opts.Select case *arvados.ListOptions: rOpts.Select = opts.Select rOpts.Count = opts.Count + case *arvados.GroupContentsOptions: + rOpts.Select = opts.Select + rOpts.Count = opts.Count } return rOpts, nil } -func (rtr *router) sendResponse(w http.ResponseWriter, resp interface{}, opts responseOptions) { +func applySelectParam(selectParam []string, orig map[string]interface{}) map[string]interface{} { + if len(selectParam) == 0 { + return orig + } + selected := map[string]interface{}{} + for _, attr := range selectParam { + if v, ok := orig[attr]; ok { + selected[attr] = v + } + } + // Some keys are always preserved, even if not requested + for _, k := range []string{"etag", "kind", "writable_by"} { + if v, ok := orig[k]; ok { + selected[k] = v + } + } + return selected +} + +func (rtr *router) sendResponse(w http.ResponseWriter, req *http.Request, resp interface{}, opts responseOptions) { var tmp map[string]interface{} + + if resp, ok := resp.(http.Handler); ok { + // resp knows how to write its own http response + // header and body. + resp.ServeHTTP(w, req) + return + } + err := rtr.transcode(resp, &tmp) if err != nil { rtr.sendError(w, err) return } - if len(opts.Select) > 0 { - selected := map[string]interface{}{} - for _, attr := range opts.Select { - if v, ok := tmp[attr]; ok { - selected[attr] = v - } - } - tmp = selected + + respKind := kind(resp) + if respKind != "" { + tmp["kind"] = respKind } - // Format non-nil timestamps as rfc3339NanoFixed (by default - // they will have been encoded to time.RFC3339Nano, which - // omits trailing zeroes). - for k, v := range tmp { - if !strings.HasSuffix(k, "_at") { - continue - } - switch tv := v.(type) { - case *time.Time: - if tv == nil { - break - } - tmp[k] = tv.Format(rfc3339NanoFixed) - case time.Time: - tmp[k] = tv.Format(rfc3339NanoFixed) - case string: - t, err := time.Parse(time.RFC3339Nano, tv) - if err != nil { - break + if included, ok := tmp["included"]; ok && included == nil { + tmp["included"] = make([]interface{}, 0) + } + defaultItemKind := "" + if strings.HasSuffix(respKind, "List") { + defaultItemKind = strings.TrimSuffix(respKind, "List") + } + + if _, isListResponse := tmp["items"].([]interface{}); isListResponse { + items, _ := tmp["items"].([]interface{}) + included, _ := tmp["included"].([]interface{}) + for _, slice := range [][]interface{}{items, included} { + for i, item := range slice { + // Fill in "kind" by inspecting UUID/PDH if + // possible; fall back on assuming each + // Items[] entry in an "arvados#fooList" + // response should have kind="arvados#foo". + item, _ := item.(map[string]interface{}) + infix := "" + if uuid, _ := item["uuid"].(string); len(uuid) == 27 { + infix = uuid[6:11] + } + if k := kind(infixMap[infix]); k != "" { + item["kind"] = k + } else if pdh, _ := item["portable_data_hash"].(string); pdh != "" { + item["kind"] = "arvados#collection" + } else if defaultItemKind != "" { + item["kind"] = defaultItemKind + } + item = applySelectParam(opts.Select, item) + rtr.mungeItemFields(item) + slice[i] = item } - tmp[k] = t.Format(rfc3339NanoFixed) } + if opts.Count == "none" { + delete(tmp, "items_available") + } + } else { + tmp = applySelectParam(opts.Select, tmp) + rtr.mungeItemFields(tmp) } - tmp["kind"] = kind(resp) - json.NewEncoder(w).Encode(tmp) + + w.Header().Set("Content-Type", "application/json") + enc := json.NewEncoder(w) + enc.SetEscapeHTML(false) + enc.Encode(tmp) } func (rtr *router) sendError(w http.ResponseWriter, err error) { @@ -86,11 +137,85 @@ func (rtr *router) sendError(w http.ResponseWriter, err error) { httpserver.Error(w, err.Error(), code) } +var infixMap = map[string]interface{}{ + "gj3su": arvados.APIClientAuthorization{}, + "4zz18": arvados.Collection{}, + "xvhdp": arvados.ContainerRequest{}, + "dz642": arvados.Container{}, + "j7d0g": arvados.Group{}, + "8i9sb": arvados.Job{}, + "d1hrv": arvados.PipelineInstance{}, + "p5p6p": arvados.PipelineTemplate{}, + "j58dm": arvados.Specimen{}, + "q1cn2": arvados.Trait{}, + "7fd4e": arvados.Workflow{}, +} + +var specialKindTransforms = map[string]string{ + "arvados.APIClientAuthorization": "arvados#apiClientAuthorization", + "arvados.APIClientAuthorizationList": "arvados#apiClientAuthorizationList", +} + var mungeKind = regexp.MustCompile(`\..`) func kind(resp interface{}) string { - return mungeKind.ReplaceAllStringFunc(fmt.Sprintf("%T", resp), func(s string) string { + t := fmt.Sprintf("%T", resp) + if !strings.HasPrefix(t, "arvados.") { + return "" + } + if k, ok := specialKindTransforms[t]; ok { + return k + } + return mungeKind.ReplaceAllStringFunc(t, func(s string) string { // "arvados.CollectionList" => "arvados#collectionList" return "#" + strings.ToLower(s[1:]) }) } + +func (rtr *router) mungeItemFields(tmp map[string]interface{}) { + for k, v := range tmp { + if strings.HasSuffix(k, "_at") { + // Format non-nil timestamps as + // rfc3339NanoFixed (otherwise they would use + // the default time encoding, which omits + // trailing zeroes). + switch tv := v.(type) { + case *time.Time: + if tv == nil || tv.IsZero() { + tmp[k] = nil + } else { + tmp[k] = tv.Format(rfc3339NanoFixed) + } + case time.Time: + if tv.IsZero() { + tmp[k] = nil + } else { + tmp[k] = tv.Format(rfc3339NanoFixed) + } + case string: + if tv == "" { + tmp[k] = nil + } else if t, err := time.Parse(time.RFC3339Nano, tv); err != nil { + // pass through an invalid time value (?) + } else if t.IsZero() { + tmp[k] = nil + } else { + tmp[k] = t.Format(rfc3339NanoFixed) + } + } + } + // Arvados API spec says when these fields are empty + // they appear in responses as null, rather than a + // zero value. + switch k { + case "output_uuid", "output_name", "log_uuid", "description", "requesting_container_uuid", "container_uuid": + if v == "" { + tmp[k] = nil + } + case "container_count_max": + if v == float64(0) { + tmp[k] = nil + } + } + } +}