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

Merge pull request #579 from menrcom/clodo-1.0.0

Clodo 1.0.0
This commit is contained in:
Wesley Beary 2011-11-28 17:15:54 -08:00
commit 9864c78f50
26 changed files with 1173 additions and 0 deletions

View file

@ -55,6 +55,7 @@ end
require 'fog/bin/aws' require 'fog/bin/aws'
require 'fog/bin/bluebox' require 'fog/bin/bluebox'
require 'fog/bin/brightbox' require 'fog/bin/brightbox'
require 'fog/bin/clodo'
require 'fog/bin/dnsimple' require 'fog/bin/dnsimple'
require 'fog/bin/dnsmadeeasy' require 'fog/bin/dnsmadeeasy'
require 'fog/bin/dynect' require 'fog/bin/dynect'

31
lib/fog/bin/clodo.rb Normal file
View file

@ -0,0 +1,31 @@
class Clodo < Fog::Bin
class << self
def class_for(key)
case key
when :compute
Fog::Compute::Clodo
else
raise ArgumentError, "Unrecognized service: #{key}"
end
end
def [](service)
@@connections ||= Hash.new do |hash, key|
hash[key] = case key
when :compute
Formatador.display_line("[yellow][WARN] Clodo[:compute] is deprecated, use Compute[:clodo] instead[/]")
Fog::Compute.new(:provider => 'Clodo')
else
raise ArgumentError, "Unrecognized service: #{key.inspect}"
end
end
@@connections[service]
end
def services
Fog::Clodo.services
end
end
end

34
lib/fog/clodo.rb Normal file
View file

@ -0,0 +1,34 @@
require 'fog/core'
module Fog
module Clodo
extend Fog::Provider
service(:compute, 'clodo/compute', 'Compute')
def self.authenticate(options)
clodo_auth_url = options[:clodo_auth_url] || "api.clodo.ru"
url = clodo_auth_url.match(/^https?:/) ? \
clodo_auth_url : 'https://' + clodo_auth_url
uri = URI.parse(url)
connection = Fog::Connection.new(url)
@clodo_api_key = options[:clodo_api_key]
@clodo_username = options[:clodo_username]
response = connection.request({
:expects => [200, 204],
:headers => {
'X-Auth-Key' => @clodo_api_key,
'X-Auth-User' => @clodo_username
},
:host => uri.host,
:method => 'GET',
:path => (uri.path and not uri.path.empty?) ? uri.path : 'v1.0'
})
response.headers.reject do |key, value|
!['X-Server-Management-Url', 'X-Storage-Url', 'X-CDN-Management-Url', 'X-Auth-Token'].include?(key)
end
end # authenticate
end # module Clodo
end # module Fog

149
lib/fog/clodo/compute.rb Normal file
View file

