1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[rackspace|compute] Add servers model and collection.

This commit is contained in:
Brad Gignac 2012-07-30 18:22:16 -04:00
parent 773b3461ff
commit b574b27397
19 changed files with 615 additions and 2 deletions

View file

@ -18,12 +18,26 @@ module Fog
recognizes :rackspace_auth_token
model_path 'fog/rackspace/models/compute_v2'
model :server
collection :servers
model :flavor
collection :flavors
model :image
collection :images
request_path 'fog/rackspace/requests/compute_v2'
request :list_servers
request :get_server
request :create_server
request :update_server
request :delete_server
request :change_server_password
request :reboot_server
request :rebuild_server
request :resize_server
request :confirm_resize_server
request :revert_resize_server
request :list_images
request :get_image

View file

@ -1,5 +1,5 @@
require 'fog/core/collection'
require 'fog/rackspace/models/compute_V2/flavor'
require 'fog/rackspace/models/compute_v2/flavor'
module Fog
module Compute

View file

@ -1,5 +1,5 @@
require 'fog/core/collection'
require 'fog/rackspace/models/compute_V2/image'
require 'fog/rackspace/models/compute_v2/image'
module Fog
module Compute

View file

@ -0,0 +1,129 @@
require 'fog/compute/models/server'
module Fog
module Compute
class RackspaceV2
class Server < Fog::Compute::Server
# States
ACTIVE = 'ACTIVE'
BUILD = 'BUILD'
DELETED = 'DELETED'
ERROR = 'ERROR'
HARD_REBOOT = 'HARD_REBOOT'
MIGRATING = 'MIGRATING'
PASSWORD = 'PASSWORD'
REBOOT = 'REBOOT'
REBUILD = 'REBUILD'
RESCUE = 'RESCUE'
RESIZE = 'RESIZE'
REVERT_RESIZE = 'REVERT_RESIZE'
SUSPENDED = 'SUSPENDED'
UNKNOWN = 'UNKNOWN'
VERIFY_RESIZE = 'VERIFY_RESIZE'
identity :id
attribute :name
attribute :created
attribute :updated
attribute :host_id, :aliases => 'hostId'
attribute :state, :aliases => 'status'
attribute :progress
attribute :user_id
attribute :tenant_id
attribute :links
attribute :metadata
attribute :ipv4_address, :aliases => 'accessIPv4'
attribute :ipv6_address, :aliases => 'accessIPv6'
attribute :disk_config, :aliases => 'OS-DCF:diskConfig'
attribute :bandwidth, :aliases => 'rax-bandwidth:bandwidth'
attribute :addresses
attribute :flavor_id, :aliases => 'flavor', :squash => 'id'
attribute :image_id, :aliases => 'image', :squash => 'id'
def save
if identity
update
else
create
end
true
end
def create
requires :name, :image_id, :flavor_id
data = connection.create_server(name, image_id, flavor_id, 1, 1)
merge_attributes(data.body['server'])
true
end
def update
requires :identity, :name
data = connection.update_server(identity, name)
merge_attributes(data.body['server'])
true
end
def destroy
requires :identity
connection.delete_server(identity)
true
end
def flavor
requires :flavor_id
@flavor ||= connection.flavors.get(flavor_id)
end
def image
requires :image_id
@image ||= connection.images.get(image_id)
end
def ready?
state == ACTIVE
end
def reboot(type = 'SOFT')
requires :identity
connection.reboot_server(identity, type)
self.state = type == 'SOFT' ? REBOOT : HARD_REBOOT
true
end
def resize(flavor_id)
requires :identity
connection.resize_server(identity, flavor_id)
self.state = RESIZE
true
end
def rebuild(image_id)
requires :identity
connection.rebuild_server(identity, image_id)
self.state = REBUILD
true
end
def confirm_resize
requires :identity
connection.confirm_resize_server(identity)
true
end
def revert_resize
requires :identity
connection.revert_resize_server(identity)
true
end
def change_admin_password(password)
requires :identity
connection.change_server_password(identity, password)
self.state = PASSWORD
true
end
end
end
end
end

View file

