Merge branch '10969-check-docker-format'
[arvados.git] / services / api / app / controllers / arvados / v1 / schema_controller.rb
1 class Arvados::V1::SchemaController < ApplicationController
2   skip_before_filter :catch_redirect_hint
3   skip_before_filter :find_objects_for_index
4   skip_before_filter :find_object_by_uuid
5   skip_before_filter :load_filters_param
6   skip_before_filter :load_limit_offset_order_params
7   skip_before_filter :load_read_auths
8   skip_before_filter :load_where_param
9   skip_before_filter :render_404_if_no_object
10   skip_before_filter :require_auth_scope
11
12   include DbCurrentTime
13
14   def index
15     expires_in 24.hours, public: true
16     discovery = Rails.cache.fetch 'arvados_v1_rest_discovery' do
17       Rails.application.eager_load!
18       discovery = {
19         kind: "discovery#restDescription",
20         discoveryVersion: "v1",
21         id: "arvados:v1",
22         name: "arvados",
23         version: "v1",
24         revision: "20131114",
25         source_version: AppVersion.hash,
26         generatedAt: db_current_time.iso8601,
27         title: "Arvados API",
28         description: "The API to interact with Arvados.",
29         documentationLink: "http://doc.arvados.org/api/index.html",
30         defaultCollectionReplication: Rails.configuration.default_collection_replication,
31         protocol: "rest",
32         baseUrl: root_url + "arvados/v1/",
33         basePath: "/arvados/v1/",
34         rootUrl: root_url,
35         servicePath: "arvados/v1/",
36         batchPath: "batch",
37         defaultTrashLifetime: Rails.application.config.default_trash_lifetime,
38         blobSignatureTtl: Rails.application.config.blob_signature_ttl,
39         maxRequestSize: Rails.application.config.max_request_size,
40         dockerImageFormats: Rails.application.config.docker_image_formats,
41         parameters: {
42           alt: {
43             type: "string",
44             description: "Data format for the response.",
45             default: "json",
46             enum: [
47                    "json"
48                   ],
49             enumDescriptions: [
50                                "Responses with Content-Type of application/json"
51                               ],
52             location: "query"
53           },
54           fields: {
55             type: "string",
56             description: "Selector specifying which fields to include in a partial response.",
57             location: "query"
58           },
59           key: {
60             type: "string",
61             description: "API key. Your API key identifies your project and provides you with API access, quota, and reports. Required unless you provide an OAuth 2.0 token.",
62             location: "query"
63           },
64           oauth_token: {
65             type: "string",
66             description: "OAuth 2.0 token for the current user.",
67             location: "query"
68           }
69         },
70         auth: {
71           oauth2: {
72             scopes: {
73               "https://api.curoverse.com/auth/arvados" => {
74                 description: "View and manage objects"
75               },
76               "https://api.curoverse.com/auth/arvados.readonly" => {
77                 description: "View objects"
78               }
79             }
80           }
81         },
82         schemas: {},
83         resources: {}
84       }
85
86       if Rails.application.config.websocket_address
87         discovery[:websocketUrl] = Rails.application.config.websocket_address
88       elsif ENV['ARVADOS_WEBSOCKETS']
89         discovery[:websocketUrl] = root_url.sub(/^http/, 'ws') + "websocket"
90       end
91
92       ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |k|
93         begin
94           ctl_class = "Arvados::V1::#{k.to_s.pluralize}Controller".constantize
95         rescue
96           # No controller -> no discovery.
97           next
98         end
99         object_properties = {}
100         k.columns.
101           select { |col| col.name != 'id' }.
102           collect do |col|
103           if k.serialized_attributes.has_key? col.name
104             object_properties[col.name] = {
105               type: k.serialized_attributes[col.name].object_class.to_s
106             }
107           else
108             object_properties[col.name] = {
109               type: col.type
110             }
111           end
112         end
113         discovery[:schemas][k.to_s + 'List'] = {
114           id: k.to_s + 'List',
115           description: k.to_s + ' list',
116           type: "object",
117           properties: {
118             kind: {
119               type: "string",
120               description: "Object type. Always arvados##{k.to_s.camelcase(:lower)}List.",
121               default: "arvados##{k.to_s.camelcase(:lower)}List"
122             },
123             etag: {
124               type: "string",
125               description: "List version."
126             },
127             items: {
128               type: "array",
129               description: "The list of #{k.to_s.pluralize}.",
130               items: {
131                 "$ref" => k.to_s
132               }
133             },
134             next_link: {
135               type: "string",
136               description: "A link to the next page of #{k.to_s.pluralize}."
137             },
138             next_page_token: {
139               type: "string",
140               description: "The page token for the next page of #{k.to_s.pluralize}."
141             },
142             selfLink: {
143               type: "string",
144               description: "A link back to this list."
145             }
146           }
147         }
148         discovery[:schemas][k.to_s] = {
149           id: k.to_s,
150           description: k.to_s,
151           type: "object",
152           uuidPrefix: (k.respond_to?(:uuid_prefix) ? k.uuid_prefix : nil),
153           properties: {
154             uuid: {
155               type: "string",
156               description: "Object ID."
157             },
158             etag: {
159               type: "string",
160               description: "Object version."
161             }
162           }.merge(object_properties)
163         }
164         discovery[:resources][k.to_s.underscore.pluralize] = {
165           methods: {
166             get: {
167               id: "arvados.#{k.to_s.underscore.pluralize}.get",
168               path: "#{k.to_s.underscore.pluralize}/{uuid}",
169               httpMethod: "GET",
170               description: "Gets a #{k.to_s}'s metadata by UUID.",
171               parameters: {
172                 uuid: {
173                   type: "string",
174                   description: "The UUID of the #{k.to_s} in question.",
175                   required: true,
176                   location: "path"
177                 }
178               },
179               parameterOrder: [
180                                "uuid"
181                               ],
182               response: {
183                 "$ref" => k.to_s
184               },
185               scopes: [
186                        "https://api.curoverse.com/auth/arvados",
187                        "https://api.curoverse.com/auth/arvados.readonly"
188                       ]
189             },
190             list: {
191               id: "arvados.#{k.to_s.underscore.pluralize}.list",
192               path: k.to_s.underscore.pluralize,
193               httpMethod: "GET",
194               description:
195                  %|List #{k.to_s.pluralize}.
196
197                    The <code>list</code> method returns a
198                    <a href="/api/resources.html">resource list</a> of
199                    matching #{k.to_s.pluralize}. For example:
200
201                    <pre>
202                    {
203                     "kind":"arvados##{k.to_s.camelcase(:lower)}List",
204                     "etag":"",
205                     "self_link":"",
206                     "next_page_token":"",
207                     "next_link":"",
208                     "items":[
209                        ...
210                     ],
211                     "items_available":745,
212                     "_profile":{
213                      "request_time":0.157236317
214                     }
215                     </pre>|,
216               parameters: {
217                 limit: {
218                   type: "integer",
219                   description: "Maximum number of #{k.to_s.underscore.pluralize} to return.",
220                   default: "100",
221                   format: "int32",
222                   minimum: "0",
223                   location: "query",
224                 },
225                 offset: {
226                   type: "integer",
227                   description: "Number of #{k.to_s.underscore.pluralize} to skip before first returned record.",
228                   default: "0",
229                   format: "int32",
230                   minimum: "0",
231                   location: "query",
232                   },
233                 filters: {
234                   type: "array",
235                   description: "Conditions for filtering #{k.to_s.underscore.pluralize}.",
236                   location: "query"
237                 },
238                 where: {
239                   type: "object",
240                   description: "Conditions for filtering #{k.to_s.underscore.pluralize}. (Deprecated. Use filters instead.)",
241                   location: "query"
242                 },
243                 order: {
244                   type: "string",
245                   description: "Order in which to return matching #{k.to_s.underscore.pluralize}.",
246                   location: "query"
247                 },
248                 select: {
249                   type: "array",
250                   description: "Select which fields to return.",
251                   location: "query"
252                 },
253                 distinct: {
254                   type: "boolean",
255                   description: "Return each distinct object.",
256                   location: "query"
257                 },
258                 count: {
259                   type: "string",
260                   description: "Type of count to return in items_available ('none' or 'exact').",
261                   default: "exact",
262                   location: "query"
263                 }
264               },
265               response: {
266                 "$ref" => "#{k.to_s}List"
267               },
268               scopes: [
269                        "https://api.curoverse.com/auth/arvados",
270                        "https://api.curoverse.com/auth/arvados.readonly"
271                       ]
272             },
273             create: {
274               id: "arvados.#{k.to_s.underscore.pluralize}.create",
275               path: "#{k.to_s.underscore.pluralize}",
276               httpMethod: "POST",
277               description: "Create a new #{k.to_s}.",
278               parameters: {},
279               request: {
280                 required: true,
281                 properties: {
282                   k.to_s.underscore => {
283                     "$ref" => k.to_s
284                   }
285                 }
286               },
287               response: {
288                 "$ref" => k.to_s
289               },
290               scopes: [
291                        "https://api.curoverse.com/auth/arvados"
292                       ]
293             },
294             update: {
295               id: "arvados.#{k.to_s.underscore.pluralize}.update",
296               path: "#{k.to_s.underscore.pluralize}/{uuid}",
297               httpMethod: "PUT",
298               description: "Update attributes of an existing #{k.to_s}.",
299               parameters: {
300                 uuid: {
301                   type: "string",
302                   description: "The UUID of the #{k.to_s} in question.",
303                   required: true,
304                   location: "path"
305                 }
306               },
307               request: {
308                 required: true,
309                 properties: {
310                   k.to_s.underscore => {
311                     "$ref" => k.to_s
312                   }
313                 }
314               },
315               response: {
316                 "$ref" => k.to_s
317               },
318               scopes: [
319                        "https://api.curoverse.com/auth/arvados"
320                       ]
321             },
322             delete: {
323               id: "arvados.#{k.to_s.underscore.pluralize}.delete",
324               path: "#{k.to_s.underscore.pluralize}/{uuid}",
325               httpMethod: "DELETE",
326               description: "Delete an existing #{k.to_s}.",
327               parameters: {
328                 uuid: {
329                   type: "string",
330                   description: "The UUID of the #{k.to_s} in question.",
331                   required: true,
332                   location: "path"
333                 }
334               },
335               response: {
336                 "$ref" => k.to_s
337               },
338               scopes: [
339                        "https://api.curoverse.com/auth/arvados"
340                       ]
341             }
342           }
343         }
344         # Check for Rails routes that don't match the usual actions
345         # listed above
346         d_methods = discovery[:resources][k.to_s.underscore.pluralize][:methods]
347         Rails.application.routes.routes.each do |route|
348           action = route.defaults[:action]
349           httpMethod = ['GET', 'POST', 'PUT', 'DELETE'].map { |method|
350             method if route.verb.match(method)
351           }.compact.first
352           if httpMethod and
353               route.defaults[:controller] == 'arvados/v1/' + k.to_s.underscore.pluralize and
354               ctl_class.action_methods.include? action
355             if !d_methods[action.to_sym]
356               method = {
357                 id: "arvados.#{k.to_s.underscore.pluralize}.#{action}",
358                 path: route.path.spec.to_s.sub('/arvados/v1/','').sub('(.:format)','').sub(/:(uu)?id/,'{uuid}'),
359                 httpMethod: httpMethod,
360                 description: "#{action} #{k.to_s.underscore.pluralize}",
361                 parameters: {},
362                 response: {
363                   "$ref" => (action == 'index' ? "#{k.to_s}List" : k.to_s)
364                 },
365                 scopes: [
366                          "https://api.curoverse.com/auth/arvados"
367                         ]
368               }
369               route.segment_keys.each do |key|
370                 if key != :format
371                   key = :uuid if key == :id
372                   method[:parameters][key] = {
373                     type: "string",
374                     description: "",
375                     required: true,
376                     location: "path"
377                   }
378                 end
379               end
380             else
381               # We already built a generic method description, but we
382               # might find some more required parameters through
383               # introspection.
384               method = d_methods[action.to_sym]
385             end
386             if ctl_class.respond_to? "_#{action}_requires_parameters".to_sym
387               ctl_class.send("_#{action}_requires_parameters".to_sym).each do |l, v|
388                 if v.is_a? Hash
389                   method[:parameters][l] = v
390                 else
391                   method[:parameters][l] = {}
392                 end
393                 if !method[:parameters][l][:default].nil?
394                   # The JAVA SDK is sensitive to all values being strings
395                   method[:parameters][l][:default] = method[:parameters][l][:default].to_s
396                 end
397                 method[:parameters][l][:type] ||= 'string'
398                 method[:parameters][l][:description] ||= ''
399                 method[:parameters][l][:location] = (route.segment_keys.include?(l) ? 'path' : 'query')
400                 if method[:parameters][l][:required].nil?
401                   method[:parameters][l][:required] = v != false
402                 end
403               end
404             end
405             d_methods[action.to_sym] = method
406           end
407         end
408       end
409       Rails.configuration.disable_api_methods.each do |method|
410         ctrl, action = method.split('.', 2)
411         discovery[:resources][ctrl][:methods].delete(action.to_sym)
412       end
413       discovery
414     end
415     send_json discovery
416   end
417 end