Class: VagrantPlugins::ConoHa::NovaClient

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

Instance Method Summary collapse

Methods included from HttpUtils

#delete, #get, #get_api_version_list, #post

Methods included from HttpUtils::RequestLogger

#log_request, #log_response

Constructor Details

#initializeNovaClient

Returns a new instance of NovaClient.



14
15
16
17
# File 'lib/vagrant-conoha/client/nova.rb', line 14

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

Instance Method Details

#add_floating_ip(env, server_id, floating_ip) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/vagrant-conoha/client/nova.rb', line 120

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

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

#allocate_floating_ip(env, pool) ⇒ Object



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

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)
  floating_ip = JSON.parse(ips_json)['floating_ip']
  FloatingIP.new(floating_ip['ip'], floating_ip['pool'], floating_ip['instance_id'])
end

#attach_volume(env, server_id, volume_id, device = nil) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/vagrant-conoha/client/nova.rb', line 167

def attach_volume(env, server_id, volume_id, device = nil)
  instance_exists do
    attachment = post(env, "#{@session.endpoints[:compute]}/servers/#{server_id}/os-volume_attachments",
                      {
                        volumeAttachment: {
                          volumeId: volume_id,
                          device: device
                        }
                      }.to_json)
    JSON.parse(attachment)['volumeAttachment']
  end
end

#create_server(env, options) ⇒ Object



50
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
# File 'lib/vagrant-conoha/client/nova.rb', line 50

def create_server(env, options)
  server = {}.tap do |s|
    s['name'] = options[:name]
    if options[:image_ref].nil?
      s['block_device_mapping'] = [{ volume_id: options[:volume_boot][:id],
                                     device_name: options[:volume_boot][:device] }] if options[:volume_boot].key?(:id)
      s['block_device_mapping_v2'] = [{ boot_index: '0',
                                        volume_size: options[:volume_boot][:size],
                                        uuid: options[:volume_boot][:image],
                                        device_name: options[:volume_boot][:device],
                                        source_type: 'image',
                                        destination_type: 'volume',
                                        delete_on_termination: options[:volume_boot][:delete_on_destroy] }]\
                                        if options[:volume_boot].key?(:image)
    else
      s['imageRef'] = options[:image_ref]
    end
    s['flavorRef'] = options[:flavor_ref]
    s['key_name'] = options[:keypair]

    s['adminPass'] = options[:admin_pass] unless options[:admin_pass].nil? || options[:admin_pass].empty?
    s['availability_zone'] = options[:availability_zone] unless options[:availability_zone].nil?
    s['security_groups'] = options[:security_groups] unless options[:security_groups].nil?
    s['user_data'] = Base64.encode64(options[:user_data]) unless options[:user_data].nil?
    s['metadata'] = options[:metadata] unless options[:metadata].nil?
    s['networks'] = options[:networks] unless options[:networks].nil? || options[:networks].empty?
  end
  object = { server: server }
  object['os:scheduler_hints'] = options[:scheduler_hints] unless options[:scheduler_hints].nil?
  server = post(env, "#{@session.endpoints[:compute]}/servers", object.to_json)
  JSON.parse(server)['server']['id']
end

#delete_keypair_if_vagrant(env, server_id) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/vagrant-conoha/client/nova.rb', line 148

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

#delete_server(env, server_id) ⇒ Object



83
84
85
86
87
# File 'lib/vagrant-conoha/client/nova.rb', line 83

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

#get_all_flavors(env) ⇒ Object



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

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



26
27
28
29
30
31
# File 'lib/vagrant-conoha/client/nova.rb', line 26

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

#get_all_images(env) ⇒ Object



45
46
47
48
# File 'lib/vagrant-conoha/client/nova.rb', line 45

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

#get_floating_ip_pools(env) ⇒ Object



157
158
159
160
# File 'lib/vagrant-conoha/client/nova.rb', line 157

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



162
163
164
165
# File 'lib/vagrant-conoha/client/nova.rb', line 162

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



113
114
115
116
117
118
# File 'lib/vagrant-conoha/client/nova.rb', line 113

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

#import_keypair(env, public_key) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/vagrant-conoha/client/nova.rb', line 129

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



142
143
144
145
146
# File 'lib/vagrant-conoha/client/nova.rb', line 142

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



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

def resume_server(env, server_id)
  instance_exists do
    change_server_state(env, server_id, :resume)
  end
end

#start_server(env, server_id) ⇒ Object



107
108
109
110
111
# File 'lib/vagrant-conoha/client/nova.rb', line 107

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

#stop_server(env, server_id) ⇒ Object



101
102
103
104
105
# File 'lib/vagrant-conoha/client/nova.rb', line 101

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

#suspend_server(env, server_id) ⇒ Object



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

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