@ -0,0 +1,25 @@
require 'fog/core/collection'
require 'fog/rackspace/models/compute_v2/server'
module Fog
module Compute
class RackspaceV2
class Servers < Fog::Collection
model Fog::Compute::RackspaceV2::Server
def all
data = connection.list_servers.body['servers']
load(data)
end
def get(server_id)
data = connection.get_server(server_id).body['server']
new(data)
rescue Fog::Compute::RackspaceV2::NotFound
nil
end
end
end
end
end

View file

@ -0,0 +1,22 @@
module Fog
module Compute
class RackspaceV2
class Real
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
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class RackspaceV2
class Real
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
end
end
end
end

View file

@ -0,0 +1,28 @@
module Fog
module Compute
class RackspaceV2
class Real
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']['diskConfig'] = options[:disk_config] unless options[:disk_config].nil?
request(
:body => Fog::JSON.encode(data),
:expects => [202],
:method => 'POST',
:path => "servers"
)
end
end
end
end
end

View file

@ -0,0 +1,15 @@
module Fog
module Compute
class RackspaceV2
class Real
def delete_server(server_id)
request(
:expects => [204],
:method => 'DELETE',
:path => "servers/#{server_id}"
)
end
end
end
end
end

View file

@ -0,0 +1,15 @@
module Fog
module Compute
class RackspaceV2
class Real
def get_server(server_id)
request(
:expects => [200, 203, 300],
:method => 'GET',
:path => "servers/#{server_id}"
)
end
end
end
end
end

View file

@ -0,0 +1,15 @@
module Fog
module Compute
class RackspaceV2
class Real
def list_servers
request(
:expects => [200, 203, 300],
:method => 'GET',
:path => 'servers'
)
end
end
end
end
end

View file

@ -0,0 +1,22 @@
module Fog
module Compute
class RackspaceV2
class Real
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
end
end
end
end

View file

@ -0,0 +1,22 @@
module Fog
module Compute
class RackspaceV2
class Real
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
end
end
end
end

View file

@ -0,0 +1,22 @@
module Fog
module Compute
class RackspaceV2
class Real
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
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class RackspaceV2
class Real
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
end
end
end
end

View file

@ -0,0 +1,22 @@
module Fog
module Compute
class RackspaceV2
class Real
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
end
end
end
end

View file

@ -0,0 +1,61 @@
Shindo.tests('Fog::Compute::RackspaceV2 | server', ['rackspace']) do
pending if Fog.mocking?
service = Fog::Compute::RackspaceV2.new
options = {
:name => "fog_server_#{Time.now.to_i.to_s}",
:flavor_id => 2,
:image_id => '3afe97b2-26dc-49c5-a2cc-a2fc8d80c001'
}
model_tests(service.servers, options, false) do
@instance.wait_for { ready? }
tests('#reboot("SOFT")').succeeds do
@instance.reboot('SOFT')
returns('REBOOT') { @instance.state }
end
@instance.wait_for { ready? }
tests('#reboot("HARD")').succeeds do
@instance.reboot('HARD')
returns('HARD_REBOOT') { @instance.state }
end
@instance.wait_for { ready? }
tests('#rebuild').succeeds do
@instance.rebuild('5cebb13a-f783-4f8c-8058-c4182c724ccd')
returns('REBUILD') { @instance.state }
end
@instance.wait_for { ready? }
tests('#resize').succeeds do
@instance.resize(3)
returns('RESIZE') { @instance.state }
end
@instance.wait_for { state == 'VERIFY_RESIZE' }
tests('#confirm_resize').succeeds do
@instance.confirm_resize
end
@instance.wait_for { ready? }
tests('#resize').succeeds do
@instance.resize(2)
returns('RESIZE') { @instance.state }
end
@instance.wait_for { state == 'VERIFY_RESIZE' }
tests('#revert_resize').succeeds do
@instance.revert_resize
end
@instance.wait_for { ready? }
tests('#change_admin_password').succeeds do
@instance.change_admin_password('somerandompassword')
returns('PASSWORD') { @instance.state }
end
@instance.wait_for { ready? }
end
end

View file

@ -0,0 +1,14 @@
Shindo.tests('Fog::Compute::RackspaceV2 | servers', ['rackspace']) do
pending if Fog.mocking?
service = Fog::Compute::RackspaceV2.new
options = {
:name => "fog_server_#{Time.now.to_i.to_s}",
:flavor_id => 2,
:image_id => '3afe97b2-26dc-49c5-a2cc-a2fc8d80c001'
}
collection_tests(service.servers, options, false) do
@instance.wait_for { ready? }
end
end

