Class: VagrantPlugins::Openstack::OpenstackClient

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-openstack-provider/openstack_client.rb

Instance Method Summary collapse

Constructor Details

#initializeOpenstackClient

Returns a new instance of OpenstackClient.



9
10
11
12
# File 'lib/vagrant-openstack-provider/openstack_client.rb', line 9

def initialize()
  @logger = Log4r::Logger.new("vagrant_openstack::openstack_client")
  @token = nil
end

Instance Method Details

#add_floating_ip(env, server_id, floating_ip) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vagrant-openstack-provider/openstack_client.rb', line 77

def add_floating_ip(env, server_id, floating_ip)
  config = env[:machine].provider_config
  server_details = RestClient.post(config.openstack_compute_url + "/servers/" + server_id + "/action", {
    :addFloatingIp => {
        :address => floating_ip
      }
    }.to_json,
    "X-Auth-Token" => @token,
    :accept => :json,
    :content_type => :json)
end

#authenticate(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/vagrant-openstack-provider/openstack_client.rb', line 14

def authenticate(env)
  @logger.debug("Authenticating on Keystone")
  config = env[:machine].provider_config
  authentication = RestClient.post(config.openstack_auth_url, {
    :auth => {
        :tenantName => config.tenant_name,
        :passwordCredentials => {
          :username => config.username,
          :password => config.api_key
        }
      }
    }.to_json,
    :content_type => :json,
    :accept => :json)

  @token = JSON.parse(authentication)['access']['token']['id']
end

#create_server(env, name, image_ref, flavor_ref, keypair) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vagrant-openstack-provider/openstack_client.rb', line 46

def create_server(env, name, image_ref, flavor_ref, keypair)
  config = env[:machine].provider_config
  server = RestClient.post(config.openstack_compute_url + "/servers", {
    :server => {
        :name => name,
        :imageRef => image_ref,
        :flavorRef => flavor_ref,
        :key_name => keypair
      }
    }.to_json,
    "X-Auth-Token" => @token,
    :accept => :json,
    :content_type => :json)
  return JSON.parse(server)['server']['id']
end

#delete_server(env, server_id) ⇒ Object



62
63
64
65
66
67
# File 'lib/vagrant-openstack-provider/openstack_client.rb', line 62

def delete_server(env, server_id)
  config = env[:machine].provider_config
  server = RestClient.delete(config.openstack_compute_url + "/servers/" + server_id,
    "X-Auth-Token" => @token,
    :accept => :json)
end

#get_all_flavors(env) ⇒ Object



32
33
34
35
36
37
# File 'lib/vagrant-openstack-provider/openstack_client.rb', line 32

def get_all_flavors(env)
  config = env[:machine].provider_config
  flavors_json = RestClient.get(config.openstack_compute_url + "/flavors",
    {"X-Auth-Token" => @token, :accept => :json})
  return JSON.parse(flavors_json)['flavors'].map { |fl| Item.new(fl['id'], fl['name']) }
end

#get_all_images(env) ⇒ Object



39
40
41
42
43
44
# File 'lib/vagrant-openstack-provider/openstack_client.rb', line 39

def get_all_images(env)
  config = env[:machine].provider_config
  images_json = RestClient.get(config.openstack_compute_url + "/images",
    {"X-Auth-Token" => @token, :accept => :json})
  return JSON.parse(images_json)['images'].map { |im| Item.new(im['id'], im['name']) }
end

#get_server_details(env, server_id) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/vagrant-openstack-provider/openstack_client.rb', line 69

def get_server_details(env, server_id)
  config = env[:machine].provider_config
  server_details = RestClient.get(config.openstack_compute_url + "/servers/" + server_id,
    "X-Auth-Token" => @token,
    :accept => :json)
  return JSON.parse(server_details)['server']
end