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