Class: Fog::Compute::OpenStack::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/openstack/compute.rb,
lib/fog/openstack/requests/compute/get_meta.rb,
lib/fog/openstack/requests/compute/delete_meta.rb,
lib/fog/openstack/requests/compute/list_images.rb,
lib/fog/openstack/requests/compute/update_meta.rb,
lib/fog/openstack/requests/compute/create_image.rb,
lib/fog/openstack/requests/compute/delete_image.rb,
lib/fog/openstack/requests/compute/list_flavors.rb,
lib/fog/openstack/requests/compute/list_servers.rb,
lib/fog/openstack/requests/compute/set_metadata.rb,
lib/fog/openstack/requests/compute/create_server.rb,
lib/fog/openstack/requests/compute/delete_server.rb,
lib/fog/openstack/requests/compute/list_metadata.rb,
lib/fog/openstack/requests/compute/reboot_server.rb,
lib/fog/openstack/requests/compute/resize_server.rb,
lib/fog/openstack/requests/compute/server_action.rb,
lib/fog/openstack/requests/compute/update_server.rb,
lib/fog/openstack/requests/compute/list_addresses.rb,
lib/fog/openstack/requests/compute/rebuild_server.rb,
lib/fog/openstack/requests/compute/update_metadata.rb,
lib/fog/openstack/requests/compute/get_image_details.rb,
lib/fog/openstack/requests/compute/get_flavor_details.rb,
lib/fog/openstack/requests/compute/get_server_details.rb,
lib/fog/openstack/requests/compute/list_images_detail.rb,
lib/fog/openstack/requests/compute/list_flavors_detail.rb,
lib/fog/openstack/requests/compute/list_servers_detail.rb,
lib/fog/openstack/requests/compute/list_public_addresses.rb,
lib/fog/openstack/requests/compute/revert_resized_server.rb,
lib/fog/openstack/requests/compute/change_password_server.rb,
lib/fog/openstack/requests/compute/confirm_resized_server.rb,
lib/fog/openstack/requests/compute/list_private_addresses.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/fog/openstack/compute.rb', line 106

def initialize(options={})
  require 'multi_json'
  @openstack_api_key = options[:openstack_api_key]
  @openstack_username = options[:openstack_username]
  @openstack_tenant = options[:openstack_tenant]
  @openstack_compute_service_name = options[:openstack_compute_service_name] || 'nova'
  @openstack_auth_url = options[:openstack_auth_url]
  @openstack_auth_token = options[:openstack_auth_token]
  @openstack_management_url = options[:openstack_management_url]
  @openstack_must_reauthenticate = false
  @connection_options = options[:connection_options] || {}
  authenticate
  @persistent = options[:persistent] || false
  @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
end

Instance Method Details

#change_password_server(server_id, admin_password) ⇒ Object



6
7
8
9
# File 'lib/fog/openstack/requests/compute/change_password_server.rb', line 6

def change_password_server(server_id, admin_password)
  body = { 'changePassword' => { 'adminPass' => admin_password }}
  server_action(server_id, body)
end

#confirm_resized_server(server_id) ⇒ Object



6
7
8
9
# File 'lib/fog/openstack/requests/compute/confirm_resized_server.rb', line 6

def confirm_resized_server(server_id)
  body = { 'confirmResize' => nil }
  server_action(server_id, body, 204)
end

#create_image(server_id, name, metadata = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/fog/openstack/requests/compute/create_image.rb', line 6

def create_image(server_id, name, ={})
  body = { 'createImage' => {
    'name' => name,
    'metadata' => 
  }}
  data = server_action(server_id, body)
  image_id = data.headers["Location"].scan(/.*\/(.*)/).flatten
  get_image_details(image_id)
end

#create_server(name, image_ref, flavor_ref, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fog/openstack/requests/compute/create_server.rb', line 6

def create_server(name, image_ref, flavor_ref, options = {})
  data = {
    'server' => {
      'flavorRef'  => flavor_ref,
      'imageRef'   => image_ref,
      'name'       => name
    }
  }

  vanilla_options = ['metadata', 'accessIPv4', 'accessIPv6',
                     'availability_zone', 'user_data']
  vanilla_options.select{|o| options[o]}.each do |key|
    data['server'][key] = options[key]
  end

  if options['personality']
    data['server']['personality'] = []
    for file in options['personality']
      data['server']['personality'] << {
        'contents'  => Base64.encode64(file['contents']),
        'path'      => file['path']
      }
    end
  end

  request(
    :body     => MultiJson.encode(data),
    :expects  => [200, 202],
    :method   => 'POST',
    :path     => 'servers.json'
  )
