Class: VagrantPlugins::Openstack::NovaClient

Inherits:
Object
  • Object
show all
Includes:
Singleton, Domain, HttpUtils
Defined in:
lib/vagrant-openstack-provider/client/nova.rb

Instance Method Summary collapse

Methods included from HttpUtils

#delete, #get, #post

Methods included from HttpUtils::RequestLogger

#log_request, #log_response

Constructor Details

#initializeNovaClient

Returns a new instance of NovaClient.



15
16
17
18
# File 'lib/vagrant-openstack-provider/client/nova.rb', line 15

def initialize
  @logger = Log4r::Logger.new('vagrant_openstack::nova')
  @session = VagrantPlugins::Openstack.session
end

Instance Method Details

#add_floating_ip(env, server_id, floating_ip) ⇒ Object



95
96
97
98
99
100
# File 'lib/vagrant-openstack-provider/client/nova.rb', line 95

def add_floating_ip(env, server_id, floating_ip)
  check_floating_ip(env, floating_ip)

  post(env, "#{@session.endpoints[:compute]}/servers/#{server_id}/action",
       { addFloatingIp: { address: floating_ip } }.to_json)
end

#allocate_floating_ip(env, pool) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vagrant-openstack-provider/client/nova.rb', line 34

def allocate_floating_ip(env, pool)
  ips_json = post(env, "#{@session.endpoints[:compute]}/os-floating-ips",
                  {
                    pool: pool
                  }.to_json,
                  'X-Auth-Token' => @session.token,
                  :accept => :json,
                  :content_type => :json) { |res| handle_response(res) }
  floating_ip = JSON.parse(ips_json)['floating_ip']
  FloatingIP.new(floating_ip['ip'], floating_ip['pool'], floating_ip['instance_id'])
end

#create_server(env, options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vagrant-openstack-provider/client/nova.rb', line 51

def create_server(env, options)
  server = {}.tap do |s|
    s['name'] = options[:name]
    s['imageRef'] = options[:image_ref]
    s['flavorRef'] = options[:flavor_ref]
    s['key_name'] = options[:keypair]
    s['availability_zone'] = options[:availability_zone] unless options[:availability_zone].nil?
    unless options[:networks].nil? || options[:networks].empty?
      s['networks'] = []
      options[:networks].each do |uuid|
        s['networks'] << { uuid: uuid }
      end
    end
  end
  server = post(env, "#{@session.endpoints[:compute]}/servers", { server: server }.to_json)
  JSON.parse(server)['server']['id']
end

#delete_keypair_if_vagrant(env, server_id) ⇒ Object



121
122
123
124
# File 'lib/vagrant-openstack-provider/client/nova.rb', line 121

def delete_keypair_if_vagrant(env, server_id)
  keyname = get_server_details(env, server_id)['key_name']
  delete(env, "#{@session.endpoints[:compute]}/os-keypairs/#{keyname}") if keyname.start_with?('vagrant-generated-')
end

#delete_server(env, server_id) ⇒ Object



69
70
71
# File 'lib/vagrant-openstack-provider/client/nova.rb', line 69

def delete_server(env, server_id)
  delete(env, "#{@session.endpoints[:compute]}/servers/#{server_id}")
end

#get_all_flavors(env) ⇒ Object



20
21
22
23
24
25
# File 'lib/vagrant-openstack-provider/client/nova.rb', line 20

def get_all_flavors(env)
  flavors_json = get(env, "#{@session.endpoints[:compute]}/flavors/detail")
  JSON.parse(flavors_json)['flavors'].map do |fl|
    Flavor.new(fl['id'], fl['name'], fl['vcpus'], fl['ram'], fl['disk'])
  end
end

#get_all_floating_ips(env) ⇒ Object



27
28
29
30
31
32
# File 'lib/vagrant-openstack-provider/client/nova.rb', line 27

def get_all_floating_ips(env)
  ips_json = get(env, "#{@session.endpoints[:compute]}/os-floating-ips",
                 'X-Auth-Token' => @session.token,
                 :accept => :json) { |res| handle_response(res) }
  JSON.parse(ips_json)['floating_ips'].map { |n| FloatingIP.new(n['ip'], n['pool'], n['instance_id']) }
end

#get_all_images(env) ⇒ Object



46
47
48
49
# File 'lib/vagrant-openstack-provider/client/nova.rb', line 46

def get_all_images(env)
  images_json = get(env, "#{@session.endpoints[:compute]}/images")
  JSON.parse(images_json)['images'].map { |fl| Item.new(fl['id'], fl['name']) }
end

#get_floating_ip_pools(env) ⇒ Object



126
127
128
129
# File 'lib/vagrant-openstack-provider/client/nova.rb', line 126

def get_floating_ip_pools(env)
  floating_ips = get(env, "#{@session.endpoints[:compute]}/os-floating-ip-pools")
  JSON.parse(floating_ips)['floating_ip_pools']
end

#get_floating_ips(env) ⇒ Object



131
132
133
134
# File 'lib/vagrant-openstack-provider/client/nova.rb', line 131

def get_floating_ips(env)
  floating_ips = get(env, "#{@session.endpoints[:compute]}/os-floating-ips")
  JSON.parse(floating_ips)['floating_ips']
end

#get_server_details(env, server_id) ⇒ Object



90
91
92
93
# File 'lib/vagrant-openstack-provider/client/nova.rb', line 90

def get_server_details(env, server_id)
  server_details = get(env, "#{@session.endpoints[:compute]}/servers/#{server_id}")
  JSON.parse(server_details)['server']
end

#import_keypair(env, public_key) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/vagrant-openstack-provider/client/nova.rb', line 102

def import_keypair(env, public_key)
  keyname = "vagrant-generated-#{Kernel.rand(36**8).to_s(36)}"

  key_details = post(env, "#{@session.endpoints[:compute]}/os-keypairs",
                     { keypair:
                       {
                         name: keyname,
                         public_key: public_key
                       }
                     }.to_json)
  JSON.parse(key_details)['keypair']['name']
end

#import_keypair_from_file(env, public_key_path) ⇒ Object



115
116
117
118
119
# File 'lib/vagrant-openstack-provider/client/nova.rb', line 115

def import_keypair_from_file(env, public_key_path)
  fail "File specified in public_key_path #{public_key_path} doesn't exist" unless File.exist?(public_key_path)
  file = File.open(public_key_path)
  import_keypair(env, file.read)
end

#resume_server(env, server_id) ⇒ Object



77
78
79
80
# File 'lib/vagrant-openstack-provider/client/nova.rb', line 77

def resume_server(env, server_id)
  # TODO(julienvey) check status before (if pause->unpause, if suspend->resume...)
  change_server_state(env, server_id, :resume)
end

#start_server(env, server_id) ⇒ Object



86
87
88
# File 'lib/vagrant-openstack-provider/client/nova.rb', line 86

def start_server(env, server_id)
  change_server_state(env, server_id, :start)
end

#stop_server(env, server_id) ⇒ Object



82
83
84
# File 'lib/vagrant-openstack-provider/client/nova.rb', line 82

def stop_server(env, server_id)
  change_server_state(env, server_id, :stop)
end

#suspend_server(env, server_id) ⇒ Object



73
74
75
# File 'lib/vagrant-openstack-provider/client/nova.rb', line 73

def suspend_server(env, server_id)
  change_server_state(env, server_id, :suspend)
end