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