end

#delete_image(image_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/compute/delete_image.rb', line 6

def delete_image(image_id)
  request(
    :expects  => 204,
    :method   => 'DELETE',
    :path     => "images/#{image_id}"
  )
end

#delete_meta(collection_name, parent_id, key) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/compute/delete_meta.rb', line 6

def delete_meta(collection_name, parent_id, key)
  request(
    :expects  => 204,
    :method   => 'DELETE',
    :path     => "#{collection_name}/#{parent_id}/metadata/#{key}"
  )
end

#delete_server(server_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/compute/delete_server.rb', line 6

def delete_server(server_id)
  request(
    :expects => 204,
    :method => 'DELETE',
    :path   => "servers/#{server_id}"
  )
end

#get_flavor_details(flavor_ref) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/compute/get_flavor_details.rb', line 6

def get_flavor_details(flavor_ref)
  request(
    :expects  => [200, 203],
    :method   => 'GET',
    :path     => "flavors/#{flavor_ref}.json"
  )
end

#get_image_details(image_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/compute/get_image_details.rb', line 6

def get_image_details(image_id)
  request(
    :expects  => [200, 203],
    :method   => 'GET',
    :path     => "images/#{image_id}.json"
  )
end

#get_meta(collection_name, parent_id, key) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/compute/get_meta.rb', line 6

def get_meta(collection_name, parent_id, key)
  request(
    :expects  => [200, 203],
    :method   => 'GET',
    :path     => "#{collection_name}/#{parent_id}/metadata/#{key}"
  )
end

#get_server_details(server_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/compute/get_server_details.rb', line 6

def get_server_details(server_id)
  request(
    :expects  => [200, 203],
    :method   => 'GET',
    :path     => "servers/#{server_id}.json"
  )
end

#list_addresses(server_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/compute/list_addresses.rb', line 6

def list_addresses(server_id)
  request(
    :expects  => [200, 203],
    :method   => 'GET',
    :path     => "servers/#{server_id}/ips.json"
  )
end

#list_flavorsObject



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/compute/list_flavors.rb', line 6

def list_flavors
  request(
    :expects  => [200, 203],
    :method   => 'GET',
    :path     => 'flavors.json'
  )
end

#list_flavors_detailObject



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/compute/list_flavors_detail.rb', line 6

def list_flavors_detail
  request(
    :expects  => [200, 203],
    :method   => 'GET',
    :path     => 'flavors/detail.json'
  )
end

#list_imagesObject



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/compute/list_images.rb', line 6

def list_images
  request(
    :expects  => [200, 203],
    :method   => 'GET',
    :path     => 'images.json'
  )
end

#list_images_detailObject



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/compute/list_images_detail.rb', line 6

def list_images_detail
  request(
    :expects  => [200, 203],
    :method   => 'GET',
    :path     => 'images/detail.json'
  )
end

#list_metadata(collection_name, parent_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/compute/list_metadata.rb', line 6

def (collection_name, parent_id)
  request(
    :expects  => [200, 203],
    :method   => 'GET',
    :path     => "/#{collection_name}/#{parent_id}/metadata.json"
  )
end

#list_private_addresses(server_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/compute/list_private_addresses.rb', line 6

def list_private_addresses(server_id)
  request(
    :expects  => [200, 203],
    :method   => 'GET',
    :path     => "servers/#{server_id}/ips/private.json"
  )
end

#list_public_addresses(server_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/compute/list_public_addresses.rb', line 6

def list_public_addresses(server_id)
  request(
    :expects  => [200, 203],
    :method   => 'GET',
    :path     => "servers/#{server_id}/ips/public.json"
  )
end

#list_serversObject



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/compute/list_servers.rb', line 6

def list_servers
  request(
    :expects  => [200, 203],
    :method   => 'GET',
    :path     => 'servers.json'
  )
end

#list_servers_detailObject



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/compute/list_servers_detail.rb', line 6

def list_servers_detail
  request(
    :expects  => [200, 203],
    :method   => 'GET',
    :path     => 'servers/detail.json'
  )
end

