Class: Fog::Compute::RackspaceV2::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/rackspace/compute_v2.rb,
lib/fog/rackspace/requests/compute_v2/get_image.rb,
lib/fog/rackspace/requests/compute_v2/get_flavor.rb,
lib/fog/rackspace/requests/compute_v2/get_server.rb,
lib/fog/rackspace/requests/compute_v2/list_images.rb,
lib/fog/rackspace/requests/compute_v2/create_image.rb,
lib/fog/rackspace/requests/compute_v2/delete_image.rb,
lib/fog/rackspace/requests/compute_v2/list_flavors.rb,
lib/fog/rackspace/requests/compute_v2/list_servers.rb,
lib/fog/rackspace/requests/compute_v2/set_metadata.rb,
lib/fog/rackspace/requests/compute_v2/attach_volume.rb,
lib/fog/rackspace/requests/compute_v2/create_server.rb,
lib/fog/rackspace/requests/compute_v2/delete_server.rb,
lib/fog/rackspace/requests/compute_v2/list_metadata.rb,
lib/fog/rackspace/requests/compute_v2/reboot_server.rb,
lib/fog/rackspace/requests/compute_v2/resize_server.rb,
lib/fog/rackspace/requests/compute_v2/update_server.rb,
lib/fog/rackspace/requests/compute_v2/get_attachment.rb,
lib/fog/rackspace/requests/compute_v2/list_addresses.rb,
lib/fog/rackspace/requests/compute_v2/rebuild_server.rb,
lib/fog/rackspace/requests/compute_v2/update_metadata.rb,
lib/fog/rackspace/requests/compute_v2/list_attachments.rb,
lib/fog/rackspace/requests/compute_v2/delete_attachment.rb,
lib/fog/rackspace/requests/compute_v2/get_metadata_item.rb,
lib/fog/rackspace/requests/compute_v2/set_metadata_item.rb,
lib/fog/rackspace/requests/compute_v2/delete_metadata_item.rb,
lib/fog/rackspace/requests/compute_v2/revert_resize_server.rb,
lib/fog/rackspace/requests/compute_v2/confirm_resize_server.rb,
lib/fog/rackspace/requests/compute_v2/change_server_password.rb,
lib/fog/rackspace/requests/compute_v2/list_addresses_by_network.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fog/rackspace/compute_v2.rb', line 90

def initialize(options = {})
  @rackspace_api_key = options[:rackspace_api_key]
  @rackspace_username = options[:rackspace_username]
  @rackspace_auth_url = options[:rackspace_auth_url]
  @rackspace_must_reauthenticate = false
  @connection_options = options[:connection_options] || {}

  endpoint = options[:rackspace_endpoint] || DFW_ENDPOINT
  uri = URI.parse(endpoint)

  @host = uri.host
  @persistent = options[:persistent] || false
  @path = uri.path
  @port = uri.port
  @scheme = uri.scheme

  authenticate

  @connection = Fog::Connection.new(uri.to_s, @persistent, @connection_options)
end

Instance Method Details

#attach_volume(server_id, volume_id, device) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fog/rackspace/requests/compute_v2/attach_volume.rb', line 5

def attach_volume(server_id, volume_id, device)
  data = {
    'volumeAttachment' => {
      'volumeId' => volume_id
    }
  }

  data['volumeAttachment']['device'] = device if device
  
  request(
    :body => Fog::JSON.encode(data),
    :expects => [200],
    :method => 'POST',
    :path => "servers/#{server_id}/os-volume_attachments"
  )
end

#change_server_password(server_id, password) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fog/rackspace/requests/compute_v2/change_server_password.rb', line 5

def change_server_password(server_id, password)
  data = {
    'changePassword' => {
      'adminPass' => password
    }
  }

  request(
    :body => Fog::JSON.encode(data),
    :expects => [202],
    :method => 'POST',
    :path => "servers/#{server_id}/action"
  )
end

