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