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