fix response type for #index
[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 :login_required
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         next if k == ApiClientAuthorization
91         next if k == ApiClient
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               },
213               response: {
214                 "$ref" => "#{k.to_s}List"
215               },
216               scopes: [
217                        "https://api.clinicalfuture.com/auth/arvados",
218                        "https://api.clinicalfuture.com/auth/arvados.readonly"
219                       ]
220             },
221             create: {
222               id: "arvados.#{k.to_s.underscore.pluralize}.create",
223               path: "#{k.to_s.underscore.pluralize}",
224               httpMethod: "POST",
225               description: "Create a new #{k.to_s}.",
226               parameters: {
227                 k.to_s.underscore => {
228                   type: "object",
229                   required: true,
230                   location: "query",
231                   properties: object_properties
232                 }
233               },
234               response: {
235                 "$ref" => k.to_s
236               },
237               scopes: [
238                        "https://api.clinicalfuture.com/auth/arvados"
239                       ]
240             },
241             update: {
242               id: "arvados.#{k.to_s.underscore.pluralize}.update",
243               path: "#{k.to_s.underscore.pluralize}/{uuid}",
244               httpMethod: "PUT",
245               description: "Update attributes of an existing #{k.to_s}.",
246               parameters: {
247                 uuid: {
248                   type: "string",
249                   description: "The UUID of the #{k.to_s} in question.",
250                   required: true,
251                   location: "path"
252                 },
253                 k.to_s.underscore => {
254                   type: "object",
255                   required: true,
256                   location: "query",
257                   properties: object_properties
258                 }
259               },
260               response: {
261                 "$ref" => k.to_s
262               },
263               scopes: [
264                        "https://api.clinicalfuture.com/auth/arvados"
265                       ]
266             },
267             delete: {
268               id: "arvados.#{k.to_s.underscore.pluralize}.delete",
269               path: "#{k.to_s.underscore.pluralize}/{uuid}",
270               httpMethod: "DELETE",
271               description: "Delete an existing #{k.to_s}.",
272               parameters: {
273                 uuid: {
274                   type: "string",
275                   description: "The UUID of the #{k.to_s} in question.",
276                   required: true,
277                   location: "path"
278                 }
279               },
280               response: {
281                 "$ref" => k.to_s
282               },
283               scopes: [
284                        "https://api.clinicalfuture.com/auth/arvados"
285                       ]
286             }
287           }
288         }
289         # Check for Rails routes that don't match the usual actions
290         # listed above
291         d_methods = discovery[:resources][k.to_s.underscore.pluralize][:methods]
292         Rails.application.routes.routes.each do |route|
293           action = route.defaults[:action]
294           httpMethod = (route.verb && route.verb.length > 0) ? route.verb : 'GET'
295           if route.defaults[:controller] == 'arvados/v1/' + k.to_s.underscore.pluralize and
296               !d_methods[action.to_sym] and
297               ctl_class.action_methods.include? action
298             method = {
299               id: "arvados.#{k.to_s.underscore.pluralize}.#{action}",
300               path: route.path.sub('/arvados/v1/','').sub('(.:format)','').sub(/:(uu?)id/,'{uuid}'),
301               httpMethod: httpMethod,
302               description: "#{route.defaults[:action]} #{k.to_s.underscore.pluralize}",
303               parameters: {},
304               response: {
305                 "$ref" => (action == 'index' ? "#{k.to_s}List" : k.to_s)
306               },
307               scopes: [
308                        "https://api.clinicalfuture.com/auth/arvados"
309                       ]
310             }
311             route.segment_keys.each do |key|
312               if key != :format
313                 key = :uuid if key == :id
314                 method[:parameters][key] = {
315                   type: "string",
316                   description: "",
317                   required: true,
318                   location: "path"
319                 }
320               end
321             end
322             if ctl_class.respond_to? "_#{action}_requires_parameters".to_sym
323               ctl_class.send("_#{action}_requires_parameters".to_sym).each do |k, v|
324                 if v.is_a? Hash
325                   method[:parameters][k] = v
326                 else
327                   method[:parameters][k] = {}
328                 end
329                 method[:parameters][k][:type] ||= 'string'
330                 method[:parameters][k][:description] ||= ''
331                 method[:parameters][k][:location] = (route.segment_keys.include?(k) ? 'path' : 'query')
332                 if method[:parameters][k][:required].nil?
333                   method[:parameters][k][:required] = v != false
334                 end
335               end
336             end
337             d_methods[route.defaults[:action].to_sym] = method
338           end
339         end
340       end
341       discovery
342     end
343     render json: discovery
344   end
345 end