#reboot_server(server_id, type = 'SOFT') ⇒ Object



6
7
8
9
# File 'lib/fog/openstack/requests/compute/reboot_server.rb', line 6

def reboot_server(server_id, type = 'SOFT')
  body = { 'reboot' => { 'type' => type }}
  server_action(server_id, body)
end

#rebuild_server(server_id, image_ref, name, admin_pass = nil, metadata = nil, personality = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/openstack/requests/compute/rebuild_server.rb', line 6

def rebuild_server(server_id, image_ref, name, admin_pass=nil, =nil, personality=nil)

  body = { 'rebuild' => {
    'imageRef' => image_ref,
    'name' => name
  }}
  body['rebuild']['adminPass'] = admin_pass if admin_pass
  body['rebuild']['metadata'] =  if 
  body['rebuild']['personality'] = personality if personality
  server_action(server_id, body, 202)
end

#reloadObject



122
123
124
# File 'lib/fog/openstack/compute.rb', line 122

def reload
  @connection.reset
end

#request(params) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/fog/openstack/compute.rb', line 126

def request(params)
  begin
    response = @connection.request(params.merge({
      :headers  => {
        'Content-Type' => 'application/json',
        'X-Auth-Token' => @auth_token
      }.merge!(params[:headers] || {}),
      :host     => @host,
      :path     => "#{@path}/#{params[:path]}",
      :query    => ('ignore_awful_caching' << Time.now.to_i.to_s)
    }))
  rescue Excon::Errors::Unauthorized => error
    if error.response.body != 'Bad username or password' # token expiration
      @openstack_must_reauthenticate = true
      authenticate
      retry
    else # bad credentials
      raise error
    end
  rescue Excon::Errors::HTTPStatusError => error
    raise case error
    when Excon::Errors::NotFound
      Fog::Compute::OpenStack::NotFound.slurp(error)
    else
      error
    end
  end
  unless response.body.empty?
    response.body = MultiJson.decode(response.body)
  end
  response
end

#resize_server(server_id, flavor_ref) ⇒ Object



6
7
8
9
# File 'lib/fog/openstack/requests/compute/resize_server.rb', line 6

def resize_server(server_id, flavor_ref)
  body = { 'resize' => { 'flavorRef' => flavor_ref }}
  server_action(server_id, body)
end

#revert_resized_server(server_id) ⇒ Object



6
7
8
9
# File 'lib/fog/openstack/requests/compute/revert_resized_server.rb', line 6

def revert_resized_server(server_id)
  body = { 'revertResize' => nil }
  server_action(server_id, body)
end

#server_action(server_id, body, expects = 202) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/fog/openstack/requests/compute/server_action.rb', line 6

def server_action(server_id, body, expects=202)
  request(
    :body     => MultiJson.encode(body),
    :expects  => expects,
    :method   => 'POST',
    :path     => "servers/#{server_id}/action.json"
  )
end

#set_metadata(collection_name, parent_id, metadata = {}) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/fog/openstack/requests/compute/set_metadata.rb', line 7

def (collection_name, parent_id,  = {})
  request(
    :body     => MultiJson.encode({ 'metadata' =>  }),
    :expects  => 200,
    :method   => 'PUT',
    :path     => "#{collection_name}/#{parent_id}/metadata"
  )
end

#update_meta(collection_name, parent_id, key, value) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/fog/openstack/requests/compute/update_meta.rb', line 7

def update_meta(collection_name, parent_id, key, value)
  request(
    :body     => MultiJson.encode({ 'meta' => { key => value }}),
    :expects  => 200,
    :method   => 'PUT',
    :path     => "#{collection_name}/#{parent_id}/metadata/#{key}"
  )
end

#update_metadata(collection_name, parent_id, metadata = {}) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/fog/openstack/requests/compute/update_metadata.rb', line 7

def (collection_name, parent_id,  = {})
  request(
    :body     => MultiJson.encode({ 'metadata' =>  }),
    :expects  => 200,
    :method   => 'POST',
    :path     => "#{collection_name}/#{parent_id}/metadata.json"
  )
end

#update_server(server_id, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/fog/openstack/requests/compute/update_server.rb', line 6

def update_server(server_id, options = {})
  request(
    :body     => MultiJson.encode({ 'server' => options }),
    :expects  => 200,
    :method   => 'PUT',
    :path     => "servers/#{server_id}.json"
  )
end