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