Class: AbiquoAPI
Overview
Ruby Abiquo API client main class
Constant Summary
Constants included from AbiquoAPIClient
Instance Attribute Summary collapse
-
#enterprise ⇒ Object
A hash of the enterprise to which the user belongs to.
-
#http_client ⇒ Object
readonly
The AbiquoAPIClient::HTTPClient used by this instance.
-
#user ⇒ Object
An instance of AbiquoAPIClient::LinkModel representing the current user.
-
#version ⇒ Object
The Abiquo API version used by this client.
Instance Method Summary collapse
-
#delete(link, options = {}) ⇒ Object
Executes an HTTP DELETE over the AbiquoAPIClient::Link passed as parameter.
-
#get(link, options = {}) ⇒ Object
Executes an HTTP GET over the AbiquoAPIClient::Link passed as parameter.
-
#initialize(options = {}) ⇒ AbiquoAPI
constructor
Initializes a new AbiquoAPI instance.
-
#list(link, options = {}) ⇒ Object
Returns a new instance of the AbiquoAPIClient::LinkCollection class.
-
#login ⇒ Object
Performs a ‘login` call to Abiquo to retrieve user related info.
-
#new_object(hash) ⇒ Object
Returns a new instance of the AbiquoAPIClient::LinkModel class.
-
#post(link, data, options = {}) ⇒ Object
Executes an HTTP POST over the AbiquoAPIClient::Link passed as parameter.
-
#properties ⇒ Object
Loads System properties.
-
#put(link, data, options = {}) ⇒ Object
Executes an HTTP PUT over the AbiquoAPIClient::Link passed as parameter.
Constructor Details
#initialize(options = {}) ⇒ AbiquoAPI
Initializes a new AbiquoAPI instance.
Required options:
:abiquo_api_url => The URL of the Abiquo API. ie. https://yourserver/api
:abiquo_username => The username used to connect to the Abiquo API.
:abiquo_password => The password for your user.
:version => The Abiquo API version to include in each request.
Defaults to whatever is returned in the /api/version resource
:connection_options => Excon HTTP client connection .
{ :connect_timeout => <time_in_secs>,
:read_timeout => <time_in_secs>,
:write_timeout => <time_in_secs>,
:ssl_verify_peer => <true_or_false>,
:ssl_ca_path => <path_to_ca_file> }
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/abiquo-api.rb', line 53 def initialize( = {}) api_url = [:abiquo_api_url] api_username = [:abiquo_username] api_password = [:abiquo_password] api_key = [:abiquo_api_key] api_secret = [:abiquo_api_secret] access_token = [:abiquo_access_token] token_key = [:abiquo_token_key] token_secret = [:abiquo_token_secret] = [:connection_options] || {} raise "You need to set :abiquo_api_url" if api_url.nil? raise "You need to provide either basic auth, oauth credentials or OpenID access token!!" if (api_username.nil? or api_password.nil?) and (api_key.nil? or api_secret.nil? or token_key.nil? or token_secret.nil?) and (access_token.nil?) unless api_key.nil? credentials = { :consumer_key => api_key, :consumer_secret => api_secret, :token => token_key, :token_secret => token_secret } else if access_token.nil? credentials = { :api_username => api_username, :api_password => api_password } else credentials = { :access_token => access_token } end end @http_client = AbiquoAPIClient::HTTPClient.new(api_url, credentials, ) if .has_key? :version @version = [:version][0..2] else @version = @http_client.request( :expects => [200], :method => 'GET', :path => "version", :accept => 'text/plain' ).delete("\n")[0..2] end self end |
Instance Attribute Details
#enterprise ⇒ Object
A hash of the enterprise to which the user belongs to.
23 24 25 |
# File 'lib/abiquo-api.rb', line 23 def enterprise @enterprise end |
#http_client ⇒ Object (readonly)
The AbiquoAPIClient::HTTPClient used by this instance.
17 18 19 |
# File 'lib/abiquo-api.rb', line 17 def http_client @http_client end |
#user ⇒ Object
An instance of AbiquoAPIClient::LinkModel representing the current user.
29 30 31 |
# File 'lib/abiquo-api.rb', line 29 def user @user end |
#version ⇒ Object
The Abiquo API version used by this client.
34 35 36 |
# File 'lib/abiquo-api.rb', line 34 def version @version end |
Instance Method Details
#delete(link, options = {}) ⇒ Object
Executes an HTTP DELETE over the AbiquoAPIClient::Link passed as parameter.
Required parameters:
- link
-
An instance of an AbiquoAPIClient::Link.
Optional parameters:
- options
-
A hash of key/values that will be sent as query.
Returns nil
277 278 279 280 281 282 283 284 285 |
# File 'lib/abiquo-api.rb', line 277 def delete(link, = {}) resp = @http_client.request( :expects => [204,202], :method => 'DELETE', :path => link.href, :query => ) resp.nil? ? nil : AbiquoAPIClient::LinkModel.new({:client => self}.merge(resp)) end |
#get(link, options = {}) ⇒ Object
Executes an HTTP GET over the AbiquoAPIClient::Link passed as parameter.
Required parameters:
- link
-
An instance of an AbiquoAPIClient::Link.
Optional parameters:
- options
-
A hash of key/values that will be sent as query.
NOTE: The option :accept will override Accept header sent in the request.
Returns an instance of the AbiquoAPIClient::LinkModel class representing the requested resource.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/abiquo-api.rb', line 172 def get(link, = {}) accept = [:accept].nil? ? link.type : .delete(:accept) req_hash = { :expects => [200], :method => 'GET', :path => link.href, :query => } req_hash[:accept] = "#{accept}; version=#{@version};" unless accept.eql? "" resp = @http_client.request(req_hash) if resp['collection'].nil? AbiquoAPIClient::LinkModel.new(resp.merge({ :client => self})) else resp end end |
#list(link, options = {}) ⇒ Object
Returns a new instance of the AbiquoAPIClient::LinkCollection class.
Parameters:
An instance of {AbiquoAPIClient::Link} pointing to the URL of
the collection.
151 152 153 |
# File 'lib/abiquo-api.rb', line 151 def list(link, = {}) AbiquoAPI::LinkCollection.new(self.get(link, ), link.type, self) end |
#login ⇒ Object
Performs a ‘login` call to Abiquo to retrieve user related info
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/abiquo-api.rb', line 110 def login loginresp = @http_client.request( :expects => [200], :method => 'GET', :path => "login", :accept => 'application/vnd.abiquo.user+json' ) @enterprise = AbiquoAPIClient::Link.new(loginresp['links'].select {|l| l['rel'] == 'enterprise'}.first) @user = AbiquoAPIClient::LinkModel.new(loginresp.merge({:client => self})) end |
#new_object(hash) ⇒ Object
Returns a new instance of the AbiquoAPIClient::LinkModel class.
Parameters:
A hash of attributes to set in the object.
139 140 141 |
# File 'lib/abiquo-api.rb', line 139 def new_object(hash) AbiquoAPIClient::LinkModel.new(hash.merge({ :client => self})) end |
#post(link, data, options = {}) ⇒ Object
Executes an HTTP POST over the AbiquoAPIClient::Link passed as parameter.
Required parameters:
- link
-
An instance of an AbiquoAPIClient::Link.
- data
-
The data to send in the HTTP request. Usually an instance of the AbiquoAPIClient::LinkModel instance. Will be serialized to JSON before sending.
Optional parameters:
- options
-
A hash of key/values that will be sent as query.
NOTE: The option :accept and :content options will override Accept and Content-Type headers sent in the request.
Returns an instance of the AbiquoAPIClient::LinkModel class representing the requested resource or nil if the request returned empty.
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/abiquo-api.rb', line 210 def post(link, data, = {}) ctype = [:content].nil? ? link.type : .delete(:content) accept = [:accept].nil? ? link.type : .delete(:accept) req_hash = { :expects => [200, 201, 202, 204], :method => 'POST', :path => link.href, :body => data, :query => } req_hash[:accept] = "#{accept}; version=#{@version};" unless accept.eql? "" req_hash[:content] = "#{ctype}; version=#{@version};" unless ctype.eql? "" resp = @http_client.request(req_hash) resp.nil? ? nil : AbiquoAPIClient::LinkModel.new({:client => self}.merge(resp)) end |
#properties ⇒ Object
Loads System properties
124 125 126 127 128 129 130 131 |
# File 'lib/abiquo-api.rb', line 124 def properties @http_client.request( :expects => [200], :method => 'GET', :path => "config/properties", :accept => 'application/vnd.abiquo.systemproperties+json' ) end |
#put(link, data, options = {}) ⇒ Object
Executes an HTTP PUT over the AbiquoAPIClient::Link passed as parameter.
Required parameters:
- link
-
An instance of an AbiquoAPIClient::Link.
- data
-
The data to send in the HTTP request. Usually an instance of the AbiquoAPIClient::LinkModel instance. Will be serialized to JSON before sending.
Optional parameters:
- options
-
A hash of key/values that will be sent as query.
NOTE: The option :accept and :content options will override Accept and Content-Type headers sent in the request.
Returns an instance of the AbiquoAPIClient::LinkModel class representing the requested resource or nil if the request returned empty.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/abiquo-api.rb', line 247 def put(link, data, = {}) ctype = [:content].nil? ? link.type : .delete(:content) accept = [:accept].nil? ? link.type : .delete(:accept) req_hash = { :expects => [200, 201, 202, 204], :method => 'PUT', :path => link.href, :body => data, :query => } req_hash[:accept] = "#{accept}; version=#{@version};" unless accept.eql? "" req_hash[:content] = "#{ctype}; version=#{@version};" unless ctype.eql? "" resp = @http_client.request(req_hash) resp.nil? ? nil : AbiquoAPIClient::LinkModel.new({:client => self}.merge(resp)) end |