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
# 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]
  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 or oauth credentials!!" if (api_username.nil? or api_password.nil?) and 
        (api_key.nil? or api_secret.nil? or token_key.nil? or token_secret.nil?)

  unless api_key.nil?
    credentials = {
      :consumer_key => api_key,
      :consumer_secret => api_secret,
      :token => token_key,
      :token_secret => token_secret
    }
  else
    credentials = {
      :api_username => api_username,
      :api_password => api_password
    }
  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



270
271
272
273
274
275
276
277
278
# File 'lib/abiquo-api.rb', line 270

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.



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/abiquo-api.rb', line 165

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.


144
145
146
# File 'lib/abiquo-api.rb', line 144

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



103
104
105
106
107
108
109
110
111
112
# File 'lib/abiquo-api.rb', line 103

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.


132
133
134
# File 'lib/abiquo-api.rb', line 132

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.



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/abiquo-api.rb', line 203

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



117
118
119
120
121
122
123
124
# File 'lib/abiquo-api.rb', line 117

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.



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/abiquo-api.rb', line 240

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