From 05d1ae032d36e5f31098bd2f3a1cd1c06beba20d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D0=B1=D0=BE=D0=B5=D0=B2=20=D0=A0=D1=83=D0=BB=D0=BE?= =?UTF-8?q?=D0=BD=20=D0=B8=D0=B1=D0=BD=20=D0=A5=D0=B0=D1=82=D1=82=D0=B0?= =?UTF-8?q?=D0=B1?= Date: Sat, 1 Oct 2011 01:45:09 +0400 Subject: [PATCH 01/18] Add clodo support. --- lib/fog/bin/clodo.rb | 31 ++++ lib/fog/clodo.rb | 34 ++++ lib/fog/clodo/compute.rb | 150 +++++++++++++++++ lib/fog/clodo/models/compute/image.rb | 31 ++++ lib/fog/clodo/models/compute/images.rb | 25 +++ lib/fog/clodo/models/compute/server.rb | 157 ++++++++++++++++++ lib/fog/clodo/models/compute/servers.rb | 36 ++++ .../clodo/requests/compute/create_server.rb | 63 +++++++ .../clodo/requests/compute/delete_server.rb | 43 +++++ .../requests/compute/get_image_details.rb | 13 ++ .../requests/compute/get_server_details.rb | 48 ++++++ lib/fog/clodo/requests/compute/list_images.rb | 42 +++++ .../requests/compute/list_images_detail.rb | 47 ++++++ .../clodo/requests/compute/list_servers.rb | 41 +++++ .../requests/compute/list_servers_detail.rb | 53 ++++++ .../clodo/requests/compute/reboot_server.rb | 12 ++ .../clodo/requests/compute/rebuild_server.rb | 13 ++ .../clodo/requests/compute/server_action.rb | 15 ++ .../clodo/requests/compute/start_server.rb | 12 ++ lib/fog/clodo/requests/compute/stop_server.rb | 12 ++ 20 files changed, 878 insertions(+) create mode 100644 lib/fog/bin/clodo.rb create mode 100644 lib/fog/clodo.rb create mode 100644 lib/fog/clodo/compute.rb create mode 100644 lib/fog/clodo/models/compute/image.rb create mode 100644 lib/fog/clodo/models/compute/images.rb create mode 100644 lib/fog/clodo/models/compute/server.rb create mode 100644 lib/fog/clodo/models/compute/servers.rb create mode 100644 lib/fog/clodo/requests/compute/create_server.rb create mode 100644 lib/fog/clodo/requests/compute/delete_server.rb create mode 100644 lib/fog/clodo/requests/compute/get_image_details.rb create mode 100644 lib/fog/clodo/requests/compute/get_server_details.rb create mode 100644 lib/fog/clodo/requests/compute/list_images.rb create mode 100644 lib/fog/clodo/requests/compute/list_images_detail.rb create mode 100644 lib/fog/clodo/requests/compute/list_servers.rb create mode 100644 lib/fog/clodo/requests/compute/list_servers_detail.rb create mode 100644 lib/fog/clodo/requests/compute/reboot_server.rb create mode 100644 lib/fog/clodo/requests/compute/rebuild_server.rb create mode 100644 lib/fog/clodo/requests/compute/server_action.rb create mode 100644 lib/fog/clodo/requests/compute/start_server.rb create mode 100644 lib/fog/clodo/requests/compute/stop_server.rb diff --git a/lib/fog/bin/clodo.rb b/lib/fog/bin/clodo.rb new file mode 100644 index 000000000..fdad0e161 --- /dev/null +++ b/lib/fog/bin/clodo.rb @@ -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 diff --git a/lib/fog/clodo.rb b/lib/fog/clodo.rb new file mode 100644 index 000000000..744d968d0 --- /dev/null +++ b/lib/fog/clodo.rb @@ -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 diff --git a/lib/fog/clodo/compute.rb b/lib/fog/clodo/compute.rb new file mode 100644 index 000000000..ae48a8eef --- /dev/null +++ b/lib/fog/clodo/compute.rb @@ -0,0 +1,150 @@ +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]}", + :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 + @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 diff --git a/lib/fog/clodo/models/compute/image.rb b/lib/fog/clodo/models/compute/image.rb new file mode 100644 index 000000000..43ce2957e --- /dev/null +++ b/lib/fog/clodo/models/compute/image.rb @@ -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 diff --git a/lib/fog/clodo/models/compute/images.rb b/lib/fog/clodo/models/compute/images.rb new file mode 100644 index 000000000..c296ef0d7 --- /dev/null +++ b/lib/fog/clodo/models/compute/images.rb @@ -0,0 +1,25 @@ +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) + nil + end + + end + + end + end +end diff --git a/lib/fog/clodo/models/compute/server.rb b/lib/fog/clodo/models/compute/server.rb new file mode 100644 index 000000000..8d641ef0c --- /dev/null +++ b/lib/fog/clodo/models/compute/server.rb @@ -0,0 +1,157 @@ +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 :state, :aliases => 'status' + 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 + 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 + options = { + 'name' => name + } + options = options.reject {|key, value| value.nil?} + data = connection.create_server(image_id, options) + 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}, + %{echo "#{MultiJson.encode(metadata)}" >> ~/metadata.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 + 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 + +### def adminPass=(new_admin_pass) +### @vps_root_pass= new_admin_pass +### end + + end + + end + end + +end diff --git a/lib/fog/clodo/models/compute/servers.rb b/lib/fog/clodo/models/compute/servers.rb new file mode 100644 index 000000000..30d088d8b --- /dev/null +++ b/lib/fog/clodo/models/compute/servers.rb @@ -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 diff --git a/lib/fog/clodo/requests/compute/create_server.rb b/lib/fog/clodo/requests/compute/create_server.rb new file mode 100644 index 000000000..1e92a2aed --- /dev/null +++ b/lib/fog/clodo/requests/compute/create_server.rb @@ -0,0 +1,63 @@ +module Fog + module Compute + class Clodo + class Real + # Вход: + # name - название VPS + # vps_title - название VPS (может использоваться либо этот параметр, либо "name") + # vps_type - тип VPS (VirtualServer,ScaleServer) + # vps_memory - память (для ScaleServer - нижняя граница) (в MB) + # vps_memory_max - верхняя граница памяти для ScaleServer (в MB) + # vps_hdd - размер диска (в GB) + # vps_admin - тип поддержки (1 - обычная, 2 - расширенная, 3 - VIP) + # vps_os - id ОС + # Выход: + # id - номер VPS + # name - название VPS + # imageId - id ОС + # adminPass - пароль root + + def create_server(image_id, options = {}) + data = { + 'server' => { + :vps_os => image_id, + :vps_type => options[:vps_type]?options[:vps_type]:'ScaleServer', + :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 + class Mock + def create_server(image_id, options = {}) + response = Excon::response.new + response.status = 202 + + data = { + 'id' => Fog::Mock.random_numbers(6).to_i, + 'imageId' => image_id, + 'name' => options['name'] || "server_#{rand(999)}", + 'adminPass' => '23ryh8udbcbyt' + } + + self.data[:last_modified][:servers][data['id']] = Time.now + self.data[:servers][data['id']] = data + response.body = { 'server' => data.merge({'adminPass' => 'password'}) } + response + end + end + end + end + end +end diff --git a/lib/fog/clodo/requests/compute/delete_server.rb b/lib/fog/clodo/requests/compute/delete_server.rb new file mode 100644 index 000000000..ff277daa1 --- /dev/null +++ b/lib/fog/clodo/requests/compute/delete_server.rb @@ -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'] == 'BUILD' + 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 diff --git a/lib/fog/clodo/requests/compute/get_image_details.rb b/lib/fog/clodo/requests/compute/get_image_details.rb new file mode 100644 index 000000000..b71943224 --- /dev/null +++ b/lib/fog/clodo/requests/compute/get_image_details.rb @@ -0,0 +1,13 @@ +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 + end + end +end diff --git a/lib/fog/clodo/requests/compute/get_server_details.rb b/lib/fog/clodo/requests/compute/get_server_details.rb new file mode 100644 index 000000000..5c4641c6e --- /dev/null +++ b/lib/fog/clodo/requests/compute/get_server_details.rb @@ -0,0 +1,48 @@ +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 + else + raise Fog::Compute::Clodo::NotFound + end + end + + end + end + end +end diff --git a/lib/fog/clodo/requests/compute/list_images.rb b/lib/fog/clodo/requests/compute/list_images.rb new file mode 100644 index 000000000..460f32a0b --- /dev/null +++ b/lib/fog/clodo/requests/compute/list_images.rb @@ -0,0 +1,42 @@ +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 5.0.6 64 bits', 'id' => 1, 'vps_type' => 'VirtualServer', 'status' => 'ACTIVE' }, + { 'name' => 'CentOS 5.5 32 bits', 'id' => 3, 'vps_type' => 'VirtualServer', 'status' => 'ACTIVE' } + ] + } + response + end + + end + end + end +end diff --git a/lib/fog/clodo/requests/compute/list_images_detail.rb b/lib/fog/clodo/requests/compute/list_images_detail.rb new file mode 100644 index 000000000..d4a6c877d --- /dev/null +++ b/lib/fog/clodo/requests/compute/list_images_detail.rb @@ -0,0 +1,47 @@ +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' => [ + { 'name' => 'Debian 5.0.6 64 bits', 'id' => 1, 'vps_type' => 'VirtualServer', 'status' => 'ACTIVE', 'os_type' => 'debian', 'os_bits' => 64, 'os_hvm' => 0 }, + { 'name' => 'CentOS 5.5 32 bits', 'id' => 1, 'vps_type' => 'VirtualServer', 'status' => 'ACTIVE', 'os_type' => 'centos', 'os_bits' => 32, 'os_hvm' => 0 } + ] + } + response + end + + end + end + end +end diff --git a/lib/fog/clodo/requests/compute/list_servers.rb b/lib/fog/clodo/requests/compute/list_servers.rb new file mode 100644 index 000000000..676f1c42a --- /dev/null +++ b/lib/fog/clodo/requests/compute/list_servers.rb @@ -0,0 +1,41 @@ +module Fog + module Compute + class Clodo + class Real + + # List all servers (IDs and names only) + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'servers'<~Array>: + # * 'id'<~Integer> - Id of server + # * 'name<~String> - Name of server + 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'].include?(key) } + end + response.status = [200, 203][rand(1)] + response.body = { 'servers' => servers } + response + end + + end + end + end +end diff --git a/lib/fog/clodo/requests/compute/list_servers_detail.rb b/lib/fog/clodo/requests/compute/list_servers_detail.rb new file mode 100644 index 000000000..e285c0ce8 --- /dev/null +++ b/lib/fog/clodo/requests/compute/list_servers_detail.rb @@ -0,0 +1,53 @@ +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 + # * 'private'<~Array> - private address strings + # * 'metadata'<~Hash> - metadata + 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 'BUILD' + if Time.now - self.data[:last_modified][:servers][server['id']] > Fog::Mock.delay * 2 + server['status'] = 'ACTIVE' + end + end + end + + response.status = [200, 203][rand(1)] + response.body = { 'servers' => servers } + response + end + + end + end + end +end diff --git a/lib/fog/clodo/requests/compute/reboot_server.rb b/lib/fog/clodo/requests/compute/reboot_server.rb new file mode 100644 index 000000000..5a293600d --- /dev/null +++ b/lib/fog/clodo/requests/compute/reboot_server.rb @@ -0,0 +1,12 @@ +module Fog + module Compute + class Clodo + class Real + def reboot_server(id, type) + body = {'reboot' => {}} + server_action(id, body) + end + end + end + end +end diff --git a/lib/fog/clodo/requests/compute/rebuild_server.rb b/lib/fog/clodo/requests/compute/rebuild_server.rb new file mode 100644 index 000000000..cff426c8d --- /dev/null +++ b/lib/fog/clodo/requests/compute/rebuild_server.rb @@ -0,0 +1,13 @@ +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 + end + end +end diff --git a/lib/fog/clodo/requests/compute/server_action.rb b/lib/fog/clodo/requests/compute/server_action.rb new file mode 100644 index 000000000..861df5a1e --- /dev/null +++ b/lib/fog/clodo/requests/compute/server_action.rb @@ -0,0 +1,15 @@ +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 + end + end +end diff --git a/lib/fog/clodo/requests/compute/start_server.rb b/lib/fog/clodo/requests/compute/start_server.rb new file mode 100644 index 000000000..95f5357e2 --- /dev/null +++ b/lib/fog/clodo/requests/compute/start_server.rb @@ -0,0 +1,12 @@ +module Fog + module Compute + class Clodo + class Real + def start_server(id) + body = {'start' => {}} + server_action(id, body) + end + end + end + end +end diff --git a/lib/fog/clodo/requests/compute/stop_server.rb b/lib/fog/clodo/requests/compute/stop_server.rb new file mode 100644 index 000000000..0a0879b7b --- /dev/null +++ b/lib/fog/clodo/requests/compute/stop_server.rb @@ -0,0 +1,12 @@ +module Fog + module Compute + class Clodo + class Real + def stop_server(id) + body = {'stop' => {}} + server_action(id, body) + end + end + end + end +end From 86a435f7ab06381964977728d60e3ec255475f0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D0=B1=D0=BE=D0=B5=D0=B2=20=D0=A0=D1=83=D0=BB=D0=BE?= =?UTF-8?q?=D0=BD=20=D0=B8=D0=B1=D0=BD=20=D0=A5=D0=B0=D1=82=D1=82=D0=B0?= =?UTF-8?q?=D0=B1?= Date: Sat, 1 Oct 2011 01:46:05 +0400 Subject: [PATCH 02/18] Enable clodo support. --- lib/fog/bin.rb | 1 + lib/fog/compute.rb | 3 +++ lib/fog/core/errors.rb | 2 ++ lib/fog/providers.rb | 1 + 4 files changed, 7 insertions(+) diff --git a/lib/fog/bin.rb b/lib/fog/bin.rb index d0292a52d..4970bfeba 100644 --- a/lib/fog/bin.rb +++ b/lib/fog/bin.rb @@ -55,6 +55,7 @@ end require 'fog/bin/aws' require 'fog/bin/bluebox' require 'fog/bin/brightbox' +require 'fog/bin/clodo' require 'fog/bin/dnsimple' require 'fog/bin/dnsmadeeasy' require 'fog/bin/dynect' diff --git a/lib/fog/compute.rb b/lib/fog/compute.rb index 6f8b7fa8b..5e1d83b9e 100644 --- a/lib/fog/compute.rb +++ b/lib/fog/compute.rb @@ -17,6 +17,9 @@ module Fog when :brightbox require 'fog/brightbox/compute' Fog::Compute::Brightbox.new(attributes) + when :clodo + require 'fog/clodo/compute' + Fog::Compute::Clodo.new(attributes) when :ecloud require 'fog/ecloud/compute' Fog::Compute::Ecloud.new(attributes) diff --git a/lib/fog/core/errors.rb b/lib/fog/core/errors.rb index 8b3144fc8..bac559d7d 100644 --- a/lib/fog/core/errors.rb +++ b/lib/fog/core/errors.rb @@ -38,6 +38,8 @@ An alternate file may be used by placing its path in the FOG_RC environment vari :bluebox_customer_id: :brightbox_client_id: :brightbox_secret: + :clodo_api_key: + :clodo_username: :go_grid_api_key: :go_grid_shared_secret: :google_storage_access_key_id: diff --git a/lib/fog/providers.rb b/lib/fog/providers.rb index 212e450a0..914b422d7 100644 --- a/lib/fog/providers.rb +++ b/lib/fog/providers.rb @@ -1,6 +1,7 @@ require 'fog/aws' require 'fog/bluebox' require 'fog/brightbox' +require 'fog/clodo' require 'fog/dnsimple' require 'fog/dnsmadeeasy' require 'fog/dynect' From 712f11bbb452d62ffc592abede3d38f38c0c027e Mon Sep 17 00:00:00 2001 From: NomadRain Date: Wed, 5 Oct 2011 20:03:39 +0400 Subject: [PATCH 03/18] [clodo] : Added missing field. --- lib/fog/clodo/models/compute/server.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/fog/clodo/models/compute/server.rb b/lib/fog/clodo/models/compute/server.rb index 8d641ef0c..54a604dfb 100644 --- a/lib/fog/clodo/models/compute/server.rb +++ b/lib/fog/clodo/models/compute/server.rb @@ -11,6 +11,7 @@ module Fog attribute :addresses attribute :name attribute :image_id, :aliases => 'imageId' + attribute :type attribute :state, :aliases => 'status' attribute :vps_memory attribute :vps_memory_max From fe2dacec55f8fb5bcc8ba40e938c2e66d34e11dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D0=B1=D0=BE=D0=B5=D0=B2=20=D0=A0=D1=83=D0=BB=D0=BE?= =?UTF-8?q?=D0=BD=20=D0=B8=D0=B1=D0=BD=20=D0=A5=D0=B0=D1=82=D1=82=D0=B0?= =?UTF-8?q?=D0=B1?= Date: Thu, 6 Oct 2011 12:04:06 +0400 Subject: [PATCH 04/18] [clodo|compute] Added missing field (server.type) --- lib/fog/clodo/models/compute/server.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/fog/clodo/models/compute/server.rb b/lib/fog/clodo/models/compute/server.rb index 54a604dfb..413262abf 100644 --- a/lib/fog/clodo/models/compute/server.rb +++ b/lib/fog/clodo/models/compute/server.rb @@ -13,6 +13,7 @@ module Fog attribute :image_id, :aliases => 'imageId' attribute :type attribute :state, :aliases => 'status' + attribute :type attribute :vps_memory attribute :vps_memory_max attribute :vps_os_title From 0738a84cf6ce9c5a76661a9e7709be93075f11fc Mon Sep 17 00:00:00 2001 From: NomadRain Date: Fri, 14 Oct 2011 23:19:12 +0400 Subject: [PATCH 05/18] [clodo|compute] Bug fixes. --- lib/fog/clodo/models/compute/image.rb | 8 ++++---- lib/fog/clodo/models/compute/server.rb | 13 ++----------- lib/fog/clodo/requests/compute/create_server.rb | 1 - 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/lib/fog/clodo/models/compute/image.rb b/lib/fog/clodo/models/compute/image.rb index 43ce2957e..c0ac0dbe3 100644 --- a/lib/fog/clodo/models/compute/image.rb +++ b/lib/fog/clodo/models/compute/image.rb @@ -15,10 +15,10 @@ module Fog attribute :os_bits attribute :os_hvm - def initialize(new_attributes) - super(new_attributes) - merge_attributes(new_attributes['_attr']) if new_attributes['_attr'] - end + def initialize(new_attributes) + super(new_attributes) + merge_attributes(new_attributes['_attr']) if new_attributes['_attr'] + end def ready? status == 'ACTIVE' diff --git a/lib/fog/clodo/models/compute/server.rb b/lib/fog/clodo/models/compute/server.rb index 413262abf..33cc5dd18 100644 --- a/lib/fog/clodo/models/compute/server.rb +++ b/lib/fog/clodo/models/compute/server.rb @@ -44,7 +44,7 @@ module Fog def initialize(attributes={}) self.image_id ||= attributes[:vps_os] ? attributes[:vps_os] : 666 - super + super attributes end def destroy @@ -98,11 +98,7 @@ module Fog def save raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity requires :image_id - options = { - 'name' => name - } - options = options.reject {|key, value| value.nil?} - data = connection.create_server(image_id, options) + data = connection.create_server(image_id, attributes) merge_attributes(data.body['server']) true end @@ -114,7 +110,6 @@ module Fog %{echo "#{public_key}" >> ~/.ssh/authorized_keys}, %{passwd -l #{username}}, %{echo "#{MultiJson.encode(attributes)}" >> ~/attributes.json}, - %{echo "#{MultiJson.encode(metadata)}" >> ~/metadata.json} ]) rescue Errno::ECONNREFUSED sleep(1) @@ -147,10 +142,6 @@ module Fog private -### def adminPass=(new_admin_pass) -### @vps_root_pass= new_admin_pass -### end - end end diff --git a/lib/fog/clodo/requests/compute/create_server.rb b/lib/fog/clodo/requests/compute/create_server.rb index 1e92a2aed..670618534 100644 --- a/lib/fog/clodo/requests/compute/create_server.rb +++ b/lib/fog/clodo/requests/compute/create_server.rb @@ -21,7 +21,6 @@ module Fog data = { 'server' => { :vps_os => image_id, - :vps_type => options[:vps_type]?options[:vps_type]:'ScaleServer', :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, From d20d42c615d45145efa763b7161731eb97397460 Mon Sep 17 00:00:00 2001 From: Stepan G Fedorov Date: Thu, 20 Oct 2011 01:57:15 +0400 Subject: [PATCH 06/18] [clodo|compute] I don't know what is ignore_awful_caching, so i removed it. --- lib/fog/clodo/compute.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/fog/clodo/compute.rb b/lib/fog/clodo/compute.rb index ae48a8eef..15d96d631 100644 --- a/lib/fog/clodo/compute.rb +++ b/lib/fog/clodo/compute.rb @@ -97,8 +97,7 @@ module Fog 'X-Auth-Token' => @auth_token }.merge!(params[:headers] || {}), :host => @host, - :path => "#{@path}/#{params[:path]}", - :query => ('ignore_awful_caching' << Time.now.to_i.to_s) + :path => "#{@path}/#{params[:path]}" })) rescue Excon::Errors::Unauthorized => error if error.response.body != 'Bad username or password' # token expiration From 618f76e56b05c32951c6ea33332d9283032c535b Mon Sep 17 00:00:00 2001 From: Stepan G Fedorov Date: Mon, 24 Oct 2011 21:59:34 +0400 Subject: [PATCH 07/18] [clodo|compute] server.ssh with password. Not only with key. --- lib/fog/clodo/models/compute/server.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/fog/clodo/models/compute/server.rb b/lib/fog/clodo/models/compute/server.rb index 33cc5dd18..e7e1b7ae7 100644 --- a/lib/fog/clodo/models/compute/server.rb +++ b/lib/fog/clodo/models/compute/server.rb @@ -121,6 +121,7 @@ module Fog 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 From b09efb5448fb9510adfd1245f08f376b1540ba71 Mon Sep 17 00:00:00 2001 From: NomadRain Date: Sun, 30 Oct 2011 20:59:43 +0300 Subject: [PATCH 08/18] [clodo] Some cleanup before pool request. --- .../clodo/requests/compute/create_server.rb | 32 +++++++++---------- .../clodo/requests/compute/delete_server.rb | 2 +- .../requests/compute/get_image_details.rb | 29 ++++++++++------- lib/fog/clodo/requests/compute/list_images.rb | 14 ++++---- .../requests/compute/list_images_detail.rb | 24 +++++++++----- .../requests/compute/list_servers_detail.rb | 4 +-- .../clodo/requests/compute/server_action.rb | 7 ++++ 7 files changed, 67 insertions(+), 45 deletions(-) diff --git a/lib/fog/clodo/requests/compute/create_server.rb b/lib/fog/clodo/requests/compute/create_server.rb index 670618534..f091c07bc 100644 --- a/lib/fog/clodo/requests/compute/create_server.rb +++ b/lib/fog/clodo/requests/compute/create_server.rb @@ -2,20 +2,19 @@ module Fog module Compute class Clodo class Real - # Вход: - # name - название VPS - # vps_title - название VPS (может использоваться либо этот параметр, либо "name") - # vps_type - тип VPS (VirtualServer,ScaleServer) - # vps_memory - память (для ScaleServer - нижняя граница) (в MB) - # vps_memory_max - верхняя граница памяти для ScaleServer (в MB) - # vps_hdd - размер диска (в GB) - # vps_admin - тип поддержки (1 - обычная, 2 - расширенная, 3 - VIP) - # vps_os - id ОС - # Выход: - # id - номер VPS - # name - название VPS - # imageId - id ОС - # adminPass - пароль root + # 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 = { @@ -38,6 +37,7 @@ module Fog ) end + class Mock def create_server(image_id, options = {}) response = Excon::response.new @@ -46,13 +46,13 @@ module Fog data = { 'id' => Fog::Mock.random_numbers(6).to_i, 'imageId' => image_id, - 'name' => options['name'] || "server_#{rand(999)}", + 'name' => options['name'] || "VPS #{rand(999)}", 'adminPass' => '23ryh8udbcbyt' } self.data[:last_modified][:servers][data['id']] = Time.now self.data[:servers][data['id']] = data - response.body = { 'server' => data.merge({'adminPass' => 'password'}) } + response.body = { 'server' => data } response end end diff --git a/lib/fog/clodo/requests/compute/delete_server.rb b/lib/fog/clodo/requests/compute/delete_server.rb index ff277daa1..1add2f19e 100644 --- a/lib/fog/clodo/requests/compute/delete_server.rb +++ b/lib/fog/clodo/requests/compute/delete_server.rb @@ -23,7 +23,7 @@ module Fog def delete_server(server_id) response = Excon::Response.new if server = list_servers_detail.body['servers'].detect {|_| _['id'] == server_id} - if server['status'] == 'BUILD' + if server['status'] == 'is_install' response.status = 405 raise(Excon::Errors.status_error({:expects => 204}, response)) else diff --git a/lib/fog/clodo/requests/compute/get_image_details.rb b/lib/fog/clodo/requests/compute/get_image_details.rb index b71943224..ef0762e0d 100644 --- a/lib/fog/clodo/requests/compute/get_image_details.rb +++ b/lib/fog/clodo/requests/compute/get_image_details.rb @@ -1,13 +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 - end - end + 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 diff --git a/lib/fog/clodo/requests/compute/list_images.rb b/lib/fog/clodo/requests/compute/list_images.rb index 460f32a0b..5209432ae 100644 --- a/lib/fog/clodo/requests/compute/list_images.rb +++ b/lib/fog/clodo/requests/compute/list_images.rb @@ -14,10 +14,10 @@ module Fog # * 'vps_type'<~String> - VirtualServer or ScaleServer def list_images request( - :expects => [200, 203], - :method => 'GET', - :path => 'images' - ) + :expects => [200, 203], + :method => 'GET', + :path => 'images' + ) end end @@ -29,9 +29,9 @@ module Fog response.status = 200 response.body = { 'images' => [ - { 'name' => 'Debian 5.0.6 64 bits', 'id' => 1, 'vps_type' => 'VirtualServer', 'status' => 'ACTIVE' }, - { 'name' => 'CentOS 5.5 32 bits', 'id' => 3, 'vps_type' => 'VirtualServer', 'status' => 'ACTIVE' } - ] + { 'name' => 'Debian 5.0.6 64 bits', 'id' => 1, 'vps_type' => 'VirtualServer', 'status' => 'ACTIVE' }, + { 'name' => 'CentOS 5.5 32 bits', 'id' => 3, 'vps_type' => 'VirtualServer', 'status' => 'ACTIVE' } + ] } response end diff --git a/lib/fog/clodo/requests/compute/list_images_detail.rb b/lib/fog/clodo/requests/compute/list_images_detail.rb index d4a6c877d..e3d16be37 100644 --- a/lib/fog/clodo/requests/compute/list_images_detail.rb +++ b/lib/fog/clodo/requests/compute/list_images_detail.rb @@ -19,10 +19,10 @@ module Fog def list_images_detail request( - :expects => [200, 203], - :method => 'GET', - :path => 'images/detail' - ) + :expects => [200, 203], + :method => 'GET', + :path => 'images/detail' + ) end end @@ -33,10 +33,18 @@ module Fog response = Excon::Response.new response.status = 200 response.body = { - 'images' => [ - { 'name' => 'Debian 5.0.6 64 bits', 'id' => 1, 'vps_type' => 'VirtualServer', 'status' => 'ACTIVE', 'os_type' => 'debian', 'os_bits' => 64, 'os_hvm' => 0 }, - { 'name' => 'CentOS 5.5 32 bits', 'id' => 1, 'vps_type' => 'VirtualServer', 'status' => 'ACTIVE', 'os_type' => 'centos', 'os_bits' => 32, 'os_hvm' => 0 } - ] + 'images' => [{ 'os_type' => 'debian', 'os_bits' => 64, 'os_hvm' => 0, '_attr' => { + 'id' => 1, + 'name' => 'Debian 5.0.6 64 bits', + 'status' => 'ACTIVE', + 'vps_type' => 'VirtualServer' + }}, + { 'os_type' => 'centos', 'os_bits' => 32, 'os_hvm' => 0, '_attr' => { + 'name' => 'CentOS 5.5 32 bits', + 'id' => 1, + 'vps_type' => 'VirtualServer', + 'status' => 'ACTIVE', + }}] } response end diff --git a/lib/fog/clodo/requests/compute/list_servers_detail.rb b/lib/fog/clodo/requests/compute/list_servers_detail.rb index e285c0ce8..2435de125 100644 --- a/lib/fog/clodo/requests/compute/list_servers_detail.rb +++ b/lib/fog/clodo/requests/compute/list_servers_detail.rb @@ -35,9 +35,9 @@ module Fog servers = self.data[:servers].values for server in servers case server['status'] - when 'BUILD' + when 'is_install' if Time.now - self.data[:last_modified][:servers][server['id']] > Fog::Mock.delay * 2 - server['status'] = 'ACTIVE' + server['status'] = 'is_running' end end end diff --git a/lib/fog/clodo/requests/compute/server_action.rb b/lib/fog/clodo/requests/compute/server_action.rb index 861df5a1e..64c4fe055 100644 --- a/lib/fog/clodo/requests/compute/server_action.rb +++ b/lib/fog/clodo/requests/compute/server_action.rb @@ -10,6 +10,13 @@ module Fog :path => "servers/#{id}/action") end end + class Mock + def server_action(id, action) + response = Excon::Response.new + response.status = 204 + response + end + end end end end From da50ea9fcef5969db99444981e9d53ddaf526856 Mon Sep 17 00:00:00 2001 From: Stepan G Fedorov Date: Fri, 4 Nov 2011 01:42:56 +0400 Subject: [PATCH 09/18] [tests | clodo | compute ] create_server - First try. --- tests/clodo/requests/compute/server_tests.rb | 38 ++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/clodo/requests/compute/server_tests.rb diff --git a/tests/clodo/requests/compute/server_tests.rb b/tests/clodo/requests/compute/server_tests.rb new file mode 100644 index 000000000..a19b3d35b --- /dev/null +++ b/tests/clodo/requests/compute/server_tests.rb @@ -0,0 +1,38 @@ +Shindo.tests('Fog::Compute[:clodo] | server requests', ['clodo']) do + + @server_format = { + 'addresses' => { + 'public' => [String] + }, + 'id' => Integer, + 'imageId' => Integer, + 'name' => String, + 'status' => String + } + + @server_create_format = { + 'name' => String, + 'adminPass' => String, + 'imageId' => String, + 'id' => Integer + } + + + @clodo = Fog::Compute::Clodo.new + + 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 + 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 + end +end From 1059a90a534761693addde230eefdaa1e4352860 Mon Sep 17 00:00:00 2001 From: "Stepan G. Fedorov" Date: Tue, 8 Nov 2011 15:01:58 +0400 Subject: [PATCH 10/18] [clodo|compute] Actualize Mocks. --- .../clodo/requests/compute/create_server.rb | 82 +++++++++++++++---- .../requests/compute/get_server_details.rb | 3 +- lib/fog/clodo/requests/compute/list_images.rb | 10 ++- .../requests/compute/list_images_detail.rb | 18 ++-- .../clodo/requests/compute/list_servers.rb | 15 +++- .../requests/compute/list_servers_detail.rb | 2 - .../clodo/requests/compute/reboot_server.rb | 7 ++ .../clodo/requests/compute/rebuild_server.rb | 8 ++ .../clodo/requests/compute/server_action.rb | 3 + .../clodo/requests/compute/start_server.rb | 7 ++ lib/fog/clodo/requests/compute/stop_server.rb | 7 ++ 11 files changed, 133 insertions(+), 29 deletions(-) diff --git a/lib/fog/clodo/requests/compute/create_server.rb b/lib/fog/clodo/requests/compute/create_server.rb index f091c07bc..6cbf82bdf 100644 --- a/lib/fog/clodo/requests/compute/create_server.rb +++ b/lib/fog/clodo/requests/compute/create_server.rb @@ -37,26 +37,78 @@ module Fog ) end + end - class Mock - def create_server(image_id, options = {}) - response = Excon::response.new - response.status = 202 + class Mock + def create_server(image_id, options = {}) - data = { - 'id' => Fog::Mock.random_numbers(6).to_i, - 'imageId' => image_id, - 'name' => options['name'] || "VPS #{rand(999)}", - 'adminPass' => '23ryh8udbcbyt' - } + raise Excon::Errors::BadRequest.new("Invalid image ID") unless image_id > 0 - self.data[:last_modified][:servers][data['id']] = Time.now - self.data[:servers][data['id']] = data - response.body = { 'server' => data } - response - end + 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' =>[{ + 'ddosprotect' => false, + 'primary_ip' => true, + 'isp' => false, + 'ip' => '66.6.6.66' + }, + { + 'ddosprotect' => false, + '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 + diff --git a/lib/fog/clodo/requests/compute/get_server_details.rb b/lib/fog/clodo/requests/compute/get_server_details.rb index 5c4641c6e..83a33eeac 100644 --- a/lib/fog/clodo/requests/compute/get_server_details.rb +++ b/lib/fog/clodo/requests/compute/get_server_details.rb @@ -33,9 +33,10 @@ module Fog def get_server_details(server_id) response = Excon::Response.new - if server = list_servers_detail.body['servers'].detect {|_| _['id'] == server_id} + 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 diff --git a/lib/fog/clodo/requests/compute/list_images.rb b/lib/fog/clodo/requests/compute/list_images.rb index 5209432ae..c03e032ff 100644 --- a/lib/fog/clodo/requests/compute/list_images.rb +++ b/lib/fog/clodo/requests/compute/list_images.rb @@ -29,8 +29,14 @@ module Fog response.status = 200 response.body = { 'images' => [ - { 'name' => 'Debian 5.0.6 64 bits', 'id' => 1, 'vps_type' => 'VirtualServer', 'status' => 'ACTIVE' }, - { 'name' => 'CentOS 5.5 32 bits', 'id' => 3, 'vps_type' => 'VirtualServer', 'status' => 'ACTIVE' } + { '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 diff --git a/lib/fog/clodo/requests/compute/list_images_detail.rb b/lib/fog/clodo/requests/compute/list_images_detail.rb index e3d16be37..5c04ee4f9 100644 --- a/lib/fog/clodo/requests/compute/list_images_detail.rb +++ b/lib/fog/clodo/requests/compute/list_images_detail.rb @@ -33,15 +33,21 @@ module Fog response = Excon::Response.new response.status = 200 response.body = { - 'images' => [{ 'os_type' => 'debian', 'os_bits' => 64, 'os_hvm' => 0, '_attr' => { - 'id' => 1, - 'name' => 'Debian 5.0.6 64 bits', + 'images' => [{ 'os_type' => 'debian', + 'os_bits' => "64", + 'os_hvm' => "0", + '_attr' => { + 'id' => "541", + 'name' => 'Debian 6 64 bits', 'status' => 'ACTIVE', - 'vps_type' => 'VirtualServer' + 'vps_type' => 'ScaleServer' }}, - { 'os_type' => 'centos', 'os_bits' => 32, 'os_hvm' => 0, '_attr' => { + { 'os_type' => 'centos', + 'os_bits' => "32", + 'os_hvm' => "0", + '_attr' => { 'name' => 'CentOS 5.5 32 bits', - 'id' => 1, + 'id' => "31", 'vps_type' => 'VirtualServer', 'status' => 'ACTIVE', }}] diff --git a/lib/fog/clodo/requests/compute/list_servers.rb b/lib/fog/clodo/requests/compute/list_servers.rb index 676f1c42a..7342f7621 100644 --- a/lib/fog/clodo/requests/compute/list_servers.rb +++ b/lib/fog/clodo/requests/compute/list_servers.rb @@ -9,8 +9,17 @@ module Fog # * response<~Excon::Response>: # * body<~Hash>: # * 'servers'<~Array>: - # * 'id'<~Integer> - Id of server - # * 'name<~String> - Name of server + # * '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], @@ -28,7 +37,7 @@ module Fog data = list_servers_detail.body['servers'] servers = [] for server in data - servers << server.reject { |key, value| !['id', 'name'].include?(key) } + 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 } diff --git a/lib/fog/clodo/requests/compute/list_servers_detail.rb b/lib/fog/clodo/requests/compute/list_servers_detail.rb index 2435de125..e64bda0d9 100644 --- a/lib/fog/clodo/requests/compute/list_servers_detail.rb +++ b/lib/fog/clodo/requests/compute/list_servers_detail.rb @@ -15,8 +15,6 @@ module Fog # * 'status'<~String> - Current server status # * 'addresses'<~Hash>: # * 'public'<~Array> - public address strings - # * 'private'<~Array> - private address strings - # * 'metadata'<~Hash> - metadata def list_servers_detail request( :expects => [200, 203], diff --git a/lib/fog/clodo/requests/compute/reboot_server.rb b/lib/fog/clodo/requests/compute/reboot_server.rb index 5a293600d..ecdce9f89 100644 --- a/lib/fog/clodo/requests/compute/reboot_server.rb +++ b/lib/fog/clodo/requests/compute/reboot_server.rb @@ -7,6 +7,13 @@ module Fog server_action(id, body) end end + + class Mock + def reboot_server(id, type) + body = {'reboot' => {}} + server_action(id, body) + end + end end end end diff --git a/lib/fog/clodo/requests/compute/rebuild_server.rb b/lib/fog/clodo/requests/compute/rebuild_server.rb index cff426c8d..c07610e56 100644 --- a/lib/fog/clodo/requests/compute/rebuild_server.rb +++ b/lib/fog/clodo/requests/compute/rebuild_server.rb @@ -8,6 +8,14 @@ module Fog 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 diff --git a/lib/fog/clodo/requests/compute/server_action.rb b/lib/fog/clodo/requests/compute/server_action.rb index 64c4fe055..5f2ef7208 100644 --- a/lib/fog/clodo/requests/compute/server_action.rb +++ b/lib/fog/clodo/requests/compute/server_action.rb @@ -12,6 +12,9 @@ module Fog 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 diff --git a/lib/fog/clodo/requests/compute/start_server.rb b/lib/fog/clodo/requests/compute/start_server.rb index 95f5357e2..079a20ff7 100644 --- a/lib/fog/clodo/requests/compute/start_server.rb +++ b/lib/fog/clodo/requests/compute/start_server.rb @@ -7,6 +7,13 @@ module Fog server_action(id, body) end end + + class Mock + def start_server(id) + body = {'start' => {}} + server_action(id, body) + end + end end end end diff --git a/lib/fog/clodo/requests/compute/stop_server.rb b/lib/fog/clodo/requests/compute/stop_server.rb index 0a0879b7b..f8f0fba44 100644 --- a/lib/fog/clodo/requests/compute/stop_server.rb +++ b/lib/fog/clodo/requests/compute/stop_server.rb @@ -7,6 +7,13 @@ module Fog server_action(id, body) end end + + class Mock + def stop_server(id) + body = {'stop' => {}} + server_action(id, body) + end + end end end end From f1ef97f59f9ad3b10eacdcdf0b1f21b6926c6509 Mon Sep 17 00:00:00 2001 From: "Stepan G. Fedorov" Date: Tue, 8 Nov 2011 15:05:41 +0400 Subject: [PATCH 11/18] [clodo|compute] Enable :get_image_details --- lib/fog/clodo/compute.rb | 2 +- lib/fog/clodo/models/compute/images.rb | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/fog/clodo/compute.rb b/lib/fog/clodo/compute.rb index 15d96d631..48b4fbd39 100644 --- a/lib/fog/clodo/compute.rb +++ b/lib/fog/clodo/compute.rb @@ -15,7 +15,7 @@ module Fog request_path 'fog/clodo/requests/compute' request :create_server request :delete_server -# request :get_image_details # Not supported by API + request :get_image_details # Not supported by API request :list_images request :list_images_detail request :list_servers diff --git a/lib/fog/clodo/models/compute/images.rb b/lib/fog/clodo/models/compute/images.rb index c296ef0d7..a6b1843af 100644 --- a/lib/fog/clodo/models/compute/images.rb +++ b/lib/fog/clodo/models/compute/images.rb @@ -15,6 +15,9 @@ module Fog 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 From adabe65aba138c22a050df096b8076af5497d695 Mon Sep 17 00:00:00 2001 From: "Stepan G. Fedorov" Date: Tue, 8 Nov 2011 15:06:12 +0400 Subject: [PATCH 12/18] [clodo|compute] Add tests. --- tests/clodo/requests/compute/image_tests.rb | 36 +++++++ tests/clodo/requests/compute/server_tests.rb | 108 +++++++++++++++++-- 2 files changed, 138 insertions(+), 6 deletions(-) create mode 100644 tests/clodo/requests/compute/image_tests.rb diff --git a/tests/clodo/requests/compute/image_tests.rb b/tests/clodo/requests/compute/image_tests.rb new file mode 100644 index 000000000..3465e3c8f --- /dev/null +++ b/tests/clodo/requests/compute/image_tests.rb @@ -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 diff --git a/tests/clodo/requests/compute/server_tests.rb b/tests/clodo/requests/compute/server_tests.rb index a19b3d35b..17921d4ff 100644 --- a/tests/clodo/requests/compute/server_tests.rb +++ b/tests/clodo/requests/compute/server_tests.rb @@ -1,15 +1,54 @@ Shindo.tests('Fog::Compute[:clodo] | server requests', ['clodo']) do + @ip_format = { + 'ddosprotect' => Fog::Boolean, + 'primary_ip' => Fog::Boolean, + 'isp' => Fog::Boolean, + 'ip' => String + } + @server_format = { 'addresses' => { - 'public' => [String] + 'public' => [@ip_format] }, - 'id' => Integer, - 'imageId' => Integer, + '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, @@ -17,22 +56,79 @@ Shindo.tests('Fog::Compute[:clodo] | server requests', ['clodo']) do 'id' => Integer } +# Fog.mock! - @clodo = Fog::Compute::Clodo.new + 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'] + 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'] + 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 From 3fc8d8a28b2b8b43e4d14853a772699f1f4505be Mon Sep 17 00:00:00 2001 From: Stepan G Fedorov Date: Tue, 8 Nov 2011 15:37:21 +0400 Subject: [PATCH 13/18] [clodo|compute] Fix Mocks. --- .../clodo/requests/compute/create_server.rb | 82 +++++++++++++++---- .../requests/compute/get_server_details.rb | 3 +- lib/fog/clodo/requests/compute/list_images.rb | 10 ++- .../requests/compute/list_images_detail.rb | 18 ++-- .../clodo/requests/compute/list_servers.rb | 15 +++- .../requests/compute/list_servers_detail.rb | 2 - .../clodo/requests/compute/reboot_server.rb | 7 ++ .../clodo/requests/compute/rebuild_server.rb | 8 ++ .../clodo/requests/compute/server_action.rb | 3 + .../clodo/requests/compute/start_server.rb | 7 ++ lib/fog/clodo/requests/compute/stop_server.rb | 7 ++ 11 files changed, 133 insertions(+), 29 deletions(-) diff --git a/lib/fog/clodo/requests/compute/create_server.rb b/lib/fog/clodo/requests/compute/create_server.rb index f091c07bc..6cbf82bdf 100644 --- a/lib/fog/clodo/requests/compute/create_server.rb +++ b/lib/fog/clodo/requests/compute/create_server.rb @@ -37,26 +37,78 @@ module Fog ) end + end - class Mock - def create_server(image_id, options = {}) - response = Excon::response.new - response.status = 202 + class Mock + def create_server(image_id, options = {}) - data = { - 'id' => Fog::Mock.random_numbers(6).to_i, - 'imageId' => image_id, - 'name' => options['name'] || "VPS #{rand(999)}", - 'adminPass' => '23ryh8udbcbyt' - } + raise Excon::Errors::BadRequest.new("Invalid image ID") unless image_id > 0 - self.data[:last_modified][:servers][data['id']] = Time.now - self.data[:servers][data['id']] = data - response.body = { 'server' => data } - response - end + 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' =>[{ + 'ddosprotect' => false, + 'primary_ip' => true, + 'isp' => false, + 'ip' => '66.6.6.66' + }, + { + 'ddosprotect' => false, + '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 + diff --git a/lib/fog/clodo/requests/compute/get_server_details.rb b/lib/fog/clodo/requests/compute/get_server_details.rb index 5c4641c6e..83a33eeac 100644 --- a/lib/fog/clodo/requests/compute/get_server_details.rb +++ b/lib/fog/clodo/requests/compute/get_server_details.rb @@ -33,9 +33,10 @@ module Fog def get_server_details(server_id) response = Excon::Response.new - if server = list_servers_detail.body['servers'].detect {|_| _['id'] == server_id} + 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 diff --git a/lib/fog/clodo/requests/compute/list_images.rb b/lib/fog/clodo/requests/compute/list_images.rb index 5209432ae..c03e032ff 100644 --- a/lib/fog/clodo/requests/compute/list_images.rb +++ b/lib/fog/clodo/requests/compute/list_images.rb @@ -29,8 +29,14 @@ module Fog response.status = 200 response.body = { 'images' => [ - { 'name' => 'Debian 5.0.6 64 bits', 'id' => 1, 'vps_type' => 'VirtualServer', 'status' => 'ACTIVE' }, - { 'name' => 'CentOS 5.5 32 bits', 'id' => 3, 'vps_type' => 'VirtualServer', 'status' => 'ACTIVE' } + { '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 diff --git a/lib/fog/clodo/requests/compute/list_images_detail.rb b/lib/fog/clodo/requests/compute/list_images_detail.rb index e3d16be37..5c04ee4f9 100644 --- a/lib/fog/clodo/requests/compute/list_images_detail.rb +++ b/lib/fog/clodo/requests/compute/list_images_detail.rb @@ -33,15 +33,21 @@ module Fog response = Excon::Response.new response.status = 200 response.body = { - 'images' => [{ 'os_type' => 'debian', 'os_bits' => 64, 'os_hvm' => 0, '_attr' => { - 'id' => 1, - 'name' => 'Debian 5.0.6 64 bits', + 'images' => [{ 'os_type' => 'debian', + 'os_bits' => "64", + 'os_hvm' => "0", + '_attr' => { + 'id' => "541", + 'name' => 'Debian 6 64 bits', 'status' => 'ACTIVE', - 'vps_type' => 'VirtualServer' + 'vps_type' => 'ScaleServer' }}, - { 'os_type' => 'centos', 'os_bits' => 32, 'os_hvm' => 0, '_attr' => { + { 'os_type' => 'centos', + 'os_bits' => "32", + 'os_hvm' => "0", + '_attr' => { 'name' => 'CentOS 5.5 32 bits', - 'id' => 1, + 'id' => "31", 'vps_type' => 'VirtualServer', 'status' => 'ACTIVE', }}] diff --git a/lib/fog/clodo/requests/compute/list_servers.rb b/lib/fog/clodo/requests/compute/list_servers.rb index 676f1c42a..7342f7621 100644 --- a/lib/fog/clodo/requests/compute/list_servers.rb +++ b/lib/fog/clodo/requests/compute/list_servers.rb @@ -9,8 +9,17 @@ module Fog # * response<~Excon::Response>: # * body<~Hash>: # * 'servers'<~Array>: - # * 'id'<~Integer> - Id of server - # * 'name<~String> - Name of server + # * '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], @@ -28,7 +37,7 @@ module Fog data = list_servers_detail.body['servers'] servers = [] for server in data - servers << server.reject { |key, value| !['id', 'name'].include?(key) } + 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 } diff --git a/lib/fog/clodo/requests/compute/list_servers_detail.rb b/lib/fog/clodo/requests/compute/list_servers_detail.rb index 2435de125..e64bda0d9 100644 --- a/lib/fog/clodo/requests/compute/list_servers_detail.rb +++ b/lib/fog/clodo/requests/compute/list_servers_detail.rb @@ -15,8 +15,6 @@ module Fog # * 'status'<~String> - Current server status # * 'addresses'<~Hash>: # * 'public'<~Array> - public address strings - # * 'private'<~Array> - private address strings - # * 'metadata'<~Hash> - metadata def list_servers_detail request( :expects => [200, 203], diff --git a/lib/fog/clodo/requests/compute/reboot_server.rb b/lib/fog/clodo/requests/compute/reboot_server.rb index 5a293600d..ecdce9f89 100644 --- a/lib/fog/clodo/requests/compute/reboot_server.rb +++ b/lib/fog/clodo/requests/compute/reboot_server.rb @@ -7,6 +7,13 @@ module Fog server_action(id, body) end end + + class Mock + def reboot_server(id, type) + body = {'reboot' => {}} + server_action(id, body) + end + end end end end diff --git a/lib/fog/clodo/requests/compute/rebuild_server.rb b/lib/fog/clodo/requests/compute/rebuild_server.rb index cff426c8d..c07610e56 100644 --- a/lib/fog/clodo/requests/compute/rebuild_server.rb +++ b/lib/fog/clodo/requests/compute/rebuild_server.rb @@ -8,6 +8,14 @@ module Fog 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 diff --git a/lib/fog/clodo/requests/compute/server_action.rb b/lib/fog/clodo/requests/compute/server_action.rb index 64c4fe055..5f2ef7208 100644 --- a/lib/fog/clodo/requests/compute/server_action.rb +++ b/lib/fog/clodo/requests/compute/server_action.rb @@ -12,6 +12,9 @@ module Fog 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 diff --git a/lib/fog/clodo/requests/compute/start_server.rb b/lib/fog/clodo/requests/compute/start_server.rb index 95f5357e2..079a20ff7 100644 --- a/lib/fog/clodo/requests/compute/start_server.rb +++ b/lib/fog/clodo/requests/compute/start_server.rb @@ -7,6 +7,13 @@ module Fog server_action(id, body) end end + + class Mock + def start_server(id) + body = {'start' => {}} + server_action(id, body) + end + end end end end diff --git a/lib/fog/clodo/requests/compute/stop_server.rb b/lib/fog/clodo/requests/compute/stop_server.rb index 0a0879b7b..f8f0fba44 100644 --- a/lib/fog/clodo/requests/compute/stop_server.rb +++ b/lib/fog/clodo/requests/compute/stop_server.rb @@ -7,6 +7,13 @@ module Fog server_action(id, body) end end + + class Mock + def stop_server(id) + body = {'stop' => {}} + server_action(id, body) + end + end end end end From d373fed0706635c767fbeef1a1de5c19007eff63 Mon Sep 17 00:00:00 2001 From: Stepan G Fedorov Date: Tue, 8 Nov 2011 15:38:41 +0400 Subject: [PATCH 14/18] [clodo|compute] Enable get_image_details. --- lib/fog/clodo/compute.rb | 2 +- lib/fog/clodo/models/compute/images.rb | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/fog/clodo/compute.rb b/lib/fog/clodo/compute.rb index 15d96d631..48b4fbd39 100644 --- a/lib/fog/clodo/compute.rb +++ b/lib/fog/clodo/compute.rb @@ -15,7 +15,7 @@ module Fog request_path 'fog/clodo/requests/compute' request :create_server request :delete_server -# request :get_image_details # Not supported by API + request :get_image_details # Not supported by API request :list_images request :list_images_detail request :list_servers diff --git a/lib/fog/clodo/models/compute/images.rb b/lib/fog/clodo/models/compute/images.rb index c296ef0d7..a6b1843af 100644 --- a/lib/fog/clodo/models/compute/images.rb +++ b/lib/fog/clodo/models/compute/images.rb @@ -15,6 +15,9 @@ module Fog 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 From 098291eb628f1d02ab915a5d7cf1c888c2811ce9 Mon Sep 17 00:00:00 2001 From: Stepan G Fedorov Date: Tue, 8 Nov 2011 15:39:30 +0400 Subject: [PATCH 15/18] [tests | clodo | compute] Add most tests. --- tests/clodo/requests/compute/server_tests.rb | 108 +++++++++++++++++-- 1 file changed, 102 insertions(+), 6 deletions(-) diff --git a/tests/clodo/requests/compute/server_tests.rb b/tests/clodo/requests/compute/server_tests.rb index a19b3d35b..17921d4ff 100644 --- a/tests/clodo/requests/compute/server_tests.rb +++ b/tests/clodo/requests/compute/server_tests.rb @@ -1,15 +1,54 @@ Shindo.tests('Fog::Compute[:clodo] | server requests', ['clodo']) do + @ip_format = { + 'ddosprotect' => Fog::Boolean, + 'primary_ip' => Fog::Boolean, + 'isp' => Fog::Boolean, + 'ip' => String + } + @server_format = { 'addresses' => { - 'public' => [String] + 'public' => [@ip_format] }, - 'id' => Integer, - 'imageId' => Integer, + '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, @@ -17,22 +56,79 @@ Shindo.tests('Fog::Compute[:clodo] | server requests', ['clodo']) do 'id' => Integer } +# Fog.mock! - @clodo = Fog::Compute::Clodo.new + 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'] + 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'] + 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 From 3720571685d205defc7ef05bf4ca8baed2aad36b Mon Sep 17 00:00:00 2001 From: Stepan G Fedorov Date: Tue, 8 Nov 2011 15:40:31 +0400 Subject: [PATCH 16/18] [tests | clodo | compute] Add image tests. --- tests/clodo/requests/compute/image_tests.rb | 36 +++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tests/clodo/requests/compute/image_tests.rb diff --git a/tests/clodo/requests/compute/image_tests.rb b/tests/clodo/requests/compute/image_tests.rb new file mode 100644 index 000000000..3465e3c8f --- /dev/null +++ b/tests/clodo/requests/compute/image_tests.rb @@ -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 From b5dfb2f9688a96c48e31dd285870bd08b7b65f83 Mon Sep 17 00:00:00 2001 From: "Stepan G. Fedorov" Date: Fri, 25 Nov 2011 12:37:04 +0400 Subject: [PATCH 17/18] [tests | clodo ] ddosprotect field must not exist. --- tests/clodo/requests/compute/server_tests.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/clodo/requests/compute/server_tests.rb b/tests/clodo/requests/compute/server_tests.rb index 17921d4ff..4f192534a 100644 --- a/tests/clodo/requests/compute/server_tests.rb +++ b/tests/clodo/requests/compute/server_tests.rb @@ -1,7 +1,6 @@ Shindo.tests('Fog::Compute[:clodo] | server requests', ['clodo']) do @ip_format = { - 'ddosprotect' => Fog::Boolean, 'primary_ip' => Fog::Boolean, 'isp' => Fog::Boolean, 'ip' => String From ec2835dddfd626e7391053d14b6ac7b926287e3a Mon Sep 17 00:00:00 2001 From: "Stepan G. Fedorov" Date: Fri, 25 Nov 2011 12:48:42 +0400 Subject: [PATCH 18/18] [clodo|compute] Remove ddosprotect field from Mock. --- lib/fog/clodo/requests/compute/create_server.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/fog/clodo/requests/compute/create_server.rb b/lib/fog/clodo/requests/compute/create_server.rb index 6cbf82bdf..4d719b95f 100644 --- a/lib/fog/clodo/requests/compute/create_server.rb +++ b/lib/fog/clodo/requests/compute/create_server.rb @@ -65,13 +65,11 @@ module Fog 'vps_root_pass' => data['adminPass'], 'status' => "is_running", 'addresses' => {'public' =>[{ - 'ddosprotect' => false, 'primary_ip' => true, 'isp' => false, 'ip' => '66.6.6.66' }, { - 'ddosprotect' => false, 'primary_ip' => false, 'isp' => false, 'ip' => '13.13.13.13'