From 4028a9169726ea78e25ffa8eb0a7f76b126e3864 Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Fri, 10 May 2019 16:00:37 -0400 Subject: [PATCH] 14287: Add "kind" key to controller responses. Arvados-DCO-1.1-Signed-off-by: Tom Clegg --- lib/controller/router/response.go | 12 ++++++++++++ lib/controller/router/router_test.go | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/lib/controller/router/response.go b/lib/controller/router/response.go index ddbeee666f..9a28911401 100644 --- a/lib/controller/router/response.go +++ b/lib/controller/router/response.go @@ -6,7 +6,9 @@ package router import ( "encoding/json" + "fmt" "net/http" + "regexp" "strings" "time" @@ -68,6 +70,7 @@ func (rtr *router) sendResponse(w http.ResponseWriter, resp interface{}, opts re tmp[k] = t.Format(rfc3339NanoFixed) } } + tmp["kind"] = kind(resp) json.NewEncoder(w).Encode(tmp) } @@ -78,3 +81,12 @@ func (rtr *router) sendError(w http.ResponseWriter, err error) { } httpserver.Error(w, err.Error(), code) } + +var mungeKind = regexp.MustCompile(`\..`) + +func kind(resp interface{}) string { + return mungeKind.ReplaceAllStringFunc(fmt.Sprintf("%T", resp), func(s string) string { + // "arvados.CollectionList" => "arvados#collectionList" + return "#" + strings.ToLower(s[1:]) + }) +} diff --git a/lib/controller/router/router_test.go b/lib/controller/router/router_test.go index b3d5c9add6..9664907dae 100644 --- a/lib/controller/router/router_test.go +++ b/lib/controller/router/router_test.go @@ -64,18 +64,24 @@ func (s *RouterSuite) TestCollectionParams(c *check.C) { _, rw, jresp := s.doRequest(c, token, "GET", `/arvados/v1/collections?include_trash=true`, nil, nil) c.Check(rw.Code, check.Equals, http.StatusOK) c.Check(jresp["items_available"], check.FitsTypeOf, float64(0)) + c.Check(jresp["kind"], check.Equals, "arvados#collectionList") + c.Check(jresp["items"].([]interface{})[0].(map[string]interface{})["kind"], check.Equals, "arvados#collection") _, rw, jresp = s.doRequest(c, token, "GET", `/arvados/v1/collections`, nil, bytes.NewBufferString(`{"include_trash":true}`)) c.Check(rw.Code, check.Equals, http.StatusOK) c.Check(jresp["items"], check.FitsTypeOf, []interface{}{}) + c.Check(jresp["kind"], check.Equals, "arvados#collectionList") + c.Check(jresp["items"].([]interface{})[0].(map[string]interface{})["kind"], check.Equals, "arvados#collection") _, rw, jresp = s.doRequest(c, token, "POST", `/arvados/v1/collections`, http.Header{"Content-Type": {"application/x-www-form-urlencoded"}}, bytes.NewBufferString(`ensure_unique_name=true`)) c.Check(rw.Code, check.Equals, http.StatusOK) c.Check(jresp["uuid"], check.FitsTypeOf, "") + c.Check(jresp["kind"], check.Equals, "arvados#collection") _, rw, jresp = s.doRequest(c, token, "POST", `/arvados/v1/collections?ensure_unique_name=true`, nil, nil) c.Check(rw.Code, check.Equals, http.StatusOK) c.Check(jresp["uuid"], check.FitsTypeOf, "") + c.Check(jresp["kind"], check.Equals, "arvados#collection") } func (s *RouterSuite) TestContainerList(c *check.C) { @@ -165,6 +171,7 @@ func (s *RouterSuite) TestSelectParam(c *check.C) { _, rw, resp := s.doRequest(c, token, "GET", "/arvados/v1/containers/"+uuid+"?select="+string(j), nil, nil) c.Check(rw.Code, check.Equals, http.StatusOK) + c.Check(resp["kind"], check.Equals, "arvados#container") c.Check(resp["uuid"], check.HasLen, 27) c.Check(resp["command"], check.HasLen, 2) c.Check(resp["mounts"], check.IsNil) -- 2.30.2