@ -0,0 +1,149 @@
module Fog
module Compute
class Clodo < Fog::Service
requires :clodo_api_key, :clodo_username
recognizes :clodo_auth_url, :persistent
recognizes :clodo_auth_token, :clodo_management_url
model_path 'fog/clodo/models/compute'
model :image
collection :images
model :server
collection :servers
request_path 'fog/clodo/requests/compute'
request :create_server
request :delete_server
request :get_image_details # Not supported by API
request :list_images
request :list_images_detail
request :list_servers
request :list_servers_detail
request :get_server_details
request :server_action
request :start_server
request :stop_server
request :reboot_server
request :rebuild_server
# request :list_addresses
# request :list_private_addresses
# request :list_public_addresses
# request :confirm_resized_server
# request :revert_resized_server
# request :resize_server
# request :update_server
class Mock
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {
:last_modified => {
:images => {},
:servers => {}
},
:images => {},
:servers => {}
}
end
end
def self.reset
@data = nil
end
def initialize(options={})
require 'multi_json'
@clodo_username = options[:clodo_username]
end
def data
self.class.data[@clodo_username]
end
def reset_data
self.class.data.delete(@clodo_username)
end
end
class Real
def initialize(options={})
require 'multi_json'
@clodo_api_key = options[:clodo_api_key]
@clodo_username = options[:clodo_username]
@clodo_auth_url = options[:clodo_auth_url]
@clodo_servicenet = options[:clodo_servicenet]
@clodo_auth_token = options[:clodo_auth_token]
@clodo_management_url = options[:clodo_management_url]
@clodo_must_reauthenticate = false
authenticate
Excon.ssl_verify_peer = false if options[:clodo_servicenet] == true
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent])
end
def reload
@connection.reset
end
def request(params)
begin
response = @connection.request(params.merge({
:headers => {
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'X-Auth-Token' => @auth_token
}.merge!(params[:headers] || {}),
:host => @host,
:path => "#{@path}/#{params[:path]}"
}))
rescue Excon::Errors::Unauthorized => error
if error.response.body != 'Bad username or password' # token expiration
@clodo_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::Clodo::NotFound.slurp(error)
else
error
end
end
unless response.body.empty?
response.body = MultiJson.decode(response.body)
end
response
end
private
def authenticate
if @clodo_must_reauthenticate || @clodo_auth_token.nil?
options = {
:clodo_api_key => @clodo_api_key,
:clodo_username => @clodo_username,
:clodo_auth_url => @clodo_auth_url
}
credentials = Fog::Clodo.authenticate(options)
@auth_token = credentials['X-Auth-Token']
uri = URI.parse(credentials['X-Server-Management-Url'])
else
@auth_token = @clodo_auth_token
uri = URI.parse(@clodo_management_url)
end
@host = @clodo_servicenet == true ? "snet-#{uri.host}" : uri.host
@path = uri.path
@port = uri.port
@scheme = uri.scheme
end
end
end
end
end

View file

@ -0,0 +1,31 @@
require 'fog/core/model'
module Fog
module Compute
class Clodo
class Image < Fog::Model
identity :id
attribute :name
attribute :vps_type
attribute :status
attribute :os_type
attribute :os_bits
attribute :os_hvm
def initialize(new_attributes)
super(new_attributes)
merge_attributes(new_attributes['_attr']) if new_attributes['_attr']
end
def ready?
status == 'ACTIVE'
end
end
end
end
end

View file

@ -0,0 +1,28 @@
require 'fog/core/collection'
require 'fog/clodo/models/compute/image'
module Fog
module Compute
class Clodo
class Images < Fog::Collection
model Fog::Compute::Clodo::Image
def all
data = connection.list_images_detail.body['images']
load(data)
end
def get(image_id)
image = connection.get_image_details(image_id).body['image']
new(image) if image
rescue Fog::Compute::Clodo::NotFound
nil
end
end
end
end
end

View file

