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


51
52
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
# File 'lib/abiquo-api.rb', line 51

def initialize(options = {})
  api_url = options[:abiquo_api_url]
  api_username = options[:abiquo_username]
  api_password = options[:abiquo_password]

  raise "You need to set :abiquo_api_url, :abiquo_username and :abiquo_password" if api_url.nil? or api_username.nil? or api_password.nil?

  @http_client = AbiquoAPIClient::HTTPClient.new(api_url,
                                                api_username,
                                                api_password)
  api_path = URI.parse(api_url).path

  loginresp = @http_client.request(
    :expects  => [200],
    :method   => 'GET',
    :path     => "#{api_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}))

  @properties = @http_client.request(
    :expects  => [200],
    :method   => 'GET',
    :path     => "#{api_path}/config/properties",
    :accept   => 'application/vnd.abiquo.systemproperties+json'
    )

  if options.has_key? :version
    @version = options[:version][0..2]
  else
    @version = @http_client.request(
          :expects  => [200],
          :method   => 'GET',
          :path     => "#{api_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.



22
23
24
# File 'lib/abiquo-api.rb', line 22

def enterprise
  @enterprise
end

#http_clientObject (readonly)

The AbiquoAPIClient::HTTPClient used by this instance.



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

def http_client
  @http_client
end

#propertiesObject

The config properties for the UI.



33
34
35
# File 'lib/abiquo-api.rb', line 33

def properties
  @properties
end

#userObject

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



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

def user
  @user
end

#versionObject

The Abiquo API version used by this client.



38
39
40
# File 'lib/abiquo-api.rb', line 38

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



231
232
233
234
235
236
237
238
# File 'lib/abiquo-api.rb', line 231

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/abiquo-api.rb', line 120

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.is_a? Array
    tmp_a = []
    resp.each do |r|
      tmp_r = AbiquoAPIClient::LinkModel.new(r.merge({:client => self}))
      tmp_a << tmp_r
    end
    tmp_a
  else
    AbiquoAPIClient::LinkModel.new(resp.merge({ :client => self}))
  end
end

#new_object(hash) ⇒ Object

Returns a new instance of the AbiquoAPIClient::LinkModel class.

Parameters:

A hash of attributes to set in the object.


99
100
101
# File 'lib/abiquo-api.rb', line 99

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.



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

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.to_json,
    :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

#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.



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

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.to_json,
    :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