X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/0e077171d332434bf727a018691165a6b0621b68..4ad0281b1ec62d5f25103c27c639b84d58378eac:/lib/controller/router/response.go diff --git a/lib/controller/router/response.go b/lib/controller/router/response.go index e3ec37a6ea..309ed48f64 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" @@ -45,9 +45,11 @@ func applySelectParam(selectParam []string, orig map[string]interface{}) map[str selected[attr] = v } } - // Preserve "kind" even if not requested - if v, ok := orig["kind"]; ok { - selected["kind"] = 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 } @@ -72,6 +74,10 @@ func (rtr *router) sendResponse(w http.ResponseWriter, req *http.Request, resp i if respKind != "" { tmp["kind"] = respKind } + if included, ok := tmp["included"]; ok && included == nil { + tmp["included"] = make([]interface{}, 0) + } + defaultItemKind := "" if strings.HasSuffix(respKind, "List") { defaultItemKind = strings.TrimSuffix(respKind, "List") @@ -95,38 +101,18 @@ func (rtr *router) sendResponse(w http.ResponseWriter, req *http.Request, resp i } else if defaultItemKind != "" { item["kind"] = defaultItemKind } - items[i] = applySelectParam(opts.Select, item) + item = applySelectParam(opts.Select, item) + rtr.mungeItemFields(item) + items[i] = item } if opts.Count == "none" { delete(tmp, "items_available") } } else { tmp = applySelectParam(opts.Select, tmp) + rtr.mungeItemFields(tmp) } - // 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 - } - tmp[k] = t.Format(rfc3339NanoFixed) - } - } w.Header().Set("Content-Type", "application/json") enc := json.NewEncoder(w) enc.SetEscapeHTML(false) @@ -143,7 +129,15 @@ func (rtr *router) sendError(w http.ResponseWriter, err error) { var infixMap = map[string]interface{}{ "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 mungeKind = regexp.MustCompile(`\..`) @@ -158,3 +152,51 @@ func kind(resp interface{}) string { 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 + } + } + } +}