@ -0,0 +1,151 @@
require 'fog/core/model'
module Fog
module Compute
class Clodo
class Server < Fog::Model
identity :id
attribute :addresses
attribute :name
attribute :image_id, :aliases => 'imageId'
attribute :type
attribute :state, :aliases => 'status'
attribute :type
attribute :vps_memory
attribute :vps_memory_max
attribute :vps_os_title
attribute :vps_os_bits
attribute :vps_os_type
attribute :vps_vnc
attribute :vps_cpu_load
attribute :vps_cpu_max
attribute :vps_cpu_1h_min
attribute :vps_cpu_1h_max
attribute :vps_mem_load
attribute :vps_mem_max
attribute :vps_mem_1h_min
attribute :vps_mem_1h_max
attribute :vps_hdd_load
attribute :vps_hdd_max
attribute :vps_traf_rx
attribute :vps_traf_tx
attribute :vps_createdate
attribute :vps_billingdate
attribute :vps_update
attribute :vps_update_days
attribute :vps_root_pass, :aliases => ['adminPass','password']
attribute :vps_user_pass
attribute :vps_vnc_pass
attr_writer :private_key, :private_key_path, :public_key, :public_key_path, :username
def initialize(attributes={})
self.image_id ||= attributes[:vps_os] ? attributes[:vps_os] : 666
super attributes
end
def destroy
requires :id
connection.delete_server(id)
true
end
def image
requires :image_id
image_id # API does not support image details request. :-(
end
def private_ip_address
nil
end
def private_key_path
@private_key_path ||= Fog.credentials[:private_key_path]
@private_key_path &&= File.expand_path(@private_key_path)
end
def private_key
@private_key ||= private_key_path && File.read(private_key_path)
end
def public_ip_address
pubaddrs = addresses && addresses['public'] ? addresses['public'].select {|ip| ip['primary_ip']} : nil
pubaddrs && !pubaddrs.empty? ? pubaddrs.first['ip'] : nil
end
def public_key_path
@public_key_path ||= Fog.credentials[:public_key_path]
@public_key_path &&= File.expand_path(@public_key_path)
end
def public_key
@public_key ||= public_key_path && File.read(public_key_path)
end
def ready?
self.state == 'is_running'
end
def reboot(type = 'SOFT')
requires :id
connection.reboot_server(id, type)
true
end
def save
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
requires :image_id
data = connection.create_server(image_id, attributes)
merge_attributes(data.body['server'])
true
end
def setup(credentials = {})
requires :public_ip_address, :identity, :public_key, :username
Fog::SSH.new(public_ip_address, username, credentials).run([
%{mkdir .ssh},
%{echo "#{public_key}" >> ~/.ssh/authorized_keys},
%{passwd -l #{username}},
%{echo "#{MultiJson.encode(attributes)}" >> ~/attributes.json},
])
rescue Errno::ECONNREFUSED
sleep(1)
retry
end
def ssh(commands)
requires :public_ip_address, :identity, :username
options = {}
options[:key_data] = [private_key] if private_key
options[:password] = password if password
Fog::SSH.new(public_ip_address, username, options).run(commands)
end
def scp(local_path, remote_path, upload_options = {})
requires :public_ip_address, :username
scp_options = {}
scp_options[:key_data] = [private_key] if private_key
Fog::SCP.new(public_ip_address, username, scp_options).upload(local_path, remote_path, upload_options)
end
def username
@username ||= 'root'
end
def password
vps_root_pass
end
private
end
end
end
end

View file

@ -0,0 +1,36 @@
require 'fog/core/collection'
require 'fog/clodo/models/compute/server'
module Fog
module Compute
class Clodo
class Servers < Fog::Collection
model Fog::Compute::Clodo::Server
def all
data = connection.list_servers_detail.body['servers']
load(data)
end
def bootstrap(new_attributes = {})
server = create(new_attributes)
server.wait_for { ready? }
server.setup(:password => server.password)
server
end
def get(server_id)
if server = connection.get_server_details(server_id).body['server']
new(server)
end
rescue Fog::Compute::Clodo::NotFound
nil
end
end
end
end
end

View file

@ -0,0 +1,112 @@
module Fog
module Compute
class Clodo
class Real
# Input:
# vps_title - VDS title to display in VDS list
# vps_type - VDS type (VirtualServer,ScaleServer)
# vps_memory - memory size in megabytes (for ScaleServer - low limit)
# vps_memory_max - maximum number of ScaleServer memory megabytes to scale up.
# vps_hdd - Virtual HDD size im gigabytes.
# vps_admin - support level (1 - usual&free, 2 - extended, 3 - VIP)
# vps_os - OS ID to install
# Output:
# id - VDS ID
# name - VDS title
# imageId - OS ID
# adminPass - root password
def create_server(image_id, options = {})
data = {
'server' => {
:vps_os => image_id,
:vps_hdd => options[:vps_hdd]?options[:vps_hdd]:5,
:vps_memory => options[:vps_memory]?options[:vps_memory]:256,
:vps_memory_max => options[:vps_memory_max]?options[:vps_memory_max]:1024,
:vps_admin => options[:vps_admin]?options[:vps_admin]:1
}
}
data['server'].merge! options if options
request(
:body => MultiJson.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => 'servers'
)
end
end
class Mock
def create_server(image_id, options = {})
raise Excon::Errors::BadRequest.new("Invalid image ID") unless image_id > 0
response = Excon::Response.new
response.status = 202
id = Fog::Mock.random_numbers(6).to_i
data = {
'id' => id,
'imageId' => "#{image_id}",
'name' => options['name'] || "VPS #{rand(999)}",
'adminPass' => '23ryh8udbcbyt'
}
self.data[:last_modified][:servers][id] = Time.now
self.data[:servers][id] = {
'id' => "#{id}",
'imageId' => data['imageId'],
'name' => data['name'],
'vps_os_title' => "OSTitle",
'vps_root_pass' => data['adminPass'],
'status' => "is_running",
'addresses' => {'public' =>[{
'primary_ip' => true,
'isp' => false,
'ip' => '66.6.6.66'
},
{
'primary_ip' => false,
'isp' => false,
'ip' => '13.13.13.13'
}]},
'vps_createdate' => "#{Time.now}",
'vps_hdd_max' => "5",
'vps_traff' => nil,
'vps_mem_1h_max' => "0",
'vps_mem_load' => "0",
'vps_user_pass' => "wer45345ht",
'vps_vnc_pass' => "bi65tdfyb",
'vps_adddate' => "#{Time.now}",
'vps_update' => "#{Time.now}",
'vps_mem_1h_min' => "0",
'vps_mem_1h_avg' => nil,
'vps_memory_max' => options['vps_memory_max'] || "512",
'vps_os_version' => "6.6.6",
'vps_cpu_1h_max' => "0",
'vps_hdd_load' => "0",
'vps_disk_load' => "0",
'vps_os_type' => options['vps_os_type'] || "VirtualServer",
'type' => options['vps_os_type'] || "VirtualServer",
'vps_memory' => options['vps_memory'] || "512",
'vps_cpu_load' => "0",
'vps_update_days' => "0",
'vps_os_bits' => "64",
'vps_vnc' => "6.6.6.6:5900",
'vps_cpu_max' => "0",
'vps_cpu_1h_min' => "0",
'vps_cpu_1h_avg' => nil
}
response.body = { 'server' => data }
response
end
end
end
end
end

View file

@ -0,0 +1,43 @@
module Fog
module Compute
class Clodo
class Real
# Delete an existing server
#
# ==== Parameters
# * id<~Integer> - Id of server to delete
#
def delete_server(server_id)
request(
:expects => 204,
:method => 'DELETE',
:path => "servers/#{server_id}"
)
end
end
class Mock
def delete_server(server_id)
response = Excon::Response.new
if server = list_servers_detail.body['servers'].detect {|_| _['id'] == server_id}
if server['status'] == 'is_install'
response.status = 405
raise(Excon::Errors.status_error({:expects => 204}, response))
else
self.data[:last_modified][:servers].delete(server_id)
self.data[:servers].delete(server_id)
response.status = 204
end
response
else
raise Fog::Compute::Clodo::NotFound
end
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class Clodo
class Real
def get_image_details(image_id)
request(:expects => [200,203],
:method => 'GET',
:path => "images/#{image_id}")
end
end
class Mock
def get_image_details(image_id)
response = Excon::Response.new
response.status = 404
response
end
end
end
end
end

View file

@ -0,0 +1,49 @@
module Fog
module Compute
class Clodo
class Real
# Get details about a server
#
# ==== Parameters
# * server_id<~Integer> - Id of server to get details for
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'server'<~Hash>:
# * 'addresses'<~Hash>:
# * 'public'<~Array> - public address strings
# * 'private'<~Array> - private address strings
# * 'id'<~Integer> - Id of server
# * 'imageId'<~Integer> - Id of image used to boot server
# * 'name<~String> - Name of server
# * 'status'<~String> - Current server status
def get_server_details(server_id)
request(
:expects => [200, 203],
:method => 'GET',
:path => "servers/#{server_id}"
)
end
end
class Mock
def get_server_details(server_id)
response = Excon::Response.new
if server = list_servers_detail.body['servers'].detect {|_| _['id'] == "#{server_id}"}
response.status = [200, 203][rand(1)]
response.body = { 'server' => server }
response.body['server']['id'] = server['id'].to_i
response
else
raise Fog::Compute::Clodo::NotFound
end
end
end
end
end
end

View file

@ -0,0 +1,48 @@
module Fog
module Compute
class Clodo
class Real
# List all images (IDs and names only)
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'id'<~Integer> - Id of the image
# * 'name'<~String> - Name of the image
# * 'status'<~String> - Status of the image
# * 'vps_type'<~String> - VirtualServer or ScaleServer
def list_images
request(
:expects => [200, 203],
:method => 'GET',
:path => 'images'
)
end
end
class Mock
def list_images
response = Excon::Response.new
response.status = 200
response.body = {
'images' => [
{ 'name' => 'Debian 6 64 bits',
'id' => "541",
'vps_type' => 'ScaleServer',
'status' => 'ACTIVE' },
{ 'name' => 'CentOS 5.5 32 bits',
'id' => "31",
'vps_type' => 'VirtualServer',
'status' => 'ACTIVE' }
]
}
response
end
end
end
end
end

View file

@ -0,0 +1,61 @@
module Fog
module Compute
class Clodo
class Real
# List all images
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'os_type'<~String> - OS distribution
# * 'os_bits'<~Integer> - OS bits
# * 'os_hvm'<~Integer> - HVM flag
# * '_attr'<~Hash>:
# * 'id'<~Integer> - Id of the image
# * 'name'<~String> - Name of the image
# * 'status'<~String> - Status of the image
# * 'vps_type'<~String> - VirtualServer or ScaleServer
def list_images_detail
request(
:expects => [200, 203],
:method => 'GET',
:path => 'images/detail'
)
end
end
class Mock
def list_images_detail
response = Excon::Response.new
response.status = 200
response.body = {
'images' => [{ 'os_type' => 'debian',
'os_bits' => "64",
'os_hvm' => "0",
'_attr' => {
'id' => "541",
'name' => 'Debian 6 64 bits',
'status' => 'ACTIVE',
'vps_type' => 'ScaleServer'
}},
{ 'os_type' => 'centos',
'os_bits' => "32",
'os_hvm' => "0",
'_attr' => {
'name' => 'CentOS 5.5 32 bits',
'id' => "31",
'vps_type' => 'VirtualServer',
'status' => 'ACTIVE',
}}]
}
response
end
end
end
end
end

View file

@ -0,0 +1,50 @@
module Fog
module Compute
class Clodo
class Real
# List all servers (IDs and names only)
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'servers'<~Array>:
# * 'id'<~String> - Id of server
# * 'name'<~String> - Name of server
# * 'addresses'<~Hash>:
# * 'public'<~Array>:
# * 'dosprotect'<~Bool> - DDoS protection enabled
# * 'primary_ip'<~Bool> - Is a primary IP-address
# * 'isp'<~Bool> - ISPManager license enabled
# * 'ip'<~String> - IP-address
# * 'imageId'<~String> - ID of OS image installed
# * 'type'<~String> - Type (ScaleServer or Virtual Server)
# * 'status'<~String> - Server's status
def list_servers
request(
:expects => [200, 203],
:method => 'GET',
:path => 'servers'
)
end
end
class Mock
def list_servers
response = Excon::Response.new
data = list_servers_detail.body['servers']
servers = []
for server in data
servers << server.reject { |key, value| !['id', 'name', 'addresses', 'imageId', 'type', 'status', 'state'].include?(key) }
end
response.status = [200, 203][rand(1)]
response.body = { 'servers' => servers }
response
end
end
end
end
end

View file

@ -0,0 +1,51 @@
module Fog
module Compute
class Clodo
class Real
# List all servers details
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'servers'<~Array>:
# * 'id'<~Integer> - Id of server
# * 'name<~String> - Name of server
# * 'imageId'<~Integer> - Id of image used to boot server
# * 'status'<~String> - Current server status
# * 'addresses'<~Hash>:
# * 'public'<~Array> - public address strings
def list_servers_detail
request(
:expects => [200, 203],
:method => 'GET',
:path => 'servers/detail'
)
end
end
class Mock
def list_servers_detail
response = Excon::Response.new
servers = self.data[:servers].values
for server in servers
case server['status']
when 'is_install'
if Time.now - self.data[:last_modified][:servers][server['id']] > Fog::Mock.delay * 2
server['status'] = 'is_running'
end
end
end
response.status = [200, 203][rand(1)]
response.body = { 'servers' => servers }
response
end
end
end
end
end

View file

@ -0,0 +1,19 @@
module Fog
module Compute
class Clodo
class Real
def reboot_server(id, type)
body = {'reboot' => {}}
server_action(id, body)
end
end
class Mock
def reboot_server(id, type)
body = {'reboot' => {}}
server_action(id, body)
end
end
end
end
end

View file

@ -0,0 +1,21 @@
module Fog
module Compute
class Clodo
class Real
def rebuild_server(id, image_id, vps_isp = nil)
body = {'rebuild' => {'imageId' => image_id}}
body['rebuild']['vps_isp'] = vps_isp if vps_isp
server_action(id, body)
end
end
class Mock
def rebuild_server(id, image_id, vps_isp = nil)
body = {'rebuild' => {'imageId' => image_id}}
body['rebuild']['vps_isp'] = vps_isp if vps_isp
server_action(id, body)
end
end
end
end
end

View file

@ -0,0 +1,25 @@
module Fog
module Compute
class Clodo
class Real
def server_action(id, action)
request(
:body => MultiJson.encode(action),
:expects => [204],
:method => 'POST',
:path => "servers/#{id}/action")
end
end
class Mock
def server_action(id, action)
raise Excon::Errors::BadRequest.new("Invalid server id #{id}.") unless id > 0
response = Excon::Response.new
response.status = 204
response
end
end
end
end
end

View file

@ -0,0 +1,19 @@
module Fog
module Compute
class Clodo
class Real
def start_server(id)
body = {'start' => {}}
server_action(id, body)
end
end
class Mock
def start_server(id)
body = {'start' => {}}
server_action(id, body)
end
end
end
end
end

View file

@ -0,0 +1,19 @@
module Fog
module Compute
class Clodo
class Real
def stop_server(id)
body = {'stop' => {}}
server_action(id, body)
end
end
class Mock
def stop_server(id)
body = {'stop' => {}}
server_action(id, body)
end
end
end
end
end

View file

@ -17,6 +17,9 @@ module Fog
when :brightbox when :brightbox
require 'fog/brightbox/compute' require 'fog/brightbox/compute'
Fog::Compute::Brightbox.new(attributes) Fog::Compute::Brightbox.new(attributes)
when :clodo
require 'fog/clodo/compute'
Fog::Compute::Clodo.new(attributes)
when :ecloud when :ecloud
require 'fog/ecloud/compute' require 'fog/ecloud/compute'
Fog::Compute::Ecloud.new(attributes) Fog::Compute::Ecloud.new(attributes)

View file

@ -38,6 +38,8 @@ An alternate file may be used by placing its path in the FOG_RC environment vari
:bluebox_customer_id: :bluebox_customer_id:
:brightbox_client_id: :brightbox_client_id:
:brightbox_secret: :brightbox_secret:
:clodo_api_key:
:clodo_username:
:go_grid_api_key: :go_grid_api_key:
:go_grid_shared_secret: :go_grid_shared_secret:
:google_storage_access_key_id: :google_storage_access_key_id:

View file

@ -1,6 +1,7 @@
require 'fog/aws' require 'fog/aws'
require 'fog/bluebox' require 'fog/bluebox'
require 'fog/brightbox' require 'fog/brightbox'
require 'fog/clodo'
require 'fog/dnsimple' require 'fog/dnsimple'
require 'fog/dnsmadeeasy' require 'fog/dnsmadeeasy'
require 'fog/dynect' require 'fog/dynect'

View file

@ -0,0 +1,36 @@
Shindo.tests('Fog::Compute[:clodo] | image requests', ['clodo']) do
### Fog.mock!
clodo = Fog::Compute[:clodo]
@image_format = {
'id' => String,
'name' => String,
'status' => String,
'vps_type' => String
}
@image_details_format = {
'os_type' => String,
'os_bits' => String,
'os_hvm' => String,
'_attr' => @image_format
}
tests("success") do
tests("- list_images").formats([@image_format]) do
clodo.list_images.body['images']
end
tests("- list_images_detail").formats([@image_details_format]) do
clodo.list_images_detail.body['images']
end
end
tests("failure") do
tests("- get_image_details(541)").returns(nil) do
clodo.images.get(541)
end
end
end

View file

@ -0,0 +1,133 @@
Shindo.tests('Fog::Compute[:clodo] | server requests', ['clodo']) do
@ip_format = {
'primary_ip' => Fog::Boolean,
'isp' => Fog::Boolean,
'ip' => String
}
@server_format = {
'addresses' => {
'public' => [@ip_format]
},
'id' => String,
'imageId' => String,
'name' => String,
'type' => String,
'status' => String
}
@server_details_format = @server_format.merge({
'id' => Integer,
'vps_createdate' => String,
'vps_hdd_max' => String,
'vps_traff' => NilClass,
'vps_mem_1h_max' => String,
'vps_mem_load' => String,
'vps_user_pass' => String,
'vps_vnc_pass' => String,
'vps_adddate' => String,
'vps_os_title' => String,
'vps_update' => String,
'vps_mem_1h_min' => String,
'vps_mem_1h_avg' => NilClass,
'vps_memory_max' => String,
'vps_os_version' => String,
'vps_cpu_1h_max' => String,
'vps_hdd_load' => String,
'vps_disk_load' => String,
'vps_os_type' => String,
'vps_memory' => String,
'vps_cpu_load' => String,
'vps_update_days' => String,
'vps_os_bits' => String,
'vps_vnc' => String,
'vps_cpu_max' => String,
'vps_cpu_1h_min' => String,
'vps_cpu_1h_avg' => NilClass,
'vps_root_pass' => String
})
@server_create_format = {
'name' => String,
'adminPass' => String,
'imageId' => String,
'id' => Integer
}
# Fog.mock!
clodo = Fog::Compute[:clodo]
tests('success') do
tests('- create_server(541)').formats(@server_create_format) do
data = clodo.create_server(541,{:vps_type => 'ScaleServer'}).body['server']
@server_id = data['id']
data
end
tests('- list_servers(ready)').formats([@server_format]) do
clodo.list_servers.body['servers'].reject {|s| !['is_running', 'is_disabled'].include?(s['status']) }
end
tests('- list_servers(not ready)').formats([@server_format.merge({'addresses'=>{'public'=>NilClass}})]) do
clodo.list_servers.body['servers'].reject {|s| !['is_request'].include?(s['status']) }
end
clodo.servers.get(@server_id).wait_for { ready? || state == 'is_error' } unless Fog.mocking?
tests("- get_server_details(#{@server_id})").formats(@server_details_format) do
clodo.get_server_details(@server_id).body['server']
end
tests("- reboot_server(#{@server_id})").succeeds do
clodo.reboot_server(@server_id, :hard)
end
clodo.servers.get(@server_id).wait_for { ready? || state == 'is_error' } unless Fog.mocking?
tests("- stop_server(#{@server_id})").succeeds do
clodo.stop_server(@server_id)
end
unless Fog.mocking?
clodo.servers.get(@server_id).wait_for { state == 'is_disabled' || state == 'is_disabled' }
end
tests("- start_server(#{@server_id})").succeeds do
clodo.start_server(@server_id)
end
clodo.servers.get(@server_id).wait_for { ready? || state == 'is_error' } unless Fog.mocking?
tests("- delete_server(#{@server_id})").succeeds do
clodo.delete_server(@server_id)
end
end
tests('failure') do
tests('- create_server(0)').raises(Excon::Errors::BadRequest) do
data = clodo.create_server(0,{:vps_type => 'ScaleServer'}).body['server']
@server_id = data['id']
data
end
tests("- reboot_server(0)").raises(Excon::Errors::BadRequest) do
clodo.reboot_server(0, :hard)
end
tests("- stop_server(0)").raises(Excon::Errors::BadRequest) do
clodo.stop_server(0)
end
tests("- start_server(0)").raises(Excon::Errors::BadRequest) do
clodo.start_server(0)
end
## delete_server(0) in actial API, works not as it must,
## so I do not include this test in tests sequence.
# tests("- delete_server(0)").raises(Fog::Compute::Clodo::NotFound) do
# clodo.delete_server(0)
# end
end
end