0b004635082968e5f6784b2c65d5cf08f89739c4
[arvados.git] / sdk / ruby / lib / arvados.rb
1 require 'rubygems'
2 require 'google/api_client'
3 require 'active_support/inflector'
4 require 'json'
5
6 ActiveSupport::Inflector.inflections do |inflect|
7   inflect.irregular 'specimen', 'specimens'
8   inflect.irregular 'human', 'humans'
9 end
10
11 module Kernel
12   def suppress_warnings
13     original_verbosity = $VERBOSE
14     $VERBOSE = nil
15     result = yield
16     $VERBOSE = original_verbosity
17     return result
18   end
19 end
20
21 class Arvados
22
23   class TransactionFailedError < StandardError
24   end
25
26   @@debuglevel = 0
27   class << self
28     attr_accessor :debuglevel
29   end
30
31   def initialize(opts={})
32     @application_version ||= 0.0
33     @application_name ||= File.split($0).last
34
35     @arvados_api_version = opts[:api_version] ||
36       ENV['ARVADOS_API_VERSION'] ||
37       'v1'
38     @arvados_api_host = opts[:api_host] ||
39       ENV['ARVADOS_API_HOST'] or
40       raise "#{$0}: no :api_host or ENV[ARVADOS_API_HOST] provided."
41     @arvados_api_token = opts[:api_token] ||
42       ENV['ARVADOS_API_TOKEN'] or
43       raise "#{$0}: no :api_token or ENV[ARVADOS_API_TOKEN] provided."
44
45     if (opts[:api_host] ? opts[:suppress_ssl_warnings] :
46         ENV['ARVADOS_API_HOST_INSECURE'])
47       suppress_warnings do
48         OpenSSL::SSL.const_set 'VERIFY_PEER', OpenSSL::SSL::VERIFY_NONE
49       end
50     end
51
52     # Define a class and an Arvados instance method for each Arvados
53     # resource. After this, self.job will return Arvados::Job;
54     # self.job.new() and self.job.find() will do what you want.
55     _arvados = self
56     namespace_class = Arvados.const_set "A#{self.object_id}", Class.new
57     self.arvados_api.schemas.each do |classname, schema|
58       next if classname.match /List$/
59       klass = Class.new(Arvados::Model) do
60         def self.arvados
61           @arvados
62         end
63         def self.api_models_sym
64           @api_models_sym
65         end
66         def self.api_model_sym
67           @api_model_sym
68         end
69       end
70
71       # Define the resource methods (create, get, update, delete, ...)
72       self.
73         arvados_api.
74         send(classname.underscore.split('/').last.pluralize.to_sym).
75         discovered_methods.
76         each do |method|
77         class << klass; self; end.class_eval do
78           define_method method.name do |*params|
79             self.api_exec(method.name.to_sym, *params)
80           end
81         end
82       end
83
84       # Give the new class access to the API
85       klass.instance_eval do
86         @arvados = _arvados
87         # These should be pulled from the discovery document instead:
88         @api_models_sym = classname.underscore.split('/').last.pluralize.to_sym
89         @api_model_sym = classname.underscore.split('/').last.to_sym
90       end
91
92       # Create the new class in namespace_class so it doesn't
93       # interfere with classes created by other Arvados objects. The
94       # result looks like Arvados::A26949680::Job.
95       namespace_class.const_set classname, klass
96
97       self.class.class_eval do
98         define_method classname.underscore do
99           klass
100         end
101       end
102     end
103   end
104
105   class Google::APIClient
106     def discovery_document(api, version)
107       api = api.to_s
108       return @discovery_documents["#{api}:#{version}"] ||=
109         begin
110           response = self.execute!(
111                                    :http_method => :get,
112                                    :uri => self.discovery_uri(api, version),
113                                    :authenticated => false
114                                    )
115           response.body.class == String ? JSON.parse(response.body) : response.body
116         end
117     end
118   end
119
120   def client
121     @client ||= Google::APIClient.
122       new(:host => @arvados_api_host,
123           :application_name => @application_name,
124           :application_version => @application_version.to_s)
125   end
126
127   def arvados_api
128     @arvados_api ||= self.client.discovered_api('arvados', @arvados_api_version)
129   end
130
131   def self.debuglog(message, verbosity=1)
132     $stderr.puts "#{File.split($0).last} #{$$}: #{message}" if @@debuglevel >= verbosity
133   end
134
135   class Model
136     def self.arvados_api
137       arvados.arvados_api
138     end
139     def self.client
140       arvados.client
141     end
142     def self.debuglog(*args)
143       arvados.class.debuglog *args
144     end
145     def debuglog(*args)
146       self.class.arvados.class.debuglog *args
147     end
148     def self.api_exec(method, parameters={})
149       parameters = parameters.
150         merge(:api_token => ENV['ARVADOS_API_TOKEN'])
151       parameters.each do |k,v|
152         parameters[k] = v.to_json if v.is_a? Array or v.is_a? Hash
153       end
154       result = client.
155         execute(:api_method => arvados_api.send(api_models_sym).send(method),
156                 :authenticated => false,
157                 :parameters => parameters)
158       resp = JSON.parse result.body, :symbolize_names => true
159       if resp[:errors]
160         raise Arvados::TransactionFailedError.new(resp[:errors])
161       elsif resp[:uuid] and resp[:etag]
162         self.new(resp)
163       elsif resp[:items].is_a? Array
164         resp.merge(items: resp[:items].collect do |i|
165                      self.new(i)
166                    end)
167       else
168         resp
169       end
170     end
171
172     def []=(x,y)
173       @attributes_to_update[x] = y
174       @attributes[x] = y
175     end
176     def [](x)
177       if @attributes[x].is_a? Hash or @attributes[x].is_a? Array
178         # We won't be notified via []= if these change, so we'll just
179         # assume they are going to get changed, and submit them if
180         # save() is called.
181         @attributes_to_update[x] = @attributes[x]
182       end
183       @attributes[x]
184     end
185     def save
186       @attributes_to_update.keys.each do |k|
187         @attributes_to_update[k] = @attributes[k]
188       end
189       j = self.class.api_exec :update, {
190         :uuid => @attributes[:uuid],
191         self.class.api_model_sym => @attributes_to_update.to_json
192       }
193       unless j.respond_to? :[] and j[:uuid]
194         debuglog "Failed to save #{self.to_s}: #{j[:errors] rescue nil}", 0
195         nil
196       else
197         @attributes_to_update = {}
198         @attributes = j
199       end
200     end
201
202     protected
203
204     def initialize(j)
205       @attributes_to_update = {}
206       @attributes = j
207     end
208   end
209 end