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