View file

@ -0,0 +1,147 @@
Shindo.tests('Fog::Compute::RackspaceV2 | server_tests', ['rackspace']) do
pending if Fog.mocking?
LINK_FORMAT = {
'href' => String,
'rel' => String
}
SERVER_FORMAT = {
'id' => String,
'name' => String,
'hostId' => Fog::Nullable::String,
'created' => Fog::Nullable::String,
'updated' => Fog::Nullable::String,
'status' => Fog::Nullable::String,
'progress' => Fog::Nullable::Integer,
'user_id' => Fog::Nullable::String,
'tenant_id' => Fog::Nullable::String,
'links' => [LINK_FORMAT],
'metadata' => Fog::Nullable::Hash
}
LIST_SERVERS_FORMAT = {
'servers' => [SERVER_FORMAT]
}
GET_SERVER_FORMAT = {
'server' => SERVER_FORMAT.merge({
'accessIPv4' => String,
'accessIPv6' => String,
'OS-DCF:diskConfig' => String,
'rax-bandwidth:bandwidth' => Fog::Nullable::Array,
'addresses' => Fog::Nullable::Hash,
'flavor' => {
'id' => String,
'links' => [LINK_FORMAT]
},
'image' => {
'id' => String,
'links' => [LINK_FORMAT]
}
})
}
CREATE_SERVER_FORMAT = {
'server' => {
'id' => String,
'adminPass' => String,
'links' => [LINK_FORMAT],
'OS-DCF:diskConfig' => String
}
}
service = Fog::Compute.new(:provider => 'Rackspace', :version => 'V2')
tests('success') do
server_id = nil
server_name = 'fog' + Time.now.to_i.to_s
image_id = '3afe97b2-26dc-49c5-a2cc-a2fc8d80c001' # Ubuntu 11.10
flavor_id = '2' # 512 MB
tests("#create_server(#{server_name}, #{image_id}, #{flavor_id}, 1, 1)").formats(CREATE_SERVER_FORMAT) do
body = service.create_server(server_name, image_id, flavor_id, 1, 1).body
server_id = body['server']['id']
body
end
tests('#list_servers').formats(LIST_SERVERS_FORMAT) do
service.list_servers.body
end
tests('#get_server').formats(GET_SERVER_FORMAT) do
service.get_server(server_id).body
end
until service.get_server(server_id).body['server']['status'] == 'ACTIVE'
sleep 10
end
tests("#update_server(#{server_id}, #{server_name}_update)").formats(GET_SERVER_FORMAT) do
service.update_server(server_id, "#{server_name}_update").body
end
tests('#change_server_password').succeeds do
service.change_server_password(server_id, 'some_server_password')
end
sleep 60
tests('#reboot_server').succeeds do
service.reboot_server(server_id, 'SOFT')
end
until service.get_server(server_id).body['server']['status'] == 'ACTIVE'
sleep 10
end
tests('#rebuild_server').succeeds do
rebuild_image_id = "5cebb13a-f783-4f8c-8058-c4182c724ccd" # Ubuntu 12.04
service.rebuild_server(server_id, rebuild_image_id)
end
until service.get_server(server_id).body['server']['status'] == 'ACTIVE'
sleep 10
end
tests('#resize_server').succeeds do
resize_flavor_id = 3 # 1GB
service.resize_server(server_id, resize_flavor_id)
end
until service.get_server(server_id).body['server']['status'] == 'VERIFY_RESIZE'
sleep 10
end
tests('#confirm_resize_server').succeeds do
service.confirm_resize_server(server_id)
end
until service.get_server(server_id).body['server']['status'] == 'ACTIVE'
sleep 10
end
tests('#resize_server').succeeds do
resize_flavor_id = 2 # 1GB
service.resize_server(server_id, resize_flavor_id)
end
until service.get_server(server_id).body['server']['status'] == 'VERIFY_RESIZE'
sleep 10
end
tests('#revert_resize_server').succeeds do
service.revert_resize_server(server_id)
end
until service.get_server(server_id).body['server']['status'] == 'ACTIVE'
sleep 10
end
tests('#delete_server').succeeds do
service.delete_server(server_id)
end
end
end