#confirm_resize_server(server_id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/rackspace/requests/compute_v2/confirm_resize_server.rb', line 5

def confirm_resize_server(server_id)
  data = {
    'confirmResize' => nil
  }

  request(
    :body => Fog::JSON.encode(data),
    :expects => [204],
    :method => 'POST',
    :path => "servers/#{server_id}/action"
  )
end

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

Create an image from a running server

Parameters

  • server_id<~Integer> - Id of server to create image from

  • name - Name of image

  • options<~Hash> - Name



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fog/rackspace/requests/compute_v2/create_image.rb', line 13

def create_image(server_id, name, options = {})
  data = {
    'createImage' => {
      'name' => name
    }
  }
  data['createImage'].merge!(options)          
  request(
    :body     => Fog::JSON.encode(data),
    :expects  => 202,
    :method   => 'POST',
    :path     => "servers/#{server_id}/action"
  )
end

#create_server(name, image_id, flavor_id, min_count, max_count, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/rackspace/requests/compute_v2/create_server.rb', line 5

def create_server(name, image_id, flavor_id, min_count, max_count, options = {})
  data = {
    'server' => {
      'name' => name,
      'imageRef' => image_id,
      'flavorRef' => flavor_id,
      'minCount' => min_count,
      'maxCount' => max_count
    }
  }

  data['server']['metadata'] = options[:metadata]  unless options[:metadata].nil?
  data['server']['OS-DCF:diskConfig'] = options[:disk_config] unless options[:disk_config].nil?
  data['server']['metadata'] = options[:metadata] unless options[:metadata].nil?
  data['server']['personality'] = options[:personality] unless options[:personality].nil?

  request(
    :body => Fog::JSON.encode(data),
    :expects => [202],
    :method => 'POST',
    :path => "servers"
  )
end

#delete_attachment(server_id, volume_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/compute_v2/delete_attachment.rb', line 5

def delete_attachment(server_id, volume_id)
  request(
    :expects => [202],
    :method => 'DELETE',
    :path => "servers/#{server_id}/os-volume_attachments/#{volume_id}"
  )
end

#delete_image(image_id) ⇒ Object

Delete an image

Parameters

  • image_id<~Integer> - Id of image to delete



11
12
13
14
15
16
17
# File 'lib/fog/rackspace/requests/compute_v2/delete_image.rb', line 11

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

#delete_metadata_item(collection, obj_id, key) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/compute_v2/delete_metadata_item.rb', line 5

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

#delete_server(server_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/compute_v2/delete_server.rb', line 5

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

#get_attachment(server_id, volume_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/compute_v2/get_attachment.rb', line 5

def get_attachment(server_id, volume_id)
  request(
    :expects => [200, 203, 300],
    :method => 'GET',
    :path => "servers/#{server_id}/os-volume_attachments/#{volume_id}"
  )
end

#get_flavor(flavor_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/compute_v2/get_flavor.rb', line 5

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

#get_image(image_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/compute_v2/get_image.rb', line 5

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

#get_metadata_item(collection, obj_id, key) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/compute_v2/get_metadata_item.rb', line 5

def (collection, obj_id, key)
  request(
    :expects => 200,
    :method => 'GET',
    :path => "/#{collection}/#{obj_id}/metadata/#{key}"
  )
end

#get_server(server_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/compute_v2/get_server.rb', line 5

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

#list_addresses(server_id) ⇒ Object



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

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

#list_addresses_by_network(server_id, network_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/rackspace/requests/compute_v2/list_addresses_by_network.rb', line 6

def list_addresses_by_network(server_id, network_id)
  request(
    :method   => 'GET',
    :expects  => 200,
    :path     => "servers/#{server_id}/ips/#{network_id}"
  )
end

#list_attachments(server_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/compute_v2/list_attachments.rb', line 5

def list_attachments(server_id)
  request(
    :expects => [200, 203, 300],
    :method => 'GET',
    :path => "servers/#{server_id}/os-volume_attachments"
  )
end

