Class: AbiquoAPI

Inherits:
Object
  • Object
show all
Includes:
AbiquoAPIClient
Defined in:
lib/abiquo-api.rb

Overview

Ruby Abiquo API client main class

Constant Summary

Constants included from AbiquoAPIClient

AbiquoAPIClient::VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

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 options.
                       { :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(options = {})
  api_url = options[:abiquo_api_url]
  api_username = options[:abiquo_username]
  api_password = options[:abiquo_password]
  api_key = options[:abiquo_api_key]
  api_secret = options[:abiquo_api_secret]
  access_token = options[:abiquo_access_token]
  token_key = options[:abiquo_token_key]
  token_secret = options[:abiquo_token_secret]
  connection_options = options[: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,
                                                connection_options)

  if options.has_key? :version
    @version = options[: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

#enterpriseObject

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_clientObject (readonly)

The AbiquoAPIClient::HTTPClient used by this instance.



17
18
19
# File 'lib/abiquo-api.rb', line 17

def http_client
  @http_client
end

#userObject

An instance of AbiquoAPIClient::LinkModel representing the current user.



29
30
31
# File 'lib/abiquo-api.rb', line 29

def user
  @user
end

#versionObject

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, options = {})
  resp = @http_client.request(
    :expects  => [204,202],
    :method   => 'DELETE',
    :path     => link.href,
    :query    => options
  )
  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, options = {})
  accept = options[:accept].nil? ? link.type : options.delete(:accept)

  req_hash = {
    :expects  => [200],
    :method   => 'GET',
    :path     => link.href,
    :query    => options
  }

  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, options = {})
  AbiquoAPI::LinkCollection.new(self.get(link, options), link.type, self)
end

#loginObject

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 
  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, options = {})
  ctype = options[:content].nil? ? link.type : options.delete(:content)
  accept = options[:accept].nil? ? link.type : options.delete(:accept)

  req_hash = {
    :expects  => [200, 201, 202, 204],
    :method   => 'POST',
    :path     => link.href,
    :body     => data,
    :query    => options
  }

  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

#propertiesObject

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, options = {})
  ctype = options[:content].nil? ? link.type : options.delete(:content)
  accept = options[:accept].nil? ? link.type : options.delete(:accept)

  req_hash = {
    :expects  => [200, 201, 202, 204],
    :method   => 'PUT',
    :path     => link.href,
    :body     => data,
    :query    => options
  }

  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