Class: VagrantPlugins::Abiquo::Helpers::ApiClient

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::Retryable
Defined in:
lib/vagrant-abiquo/helpers/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(machine) ⇒ ApiClient

Returns a new instance of ApiClient.



19
20
21
22
23
24
# File 'lib/vagrant-abiquo/helpers/client.rb', line 19

def initialize(machine)
  @timeout = 60
  @otimeout = 30
  @logger = Log4r::Logger.new('vagrant::abiquo::apiclient')
  @config = machine.provider_config
end

Instance Method Details

#find_id(entity, entity_collection, name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vagrant-abiquo/helpers/client.rb', line 26

def find_id(entity,entity_collection,name)
  JSON.parse(entity_collection)['collection'].each do |collection|
    if collection['name'] == name               
      return collection['id'].to_s
    end
  end
  raise(Errors::APIFindError, {
      :entity => entity,
      :name => name
    })
end

#find_template(entity, entity_collection, name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vagrant-abiquo/helpers/client.rb', line 38

def find_template(entity,entity_collection,name)
  JSON.parse(entity_collection)['collection'].each do |collection|
    if collection['name'] == name
      collection['links'].each do |template_link|
        if template_link['rel'] == "edit"
          return template_link['href']
        end
      end
    end
  end
  raise(Errors::APIFindError, {
      :entity => entity,
      :name => name
    })
end

#http_request(resource, method, headers = {}, data = {}) ⇒ Object

TO-DO Crear metodos de FIND_VDC / FIND_VAPP / FIND_VM y eliminarlos de create y provider



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
# File 'lib/vagrant-abiquo/helpers/client.rb', line 57

def http_request(resource, method, headers={}, data={})
  begin
    req = RestClient::Resource.new( resource, :user => @config.abiquo_api_user, :password => @config.abiquo_api_password, :timeout => @timeout, :open_timeout => @otimeout )
    case method
      when "GET"
        if headers.nil? then
          res = req.get
        else
          res = req.get(headers)
        end
      when "POST"
        if headers.nil? then
          res = req.post
        else
          res = req.post(data,headers)
        end
    end
  rescue => e
    raise(Errors::RestClientError, {
      :path => resource,
      :headers => headers,
      :data => data,
      :response => e.to_s
    })
  end
  if res.code == 202 or 
     res.code == 201 or 
     res.code == 200 then
       return res.body
  end
end