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/list_flavors.rb,
lib/fog/rackspace/requests/compute_v2/list_servers.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/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/rebuild_server.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/revert_resize_server.rb,
lib/fog/rackspace/requests/compute_v2/confirm_resize_server.rb,
lib/fog/rackspace/requests/compute_v2/change_server_password.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fog/rackspace/compute_v2.rb', line 62

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
# 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,
      'device' => 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, options = {}) ⇒ Object

Create an image from a running server

Parameters

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

  • options<~Hash> - Name

Returns

  • response<~Excon::Response>:

    • ‘image’<~Hash>:

      • ‘id’<~Integer> - Id of image

      • ‘name’<~String> - Name of image

      • ‘serverId’<~Integer> - Id of server



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/rackspace/requests/compute_v2/create_image.rb', line 18

def create_image(server_id, options = {})
  data = {
    'createImage' => {
    }
  }
  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_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_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_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_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) ⇒ Object



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

def rebuild_server(server_id, image_id)
  data = {
    'rebuild' => {
      'imageRef' => image_id
    }
  }

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

#request(params) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fog/rackspace/compute_v2.rb', line 83

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

#update_server(server_id, name) ⇒ Object



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

def update_server(server_id, name)
  data = {
    'server' => {
      'name' => name
    }
  }

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