#list_flavorsObject



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/compute_v2/list_flavors.rb', line 5

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

#list_imagesObject



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/compute_v2/list_images.rb', line 5

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

#list_metadata(collection, obj_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/compute_v2/list_metadata.rb', line 5

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

#list_serversObject



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/compute_v2/list_servers.rb', line 5

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

#reboot_server(server_id, type) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fog/rackspace/requests/compute_v2/reboot_server.rb', line 5

def reboot_server(server_id, type)
  data = {
    'reboot' => {
      'type' => type
    }
  }

  request(
    :body => Fog::JSON.encode(data),
    :expects => [202],
    :method => 'POST',
    :path => "servers/#{server_id}/action"
  )
end

#rebuild_server(server_id, image_id, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/fog/rackspace/requests/compute_v2/rebuild_server.rb', line 5

def rebuild_server(server_id, image_id, options={})
  data = {
    'rebuild' => options || {}
  }
  data['rebuild']['imageRef'] = image_id

  request(
    :body => Fog::JSON.encode(data),
    :expects => [202],
    :method => 'POST',
    :path => "servers/#{server_id}/action"
  )
end

#request(params) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/fog/rackspace/compute_v2.rb', line 111

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]}"
    }))
  rescue Excon::Errors::NotFound => error
    raise NotFound.slurp error
  rescue Excon::Errors::BadRequest => error
    raise BadRequest.slurp error
  rescue Excon::Errors::InternalServerError => error
    raise InternalServerError.slurp error
  rescue Excon::Errors::HTTPStatusError => error
    raise ServiceError.slurp error
  end
  unless response.body.empty?
    response.body = Fog::JSON.decode(response.body)
  end
  response
end

#resize_server(server_id, flavor_id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fog/rackspace/requests/compute_v2/resize_server.rb', line 5

def resize_server(server_id, flavor_id)
  data = {
    'resize' => {
      'flavorRef' => flavor_id
    }
  }

  request(
    :body => Fog::JSON.encode(data),
    :expects => [202],
    :method => 'POST',
    :path => "servers/#{server_id}/action"
  )
end

#revert_resize_server(server_id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/rackspace/requests/compute_v2/revert_resize_server.rb', line 5

def revert_resize_server(server_id)
  data = {
    'revertResize' => nil
  }

  request(
    :body => Fog::JSON.encode(data),
    :expects => [202],
    :method => 'POST',
    :path => "servers/#{server_id}/action"
  )
end

#set_metadata(collection, obj_id, metadata = {}) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/fog/rackspace/requests/compute_v2/set_metadata.rb', line 5

def (collection, obj_id,  = {})
  request(
    :expects => [200, 203],
    :method => 'PUT',
    :path => "/#{collection}/#{obj_id}/metadata",
    :body => Fog::JSON.encode('metadata' => )            
  )
end

#set_metadata_item(collection, obj_id, key, value) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/fog/rackspace/requests/compute_v2/set_metadata_item.rb', line 5

def (collection, obj_id, key, value)
  request(
    :expects => 200,
    :method => 'PUT',
    :path => "/#{collection}/#{obj_id}/metadata/#{key}",
    :body => Fog::JSON.encode('meta' => { key => value })        
  )
end

#update_metadata(collection, obj_id, metadata = {}) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/fog/rackspace/requests/compute_v2/update_metadata.rb', line 5

def (collection, obj_id,  = {})
  request(
    :expects => [200, 203],
    :method => 'POST',
    :path => "/#{collection}/#{obj_id}/metadata",
    :body => Fog::JSON.encode('metadata' => )            
  )
end

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



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/rackspace/requests/compute_v2/update_server.rb', line 5

def update_server(server_id, options={})
  data = options.is_a?(Hash) ? options : { 'name' => options } #LEGACY - second parameter was previously server name

  request(
    :body => Fog::JSON.encode('server' => data),
    :expects => [200],
    :method => 'PUT',
    :path => "servers/#{server_id}"
  )
end