Merge branch '1943-client-sdk-config-files'
[arvados.git] / services / api / app / controllers / arvados / v1 / schema_controller.rb
1 class Arvados::V1::SchemaController < ApplicationController
2   skip_before_filter :find_object_by_uuid
3   skip_before_filter :require_auth_scope_all
4
5   def show
6     classes = Rails.cache.fetch 'arvados_v1_schema' do
7       Rails.application.eager_load!
8       classes = {}
9       ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |k|
10         classes[k] = k.columns.collect do |col|
11           if k.serialized_attributes.has_key? col.name
12             { name: col.name,
13               type: k.serialized_attributes[col.name].object_class.to_s }
14           else
15             { name: col.name,
16               type: col.type }
17           end
18         end
19       end
20       classes
21     end
22     render json: classes
23   end
24
25   def discovery_rest_description
26     expires_in 24.hours, public: true
27     discovery = Rails.cache.fetch 'arvados_v1_rest_discovery' do
28       Rails.application.eager_load!
29       discovery = {
30         kind: "discovery#restDescription",
31         discoveryVersion: "v1",
32         id: "arvados:v1",
33         name: "arvados",
34         version: "v1",
35         revision: "20131114",
36         generatedAt: Time.now.iso8601,
37         title: "Arvados API",
38         description: "The API to interact with Arvados.",
39         documentationLink: "https://redmine.clinicalfuture.com/projects/arvados/",
40         protocol: "rest",
41         baseUrl: root_url + "/arvados/v1/",
42         basePath: "/arvados/v1/",
43         rootUrl: root_url,
44         servicePath: "arvados/v1/",
45         batchPath: "batch",
46         parameters: {
47           alt: {
48             type: "string",
49             description: "Data format for the response.",
50             default: "json",
51             enum: [
52                    "json"
53                   ],
54             enumDescriptions: [
55                                "Responses with Content-Type of application/json"
56                               ],
57             location: "query"
58           },
59           fields: {
60             type: "string",
61             description: "Selector specifying which fields to include in a partial response.",
62             location: "query"
63           },
64           key: {
65             type: "string",
66             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.",
67             location: "query"
68           },
69           oauth_token: {
70             type: "string",
71             description: "OAuth 2.0 token for the current user.",
72             location: "query"
73           }
74         },
75         auth: {
76           oauth2: {
77             scopes: {
78               "https://api.clinicalfuture.com/auth/arvados" => {
79                 description: "View and manage objects"
80               },
81               "https://api.clinicalfuture.com/auth/arvados.readonly" => {
82                 description: "View objects"
83               }
84             }
85           }
86         },
87         schemas: {},
88         resources: {}
89       }
90       
91       ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |k|
92         begin
93           ctl_class = "Arvados::V1::#{k.to_s.pluralize}Controller".constantize
94         rescue
95           # No controller -> no discovery.
96           next
97         end
98         object_properties = {}
99         k.columns.
100           select { |col| col.name != 'id' }.
101           collect do |col|
102           if k.serialized_attributes.has_key? col.name
103             object_properties[col.name] = {
104               type: k.serialized_attributes[col.name].object_class.to_s
105             }
106           else
107             object_properties[col.name] = {
108               type: col.type
109             }
110           end
111         end
112         discovery[:schemas][k.to_s + 'List'] = {
113           id: k.to_s + 'List',
114           description: k.to_s + ' list',
115           type: "object",
116           properties: {
117             kind: {
118               type: "string",
119               description: "Object type. Always arvados##{k.to_s.camelcase(:lower)}List.",
120               default: "arvados##{k.to_s.camelcase(:lower)}List"
121             },
122             etag: {
123               type: "string",
124               description: "List version."
125             },
126             items: {
127               type: "array",
128               description: "The list of #{k.to_s.pluralize}.",
129               items: {
130                 "$ref" => k.to_s
131               }
132             },
133             next_link: {
134               type: "string",
135               description: "A link to the next page of #{k.to_s.pluralize}."
136             },
137             next_page_token: {
138               type: "string",
139               description: "The page token for the next page of #{k.to_s.pluralize}."
140             },
141             selfLink: {
142               type: "string",
143               description: "A link back to this list."
144             }
145           }
146         }
147         discovery[:schemas][k.to_s] = {
148           id: k.to_s,
149           description: k.to_s,
150           type: "object",
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.clinicalfuture.com/auth/arvados",
185                        "https://api.clinicalfuture.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                 where: {
224                   type: "object",
225                   description: "Conditions for filtering #{k.to_s.underscore.pluralize}.",
226                   location: "query"
227                 },
228                 order: {
229                   type: "string",
230                   description: "Order in which to return matching #{k.to_s.underscore.pluralize}.",
231                   location: "query"
232                 }
233               },
234               response: {
235                 "$ref" => "#{k.to_s}List"
236               },
237               scopes: [
238                        "https://api.clinicalfuture.com/auth/arvados",
239                        "https://api.clinicalfuture.com/auth/arvados.readonly"
240                       ]
241             },
242             create: {
243               id: "arvados.#{k.to_s.underscore.pluralize}.create",
244               path: "#{k.to_s.underscore.pluralize}",
245               httpMethod: "POST",
246               description: "Create a new #{k.to_s}.",
247               parameters: {
248                 k.to_s.underscore => {
249                   type: "object",
250                   required: false,
251                   location: "query",
252                   properties: object_properties
253                 }
254               },
255               request: {
256                 required: false,
257                 properties: {
258                   k.to_s.underscore => {
259                     "$ref" => k.to_s
260                   }
261                 }
262               },
263               response: {
264                 "$ref" => k.to_s
265               },
266               scopes: [
267                        "https://api.clinicalfuture.com/auth/arvados"
268                       ]
269             },
270             update: {
271               id: "arvados.#{k.to_s.underscore.pluralize}.update",
272               path: "#{k.to_s.underscore.pluralize}/{uuid}",
273               httpMethod: "PUT",
274               description: "Update attributes of an existing #{k.to_s}.",
275               parameters: {
276                 uuid: {
277                   type: "string",
278                   description: "The UUID of the #{k.to_s} in question.",
279                   required: true,
280                   location: "path"
281                 },
282                 k.to_s.underscore => {
283                   type: "object",
284                   required: false,
285                   location: "query",
286                   properties: object_properties
287                 }
288               },
289               request: {
290                 required: false,
291                 properties: {
292                   k.to_s.underscore => {
293                     "$ref" => k.to_s
294                   }
295                 }
296               },
297               response: {
298                 "$ref" => k.to_s
299               },
300               scopes: [
301                        "https://api.clinicalfuture.com/auth/arvados"
302                       ]
303             },
304             delete: {
305               id: "arvados.#{k.to_s.underscore.pluralize}.delete",
306               path: "#{k.to_s.underscore.pluralize}/{uuid}",
307               httpMethod: "DELETE",
308               description: "Delete an existing #{k.to_s}.",
309               parameters: {
310                 uuid: {
311                   type: "string",
312                   description: "The UUID of the #{k.to_s} in question.",
313                   required: true,
314                   location: "path"
315                 }
316               },
317               response: {
318                 "$ref" => k.to_s
319               },
320               scopes: [
321                        "https://api.clinicalfuture.com/auth/arvados"
322                       ]
323             }
324           }
325         }
326         # Check for Rails routes that don't match the usual actions
327         # listed above
328         d_methods = discovery[:resources][k.to_s.underscore.pluralize][:methods]
329         Rails.application.routes.routes.each do |route|
330           action = route.defaults[:action]
331           httpMethod = ['GET', 'POST', 'PUT', 'DELETE'].map { |method|
332             method if route.verb.match(method)
333           }.compact.first
334           if httpMethod and
335               route.defaults[:controller] == 'arvados/v1/' + k.to_s.underscore.pluralize and
336               !d_methods[action.to_sym] and
337               ctl_class.action_methods.include? action
338             method = {
339               id: "arvados.#{k.to_s.underscore.pluralize}.#{action}",
340               path: route.path.spec.to_s.sub('/arvados/v1/','').sub('(.:format)','').sub(/:(uu)?id/,'{uuid}'),
341               httpMethod: httpMethod,
342               description: "#{route.defaults[:action]} #{k.to_s.underscore.pluralize}",
343               parameters: {},
344               response: {
345                 "$ref" => (action == 'index' ? "#{k.to_s}List" : k.to_s)
346               },
347               scopes: [
348                        "https://api.clinicalfuture.com/auth/arvados"
349                       ]
350             }
351             route.segment_keys.each do |key|
352               if key != :format
353                 key = :uuid if key == :id
354                 method[:parameters][key] = {
355                   type: "string",
356                   description: "",
357                   required: true,
358                   location: "path"
359                 }
360               end
361             end
362             if ctl_class.respond_to? "_#{action}_requires_parameters".to_sym
363               ctl_class.send("_#{action}_requires_parameters".to_sym).each do |k, v|
364                 if v.is_a? Hash
365                   method[:parameters][k] = v
366                 else
367                   method[:parameters][k] = {}
368                 end
369                 method[:parameters][k][:type] ||= 'string'
370                 method[:parameters][k][:description] ||= ''
371                 method[:parameters][k][:location] = (route.segment_keys.include?(k) ? 'path' : 'query')
372                 if method[:parameters][k][:required].nil?
373                   method[:parameters][k][:required] = v != false
374                 end
375               end
376             end
377             d_methods[route.defaults[:action].to_sym] = method
378           end
379         end
380       end
381       discovery
382     end
383     render json: discovery
384   end
385 end