01126bcb49a130440ec56bae76dbb78590dc9a3b
[arvados.git] / lib / controller / router / response.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package router
6
7 import (
8         "encoding/json"
9         "fmt"
10         "net/http"
11         "regexp"
12         "strings"
13         "time"
14
15         "git.arvados.org/arvados.git/sdk/go/arvados"
16         "git.arvados.org/arvados.git/sdk/go/httpserver"
17 )
18
19 const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
20
21 type responseOptions struct {
22         Select []string
23         Count  string
24 }
25
26 func (rtr *router) responseOptions(opts interface{}) (responseOptions, error) {
27         var rOpts responseOptions
28         switch opts := opts.(type) {
29         case *arvados.CreateOptions:
30                 rOpts.Select = opts.Select
31         case *arvados.UpdateOptions:
32                 rOpts.Select = opts.Select
33         case *arvados.GetOptions:
34                 rOpts.Select = opts.Select
35         case *arvados.ListOptions:
36                 rOpts.Select = opts.Select
37                 rOpts.Count = opts.Count
38         case *arvados.GroupContentsOptions:
39                 rOpts.Select = opts.Select
40                 rOpts.Count = opts.Count
41         }
42         return rOpts, nil
43 }
44
45 func applySelectParam(selectParam []string, orig map[string]interface{}) map[string]interface{} {
46         if len(selectParam) == 0 {
47                 return orig
48         }
49         selected := map[string]interface{}{}
50         for _, attr := range selectParam {
51                 if v, ok := orig[attr]; ok {
52                         selected[attr] = v
53                 }
54         }
55         // Some keys are always preserved, even if not requested
56         for _, k := range []string{"etag", "kind", "writable_by"} {
57                 if v, ok := orig[k]; ok {
58                         selected[k] = v
59                 }
60         }
61         return selected
62 }
63
64 func (rtr *router) sendResponse(w http.ResponseWriter, req *http.Request, resp interface{}, opts responseOptions) {
65         var tmp map[string]interface{}
66
67         if resp, ok := resp.(http.Handler); ok {
68                 // resp knows how to write its own http response
69                 // header and body.
70                 resp.ServeHTTP(w, req)
71                 return
72         }
73
74         err := rtr.transcode(resp, &tmp)
75         if err != nil {
76                 rtr.sendError(w, err)
77                 return
78         }
79
80         respKind := kind(resp)
81         if respKind != "" {
82                 tmp["kind"] = respKind
83         }
84         if included, ok := tmp["included"]; ok && included == nil {
85                 tmp["included"] = make([]interface{}, 0)
86         }
87         defaultItemKind := ""
88         if strings.HasSuffix(respKind, "List") {
89                 defaultItemKind = strings.TrimSuffix(respKind, "List")
90         }
91
92         if _, isListResponse := tmp["items"].([]interface{}); isListResponse {
93                 items, _ := tmp["items"].([]interface{})
94                 included, _ := tmp["included"].([]interface{})
95                 for _, slice := range [][]interface{}{items, included} {
96                         for i, item := range slice {
97                                 // Fill in "kind" by inspecting UUID/PDH if
98                                 // possible; fall back on assuming each
99                                 // Items[] entry in an "arvados#fooList"
100                                 // response should have kind="arvados#foo".
101                                 item, _ := item.(map[string]interface{})
102                                 infix := ""
103                                 if uuid, _ := item["uuid"].(string); len(uuid) == 27 {
104                                         infix = uuid[6:11]
105                                 }
106                                 if k := kind(infixMap[infix]); k != "" {
107                                         item["kind"] = k
108                                 } else if pdh, _ := item["portable_data_hash"].(string); pdh != "" {
109                                         item["kind"] = "arvados#collection"
110                                 } else if defaultItemKind != "" {
111                                         item["kind"] = defaultItemKind
112                                 }
113                                 item = applySelectParam(opts.Select, item)
114                                 rtr.mungeItemFields(item)
115                                 slice[i] = item
116                         }
117                 }
118                 if opts.Count == "none" {
119                         delete(tmp, "items_available")
120                 }
121         } else {
122                 tmp = applySelectParam(opts.Select, tmp)
123                 rtr.mungeItemFields(tmp)
124         }
125
126         w.Header().Set("Content-Type", "application/json")
127         enc := json.NewEncoder(w)
128         enc.SetEscapeHTML(false)
129         enc.Encode(tmp)
130 }
131
132 func (rtr *router) sendError(w http.ResponseWriter, err error) {
133         code := http.StatusInternalServerError
134         if err, ok := err.(interface{ HTTPStatus() int }); ok {
135                 code = err.HTTPStatus()
136         }
137         httpserver.Error(w, err.Error(), code)
138 }
139
140 var infixMap = map[string]interface{}{
141         "4zz18": arvados.Collection{},
142         "xvhdp": arvados.ContainerRequest{},
143         "dz642": arvados.Container{},
144         "j7d0g": arvados.Group{},
145         "8i9sb": arvados.Job{},
146         "d1hrv": arvados.PipelineInstance{},
147         "p5p6p": arvados.PipelineTemplate{},
148         "j58dm": arvados.Specimen{},
149         "q1cn2": arvados.Trait{},
150         "7fd4e": arvados.Workflow{},
151 }
152
153 var mungeKind = regexp.MustCompile(`\..`)
154
155 func kind(resp interface{}) string {
156         t := fmt.Sprintf("%T", resp)
157         if !strings.HasPrefix(t, "arvados.") {
158                 return ""
159         }
160         return mungeKind.ReplaceAllStringFunc(t, func(s string) string {
161                 // "arvados.CollectionList" => "arvados#collectionList"
162                 return "#" + strings.ToLower(s[1:])
163         })
164 }
165
166 func (rtr *router) mungeItemFields(tmp map[string]interface{}) {
167         for k, v := range tmp {
168                 if strings.HasSuffix(k, "_at") {
169                         // Format non-nil timestamps as
170                         // rfc3339NanoFixed (otherwise they would use
171                         // the default time encoding, which omits
172                         // trailing zeroes).
173                         switch tv := v.(type) {
174                         case *time.Time:
175                                 if tv == nil || tv.IsZero() {
176                                         tmp[k] = nil
177                                 } else {
178                                         tmp[k] = tv.Format(rfc3339NanoFixed)
179                                 }
180                         case time.Time:
181                                 if tv.IsZero() {
182                                         tmp[k] = nil
183                                 } else {
184                                         tmp[k] = tv.Format(rfc3339NanoFixed)
185                                 }
186                         case string:
187                                 if tv == "" {
188                                         tmp[k] = nil
189                                 } else if t, err := time.Parse(time.RFC3339Nano, tv); err != nil {
190                                         // pass through an invalid time value (?)
191                                 } else if t.IsZero() {
192                                         tmp[k] = nil
193                                 } else {
194                                         tmp[k] = t.Format(rfc3339NanoFixed)
195                                 }
196                         }
197                 }
198                 // Arvados API spec says when these fields are empty
199                 // they appear in responses as null, rather than a
200                 // zero value.
201                 switch k {
202                 case "output_uuid", "output_name", "log_uuid", "description", "requesting_container_uuid", "container_uuid":
203                         if v == "" {
204                                 tmp[k] = nil
205                         }
206                 case "container_count_max":
207                         if v == float64(0) {
208                                 tmp[k] = nil
209                         }
210                 }
211         }
212 }