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