Merge branch '1881-normalize-collection-uuids'
[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: "List #{k.to_s.underscore.pluralize}.",
193               parameters: {
194                 limit: {
195                   type: "integer",
196                   description: "Maximum number of #{k.to_s.underscore.pluralize} to return.",
197                   default: 100,
198                   format: "int32",
199                   minimum: 0,
200                   location: "query"
201                 },
202                 pageToken: {
203                   type: "string",
204                   description: "Page token.",
205                   location: "query"
206                 },
207                 q: {
208                   type: "string",
209                   description: "Query string for searching #{k.to_s.underscore.pluralize}.",
210                   location: "query"
211                 },
212                 where: {
213                   type: "object",
214                   description: "Conditions for filtering #{k.to_s.underscore.pluralize}.",
215                   location: "query"
216                 },
217                 order: {
218                   type: "string",
219                   description: "Order in which to return matching #{k.to_s.underscore.pluralize}.",
220                   location: "query"
221                 }
222               },
223               response: {
224                 "$ref" => "#{k.to_s}List"
225               },
226               scopes: [
227                        "https://api.clinicalfuture.com/auth/arvados",
228                        "https://api.clinicalfuture.com/auth/arvados.readonly"
229                       ]
230             },
231             create: {
232               id: "arvados.#{k.to_s.underscore.pluralize}.create",
233               path: "#{k.to_s.underscore.pluralize}",
234               httpMethod: "POST",
235               description: "Create a new #{k.to_s}.",
236               parameters: {
237                 k.to_s.underscore => {
238                   type: "object",
239                   required: false,
240                   location: "query",
241                   properties: object_properties
242                 }
243               },
244               request: {
245                 required: false,
246                 properties: {
247                   k.to_s.underscore => {
248                     "$ref" => k.to_s
249                   }
250                 }
251               },
252               response: {
253                 "$ref" => k.to_s
254               },
255               scopes: [
256                        "https://api.clinicalfuture.com/auth/arvados"
257                       ]
258             },
259             update: {
260               id: "arvados.#{k.to_s.underscore.pluralize}.update",
261               path: "#{k.to_s.underscore.pluralize}/{uuid}",
262               httpMethod: "PUT",
263               description: "Update attributes of an existing #{k.to_s}.",
264               parameters: {
265                 uuid: {
266                   type: "string",
267                   description: "The UUID of the #{k.to_s} in question.",
268                   required: true,
269                   location: "path"
270                 },
271                 k.to_s.underscore => {
272                   type: "object",
273                   required: false,
274                   location: "query",
275                   properties: object_properties
276                 }
277               },
278               request: {
279                 required: false,
280                 properties: {
281                   k.to_s.underscore => {
282                     "$ref" => k.to_s
283                   }
284                 }
285               },
286               response: {
287                 "$ref" => k.to_s
288               },
289               scopes: [
290                        "https://api.clinicalfuture.com/auth/arvados"
291                       ]
292             },
293             delete: {
294               id: "arvados.#{k.to_s.underscore.pluralize}.delete",
295               path: "#{k.to_s.underscore.pluralize}/{uuid}",
296               httpMethod: "DELETE",
297               description: "Delete an existing #{k.to_s}.",
298               parameters: {
299                 uuid: {
300                   type: "string",
301                   description: "The UUID of the #{k.to_s} in question.",
302                   required: true,
303                   location: "path"
304                 }
305               },
306               response: {
307                 "$ref" => k.to_s
308               },
309               scopes: [
310                        "https://api.clinicalfuture.com/auth/arvados"
311                       ]
312             }
313           }
314         }
315         # Check for Rails routes that don't match the usual actions
316         # listed above
317         d_methods = discovery[:resources][k.to_s.underscore.pluralize][:methods]
318         Rails.application.routes.routes.each do |route|
319           action = route.defaults[:action]
320           httpMethod = ['GET', 'POST', 'PUT', 'DELETE'].map { |method|
321             method if route.verb.match(method)
322           }.compact.first
323           if httpMethod and
324               route.defaults[:controller] == 'arvados/v1/' + k.to_s.underscore.pluralize and
325               !d_methods[action.to_sym] and
326               ctl_class.action_methods.include? action
327             method = {
328               id: "arvados.#{k.to_s.underscore.pluralize}.#{action}",
329               path: route.path.spec.to_s.sub('/arvados/v1/','').sub('(.:format)','').sub(/:(uu)?id/,'{uuid}'),
330               httpMethod: httpMethod,
331               description: "#{route.defaults[:action]} #{k.to_s.underscore.pluralize}",
332               parameters: {},
333               response: {
334                 "$ref" => (action == 'index' ? "#{k.to_s}List" : k.to_s)
335               },
336               scopes: [
337                        "https://api.clinicalfuture.com/auth/arvados"
338                       ]
339             }
340             route.segment_keys.each do |key|
341               if key != :format
342                 key = :uuid if key == :id
343                 method[:parameters][key] = {
344                   type: "string",
345                   description: "",
346                   required: true,
347                   location: "path"
348                 }
349               end
350             end
351             if ctl_class.respond_to? "_#{action}_requires_parameters".to_sym
352               ctl_class.send("_#{action}_requires_parameters".to_sym).each do |k, v|
353                 if v.is_a? Hash
354                   method[:parameters][k] = v
355                 else
356                   method[:parameters][k] = {}
357                 end
358                 method[:parameters][k][:type] ||= 'string'
359                 method[:parameters][k][:description] ||= ''
360                 method[:parameters][k][:location] = (route.segment_keys.include?(k) ? 'path' : 'query')
361                 if method[:parameters][k][:required].nil?
362                   method[:parameters][k][:required] = v != false
363                 end
364               end
365             end
366             d_methods[route.defaults[:action].to_sym] = method
367           end
368         end
369       end
370       discovery
371     end
372     render json: discovery
373   end
374 end