Modify API discovery document and CLI tool so resource objects can be
[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     discovery = Rails.cache.fetch 'arvados_v1_rest_discovery' do
27       Rails.application.eager_load!
28       discovery = {
29         kind: "discovery#restDescription",
30         discoveryVersion: "v1",
31         id: "arvados:v1",
32         name: "arvados",
33         version: "v1",
34         revision: "20130226",
35         title: "Arvados API",
36         description: "The API to interact with Arvados.",
37         documentationLink: "https://redmine.clinicalfuture.com/projects/arvados/",
38         protocol: "rest",
39         baseUrl: root_url + "/arvados/v1/",
40         basePath: "/arvados/v1/",
41         rootUrl: root_url,
42         servicePath: "arvados/v1/",
43         batchPath: "batch",
44         parameters: {
45           alt: {
46             type: "string",
47             description: "Data format for the response.",
48             default: "json",
49             enum: [
50                    "json"
51                   ],
52             enumDescriptions: [
53                                "Responses with Content-Type of application/json"
54                               ],
55             location: "query"
56           },
57           fields: {
58             type: "string",
59             description: "Selector specifying which fields to include in a partial response.",
60             location: "query"
61           },
62           key: {
63             type: "string",
64             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.",
65             location: "query"
66           },
67           oauth_token: {
68             type: "string",
69             description: "OAuth 2.0 token for the current user.",
70             location: "query"
71           }
72         },
73         auth: {
74           oauth2: {
75             scopes: {
76               "https://api.clinicalfuture.com/auth/arvados" => {
77                 description: "View and manage objects"
78               },
79               "https://api.clinicalfuture.com/auth/arvados.readonly" => {
80                 description: "View objects"
81               }
82             }
83           }
84         },
85         schemas: {},
86         resources: {}
87       }
88       
89       ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |k|
90         begin
91           ctl_class = "Arvados::V1::#{k.to_s.pluralize}Controller".constantize
92         rescue
93           # No controller -> no discovery.
94           next
95         end
96         object_properties = {}
97         k.columns.
98           select { |col| col.name != 'id' }.
99           collect do |col|
100           if k.serialized_attributes.has_key? col.name
101             object_properties[col.name] = {
102               type: k.serialized_attributes[col.name].object_class.to_s
103             }
104           else
105             object_properties[col.name] = {
106               type: col.type
107             }
108           end
109         end
110         discovery[:schemas][k.to_s + 'List'] = {
111           id: k.to_s + 'List',
112           description: k.to_s + ' list',
113           type: "object",
114           properties: {
115             kind: {
116               type: "string",
117               description: "Object type. Always arvados##{k.to_s.camelcase(:lower)}List.",
118               default: "arvados##{k.to_s.camelcase(:lower)}List"
119             },
120             etag: {
121               type: "string",
122               description: "List version."
123             },
124             items: {
125               type: "array",
126               description: "The list of #{k.to_s.pluralize}.",
127               items: {
128                 "$ref" => k.to_s
129               }
130             },
131             next_link: {
132               type: "string",
133               description: "A link to the next page of #{k.to_s.pluralize}."
134             },
135             next_page_token: {
136               type: "string",
137               description: "The page token for the next page of #{k.to_s.pluralize}."
138             },
139             selfLink: {
140               type: "string",
141               description: "A link back to this list."
142             }
143           }
144         }
145         discovery[:schemas][k.to_s] = {
146           id: k.to_s,
147           description: k.to_s,
148           type: "object",
149           properties: {
150             uuid: {
151               type: "string",
152               description: "Object ID."
153             },
154             etag: {
155               type: "string",
156               description: "Object version."
157             }
158           }.merge(object_properties)
159         }
160         discovery[:resources][k.to_s.underscore.pluralize] = {
161           methods: {
162             get: {
163               id: "arvados.#{k.to_s.underscore.pluralize}.get",
164               path: "#{k.to_s.underscore.pluralize}/{uuid}",
165               httpMethod: "GET",
166               description: "Gets a #{k.to_s}'s metadata by UUID.",
167               parameters: {
168                 uuid: {
169                   type: "string",
170                   description: "The UUID of the #{k.to_s} in question.",
171                   required: true,
172                   location: "path"
173                 }
174               },
175               parameterOrder: [
176                                "uuid"
177                               ],
178               response: {
179                 "$ref" => k.to_s
180               },
181               scopes: [
182                        "https://api.clinicalfuture.com/auth/arvados",
183                        "https://api.clinicalfuture.com/auth/arvados.readonly"
184                       ]
185             },
186             list: {
187               id: "arvados.#{k.to_s.underscore.pluralize}.list",
188               path: k.to_s.underscore.pluralize,
189               httpMethod: "GET",
190               description: "List #{k.to_s.underscore.pluralize}.",
191               parameters: {
192                 limit: {
193                   type: "integer",
194                   description: "Maximum number of #{k.to_s.underscore.pluralize} to return.",
195                   default: "100",
196                   format: "int32",
197                   minimum: "0",
198                   location: "query"
199                 },
200                 pageToken: {
201                   type: "string",
202                   description: "Page token.",
203                   location: "query"
204                 },
205                 q: {
206                   type: "string",
207                   description: "Query string for searching #{k.to_s.underscore.pluralize}.",
208                   location: "query"
209                 },
210                 where: {
211                   type: "object",
212                   description: "Conditions for filtering #{k.to_s.underscore.pluralize}.",
213                   location: "query"
214                 },
215                 order: {
216                   type: "string",
217                   description: "Order in which to return matching #{k.to_s.underscore.pluralize}.",
218                   location: "query"
219                 }
220               },
221               response: {
222                 "$ref" => "#{k.to_s}List"
223               },
224               scopes: [
225                        "https://api.clinicalfuture.com/auth/arvados",
226                        "https://api.clinicalfuture.com/auth/arvados.readonly"
227                       ]
228             },
229             create: {
230               id: "arvados.#{k.to_s.underscore.pluralize}.create",
231               path: "#{k.to_s.underscore.pluralize}",
232               httpMethod: "POST",
233               description: "Create a new #{k.to_s}.",
234               parameters: {
235                 k.to_s.underscore => {
236                   type: "object",
237                   required: false,
238                   location: "query",
239                   properties: object_properties
240                 }
241               },
242               request: {
243                 required: false,
244                 properties: {
245                   k.to_s => {
246                     "$ref" => k.to_s
247                   }
248                 }
249               },
250               response: {
251                 "$ref" => k.to_s
252               },
253               scopes: [
254                        "https://api.clinicalfuture.com/auth/arvados"
255                       ]
256             },
257             update: {
258               id: "arvados.#{k.to_s.underscore.pluralize}.update",
259               path: "#{k.to_s.underscore.pluralize}/{uuid}",
260               httpMethod: "PUT",
261               description: "Update attributes of an existing #{k.to_s}.",
262               parameters: {
263                 uuid: {
264                   type: "string",
265                   description: "The UUID of the #{k.to_s} in question.",
266                   required: true,
267                   location: "path"
268                 },
269                 k.to_s.underscore => {
270                   type: "object",
271                   required: false,
272                   location: "query",
273                   properties: object_properties
274                 }
275               },
276               request: {
277                 required: false,
278                 properties: {
279                   k.to_s => {
280                     "$ref" => k.to_s
281                   }
282                 }
283               },
284               response: {
285                 "$ref" => k.to_s
286               },
287               scopes: [
288                        "https://api.clinicalfuture.com/auth/arvados"
289                       ]
290             },
291             delete: {
292               id: "arvados.#{k.to_s.underscore.pluralize}.delete",
293               path: "#{k.to_s.underscore.pluralize}/{uuid}",
294               httpMethod: "DELETE",
295               description: "Delete an existing #{k.to_s}.",
296               parameters: {
297                 uuid: {
298                   type: "string",
299                   description: "The UUID of the #{k.to_s} in question.",
300                   required: true,
301                   location: "path"
302                 }
303               },
304               response: {
305                 "$ref" => k.to_s
306               },
307               scopes: [
308                        "https://api.clinicalfuture.com/auth/arvados"
309                       ]
310             }
311           }
312         }
313         # Check for Rails routes that don't match the usual actions
314         # listed above
315         d_methods = discovery[:resources][k.to_s.underscore.pluralize][:methods]
316         Rails.application.routes.routes.each do |route|
317           action = route.defaults[:action]
318           httpMethod = (route.verb && route.verb.length > 0) ? route.verb : 'GET'
319           if route.defaults[:controller] == 'arvados/v1/' + k.to_s.underscore.pluralize and
320               !d_methods[action.to_sym] and
321               ctl_class.action_methods.include? action
322             method = {
323               id: "arvados.#{k.to_s.underscore.pluralize}.#{action}",
324               path: route.path.sub('/arvados/v1/','').sub('(.:format)','').sub(/:(uu)?id/,'{uuid}'),
325               httpMethod: httpMethod,
326               description: "#{route.defaults[:action]} #{k.to_s.underscore.pluralize}",
327               parameters: {},
328               response: {
329                 "$ref" => (action == 'index' ? "#{k.to_s}List" : k.to_s)
330               },
331               scopes: [
332                        "https://api.clinicalfuture.com/auth/arvados"
333                       ]
334             }
335             route.segment_keys.each do |key|
336               if key != :format
337                 key = :uuid if key == :id
338                 method[:parameters][key] = {
339                   type: "string",
340                   description: "",
341                   required: true,
342                   location: "path"
343                 }
344               end
345             end
346             if ctl_class.respond_to? "_#{action}_requires_parameters".to_sym
347               ctl_class.send("_#{action}_requires_parameters".to_sym).each do |k, v|
348                 if v.is_a? Hash
349                   method[:parameters][k] = v
350                 else
351                   method[:parameters][k] = {}
352                 end
353                 method[:parameters][k][:type] ||= 'string'
354                 method[:parameters][k][:description] ||= ''
355                 method[:parameters][k][:location] = (route.segment_keys.include?(k) ? 'path' : 'query')
356                 if method[:parameters][k][:required].nil?
357                   method[:parameters][k][:required] = v != false
358                 end
359               end
360             end
361             d_methods[route.defaults[:action].to_sym] = method
362           end
363         end
364       end
365       discovery
366     end
367     render json: discovery
368   end
369 end