From 853755d32fb09426b0593501f458d60d307d36ba Mon Sep 17 00:00:00 2001 From: Eugene Howe & Josh Lane Date: Tue, 27 Nov 2012 16:57:16 -0800 Subject: [PATCH] [ecloud] improvements and some mocking --- lib/fog/core/collection.rb | 4 +- lib/fog/ecloud.rb | 24 +- lib/fog/ecloud/compute.rb | 1048 +++++++++++------ .../models/compute/admin_organization.rb | 24 +- .../models/compute/admin_organizations.rb | 5 - lib/fog/ecloud/models/compute/compute_pool.rb | 28 +- .../ecloud/models/compute/detached_disk.rb | 27 + .../ecloud/models/compute/detached_disks.rb | 31 + lib/fog/ecloud/models/compute/environment.rb | 19 +- lib/fog/ecloud/models/compute/environments.rb | 2 +- lib/fog/ecloud/models/compute/group.rb | 2 + lib/fog/ecloud/models/compute/groups.rb | 21 +- .../models/compute/hardware_configuration.rb | 8 +- .../models/compute/hardware_configurations.rb | 2 +- .../ecloud/models/compute/internet_service.rb | 32 +- .../models/compute/internet_services.rb | 15 +- lib/fog/ecloud/models/compute/ip_address.rb | 20 +- lib/fog/ecloud/models/compute/ip_addresses.rb | 7 +- lib/fog/ecloud/models/compute/layout.rb | 2 +- lib/fog/ecloud/models/compute/network.rb | 26 +- lib/fog/ecloud/models/compute/networks.rb | 12 +- lib/fog/ecloud/models/compute/node.rb | 10 +- lib/fog/ecloud/models/compute/nodes.rb | 7 +- .../models/compute/operating_system_family.rb | 2 +- lib/fog/ecloud/models/compute/organization.rb | 10 +- lib/fog/ecloud/models/compute/public_ip.rb | 2 +- lib/fog/ecloud/models/compute/public_ips.rb | 7 +- lib/fog/ecloud/models/compute/row.rb | 7 +- lib/fog/ecloud/models/compute/rows.rb | 9 +- lib/fog/ecloud/models/compute/server.rb | 200 ++-- lib/fog/ecloud/models/compute/servers.rb | 27 +- lib/fog/ecloud/models/compute/ssh_key.rb | 2 +- lib/fog/ecloud/models/compute/task.rb | 3 + lib/fog/ecloud/models/compute/template.rb | 17 +- .../compute/virtual_machine_assigned_ip.rb | 32 + .../compute/virtual_machine_assigned_ips.rb | 27 + .../admin_edit_authentication_levels.rb | 3 +- .../compute/get_admin_organization.rb | 14 + .../compute/get_admin_organizations.rb | 11 - .../requests/compute/get_compute_pool.rb | 11 + .../requests/compute/get_compute_pools.rb | 21 + .../requests/compute/get_detached_disk.rb | 21 + .../requests/compute/get_detached_disks.rb | 33 + .../requests/compute/get_environment.rb | 21 + lib/fog/ecloud/requests/compute/get_group.rb | 9 +- lib/fog/ecloud/requests/compute/get_groups.rb | 9 +- .../compute/get_hardware_configuration.rb | 19 + .../compute/get_hardware_configurations.rb | 4 + .../requests/compute/get_internet_service.rb | 12 + .../requests/compute/get_internet_services.rb | 8 + .../ecloud/requests/compute/get_ip_address.rb | 11 + .../requests/compute/get_ip_addresses.rb | 11 - lib/fog/ecloud/requests/compute/get_layout.rb | 9 +- .../ecloud/requests/compute/get_layouts.rb | 9 +- .../ecloud/requests/compute/get_network.rb | 11 + .../ecloud/requests/compute/get_networks.rb | 20 + lib/fog/ecloud/requests/compute/get_node.rb | 11 + lib/fog/ecloud/requests/compute/get_nodes.rb | 9 + .../requests/compute/get_operating_system.rb | 14 + .../compute/get_operating_system_families.rb | 20 + .../compute/get_operating_system_family.rb | 11 - .../requests/compute/get_organization.rb | 14 + .../requests/compute/get_organizations.rb | 16 +- .../ecloud/requests/compute/get_public_ip.rb | 12 +- .../ecloud/requests/compute/get_public_ips.rb | 22 + lib/fog/ecloud/requests/compute/get_row.rb | 9 +- lib/fog/ecloud/requests/compute/get_rows.rb | 3 +- lib/fog/ecloud/requests/compute/get_server.rb | 11 + .../ecloud/requests/compute/get_servers.rb | 33 +- .../ecloud/requests/compute/get_ssh_key.rb | 11 + .../ecloud/requests/compute/get_ssh_keys.rb | 20 + lib/fog/ecloud/requests/compute/get_task.rb | 6 +- .../ecloud/requests/compute/get_template.rb | 11 + .../ecloud/requests/compute/get_templates.rb | 38 + .../get_virtual_machine_assigned_ips.rb | 44 + .../ecloud/requests/compute/groups_delete.rb | 17 +- .../compute/internet_service_create.rb | 37 +- .../compute/internet_service_delete.rb | 23 + .../requests/compute/node_service_create.rb | 50 +- .../requests/compute/node_service_delete.rb | 24 + .../ecloud/requests/compute/rows_delete.rb | 12 +- .../compute/virtual_machine_add_ip.rb | 40 - .../compute/virtual_machine_attach_disk.rb | 62 + .../virtual_machine_create_from_template.rb | 152 ++- .../compute/virtual_machine_delete.rb | 37 +- .../compute/virtual_machine_detach_disk.rb | 71 ++ .../virtual_machine_edit_assigned_ips.rb | 82 ++ ...ual_machine_edit_hardware_configuration.rb | 30 +- .../compute/virtual_machine_import.rb | 126 +- tests/compute/helper.rb | 33 +- tests/compute/models/flavors_tests.rb | 2 +- tests/compute/models/server_tests.rb | 2 + tests/compute/models/servers_tests.rb | 3 +- .../models/admin_organization_tests.rb | 16 + .../compute/models/compute_pool_tests.rb | 17 + .../compute/models/detached_disk_tests.rb | 26 + .../compute/models/environment_tests.rb | 23 + .../compute/models/internet_service_tests.rb | 28 + .../ecloud/compute/models/ip_address_tests.rb | 19 + tests/ecloud/compute/models/network_tests.rb | 17 + .../models/operating_system_families_tests.rb | 13 + .../compute/models/operating_system_tests.rb | 22 + .../compute/models/organization_tests.rb | 21 + .../ecloud/compute/models/public_ip_tests.rb | 19 + tests/ecloud/compute/models/server_tests.rb | 143 +++ tests/ecloud/compute/models/ssh_key_tests.rb | 19 + tests/ecloud/compute/models/template_tests.rb | 20 + 107 files changed, 2816 insertions(+), 693 deletions(-) create mode 100644 lib/fog/ecloud/models/compute/detached_disk.rb create mode 100644 lib/fog/ecloud/models/compute/detached_disks.rb create mode 100644 lib/fog/ecloud/models/compute/virtual_machine_assigned_ip.rb create mode 100644 lib/fog/ecloud/models/compute/virtual_machine_assigned_ips.rb delete mode 100644 lib/fog/ecloud/requests/compute/get_admin_organizations.rb create mode 100644 lib/fog/ecloud/requests/compute/get_detached_disk.rb create mode 100644 lib/fog/ecloud/requests/compute/get_detached_disks.rb delete mode 100644 lib/fog/ecloud/requests/compute/get_ip_addresses.rb delete mode 100644 lib/fog/ecloud/requests/compute/get_operating_system_family.rb create mode 100644 lib/fog/ecloud/requests/compute/get_virtual_machine_assigned_ips.rb delete mode 100644 lib/fog/ecloud/requests/compute/virtual_machine_add_ip.rb create mode 100644 lib/fog/ecloud/requests/compute/virtual_machine_attach_disk.rb create mode 100644 lib/fog/ecloud/requests/compute/virtual_machine_detach_disk.rb create mode 100644 lib/fog/ecloud/requests/compute/virtual_machine_edit_assigned_ips.rb create mode 100644 tests/ecloud/compute/models/admin_organization_tests.rb create mode 100644 tests/ecloud/compute/models/compute_pool_tests.rb create mode 100644 tests/ecloud/compute/models/detached_disk_tests.rb create mode 100644 tests/ecloud/compute/models/environment_tests.rb create mode 100644 tests/ecloud/compute/models/internet_service_tests.rb create mode 100644 tests/ecloud/compute/models/ip_address_tests.rb create mode 100644 tests/ecloud/compute/models/network_tests.rb create mode 100644 tests/ecloud/compute/models/operating_system_families_tests.rb create mode 100644 tests/ecloud/compute/models/operating_system_tests.rb create mode 100644 tests/ecloud/compute/models/organization_tests.rb create mode 100644 tests/ecloud/compute/models/public_ip_tests.rb create mode 100644 tests/ecloud/compute/models/server_tests.rb create mode 100644 tests/ecloud/compute/models/ssh_key_tests.rb create mode 100644 tests/ecloud/compute/models/template_tests.rb diff --git a/lib/fog/core/collection.rb b/lib/fog/core/collection.rb index 463023487..5c9f2a374 100644 --- a/lib/fog/core/collection.rb +++ b/lib/fog/core/collection.rb @@ -103,10 +103,10 @@ module Fog raise(ArgumentError.new("Initialization parameters must be an attributes hash, got #{attributes.class} #{attributes.inspect}")) end model.new( - attributes.merge( + { :collection => self, :connection => connection - ) + }.merge(attributes) ) end diff --git a/lib/fog/ecloud.rb b/lib/fog/ecloud.rb index eef8c6f29..d61730edd 100644 --- a/lib/fog/ecloud.rb +++ b/lib/fog/ecloud.rb @@ -2,16 +2,30 @@ require(File.expand_path(File.join(File.dirname(__FILE__), 'core'))) module Fog module Ecloud + ECLOUD_OPTIONS = [:ecloud_authentication_method] extend Fog::Provider service(:compute, 'ecloud/compute', 'Compute') - end -end + def self.keep(hash, *keys) + {}.tap do |kept| + keys.each{|k| kept[k]= hash[k] if hash.key?(k)} + end + end -module Fog - module Ecloud - ECLOUD_OPTIONS = [:ecloud_authentication_method] + def self.slice(hash, *keys) + hash.dup.tap do |sliced| + keys.each{|k| sliced.delete(k)} + end + end + + def self.ip_address + 4.times.map{ Fog::Mock.random_numbers(3) }.join(".") + end + + def self.mac_address + 6.times.map{ Fog::Mock.random_numbers(2) }.join(":") + end end end diff --git a/lib/fog/ecloud/compute.rb b/lib/fog/ecloud/compute.rb index c2907d11c..4a5b69022 100644 --- a/lib/fog/ecloud/compute.rb +++ b/lib/fog/ecloud/compute.rb @@ -1,33 +1,12 @@ require 'fog/ecloud/collection' -module Fog - module Ecloud - class Model < Fog::Model +require 'fog/ecloud/model' +require 'builder' - attr_accessor :loaded - alias_method :loaded?, :loaded - - def reload - instance = super - @loaded = true - instance - end - - def load_unless_loaded! - unless @loaded - reload - end - end - - end - end -end module Fog module Compute - - class Ecloud < Fog::Service - API_URL = "https://services.enterprisecloud.terremark.com" + attr_reader :authentication_method, :version #### Credentials @@ -38,346 +17,271 @@ module Fog model_path 'fog/ecloud/models/compute' model :organization collection :organizations - model :location - collection :locations - model :catalog_item - collection :catalog - model :catalog_configuration - collection :catalog_configurations - model :environment - collection :environments - model :task - collection :tasks - model :compute_pool - collection :compute_pools - model :server - collection :servers - model :hardware_configuration - collection :hardware_configurations - model :server_configuration_option - collection :server_configuration_options - model :guest_process - collection :guest_processes - model :layout - collection :layouts - model :row - collection :rows - model :group - collection :groups - model :internet_service - collection :internet_services - model :node - collection :nodes - model :monitor - collection :monitors - model :cpu_usage_detail - collection :cpu_usage_detail_summary - model :memory_usage_detail - collection :memory_usage_detail_summary - model :storage_usage_detail - collection :storage_usage_detail_summary - model :operating_system_family - collection :operating_system_families - model :operating_system - collection :operating_systems - model :template - collection :templates - model :firewall_acl - collection :firewall_acls - model :network - collection :networks - model :ip_address - collection :ip_addresses - model :physical_device - collection :physical_devices - model :public_ip - collection :public_ips - model :trusted_network_group - collection :trusted_network_groups - model :backup_internet_service - collection :backup_internet_services - model :rnat - collection :rnats - model :association - collection :associations - model :tag - collection :tags - model :admin_organization - collection :admin_organizations - model :ssh_key - collection :ssh_keys - model :password_complexity_rule - collection :password_complexity_rules - model :authentication_level - collection :authentication_levels - model :login_banner - collection :login_banners - model :user - collection :users - model :role - collection :roles - model :ssh_key - collection :ssh_keys - model :support_ticket - collection :support_tickets + model :location + collection :locations + model :catalog_item + collection :catalog + model :catalog_configuration + collection :catalog_configurations + model :environment + collection :environments + model :task + collection :tasks + model :compute_pool + collection :compute_pools + model :server + collection :servers + model :virtual_machine_assigned_ip + collection :virtual_machine_assigned_ips + model :hardware_configuration + collection :hardware_configurations + model :server_configuration_option + collection :server_configuration_options + model :guest_process + collection :guest_processes + model :layout + collection :layouts + model :row + collection :rows + model :group + collection :groups + model :internet_service + collection :internet_services + model :node + collection :nodes + model :monitor + collection :monitors + model :cpu_usage_detail + collection :cpu_usage_detail_summary + model :memory_usage_detail + collection :memory_usage_detail_summary + model :storage_usage_detail + collection :storage_usage_detail_summary + model :operating_system_family + collection :operating_system_families + model :operating_system + collection :operating_systems + model :template + collection :templates + model :firewall_acl + collection :firewall_acls + model :network + collection :networks + model :ip_address + collection :ip_addresses + model :physical_device + collection :physical_devices + model :public_ip + collection :public_ips + model :trusted_network_group + collection :trusted_network_groups + model :backup_internet_service + collection :backup_internet_services + model :rnat + collection :rnats + model :association + collection :associations + model :tag + collection :tags + model :admin_organization + collection :admin_organizations + model :ssh_key + collection :ssh_keys + model :password_complexity_rule + collection :password_complexity_rules + model :authentication_level + collection :authentication_levels + model :login_banner + collection :login_banners + model :user + collection :users + model :role + collection :roles + model :ssh_key + collection :ssh_keys + model :support_ticket + collection :support_tickets + model :detached_disk + collection :detached_disks #### Requests request_path 'fog/ecloud/requests/compute' - request :get_organization - request :get_organizations - request :get_location - request :get_locations - request :get_environment - request :get_task - request :get_tasks - request :get_compute_pool - request :get_compute_pools - request :get_server - request :get_servers - request :get_network - request :get_networks - request :get_physical_device - request :get_physical_devices - request :get_layout - request :get_layouts - request :get_row - request :get_rows - request :get_group - request :get_groups - request :get_public_ip - request :get_public_ips - request :get_network_summary - request :get_internet_service - request :get_internet_services - request :get_node - request :get_nodes - request :get_firewall_acl - request :get_firewall_acls - request :get_trusted_network_group - request :get_trusted_network_groups - request :get_catalog - request :get_catalog_item - request :get_tag - request :get_tags - request :get_hardware_configuration - request :get_hardware_configurations - request :get_server_configuration_option - request :get_server_configuration_options - request :get_guest_process - request :get_guest_processes - request :get_cpu_usage_detail - request :get_cpu_usage_detail_summary - request :get_memory_usage_detail - request :get_memory_usage_detail_summary - request :get_storage_usage_detail - request :get_storage_usage_detail_summary - request :get_operating_system_family - request :get_operating_system_families - request :get_operating_system - request :get_template - request :get_templates - request :get_monitor - request :get_monitors - request :get_backup_internet_service - request :get_backup_internet_services - request :get_rnat - request :get_rnats - request :get_association - request :get_associations + request :backup_internet_service_create + request :backup_internet_service_delete + request :backup_internet_service_edit + request :compute_pool_edit + request :firewall_acls_create + request :firewall_acls_delete request :get_admin_organization - request :get_admin_organizations - request :get_ssh_key - request :get_ssh_keys - request :get_password_complexity_rule - request :get_password_complexity_rules - request :get_authentication_level - request :get_authentication_levels - request :get_login_banner - request :get_login_banners - request :get_user - request :get_users - request :get_role - request :get_roles request :get_api_key request :get_api_keys - request :get_support_ticket - request :get_support_tickets - request :get_ip_address - request :get_ip_addresses + request :get_association + request :get_associations + request :get_authentication_level + request :get_authentication_levels + request :get_backup_internet_service + request :get_backup_internet_services + request :get_catalog request :get_catalog_configuration request :get_catalog_configurations - request :compute_pool_edit - request :virtual_machine_create_from_template - request :virtual_machine_import - request :virtual_machine_copy - request :virtual_machine_copy_identical - request :virtual_machine_edit - request :virtual_machine_add_ip - request :virtual_machine_edit_hardware_configuration - request :virtual_machine_delete - request :virtual_machine_upload_file + request :get_catalog_item + request :get_compute_pool + request :get_compute_pools + request :get_cpu_usage_detail + request :get_cpu_usage_detail_summary + request :get_environment + request :get_firewall_acl + request :get_firewall_acls + request :get_group + request :get_groups + request :get_guest_process + request :get_guest_processes + request :get_hardware_configuration + request :get_internet_service + request :get_internet_services + request :get_ip_address + request :get_layout + request :get_layouts + request :get_location + request :get_locations + request :get_login_banner + request :get_login_banners + request :get_memory_usage_detail + request :get_memory_usage_detail_summary + request :get_monitor + request :get_monitors + request :get_network + request :get_network_summary + request :get_networks + request :get_node + request :get_nodes + request :get_operating_system + request :get_operating_system_families + request :get_organization + request :get_organizations + request :get_password_complexity_rule + request :get_password_complexity_rules + request :get_physical_device + request :get_physical_devices + request :get_public_ip + request :get_public_ips + request :get_rnat + request :get_rnats + request :get_role + request :get_roles + request :get_row + request :get_rows + request :get_server + request :get_server_configuration_option + request :get_server_configuration_options + request :get_servers + request :get_ssh_key + request :get_ssh_keys + request :get_storage_usage_detail + request :get_storage_usage_detail_summary + request :get_support_ticket + request :get_support_tickets + request :get_tag + request :get_tags + request :get_task + request :get_tasks + request :get_template + request :get_templates + request :get_trusted_network_group + request :get_trusted_network_groups + request :get_user + request :get_users + request :get_virtual_machine_assigned_ips + request :get_detached_disks + request :get_detached_disk + request :groups_create + request :groups_delete + request :groups_edit + request :groups_movedown + request :groups_moveup request :internet_service_create - request :internet_service_edit request :internet_service_delete - request :backup_internet_service_create - request :backup_internet_service_edit - request :backup_internet_service_delete + request :internet_service_edit + request :monitors_create_default + request :monitors_create_ecv + request :monitors_create_http + request :monitors_create_loopback + request :monitors_create_ping + request :monitors_disable + request :monitors_edit_ecv + request :monitors_edit_http + request :monitors_edit_ping + request :monitors_enable request :node_service_create request :node_service_delete request :node_service_edit - request :monitors_create_default - request :monitors_create_ping - request :monitors_create_http - request :monitors_create_ecv - request :monitors_create_loopback - request :monitors_edit_ping - request :monitors_edit_http - request :monitors_edit_ecv - request :monitors_enable - request :monitors_disable - request :rnat_associations_edit_network - request :rnat_associations_create_device - request :rnat_associations_delete - request :rows_create - request :rows_edit - request :rows_moveup - request :rows_movedown - request :rows_delete - request :groups_create - request :groups_edit - request :groups_moveup - request :groups_movedown - request :groups_delete - request :trusted_network_groups_create - request :trusted_network_groups_edit - request :trusted_network_groups_delete - request :firewall_acls_create - request :firewall_acls_delete - request :public_ip_activate - request :power_on request :power_off + request :power_on request :power_reset request :power_shutdown + request :public_ip_activate + request :rnat_associations_create_device + request :rnat_associations_delete + request :rnat_associations_edit_network + request :rows_create + request :rows_delete + request :rows_edit + request :rows_movedown + request :rows_moveup + request :trusted_network_groups_create + request :trusted_network_groups_delete + request :trusted_network_groups_edit + request :virtual_machine_edit_assigned_ips + request :virtual_machine_copy + request :virtual_machine_copy_identical + request :virtual_machine_create_from_template + request :virtual_machine_delete + request :virtual_machine_edit + request :virtual_machine_edit_hardware_configuration + request :virtual_machine_import + request :virtual_machine_upload_file + request :virtual_machine_detach_disk + request :virtual_machine_attach_disk module Shared attr_reader :versions_uri - def default_organization_uri - @default_organization_uri ||= begin - unless @login_results - do_login - end - case @login_results.body[:Org] - when Array - @login_results.body[:Org].first[:href] - when Hash - @login_results.body[:Org][:href] - else - nil - end - end - end - - # login handles the auth, but we just need the Set-Cookie - # header from that call. - def do_login - @login_results = get_organizations - end - - def ecloud_xmlns - { - "xmlns" => "urn:tmrk:eCloudExtensions-2.8", - "xmlns:i" => "http://www.w3.org/2001/XMLSchema-instance" - } - end - - def ensure_unparsed(uri) - if uri.is_a?(String) - uri - else - uri.to_s - end - end - - def supported_versions - @supported_versions ||= get_versions(@versions_uri).body[:VersionInfo] - end - - def xmlns - { "xmlns" => "http://www.vmware.com/vcloud/v0.8", - "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", - "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema" } - end - def validate_data(required_opts = [], options = {}) unless required_opts.all? { |opt| options.has_key?(opt) } raise ArgumentError.new("Required data missing: #{(required_opts - options.keys).map(&:inspect).join(", ")}") end end - end - - class Mock - def self.data - @data ||= Hash.new do |hash, key| - hash[key] = {} - end + def id_from_uri(uri) + uri.match(/(\d+)$/)[1].to_i end - def self.reset - @data = nil - end - - def initialize(options={}) - @ecloud_api_key = options[:ecloud] - end - - def data - self.class.data[@ecloud_api_key] - end - - def reset_data - self.class.data.delete(@ecloud_api_key) + def default_organization_uri + "/cloudapi/ecloud/organizations/" end end - class Real include Shared class << self - def basic_request(*args) - self.class_eval <<-EOS, __FILE__,__LINE__ - def #{args[0]}(uri) - request({ - :expects => #{args[1] || 200}, - :method => '#{args[2] || 'GET'}', - :headers => #{args[3] ? args[3].inspect : '{}'}, - :body => '#{args[4] ? args[4] : ''}', - :parse => true, - :uri => uri }) - end - EOS - end - - def unauthenticated_basic_request(*args) - self.class_eval <<-EOS, __FILE__,__LINE__ - def #{args[0]}(uri) - unauthenticated_request({ - :expects => #{args[1] || 200}, - :method => '#{args[2] || 'GET'}', - :headers => #{args[3] ? args[3].inspect : '{}'}, - :parse => true, - :uri => uri }) - end - EOS + def basic_request(name, expects=[200], method=:get, headers={}, body='') + define_method(name) do |uri| + request({ + :expects => expects, + :method => method, + :headers => headers, + :body => body, + :parse => true, + :uri => uri + }) + end end end def initialize(options = {}) - require 'builder' require 'fog/core/parser' @connections = {} @connection_options = options[:connection_options] || {} @@ -392,26 +296,14 @@ module Fog @username = options[:ecloud_username] @password = options[:ecloud_password] if @username.nil? || @password.nil? - raise RuntimeError, "No credentials (cloud auth, or basic auth) passed!" + raise ArgumentError, "No credentials (cloud auth, or basic auth) passed!" end else - @hmac = Fog::HMAC.new("sha256", @private_key) + @hmac = Fog::HMAC.new("sha256", @private_key) end end - def default_organization_uri - "/cloudapi/ecloud/organizations/" - end - def request(params) - begin - do_request(params) - rescue Excon::Errors::Unauthorized => e - raise RuntimeError, "Invalid authentication data: #{e}" - end - end - - def do_request(params) # Convert the uri to a URI if it's a string. if params[:uri].is_a?(String) params[:uri] = URI.parse(@host + params[:uri]) @@ -426,13 +318,16 @@ module Fog headers = set_extra_headers_for(params) || set_extra_headers_for({}) # Make the request - options = {:expects => params[:expects] || 200, :method => params[:method] || 'GET', :path => params[:uri].path + "#{"?#{params[:uri].query}" if params[:uri].query}", :headers => headers} - unless params[:body].empty? + options = { + :expects => (params[:expects] || 200), + :method => params[:method] || 'GET', + :path => params[:uri].path + "#{"?#{params[:uri].query}" if params[:uri].query}", + :headers => headers + } + unless params[:body].nil? || params[:body].empty? options.merge!({:body => params[:body]}) end - #puts options[:body].inspect response = @connections[host_url].request(options) - # Parse the response body into a hash unless response.body.empty? if params[:parse] @@ -444,6 +339,7 @@ module Fog response.body = document.body end end + response end @@ -451,27 +347,27 @@ module Fog # if Authorization and x-tmrk-authorization are used, the x-tmrk-authorization takes precendence. def set_extra_headers_for(params) - maor_headers = { - 'x-tmrk-version' => @version, - 'Date' => Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S GMT"), - } - params[:headers].merge!(maor_headers) - if params[:method]=="POST" || params[:method]=="PUT" - params[:headers].merge!({"Content-Type" => 'application/xml'}) unless params[:headers]['Content-Type'] - params[:headers].merge!({"Accept" => 'application/xml'}) - end - unless params[:body].empty? + params[:headers] = { + 'x-tmrk-version' => @version, + 'Date' => Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S GMT"), + }.merge(params[:headers] || {}) + if params[:method]=="POST" || params[:method]=="PUT" + params[:headers].merge!({"Content-Type" => 'application/xml'}) unless params[:headers]['Content-Type'] + params[:headers].merge!({"Accept" => 'application/xml'}) + end + unless params[:body].nil? || params[:body].empty? params[:headers].merge!({"x-tmrk-contenthash" => "Sha256 #{Base64.encode64(Digest::SHA2.digest(params[:body].to_s)).chomp}"}) - end - if @authentication_method == :basic_auth - params[:headers].merge!({'Authorization' => "Basic #{Base64.encode64(@username+":"+@password).delete("\r\n")}"}) - elsif @authentication_method == :cloud_api_auth - signature = cloud_api_signature(params) - params[:headers].merge!({ - "x-tmrk-authorization" => %{CloudApi AccessKey="#{@access_key}" SignatureType="HmacSha256" Signature="#{signature}"} - }) - end - params[:headers] + end + if @authentication_method == :basic_auth + params[:headers].merge!({'Authorization' => "Basic #{Base64.encode64(@username+":"+@password).delete("\r\n")}"}) + elsif @authentication_method == :cloud_api_auth + signature = cloud_api_signature(params) + params[:headers].merge!({ + "x-tmrk-authorization" => %{CloudApi AccessKey="#{@access_key}" SignatureType="HmacSha256" Signature="#{signature}"}, + "Authorization" => %{CloudApi AccessKey="#{@access_key}" SignatureType="HmacSha256" Signature="#{signature}"} + }) + end + params[:headers] end def cloud_api_signature(params) @@ -480,13 +376,14 @@ module Fog path = params[:uri].path canonicalized_headers = canonicalize_headers(headers) canonicalized_resource = canonicalize_resource(path) - string = [ verb, - headers['Content-Length'].to_s, - headers['Content-Type'].to_s, - headers['Date'].to_s, - canonicalized_headers, - canonicalized_resource + "\n" - ].join("\n") + string = [ + verb, + headers['Content-Length'].to_s, + headers['Content-Type'].to_s, + headers['Date'].to_s, + canonicalized_headers, + canonicalized_resource + "\n" + ].join("\n") Base64.encode64(@hmac.sign(string)).chomp end @@ -510,6 +407,421 @@ module Fog end + class Mock + include Shared + + def self.data + @data ||= Hash.new do |hash, key| + hash[key] = begin + compute_pool_id = Fog.credentials[:ecloud_compute_pool_id] || Fog::Mock.random_numbers(3).to_i + environment_id = Fog.credentials[:ecloud_environment_id] || Fog::Mock.random_numbers(3).to_i + public_ip_id = Fog.credentials[:ecloud_public_ip_id] || Fog::Mock.random_numbers(6).to_i + internet_service_id = Fog::Mock.random_numbers(6).to_i + node_service_id = Fog::Mock.random_numbers(6).to_i + environment_name = Fog.credentials[:ecloud_environment_name] || Fog::Mock.random_letters(12) + location_id = Fog::Mock.random_numbers(4).to_i + network_id = Fog.credentials[:ecloud_network_id] || Fog::Mock.random_numbers(6).to_i + network_ip = Fog::Ecloud.ip_address + public_ip = Fog.credentials[:ecloud_public_ip_name] || Fog::Ecloud.ip_address + ip_address_id = Fog::Ecloud.ip_address + ip_address2_id = Fog::Ecloud.ip_address + operating_system_id = Fog::Mock.random_numbers(7).to_i + operating_system_family_id = Fog::Mock.random_numbers(7).to_i + organization_id = Fog::Mock.random_numbers(7).to_i + organization_name = Fog::Mock.random_letters(7) + template_id = Fog.credentials[:ecloud_template_id] || Fog::Mock.random_numbers(7).to_i + ssh_key_id = Fog.credentials[:ecloud_ssh_key_id] || Fog::Mock.random_numbers(4).to_i + ssh_key_name = Fog.credentials[:ecloud_ssh_key_name] || "root" + + environment = { + :id => environment_id, + :href => "/cloudapi/ecloud/environments/#{environment_id}", + :name => environment_name, + :type => "application/vnd.tmrk.cloud.environment" + } + + organization = { + :href => "/cloudapi/ecloud/organizations/#{organization_id}", + :type => "application/vnd.tmrk.cloud.organization", + :name => organization_name, + :Links => { + :Link => [ + Fog::Ecloud.keep(environment, :href, :name, :type), + { + :href => "/cloudapi/ecloud/admin/organizations/#{organization_id}", + :name => organization_name, + :type => "application/vnd.tmrk.cloud.admin.organization", + :rel => "alternate", + }, + { + :href => "/cloudapi/ecloud/devicetags/organizations/#{organization_id}", + :type => "application/vnd.tmrk.cloud.deviceTag; type=collection", + :rel => "down", + }, + { + :href => "/cloudapi/ecloud/alerts/organizations/#{organization_id}", + :type => "application/vnd.tmrk.cloud.alertLog", + :rel => "down", + }, + ], + }, + :Locations => { + :Location => [ + { + :href => "/cloudapi/ecloud/locations/#{location_id}", + :name => organization_name, + :Catalog => { + :href => "/cloudapi/ecloud/admin/catalog/organizations/#{organization_id}/locations/#{location_id}", + :type => "application/vnd.tmrk.cloud.admin.catalogEntry; type=collection" + }, + :Environments => { :Environment => [environment] } + } + ] + } + } + environment.merge!( + :Links => { + :Link => [ Fog::Ecloud.keep(organization, :href, :name, :type), ] + } + ) + + admin_organization = { + :id => organization_id, + :href => "/cloudapi/ecloud/admin/organizations/#{organization_id}", + :type => "application/vnd.tmrk.cloud.admin.organization", + :name => organization_name, + :Links => { + :Link => [ + Fog::Ecloud.keep(organization, :href, :type, :name) + ], + }, + :organization_id => organization_id, + } + + compute_pool = { + :id => compute_pool_id, + :href => "/cloudapi/ecloud/computepools/#{compute_pool_id}", + :name => Fog::Mock.random_letters(12), + :type => "application/vnd.tmrk.cloud.computePool", + :environment_id => environment_id, + :Links => { + :Link => [ + Fog::Ecloud.keep(organization, :href, :name, :type), + Fog::Ecloud.keep(environment, :href, :name, :type), + ] + } + } + + + public_ip = { + :id => public_ip_id, + :href => "/cloudapi/ecloud/publicips/#{public_ip_id}", + :name => public_ip, + :type => "application/vnd.tmrk.cloud.publicIp", + :IpType => "none", + :environment_id => environment_id, + :Links => { + :Link => [ + Fog::Ecloud.keep(environment, :href, :name, :type), + ], + }, + :InternetServices => { + :InternetService => [ + ], + }, + } + + internet_service = { + :id => internet_service_id, + :href => "/cloudapi/ecloud/internetservices/#{internet_service_id}", + :name => Fog::Mock.random_letters(6), + :type => "application/vnd.tmrk.cloud.internetService", + :public_ip_id => public_ip_id, + :Links => { + :Link => [ + Fog::Ecloud.keep(public_ip, :href, :name, :type), + ], + }, + :NodeServices => { + :NodeService => [ + ] + }, + } + + node_service = { + :id => node_service_id, + :href => "/cloudapi/ecloud/nodeservices/#{node_service_id}", + :name => Fog::Mock.random_letters(6), + :type => "application/vnd.tmrk.cloud.nodeService", + :internet_service_id => internet_service_id, + :Links => { + :Link => [ + Fog::Ecloud.keep(internet_service, :href, :name, :type) + ], + }, + } + + internet_service[:NodeServices][:NodeService].push(node_service) + public_ip[:InternetServices][:InternetService].push(internet_service) + + network = { + :id => network_id, + :href => "/cloudapi/ecloud/networks/#{network_id}", + :name => "#{network_ip}/#{Fog::Mock.random_numbers(2)}", + :type => "application/vnd.tmrk.cloud.network", + :Address => network_ip, + :NetworkType => "Dmz", + :BroadcastAddress => network_ip, + :GatewayAddress => network_ip, + :environment_id => environment_id, + :Links => { + :Link => [ + Fog::Ecloud.keep(environment, :href, :name, :type), + ] + }, + :IpAddresses => { + :IpAddress => [], + }, + } + + ip_address = { + :id => ip_address_id, + :href => "/cloudapi/ecloud/ipaddresses/networks/#{network_id}/#{ip_address_id}", + :name => ip_address_id, + :type => "application/vnd.tmrk.cloud.ipAddress", + :network_id => network_id, + :Links => { + :Link => [ Fog::Ecloud.keep(network, :href, :name, :type), ], + }, + :Reserved => "false", + :Host => nil, + :DetectedOn => nil, + } + + ip_address2 = ip_address.dup.merge(:id => ip_address2_id, :href => "/cloudapi/ecloud/ipaddresses/networks/#{network_id}/#{ip_address2_id}", :name => ip_address2_id) + + network[:IpAddresses][:IpAddress].push(ip_address).push(ip_address2) + + + short_name = "solaris10_64guest" + operating_system = { + :short_name => short_name, + :compute_pool_id => compute_pool_id, + :href => "/cloudapi/ecloud/operatingsystems/#{short_name}/computepools/#{compute_pool_id}", + :name => "Sun Solaris 10 (64-bit)", + :type => "application/vnd.tmrk.cloud.operatingSystem", + :FamilyName => "Solaris", + :Links => { + :Link => Fog::Ecloud.keep(compute_pool, :href, :name, :type), + }, + :ConfigurationOptions => { + :Processor => { + :Minimum => "1", + :Maximum => "8", + :StepFactor => "1" + }, + :Memory => { + :MinimumSize => { + :Unit => "MB", + :Value => "800" + }, + :MaximumSize => { + :Unit => "MB", + :Value => "16384" + }, + :StepFactor => { + :Unit => "MB", + :Value => "4" + } + }, + :Disk => { + :Minimum => "1", + :Maximum => "15", + :SystemDisk => { + :ResourceCapacityRange => { + :MinimumSize => { + :Unit => "GB", + :Value => "1" + }, + :MaximumSize => { + :Unit => "GB", + :Value => "512" + }, + :StepFactor => { + :Unit => "GB", + :Value => "1"} + }, + :MonthlyCost => "0" + }, + :DataDisk => { + :ResourceCapacityRange => { + :MinimumSize => { + :Unit => "GB", + :Value => "1" + }, + :MaximumSize => { + :Unit => "GB", + :Value => "512" + }, + :StepFactor => { + :Unit => "GB", + :Value => "1" + } + }, + :MonthlyCost => "0" + } + }, + :NetworkAdapter=> { + :Minimum => "1", + :Maximum => "4", + :StepFactor => "1" + } + } + } + + + template = { + :id => template_id, + :href => "/cloudapi/ecloud/templates/#{template_id}/computepools/#{compute_pool_id}", + :type => "application/vnd.tmrk.cloud.template", + :name => "Sun Solaris 10 (x64)", + :compute_pool_id => compute_pool_id, + :OperatingSystem => Fog::Ecloud.keep(operating_system, :href, :name, :type), + :Memory => { + :MinimumSize => { + :Unit => "MB", + :Value => "800" + }, + :MaximumSize => { + :Unit => "MB", + :Value => "16384" + }, + :StepFactor => { + :Unit => "MB", + :Value => "4" + } + }, + :Storage => { + :Size => { + :Unit => "GB", + :Value => "7" + } + }, + :NetworkAdapters => "1", + :Links => { + :Link => [ + Fog::Ecloud.keep(compute_pool, :href, :name, :type), + ] + } + } + + operating_system_family = { + :id => operating_system_family_id, + :compute_pool_id => compute_pool_id, + :OperatingSystemFamily => { + :Name => "Linux", + :OperatingSystems => { + :OperatingSystem => [Fog::Ecloud.keep(operating_system, :href, :name, :type)], + } + }, + :Links => { + :Link => [ + Fog::Ecloud.keep(compute_pool, :href, :name, :type), + ] + } + } + + ssh_key = { + :id => ssh_key_id, + :href => "/cloudapi/ecloud/admin/sshKeys/#{ssh_key_id}", + :name => ssh_key_name, + :admin_organization_id => organization_id, + :Links => { + :Link => [ + Fog::Ecloud.keep(admin_organization, :href, :name, :type), + Fog::Ecloud.keep(organization, :href, :name, :type), + ] + }, + :Default => "true", + :FingerPrint => Fog::Ecloud.mac_address + } + + layout = { + :id => environment_id, + :href => "/cloudapi/ecloud/layout/environments/#{environment_id}", + :type => "application/vnd.tmrk.cloud.deviceLayout", + :Links => { + :Link => [ + Fog::Ecloud.keep(environment, :name, :href, :type), + ], + }, + :Rows => { + :Row => [ + ], + }, + :environment_id => environment_id + } + + + { + :compute_pools => {compute_pool_id => compute_pool}, + :environments => {environment_id => environment}, + :public_ips => {public_ip_id => public_ip}, + :internet_services => {internet_service_id => internet_service}, + :node_services => {node_service_id => node_service}, + :networks => {network_id => network}, + :organizations => {organization_id => organization}, + :admin_organizations => {organization_id => admin_organization}, + :operating_systems => {operating_system_id => operating_system}, + :operating_system_families => {operating_system_family_id => operating_system_family}, + :servers => {}, + :tasks => {}, + :templates => {template_id => template}, + :ssh_keys => {ssh_key_id => ssh_key}, + :detached_disks => {}, + :template_href => (Fog.credentials[:ecloud_template_href] || "/cloudapi/ecloud/templates/#{template_id}/computepools/#{compute_pool_id}"), + :rows => {}, + :groups => {}, + :layouts => {environment_id => layout}, + } + end + end + end + + def self.reset + @data = nil + end + + def initialize(options={}) + @ecloud_api_key = options[:ecloud] + end + + def data + self.class.data[@ecloud_api_key] + end + + def reset_data + self.class.data.delete(@ecloud_api_key) + end + + def response(params={}) + body = params[:body] + headers = { + "Content-Type" => "application/xml" + }.merge(params[:headers] || {}) + status = params[:status] || 200 + + response = Excon::Response.new(:body => body, :headers => headers, :status => status) + if params.has_key?(:expects) && ![*params[:expects]].include?(response.status) + raise(Excon::Errors.status_error(params, response)) + else response + end + end + + def deep_copy(o) + Marshal.load(Marshal.dump(o)) + end + end end end end diff --git a/lib/fog/ecloud/models/compute/admin_organization.rb b/lib/fog/ecloud/models/compute/admin_organization.rb index 073e28a73..1bfff217a 100644 --- a/lib/fog/ecloud/models/compute/admin_organization.rb +++ b/lib/fog/ecloud/models/compute/admin_organization.rb @@ -4,34 +4,38 @@ module Fog class AdminOrganization < Fog::Ecloud::Model identity :href - attribute :name, :aliases => :Name - attribute :type, :aliases => :Type - attribute :other_links, :aliases => :Links + attribute :name, :aliases => :Name + attribute :type, :aliases => :Type + attribute :other_links, :aliases => :Links, :squash => :Link attribute :multifactor_summary, :aliases => :MultifactorSummary - attribute :support_access, :aliases => :SupportAccess + attribute :support_access, :aliases => :SupportAccess def ssh_keys - @ssh_keys = Fog::Compute::Ecloud::SshKeys.new(:connection => connection, :href => "/cloudapi/ecloud/admin/sshKeys/organizations/#{org_id}") + @ssh_keys = Fog::Compute::Ecloud::SshKeys.new(:connection => connection, :href => "/cloudapi/ecloud/admin/sshKeys/organizations/#{organization.id}") end def password_complexity_rules - @password_complexity_rules = Fog::Compute::Ecloud::PasswordComplexityRules.new(:connection => connection, :href => "/cloudapi/ecloud/admin/organizations/#{org_id}/passwordComplexityRules") + @password_complexity_rules = Fog::Compute::Ecloud::PasswordComplexityRules.new(:connection => connection, :href => "/cloudapi/ecloud/admin/organizations/#{organization.id}/passwordComplexityRules") end def login_banner - @login_banner = Fog::Compute::Ecloud::LoginBanner.new(:connection => connection, :href => "/cloudapi/ecloud/admin/organizations/#{org_id}/loginBanner") + @login_banner = Fog::Compute::Ecloud::LoginBanner.new(:connection => connection, :href => "/cloudapi/ecloud/admin/organizations/#{organization.id}/loginBanner") end def authentication_levels - @authentication_levels = Fog::Compute::Ecloud::AuthenticationLevels.new(:connection => connection, :href => "/cloudapi/ecloud/admin/organizations/#{org_id}/authenticationLevels") + @authentication_levels = Fog::Compute::Ecloud::AuthenticationLevels.new(:connection => connection, :href => "/cloudapi/ecloud/admin/organizations/#{organization.id}/authenticationLevels") end def id href.scan(/\d+/)[0] end - def org_id - other_links[:Link].detect { |l| l[:type] == "application/vnd.tmrk.cloud.organization" }[:href].scan(/\d+/)[0] + def organization + @organization ||= begin + reload unless other_links + organization_link = other_links.find{|l| l[:type] == "application/vnd.tmrk.cloud.organization"} + self.connection.organizations.new(organization_link) + end end end end diff --git a/lib/fog/ecloud/models/compute/admin_organizations.rb b/lib/fog/ecloud/models/compute/admin_organizations.rb index 0cfc2fc6a..1d87f2655 100644 --- a/lib/fog/ecloud/models/compute/admin_organizations.rb +++ b/lib/fog/ecloud/models/compute/admin_organizations.rb @@ -9,11 +9,6 @@ module Fog model Fog::Compute::Ecloud::AdminOrganization - def all - data = connection.get_admin_organizations(href).body - load(data) - end - def get(uri) if data = connection.get_admin_organization(uri) new(data.body) diff --git a/lib/fog/ecloud/models/compute/compute_pool.rb b/lib/fog/ecloud/models/compute/compute_pool.rb index 5f301ae5b..33fa1bf07 100644 --- a/lib/fog/ecloud/models/compute/compute_pool.rb +++ b/lib/fog/ecloud/models/compute/compute_pool.rb @@ -5,13 +5,13 @@ module Fog identity :href - attribute :href, :aliases => :Href - attribute :name, :aliases => :Name - attribute :type, :aliases => :Type - attribute :other_links, :aliases => :Links - attribute :all_servers, :aliases => :VirtualMachines - attribute :purchased, :aliases => :Purchased - attribute :cpu_burst, :aliases => :CpuBurst + attribute :href, :aliases => :Href + attribute :name, :aliases => :Name + attribute :type, :aliases => :Type + attribute :other_links, :aliases => :Links, :squash => :Link + attribute :all_servers, :aliases => :VirtualMachines + attribute :purchased, :aliases => :Purchased + attribute :cpu_burst, :aliases => :CpuBurst attribute :memory_burst, :aliases => :MemoryBurst def servers @@ -41,7 +41,19 @@ module Fog end def templates - @templates ||= Fog::Compute::Ecloud::Templates.new(:connection => connection, :href => "/cloudapi/ecloud/templates/computePools/#{id}") + @templates ||= self.connection.templates(:href => "/cloudapi/ecloud/templates/computePools/#{id}") + end + + def detached_disks + @detached_disks ||= self.connection.detached_disks(:href => "/cloudapi/ecloud/detacheddisks/computepools/#{id}") + end + + def environment + @environment ||= begin + reload unless other_links + environment_link = other_links.find{|l| l[:type] == "application/vnd.tmrk.cloud.environment"} + self.connection.environments.get(environment_link[:href]) + end end def edit(options) diff --git a/lib/fog/ecloud/models/compute/detached_disk.rb b/lib/fog/ecloud/models/compute/detached_disk.rb new file mode 100644 index 000000000..b332cb958 --- /dev/null +++ b/lib/fog/ecloud/models/compute/detached_disk.rb @@ -0,0 +1,27 @@ +module Fog + module Compute + class Ecloud + class DetachedDisk < Fog::Ecloud::Model + identity :href + + attribute :name, :aliases => :Name + attribute :type, :aliases => :Type + attribute :other_links, :aliases => :Links + attribute :status, :aliases => :Status + attribute :size, :aliases => :Size + + def ready? + unless status =~ /AttachInProgress|DetachInProgress/ + true + else + false + end + end + + def id + href.scan(/\d+/)[0] + end + end + end + end +end diff --git a/lib/fog/ecloud/models/compute/detached_disks.rb b/lib/fog/ecloud/models/compute/detached_disks.rb new file mode 100644 index 000000000..532f896ee --- /dev/null +++ b/lib/fog/ecloud/models/compute/detached_disks.rb @@ -0,0 +1,31 @@ +require 'fog/ecloud/models/compute/detached_disk' + +module Fog + module Compute + class Ecloud + class DetachedDisks < Fog::Ecloud::Collection + + identity :href + + model Fog::Compute::Ecloud::DetachedDisk + + def all + data = connection.get_detached_disks(href).body[:DetachedDisk] + data = [] if data.nil? + load(data) + end + + def get(uri) + data = connection.get_detached_disk(uri).body + if data == "" + new({}) + else + new(data) + end + rescue Fog::Errors::NotFound + nil + end + end + end + end +end diff --git a/lib/fog/ecloud/models/compute/environment.rb b/lib/fog/ecloud/models/compute/environment.rb index dc2a65ef5..951e83cf0 100644 --- a/lib/fog/ecloud/models/compute/environment.rb +++ b/lib/fog/ecloud/models/compute/environment.rb @@ -9,7 +9,7 @@ module Fog attribute :name attribute :type - attribute :other_links, :aliases => :Links + attribute :other_links, :aliases => :Links, :squash => :Link def public_ips @public_ips ||= Fog::Compute::Ecloud::PublicIps.new(:connection => connection, :href => "/cloudapi/ecloud/publicIps/environments/#{id}") @@ -28,7 +28,7 @@ module Fog end def networks - @networks ||= Fog::Compute::Ecloud::Networks.new(:connection => connection, :href => "/cloudapi/ecloud/networks/environments/#{id}") + @networks ||= self.connection.networks(:href => "/cloudapi/ecloud/networks/environments/#{id}") end def servers @@ -45,11 +45,11 @@ module Fog end def layout - @layout ||= Fog::Compute::Ecloud::Layouts.new(:connection => connection, :href => "/cloudapi/ecloud/layout/environments/#{id}").first + @layout ||= self.connection.layouts(:href => "/cloudapi/ecloud/layout/environments/#{id}").first end def rows - layout.rows + @rows ||= layout.rows end def tasks @@ -73,8 +73,7 @@ module Fog end def catalog - org_href = other_links[:Link].detect { |l| l[:type] == "application/vnd.tmrk.cloud.organization" }[:href] - @catalog ||= Fog::Compute::Ecloud::Catalog.new(:connection => connection, :href => "/cloudapi/ecloud/admin/catalog/organizations/#{org_href.scan(/\d+/)[0]}") + @catalog = connection.catalog(:href => "/cloudapi/ecloud/admin/catalog/organizations/#{organization.id}") end def rnats @@ -98,6 +97,14 @@ module Fog def id href.scan(/\d+/)[0] end + + def organization + @organization ||= begin + reload unless other_links + organization_link = other_links.find{|l| l[:type] == "application/vnd.tmrk.cloud.organization"} + self.connection.organizations.new(organization_link) + end + end end Vdc = Environment end diff --git a/lib/fog/ecloud/models/compute/environments.rb b/lib/fog/ecloud/models/compute/environments.rb index a0ed7fd35..73df64a26 100644 --- a/lib/fog/ecloud/models/compute/environments.rb +++ b/lib/fog/ecloud/models/compute/environments.rb @@ -14,7 +14,7 @@ module Fog def all data = [] - connection.get_organization(href).body[:Locations][:Location].each do |d| + connection.get_organization(href).body[:Locations][:Location].each do |d| if d[:Environments][:Environment].is_a?(Array) d[:Environments][:Environment].each { |e| data << e } else diff --git a/lib/fog/ecloud/models/compute/group.rb b/lib/fog/ecloud/models/compute/group.rb index da50a7511..c8dd420fe 100644 --- a/lib/fog/ecloud/models/compute/group.rb +++ b/lib/fog/ecloud/models/compute/group.rb @@ -33,6 +33,8 @@ module Fog def id href.scan(/\d+/)[0] end + + alias destroy delete end end end diff --git a/lib/fog/ecloud/models/compute/groups.rb b/lib/fog/ecloud/models/compute/groups.rb index 5c2282d02..7e3b07a5b 100644 --- a/lib/fog/ecloud/models/compute/groups.rb +++ b/lib/fog/ecloud/models/compute/groups.rb @@ -11,15 +11,26 @@ module Fog def all data = connection.get_groups(href).body - data = data[:Groups] ? data[:Groups][:Group] : data - load(data) + data = if data == "" + "" + else + data[:Groups] ? data[:Groups][:Group] : data + end + if data == "" || !data.is_a?(Array) && data[:type] == "application/vnd.tmrk.cloud.layoutRow" + nil + else + load(data) + end end def get(uri) - if data = connection.get_group(uri) - new(data.body) + data = connection.get_group(uri).body + if data == "" + nil + else + new(data) end - rescue Fog::Errors::NotFound + rescue Excon::Errors::NotFound nil end end diff --git a/lib/fog/ecloud/models/compute/hardware_configuration.rb b/lib/fog/ecloud/models/compute/hardware_configuration.rb index 124b3e782..2b2f520e1 100644 --- a/lib/fog/ecloud/models/compute/hardware_configuration.rb +++ b/lib/fog/ecloud/models/compute/hardware_configuration.rb @@ -4,10 +4,10 @@ module Fog class HardwareConfiguration < Fog::Ecloud::Model identity :href - attribute :processor_count, :aliases => :ProcessorCount, :type => :integer - attribute :mem, :aliases => :Memory - attribute :storage, :aliases => :Disks - attribute :network_cards, :aliases => :Nics + attribute :processor_count, :aliases => :ProcessorCount, :type => :integer + attribute :memory, :aliases => :Memory, :squash => :Value # {:Memory => {:Value => 15}} + attribute :storage, :aliases => :Disks, :squash => :Disk + attribute :network_cards, :aliases => :Nics, :squash => :Nic def id href.scan(/\d+/)[0] diff --git a/lib/fog/ecloud/models/compute/hardware_configurations.rb b/lib/fog/ecloud/models/compute/hardware_configurations.rb index 092b85285..c53db3b18 100644 --- a/lib/fog/ecloud/models/compute/hardware_configurations.rb +++ b/lib/fog/ecloud/models/compute/hardware_configurations.rb @@ -10,7 +10,7 @@ module Fog model Fog::Compute::Ecloud::HardwareConfiguration def all - data = connection.get_hardware_configurations(href).body + data = connection.get_server(href).body load(data) end diff --git a/lib/fog/ecloud/models/compute/internet_service.rb b/lib/fog/ecloud/models/compute/internet_service.rb index 2551eebb0..99878299b 100644 --- a/lib/fog/ecloud/models/compute/internet_service.rb +++ b/lib/fog/ecloud/models/compute/internet_service.rb @@ -4,20 +4,24 @@ module Fog class InternetService < Fog::Ecloud::Model identity :href - attribute :name, :aliases => :Name - attribute :type, :aliases => :Type - attribute :other_links, :aliases => :Links - attribute :actions, :aliases => :Actions - attribute :protocol, :aliases => :Protocol - attribute :port, :aliases => :Port, :type => :integer - attribute :enabled, :aliases => :Enabled, :type => :boolean - attribute :description, :aliases => :Description - attribute :public_ip, :aliases => :PublicIp - attribute :persistence, :aliases => :Persistence - attribute :redirect_url, :aliases => :RedirectUrl - attribute :trusted_network_group, :aliases => :TrustedNetworkGroup + attribute :name, :aliases => :Name + attribute :type, :aliases => :Type + attribute :other_links, :aliases => :Links + attribute :actions, :aliases => :Actions + attribute :protocol, :aliases => :Protocol + attribute :port, :aliases => :Port, :type => :integer + attribute :enabled, :aliases => :Enabled, :type => :boolean + attribute :description, :aliases => :Description + attribute :public_ip, :aliases => :PublicIp + attribute :persistence, :aliases => :Persistence + attribute :redirect_url, :aliases => :RedirectUrl + attribute :trusted_network_group, :aliases => :TrustedNetworkGroup attribute :backup_internet_service, :aliases => :BackupInternetService + def ready? + !self.port.nil? + end + def nodes @nodes ||= Fog::Compute::Ecloud::Nodes.new(:connection => connection, :href => href) end @@ -43,7 +47,7 @@ module Fog def delete data = connection.internet_service_delete(href).body - task = Fog::Compute::Ecloud::Tasks.new(:connection => connection, :href => data[:href])[0] + self.connection.tasks.new(data) end def create_monitor(options = {}) @@ -85,6 +89,8 @@ module Fog service_data.reject! {|k, v| v.nil? } service_data end + + alias destroy delete end end end diff --git a/lib/fog/ecloud/models/compute/internet_services.rb b/lib/fog/ecloud/models/compute/internet_services.rb index 12207f1b8..a8f3c7bc9 100644 --- a/lib/fog/ecloud/models/compute/internet_services.rb +++ b/lib/fog/ecloud/models/compute/internet_services.rb @@ -19,8 +19,11 @@ module Fog end def get(uri) - if data = connection.get_internet_service(uri) - new(data.body) + data = connection.get_internet_service(uri).body + if data == "" + new({}) + else + new(data) end rescue Fog::Errors::NotFound nil @@ -28,10 +31,10 @@ module Fog def create(options) options[:uri] = "/cloudapi/ecloud/internetServices/publicIps/#{public_ip_id}/action/createInternetService" - options[:protocol] ||= "TCP" - options[:enabled] ||= true - options[:description] ||= "" - options[:persistence] ||= {} + options[:protocol] ||= "TCP" + options[:enabled] ||= true + options[:description] ||= "" + options[:persistence] ||= {} options[:persistence][:type] ||= "None" data = connection.internet_service_create(options).body object = new(data) diff --git a/lib/fog/ecloud/models/compute/ip_address.rb b/lib/fog/ecloud/models/compute/ip_address.rb index c73e6a177..33479264e 100644 --- a/lib/fog/ecloud/models/compute/ip_address.rb +++ b/lib/fog/ecloud/models/compute/ip_address.rb @@ -6,7 +6,7 @@ module Fog attribute :name, :aliases => :Name attribute :type, :aliases => :Type - attribute :other_links, :aliases => :Links + attribute :other_links, :aliases => :Links, :squash => :Link attribute :host, :aliases => :Host attribute :detected_on, :aliases => :DetectedOn attribute :rnat, :aliases => :RnatAddress @@ -15,9 +15,23 @@ module Fog def status (detected_on || host) ? "Assigned" : "Available" end - + def id - href.scan(/\d+/)[0] + href.match(/((\d+{1,3}\.){3}(\d+{1,3}))$/)[1] + end + + def server + @server ||= begin + reload unless other_links + server_link = other_links.find{|l| l[:type] == "application/vnd.tmrk.cloud.virtualMachine"} + self.connection.servers.get(server_link[:href]) + end + end + + def network + reload if other_links.nil? + network_href = other_links.detect { |l| l[:type] == "application/vnd.tmrk.cloud.network" }[:href] + network = self.connection.networks.get(network_href) end end end diff --git a/lib/fog/ecloud/models/compute/ip_addresses.rb b/lib/fog/ecloud/models/compute/ip_addresses.rb index b842ed1e1..225a32c6a 100644 --- a/lib/fog/ecloud/models/compute/ip_addresses.rb +++ b/lib/fog/ecloud/models/compute/ip_addresses.rb @@ -10,7 +10,12 @@ module Fog model Fog::Compute::Ecloud::IpAddress def all - data = connection.get_ip_addresses(href).body[:IpAddresses][:IpAddress] + data = connection.get_network(href).body + data = if data[:IpAddresses] + data[:IpAddresses][:IpAddress] + else + data + end data = data.nil? ? [] : data load(data) end diff --git a/lib/fog/ecloud/models/compute/layout.rb b/lib/fog/ecloud/models/compute/layout.rb index 09932755e..75cdea00f 100644 --- a/lib/fog/ecloud/models/compute/layout.rb +++ b/lib/fog/ecloud/models/compute/layout.rb @@ -8,7 +8,7 @@ module Fog attribute :other_links, :aliases => :Links def rows - @rows = Fog::Compute::Ecloud::Rows.new(:connection => connection, :href => href) + @rows ||= self.connection.rows(:href => href) end def id diff --git a/lib/fog/ecloud/models/compute/network.rb b/lib/fog/ecloud/models/compute/network.rb index 244908cbb..f3855f6f2 100644 --- a/lib/fog/ecloud/models/compute/network.rb +++ b/lib/fog/ecloud/models/compute/network.rb @@ -4,14 +4,14 @@ module Fog class Network < Fog::Ecloud::Model identity :href - attribute :name, :aliases => :Name - attribute :type, :aliases => :Type - attribute :other_links, :aliases => :Links - attribute :address, :aliases => :Address - attribute :network_type, :aliases => :NetworkType + attribute :name, :aliases => :Name + attribute :type, :aliases => :Type + attribute :other_links, :aliases => :Links, :squash => :Link + attribute :address, :aliases => :Address + attribute :network_type, :aliases => :NetworkType attribute :broadcast_address, :aliases => :BroadcastAddress - attribute :gateway_address, :aliases => :GatewayAddress - attribute :rnat_address, :aliases => :RnatAddress + attribute :gateway_address, :aliases => :GatewayAddress + attribute :rnat_address, :aliases => :RnatAddress def rnats @rnats ||= Fog::Compute::Ecloud::Rnats.new(:connection => connection, :href => "cloudapi/ecloud/rnats/networks/#{id}") @@ -26,10 +26,20 @@ module Fog data = connection.rnat_associations_edit_network(options).body task = Fog::Compute::Ecloud::Tasks.new(:connection => connection, :href => data[:href])[0] end - + def id href.scan(/\d+/)[0] end + + def environment + reload if other_links.nil? + environment_href = other_links.detect { |l| l[:type] == "application/vnd.tmrk.cloud.environment" }[:href] + self.connection.environments.get(environment_href) + end + + def location + environment.id + end end end end diff --git a/lib/fog/ecloud/models/compute/networks.rb b/lib/fog/ecloud/models/compute/networks.rb index 3c2a698b9..1763fd608 100644 --- a/lib/fog/ecloud/models/compute/networks.rb +++ b/lib/fog/ecloud/models/compute/networks.rb @@ -5,14 +5,18 @@ module Fog class Ecloud class Networks < Fog::Ecloud::Collection - identity :href + attribute :href, :aliases => :Href model Fog::Compute::Ecloud::Network def all - data = connection.get_networks(href).body - data = data[:Networks] ? data[:Networks][:Network] : data[:Network] - data = data.nil? ? [] : data + body = connection.get_networks(self.href).body + body = body[:Networks] ? body[:Networks][:Network] : body[:Network] + data = case body + when NilClass then [] + when Array then body + when Hash then [body] + end load(data) end diff --git a/lib/fog/ecloud/models/compute/node.rb b/lib/fog/ecloud/models/compute/node.rb index c5c383e5c..c657df25b 100644 --- a/lib/fog/ecloud/models/compute/node.rb +++ b/lib/fog/ecloud/models/compute/node.rb @@ -13,13 +13,17 @@ module Fog attribute :enabled, :aliases => :Enabled, :type => :boolean attribute :description, :aliases => :Description + def ready? + !self.name.nil? + end + def tasks @tasks ||= Fog::Compute::Ecloud::Tasks.new(:connection => connection, :href => "/cloudapi/ecloud/tasks/virtualMachines/#{id}") end def delete data = connection.node_service_delete(href).body - task = Fog::Compute::Ecloud::Tasks.new(:connection => connection, :href => data[:href])[0] + self.connection.tasks.new(data) end def edit(options) @@ -29,10 +33,12 @@ module Fog data = connection.node_service_edit(options).body task = Fog::Compute::Ecloud::Tasks.new(:connection => connection, :href => data[:href])[0] end - + def id href.scan(/\d+/)[0] end + + alias destroy delete end end end diff --git a/lib/fog/ecloud/models/compute/nodes.rb b/lib/fog/ecloud/models/compute/nodes.rb index c2ca5359f..07264a46b 100644 --- a/lib/fog/ecloud/models/compute/nodes.rb +++ b/lib/fog/ecloud/models/compute/nodes.rb @@ -19,8 +19,11 @@ module Fog end def get(uri) - if data = connection.get_node(uri) - new(data.body) + data = connection.get_node(uri).body + if data == "" + new({}) + else + new(data) end rescue Fog::Errors::NotFound nil diff --git a/lib/fog/ecloud/models/compute/operating_system_family.rb b/lib/fog/ecloud/models/compute/operating_system_family.rb index 1a6a68134..622a8b9ec 100644 --- a/lib/fog/ecloud/models/compute/operating_system_family.rb +++ b/lib/fog/ecloud/models/compute/operating_system_family.rb @@ -9,7 +9,7 @@ module Fog attribute :operating_system_family, :aliases => :OperatingSystems def operating_systems - @operating_systems ||= Fog::Compute::Ecloud::OperatingSystems.new(:connection => connection, :data => operating_system_family[:OperatingSystem]) + @operating_systems ||= self.connection.operating_systems(:data => operating_system_family[:OperatingSystem]) end def id diff --git a/lib/fog/ecloud/models/compute/organization.rb b/lib/fog/ecloud/models/compute/organization.rb index 75c3ca7f4..1e1ab1d47 100644 --- a/lib/fog/ecloud/models/compute/organization.rb +++ b/lib/fog/ecloud/models/compute/organization.rb @@ -9,26 +9,26 @@ module Fog attribute :name, :aliases => :Name attribute :type, :aliases => :Type - attribute :other_links, :aliases => :Links + attribute :other_links, :aliases => :Links, :squash => :Link def locations @locations ||= Fog::Compute::Ecloud::Locations.new( :connection => connection, :href => href ) end def environments - @environments ||= Fog::Compute::Ecloud::Environments.new(:connection => connection, :href => href) + @environments ||= self.connection.environments(:href => href) end def tags - @tags ||= Fog::Compute::Ecloud::Tags.new(:connection => connection, :href => "/cloudapi/ecloud/deviceTags/organizations/#{id}") + @tags ||= self.connection.tags(:href => "/cloudapi/ecloud/deviceTags/organizations/#{id}") end def admin - @admin ||= Fog::Compute::Ecloud::AdminOrganizations.new(:connection => connection, :href => "/cloudapi/ecloud/admin/organizations/#{id}").first + @admin ||= self.connection.admin_organizations.new(:href => "/cloudapi/ecloud/admin/organizations/#{id}") end def users - @users ||= Fog::Compute::Ecloud::Users.new(:connection => connection, :href => "/cloudapi/ecloud/admin/users/organizations/#{id}") + @users ||= self.connection.users(:href => "/cloudapi/ecloud/admin/users/organizations/#{id}") end def support_tickets(type = :open) diff --git a/lib/fog/ecloud/models/compute/public_ip.rb b/lib/fog/ecloud/models/compute/public_ip.rb index f753605cf..6b912ca65 100644 --- a/lib/fog/ecloud/models/compute/public_ip.rb +++ b/lib/fog/ecloud/models/compute/public_ip.rb @@ -12,7 +12,7 @@ module Fog def internet_services @internet_services = Fog::Compute::Ecloud::InternetServices.new(:connection => connection, :href => href) end - + def environment_id other_links[:Link].detect { |l| l[:type] == "application/vnd.tmrk.cloud.environment" }[:href].scan(/\d+/)[0] end diff --git a/lib/fog/ecloud/models/compute/public_ips.rb b/lib/fog/ecloud/models/compute/public_ips.rb index 456108172..79ed79626 100644 --- a/lib/fog/ecloud/models/compute/public_ips.rb +++ b/lib/fog/ecloud/models/compute/public_ips.rb @@ -16,8 +16,11 @@ module Fog end def get(uri) - if data = connection.get_public_ip(uri) - new(data.body) + data = connection.get_public_ip(uri).body + if data == "" + new({}) + else + new(data) end rescue Fog::Errors::NotFound nil diff --git a/lib/fog/ecloud/models/compute/row.rb b/lib/fog/ecloud/models/compute/row.rb index ad8113af8..022f4a6b8 100644 --- a/lib/fog/ecloud/models/compute/row.rb +++ b/lib/fog/ecloud/models/compute/row.rb @@ -10,7 +10,7 @@ module Fog attribute :index, :aliases => :Index def groups - @groups = Fog::Compute::Ecloud::Groups.new(:connection => connection, :href => href) + @groups = self.connection.groups(:href => href) end def edit(options) @@ -37,16 +37,19 @@ module Fog options[:row_name] = name options[:href] = href data = connection.groups_create(options).body - group = Fog::Compute::Ecloud::Groups.new(:connection => connection, :href => data[:href])[0] + group = self.connection.groups.new(data) end def environment_id + reload if other_links.nil? other_links[:Link][:href].scan(/\d+/)[0] end def id href.scan(/\d+/)[0] end + + alias destroy delete end end end diff --git a/lib/fog/ecloud/models/compute/rows.rb b/lib/fog/ecloud/models/compute/rows.rb index e3dd7cb68..fa7806210 100644 --- a/lib/fog/ecloud/models/compute/rows.rb +++ b/lib/fog/ecloud/models/compute/rows.rb @@ -15,10 +15,13 @@ module Fog end def get(uri) - if data = connection.get_row(uri) - new(data.body) + data = connection.get_row(uri).body + if data == "" + nil + else + new(data) end - rescue Fog::Errors::NotFound + rescue Excon::Errors::NotFound nil end diff --git a/lib/fog/ecloud/models/compute/server.rb b/lib/fog/ecloud/models/compute/server.rb index 69f0468ec..0e4d4143c 100644 --- a/lib/fog/ecloud/models/compute/server.rb +++ b/lib/fog/ecloud/models/compute/server.rb @@ -2,80 +2,69 @@ module Fog module Compute class Ecloud class Server < Fog::Ecloud::Model + extend Forwardable + identity :href - attribute :name, :aliases => :Name - attribute :type , :aliases => :Type - attribute :other_links, :aliases => :Link - attribute :status, :aliases => :Status - attribute :storage, :aliases => :Storage - attribute :ip_addresses, :aliases => :IpAddresses - attribute :operating_system, :aliases => :OperatingSystem - attribute :powered_on, :aliases => :PoweredOn, :type => :boolean - attribute :tools_status, :aliases => :ToolsStatus - attribute :cpus, :aliases => :ProcessorCount, :type => :integer - attribute :memory, :aliases => :Memory - attribute :description, :aliases => :Description - attribute :tags, :aliases => :Tags - attribute :layout, :aliases => :Layout + attribute :description, :aliases => :Description + attribute :hardware_configuration, :aliases => :HardwareConfiguration + attribute :ip_addresses, :aliases => :IpAddresses, :squash => :AssignedIpAddresses + attribute :layout, :aliases => :Layout + attribute :name, :aliases => :Name + attribute :operating_system, :aliases => :OperatingSystem + attribute :other_links, :aliases => :Links, :squash => :Link + attribute :powered_on, :aliases => :PoweredOn, :type => :boolean + attribute :status, :aliases => :Status + attribute :tags, :aliases => :Tags + attribute :tools_status, :aliases => :ToolsStatus + attribute :type, :aliases => :Type + + def cpus + hardware_configuration.processor_count + end + + def memory # always in MB + hardware_configuration.memory.to_i + end + + def location + end + + def flavor_id + {:ram => hardware_configuration.memory.to_i, :cpus => hardware_configuration.processor_count} + end + + def storage + hardware_configuration.storage[:Disk] + end def tasks - @tasks ||= Fog::Compute::Ecloud::Tasks.new(:connection => connection, :href => "/cloudapi/ecloud/tasks/virtualMachines/#{id}") + @tasks ||= self.connection.tasks(:href => "/cloudapi/ecloud/tasks/virtualMachines/#{id}") end def processes @processes ||= Fog::Compute::Ecloud::GuestProcesses.new(:connection, connection, :href => "/cloudapi/ecloud/virtualMachines/#{id}/guest/processes") end + def hardware_configuration=(hardware_configuration) + @hardware_configuration = self.connection.hardware_configurations.new(hardware_configuration) + end + def hardware_configuration - @hardware_configuration ||= Fog::Compute::Ecloud::HardwareConfigurations.new(:connection => connection, :href => "/cloudapi/ecloud/virtualMachines/#{id}/hardwareConfiguration")[0] + @hardware_configuration ||= self.connection.hardware_configurations.new(:href => "/cloudapi/ecloud/virtualMachines/#{id}/hardwareConfiguration") + @hardware_configuration.reload end def configuration @configuration ||= Fog::Compute::Ecloud::ServerConfigurationOptions.new(:connection => connection, :href => "/cloudapi/ecloud/virtualMachines/#{id}/configurationOptions")[0] end - def ips - network_hash = ip_addresses[:AssignedIpAddresses][:Networks] || [] - network_hash[:Network] = network_hash[:Network].is_a?(Hash) ? [network_hash[:Network]] : network_hash[:Network] - network_hash[:Network].each do |network| - network[:IpAddresses][:IpAddress] = network[:IpAddresses][:IpAddress].is_a?(String) ? [network[:IpAddresses][:IpAddress]] : network[:IpAddresses][:IpAddress] - end - @ips = nil - networks = Fog::Compute::Ecloud::Networks.new(:connection => connection, :href => "/cloudapi/ecloud/virtualMachines/#{id}/assignedIps") - networks.each do |network| - if networks.index(network) == 0 - if @ips.nil? - @ips = network.ips.select do |ip| - network_hash[:Network].any? do |network| - network[:IpAddresses][:IpAddress].include?(ip.name) - end - end - else - network.ips.each do |ip| - network_hash[:Network].any? do |network| - network[:IpAddresses][:IpAddress].each do |i| - @ips << ip if i == ip.name - end - end - end - end - else - network.ips.each do |ip| - network_hash[:Network].each do |network| - network[:IpAddresses][:IpAddress].each do |i| - @ips << ip if i == ip.name - end - end - end - end - end - @ips + @ips = self.connection.virtual_machine_assigned_ips(:virtual_machine_id => self.id) end def networks - @networks ||= Fog::Compute::Ecloud::Networks.new(:connection => connection, :href => "/cloudapi/ecloud/virtualMachines/#{id}/assignedIps") + @networks ||= self.connection.networks(:href => "/cloudapi/ecloud/virtualMachines/#{id}/assignedIps") end def power_on @@ -96,7 +85,7 @@ module Fog def delete data = connection.virtual_machine_delete(href).body - task = Fog::Compute::Ecloud::Tasks.new(:connection => connection, :href => data[:href])[0] + self.connection.tasks.new(data) end def copy(options = {}) @@ -165,20 +154,43 @@ module Fog end def disks - c = hardware_configuration.reload.storage[:Disk] + c = hardware_configuration.reload.storage c = c.is_a?(Hash) ? [c] : c @disks = c end def add_disk(size) index = disks.map { |d| d[:Index].to_i }.sort[-1] + 1 - vm_disks = disks << {:Index => index, :Size=>{:Unit => "GB", :Value => size}} + vm_disks = disks << {:Index => index.to_s, :Size=>{:Unit => "GB", :Value => size.to_s}, :Name => "Hard Disk #{index + 1}"} data = connection.virtual_machine_edit_hardware_configuration(href + "/hardwareConfiguration", _configuration_data(:disks => vm_disks)).body - task = Fog::Compute::Ecloud::Tasks.new(:connection => connection, :href => data[:href])[0] + task = self.connection.tasks.new(data) + end + + def detach_disk(index) + options = {} + options[:disk] = disks.detect { |disk_hash| disk_hash[:Index] == index.to_s } + options[:name] = self.name + options[:description] = self.description + data = connection.virtual_machine_detach_disk(href + "/hardwareconfiguration/disks/actions/detach", options).body + detached_disk = self.connection.detached_disks.new(data) + end + + def attach_disk(detached_disk) + options = {} + options[:name] = detached_disk.name + options[:href] = detached_disk.href + data = connection.virtual_machine_attach_disk(href + "/hardwareconfiguration/disks/actions/attach", options).body + task = self.connection.tasks.new(data) + end + + def delete_disk(index) + vm_disks = disks.delete_if { |h| h[:Index] == index.to_s } + data = connection.virtual_machine_edit_hardware_configuration(href + "/hardwareconfiguration", _configuration_data(:disks => vm_disks)).body + task = self.connection.tasks.new(data) end def nics - c = hardware_configuration.network_cards[:Nic] + c = hardware_configuration.network_cards c = c.is_a?(Hash) ? [c] : c @nics = c end @@ -187,22 +199,21 @@ module Fog unit_number = nics.map { |n| n[:UnitNumber].to_i }.sort[-1] + 1 vm_nics = nics << {:UnitNumber => unit_number, :Network => {:href => network.href, :name => network.name, :type => "application/vnd.tmrk.cloud.network"}} data = connection.virtual_machine_edit_hardware_configuration(href + "/hardwareConfiguration", _configuration_data(:nics => vm_nics)).body - task = Fog::Compute::Ecloud::Tasks.new(:connection => connection, :href => data[:href])[0] + task = self.connection.tasks.new(:href => data[:href])[0] end def add_ip(options) - begin - slice_ips = ips - rescue - slice_ips = [] - end - begin - slice_networks = networks - rescue - slice_networks = [] - end - slice_networks = slice_networks.map { |n| {:href => n.href, :name => n.name.split(' ')[0], :type => 'application/vnd.tmrk.cloud.network'} }.push({:href => options[:href], :name => options[:network_name], :type => 'application/vnd.tmrk.cloud.network'}).uniq - slice_ips = slice_ips.map { |i| {:name => i.name, :network_name => i.other_links[:Link][:name]} }.push({:name => options[:ip], :network_name => options[:network_name]}) + slice_ips = begin + ips + rescue + [] + end + slice_networks = if slice_ips.empty? + [] + else + ips.map { |ip| {:href => ip.network.href, :name => ip.network.name.split(' ')[0], :type => ip.network.type} }.push({:href => options[:href], :name => options[:network_name], :type => "application/vnd.tmrk.cloud.network"}).uniq + end + slice_ips = slice_ips.map { |i| {:name => i.address.name, :network_name => i.network.name} }.push({:name => options[:ip], :network_name => options[:network_name]}).uniq slice_ips.each do |ip| slice_networks.each do |network| if network[:name] == ip[:network_name] @@ -211,8 +222,37 @@ module Fog end end end - data = connection.virtual_machine_add_ip(href + "/assignedIps", slice_networks).body - task = Fog::Compute::Ecloud::Tasks.new(:connection => connection, :href => data[:href])[0] + data = connection.virtual_machine_edit_assigned_ips(href + "/assignedIps", slice_networks).body + task = self.connection.tasks.new(data) + end + + def delete_ip(options) + slice_ips = begin + ips + rescue + [] + end + slice_networks = if slice_ips.empty? + [] + else + ips.map do |ip| + { + :href => ip.network.href, + :name => ip.network.name.split(' ')[0], + :type => ip.network.type, + } + end#.delete_if { |ip| ip[:href] == options[:href] && ip[:name] == options[:network_name] } + end + slice_ips.map! { |i| {:name => i.address.name, :network_name => i.network.name, :network_name => i.network.name } }.delete_if { |ip| ip[:name] == options[:ip] } + slice_ips.each do |ip| + slice_networks.each do |network| + if network[:name] == ip[:network_name] + network[:ips].delete(ip[:name]) + end + end + end + data = connection.virtual_machine_edit_assigned_ips(href + "/assignedips", slice_networks).body + task = self.connection.tasks.new(data) end def upload_file(options) @@ -222,7 +262,7 @@ module Fog def storage_size vm_disks = disks - disks.map! { |d| d[:Size][:Value].to_i }.inject(0){|sum,item| sum + item} * 1024 * 1024 + disks.map! { |d| d[:Size][:Value].to_i }.inject(0){|sum,item| sum + item} * 1024 * 1024 end def ready? @@ -243,11 +283,16 @@ module Fog end def compute_pool_id - other_links[:Link].detect { |l| l[:type] == "application/vnd.tmrk.cloud.computePool" }[:href].scan(/\d+/)[0] + other_links.detect { |l| l[:type] == "application/vnd.tmrk.cloud.computePool" }[:href].scan(/\d+/)[0] + end + + def compute_pool + reload if other_links.nil? + @compute_pool = self.connection.compute_pools.new(:href => other_links.detect { |l| l[:type] == "application/vnd.tmrk.cloud.computePool" }[:href]) end def environment_id - other_links[:Link].detect { |l| l[:type] == "application/vnd.tmrk.cloud.environment" }[:href].scan(/\d+/)[0] + other_links.detect { |l| l[:type] == "application/vnd.tmrk.cloud.environment" }[:href].scan(/\d+/)[0] end def id @@ -257,7 +302,7 @@ module Fog private def _configuration_data(options = {}) - {:cpus => (options[:cpus] || hardware_configuration.processor_count), :memory => (options[:memory] || hardware_configuration.mem), :disks => (options[:disks] || disks), :nics => (options[:nics] || nics)} + {:cpus => (options[:cpus] || hardware_configuration.processor_count), :memory => (options[:memory] || hardware_configuration.memory), :disks => (options[:disks] || disks), :nics => (options[:nics] || nics)} end def power_operation(op) @@ -271,6 +316,7 @@ module Fog true end + alias destroy delete end end end diff --git a/lib/fog/ecloud/models/compute/servers.rb b/lib/fog/ecloud/models/compute/servers.rb index 85558cdf7..2d2605397 100644 --- a/lib/fog/ecloud/models/compute/servers.rb +++ b/lib/fog/ecloud/models/compute/servers.rb @@ -4,7 +4,7 @@ module Fog module Compute class Ecloud class Servers < Fog::Ecloud::Collection - + model Fog::Compute::Ecloud::Server identity :href @@ -22,10 +22,13 @@ module Fog end def get(uri) - if data = connection.get_server(uri) - new(data.body) + data = connection.get_server(uri).body + if data == "" + new({}) + else + new(data) end - rescue Fog::Errors::NotFound + rescue Excon::Errors::NotFound nil end @@ -34,21 +37,21 @@ module Fog end def create( template_uri, options ) - options[:cpus] ||= 1 - options[:memory] ||= 512 + options[:cpus] ||= 1 + options[:memory] ||= 512 options[:description] ||= "" - options[:tags] ||= [] + options[:tags] ||= [] + if template_uri =~ /\/templates\/\d+/ options[:uri] = href + "/action/createVirtualMachine" options[:customization] ||= :linux options[:powered_on] ||= false if options[:ips] - options[:ips] = options[:ips].is_a?(String) ? [options[:ips]] : options[:ips] + options[:ips] = [*options[:ips]] else - options[:network_uri] = options[:network_uri].is_a?(String) ? [options[:network_uri]] : options[:network_uri] - options[:network_uri].each do |uri| + [*options[:network_uri]].each do |uri| index = options[:network_uri].index(uri) - ip = Fog::Compute::Ecloud::IpAddresses.new(:connection => connection, :href => uri).detect { |i| i.host == nil }.name + ip = self.connection.ip_addresses(:href => uri).detect { |i| i.host == nil && i.detected_on.nil? }.name options[:ips] ||= [] options[:ips][index] = ip end @@ -58,7 +61,7 @@ module Fog options[:uri] = href + "/action/importVirtualMachine" data = connection.virtual_machine_import( template_uri, options ).body end - object = new(data) + object = self.connection.servers.new(data) object end diff --git a/lib/fog/ecloud/models/compute/ssh_key.rb b/lib/fog/ecloud/models/compute/ssh_key.rb index 9b1b56728..ad39d0821 100644 --- a/lib/fog/ecloud/models/compute/ssh_key.rb +++ b/lib/fog/ecloud/models/compute/ssh_key.rb @@ -10,7 +10,7 @@ module Fog attribute :default, :aliases => :Default, :type => :boolean attribute :finger_print, :aliases => :FingerPrint - + def id href.scan(/\d+/)[0] end diff --git a/lib/fog/ecloud/models/compute/task.rb b/lib/fog/ecloud/models/compute/task.rb index 2cfbca998..46470e0f1 100644 --- a/lib/fog/ecloud/models/compute/task.rb +++ b/lib/fog/ecloud/models/compute/task.rb @@ -14,6 +14,9 @@ module Fog attribute :error_message, :aliases => :ErrorMessage attribute :initiated_by, :aliases => :InitiatedBy + def ready? + !self.completed_time.nil? + end end end end diff --git a/lib/fog/ecloud/models/compute/template.rb b/lib/fog/ecloud/models/compute/template.rb index 9fffb8e3d..96a64e6f1 100644 --- a/lib/fog/ecloud/models/compute/template.rb +++ b/lib/fog/ecloud/models/compute/template.rb @@ -4,17 +4,16 @@ module Fog class Template < Fog::Ecloud::Model identity :href - attribute :name, :aliases => :Name - attribute :type, :aliases => :Type - attribute :other_links, :aliases => :Links - attribute :operating_system, :aliases => :OperatingSystem - attribute :description, :aliases => :Description - attribute :storage, :aliases => :Storage - attribute :network_adapters, :aliases => :NetworkAdapters - attribute :customization, :aliases => :Customization + attribute :name, :aliases => :Name + attribute :type, :aliases => :Type + attribute :other_links, :aliases => :Links + attribute :operating_system, :aliases => :OperatingSystem + attribute :description, :aliases => :Description + attribute :storage, :aliases => :Storage + attribute :network_adapters, :aliases => :NetworkAdapters + attribute :customization, :aliases => :Customization attribute :licensed_software, :aliases => :LicensedSoftware - def id href.scan(/\d+/)[0] end diff --git a/lib/fog/ecloud/models/compute/virtual_machine_assigned_ip.rb b/lib/fog/ecloud/models/compute/virtual_machine_assigned_ip.rb new file mode 100644 index 000000000..5ae4f1abe --- /dev/null +++ b/lib/fog/ecloud/models/compute/virtual_machine_assigned_ip.rb @@ -0,0 +1,32 @@ +module Fog + module Compute + class Ecloud + class VirtualMachineAssignedIp < Fog::Ecloud::Model + identity :href + + attribute :network, :aliases => :Networks + attribute :address + + def id + href.scan(/\d+/)[0] + end + + def network=(network) + network = network.dup + network_address = network[:Network] + @network = self.connection.networks.new(network_address) + network_id = @network.href.match(/(\d+)$/)[1] + address_ip = network_address[:IpAddresses][:IpAddress] + @address = self.connection.ip_addresses.new( + :href => "/cloudapi/ecloud/ipaddresses/networks/#{network_id}/#{address_ip}", + :name => address_ip + ) + end + attr_reader :network + + def address=(address); end + attr_reader :address + end + end + end +end diff --git a/lib/fog/ecloud/models/compute/virtual_machine_assigned_ips.rb b/lib/fog/ecloud/models/compute/virtual_machine_assigned_ips.rb new file mode 100644 index 000000000..12ec12efa --- /dev/null +++ b/lib/fog/ecloud/models/compute/virtual_machine_assigned_ips.rb @@ -0,0 +1,27 @@ +require 'fog/ecloud/models/compute/virtual_machine_assigned_ip' + +module Fog + module Compute + class Ecloud + class VirtualMachineAssignedIps < Fog::Ecloud::Collection + + identity :virtual_machine_id + + model Fog::Compute::Ecloud::VirtualMachineAssignedIp + + def all + data = connection.get_virtual_machine_assigned_ips(self.identity).body + load(data) + end + + def get(uri) + if data = connection.get_virtual_machine_assigned_ip(self.identity) + new(data.body) + end + rescue Fog::Errors::NotFound + nil + end + end + end + end +end diff --git a/lib/fog/ecloud/requests/compute/admin_edit_authentication_levels.rb b/lib/fog/ecloud/requests/compute/admin_edit_authentication_levels.rb index 19f82c82d..5cd20ff07 100644 --- a/lib/fog/ecloud/requests/compute/admin_edit_authentication_levels.rb +++ b/lib/fog/ecloud/requests/compute/admin_edit_authentication_levels.rb @@ -18,7 +18,6 @@ module Fog private - def build_authentication_levels_edit(data) xml = Builder::XmlMarkup.new xml.AuthenticationLevels do @@ -26,7 +25,7 @@ module Fog xml.SHA1Enabled data[:sha1] xml.SHA256Enabled data[:sha256] xml.SHA512Enabled data[:sha512] - end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_admin_organization.rb b/lib/fog/ecloud/requests/compute/get_admin_organization.rb index 5125bb819..4d7fa0125 100644 --- a/lib/fog/ecloud/requests/compute/get_admin_organization.rb +++ b/lib/fog/ecloud/requests/compute/get_admin_organization.rb @@ -6,6 +6,20 @@ module Fog basic_request :get_admin_organization end + class Mock + def get_admin_organization(uri) + + organization_id = id_from_uri(uri) + admin_organization = self.data[:admin_organizations][organization_id] + + if admin_organization + body = Fog::Ecloud.slice(admin_organization, :id, :organization_id) + + response(:body => body) + else response(:status => 404) # ? + end + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_admin_organizations.rb b/lib/fog/ecloud/requests/compute/get_admin_organizations.rb deleted file mode 100644 index eaab6f98a..000000000 --- a/lib/fog/ecloud/requests/compute/get_admin_organizations.rb +++ /dev/null @@ -1,11 +0,0 @@ -module Fog - module Compute - class Ecloud - - class Real - basic_request :get_admin_organizations - end - - end - end -end diff --git a/lib/fog/ecloud/requests/compute/get_compute_pool.rb b/lib/fog/ecloud/requests/compute/get_compute_pool.rb index b5e030a00..02e97ace2 100644 --- a/lib/fog/ecloud/requests/compute/get_compute_pool.rb +++ b/lib/fog/ecloud/requests/compute/get_compute_pool.rb @@ -6,6 +6,17 @@ module Fog basic_request :get_compute_pool end + class Mock + def get_compute_pool(uri) + compute_pool_id = id_from_uri(uri) + compute_pool = self.data[:compute_pools][compute_pool_id] + + if compute_pool + response(:body => Fog::Ecloud.slice(compute_pool, :id, :environment)) + else response(:status => 404) # ? + end + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_compute_pools.rb b/lib/fog/ecloud/requests/compute/get_compute_pools.rb index 9f90df138..f1be76e32 100644 --- a/lib/fog/ecloud/requests/compute/get_compute_pools.rb +++ b/lib/fog/ecloud/requests/compute/get_compute_pools.rb @@ -6,6 +6,27 @@ module Fog basic_request :get_compute_pools end + class Mock + def get_compute_pools(uri) # /cloudapi/ecloud/computepools/environments/534 + environment_id = id_from_uri(uri) + environment = self.data[:environments][environment_id] + + compute_pools = self.data[:compute_pools].values.select{|cp| cp[:environment_id] == environment_id} + + compute_pools = compute_pools.map{|cp| Fog::Ecloud.slice(cp, :id, :environment_id)} + + compute_pool_response = {:ComputePool => (compute_pools.size > 1 ? compute_pools : compute_pools.first)} # GAH + body = { + :href => uri, + :type => "application/vnd.tmrk.cloud.computePool; type=collection", + :Links => { + :Link => environment, + } + }.merge(compute_pool_response) + + response(:body => body) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_detached_disk.rb b/lib/fog/ecloud/requests/compute/get_detached_disk.rb new file mode 100644 index 000000000..a31956faa --- /dev/null +++ b/lib/fog/ecloud/requests/compute/get_detached_disk.rb @@ -0,0 +1,21 @@ +module Fog + module Compute + class Ecloud + class Real + basic_request :get_detached_disk + end + + class Mock + def get_detached_disk(uri) + detached_disk_id = id_from_uri(uri) + detached_disk = self.data[:detached_disks][detached_disk_id] + + if detached_disk + response(:body => Fog::Ecloud.slice(detached_disk, :id, :compute_pool_id)) + else response(:status => 404) # ? + end + end + end + end + end +end diff --git a/lib/fog/ecloud/requests/compute/get_detached_disks.rb b/lib/fog/ecloud/requests/compute/get_detached_disks.rb new file mode 100644 index 000000000..b0ef60658 --- /dev/null +++ b/lib/fog/ecloud/requests/compute/get_detached_disks.rb @@ -0,0 +1,33 @@ +module Fog + module Compute + class Ecloud + + class Real + basic_request :get_detached_disks + end + + class Mock + def get_detached_disks(uri) + + compute_pool_id = id_from_uri(uri) + compute_pool = self.data[:compute_pools][compute_pool_id] + + detached_disks = self.data[:detached_disks].values.select{|cp| cp[:compute_pool_id] == compute_pool_id} + + detached_disks = detached_disks.map{|dd| Fog::Ecloud.slice(dd, :id, :compute_pool_id)} + + detached_disk_response = {:DetachedDisk => (detached_disks.size > 1 ? detached_disks : detached_disks.first)} # GAH + body = { + :href => uri, + :type => "application/vnd.tmrk.cloud.detachedDisk; type=collection", + :Links => { + :Link => Fog::Ecloud.keep(compute_pool, :name, :href, :type), + } + }.merge(detached_disk_response) + + response(:body => body) + end + end + end + end +end diff --git a/lib/fog/ecloud/requests/compute/get_environment.rb b/lib/fog/ecloud/requests/compute/get_environment.rb index 26394881c..105865c99 100644 --- a/lib/fog/ecloud/requests/compute/get_environment.rb +++ b/lib/fog/ecloud/requests/compute/get_environment.rb @@ -6,6 +6,27 @@ module Fog basic_request :get_environment end + class Mock + def get_environment(uri) + environment_id = id_from_uri(uri) + organizations = self.data[:organizations].values + environment = nil + catch(:found) do + organizations.each do |organization| + organization[:Locations][:Location].each do |location| + environment = location[:Environments][:Environment].find{|e| e[:id] == environment_id} + throw :found if environment + end + end + end + if environment + body = environment.dup + body.delete(:id) + response(:body => body) + else response(:status => 404) # ? + end + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_group.rb b/lib/fog/ecloud/requests/compute/get_group.rb index 8dcbb8757..5d316d2dc 100644 --- a/lib/fog/ecloud/requests/compute/get_group.rb +++ b/lib/fog/ecloud/requests/compute/get_group.rb @@ -1,11 +1,18 @@ module Fog module Compute class Ecloud - class Real basic_request :get_group end + class Mock + def get_group(uri) + group_id = id_from_uri(uri) + group = self.data[:groups][group_id] + + response(:body => group) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_groups.rb b/lib/fog/ecloud/requests/compute/get_groups.rb index 9694cc043..671225b7e 100644 --- a/lib/fog/ecloud/requests/compute/get_groups.rb +++ b/lib/fog/ecloud/requests/compute/get_groups.rb @@ -1,11 +1,18 @@ module Fog module Compute class Ecloud - class Real basic_request :get_groups end + class Mock + def get_groups(uri) + row_id = id_from_uri(uri) + row = self.data[:rows][row_id] + + response(:body => row) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_hardware_configuration.rb b/lib/fog/ecloud/requests/compute/get_hardware_configuration.rb index 5701e951d..0b2cd3c0a 100644 --- a/lib/fog/ecloud/requests/compute/get_hardware_configuration.rb +++ b/lib/fog/ecloud/requests/compute/get_hardware_configuration.rb @@ -6,6 +6,25 @@ module Fog basic_request :get_hardware_configuration end + class Mock + def get_hardware_configuration(uri) + server_id = uri.match(/(\d+)/)[1] + + server = self.data[:servers][server_id.to_i] + server_hardware_configuration = server[:HardwareConfiguration] + + new_hardware_configuration = { + :href => server_hardware_configuration[:href], + :type => server_hardware_configuration[:type], + :ProcessorCount => server_hardware_configuration[:ProcessorCount], + :Memory => server_hardware_configuration[:Memory], + :Disks => server_hardware_configuration[:Disks], + :Nics => server_hardware_configuration[:Nics], + } + + response(:body => {:HardwareConfiguration => new_hardware_configuration}) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_hardware_configurations.rb b/lib/fog/ecloud/requests/compute/get_hardware_configurations.rb index ed4d6f57f..c6ab8e830 100644 --- a/lib/fog/ecloud/requests/compute/get_hardware_configurations.rb +++ b/lib/fog/ecloud/requests/compute/get_hardware_configurations.rb @@ -6,6 +6,10 @@ module Fog basic_request :get_hardware_configurations end + class Mock + def get_hardware_configurations(uri) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_internet_service.rb b/lib/fog/ecloud/requests/compute/get_internet_service.rb index 6ae138469..97a8200b3 100644 --- a/lib/fog/ecloud/requests/compute/get_internet_service.rb +++ b/lib/fog/ecloud/requests/compute/get_internet_service.rb @@ -6,6 +6,18 @@ module Fog basic_request :get_internet_service end + class Mock + def get_internet_service(uri) + + internet_service_id = id_from_uri(uri) + internet_service = self.data[:internet_services][internet_service_id.to_i] + + if internet_service + response(:body => Fog::Ecloud.slice(internet_service, :id, :public_ip)) + else response(:status => 404) # ? + end + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_internet_services.rb b/lib/fog/ecloud/requests/compute/get_internet_services.rb index 67aa9a748..93ee7773b 100644 --- a/lib/fog/ecloud/requests/compute/get_internet_services.rb +++ b/lib/fog/ecloud/requests/compute/get_internet_services.rb @@ -6,6 +6,14 @@ module Fog basic_request :get_internet_services end + class Mock + def get_internet_services(uri) + public_ip_id = id_from_uri(uri) + public_ip = self.data[:public_ips][public_ip_id] + + response(:body => Fog::Ecloud.slice(public_ip, :environment_id)) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_ip_address.rb b/lib/fog/ecloud/requests/compute/get_ip_address.rb index aed2b8ce8..f4ec658fd 100644 --- a/lib/fog/ecloud/requests/compute/get_ip_address.rb +++ b/lib/fog/ecloud/requests/compute/get_ip_address.rb @@ -6,6 +6,17 @@ module Fog basic_request :get_ip_address end + class Mock + def get_ip_address(uri) + + network_id, ip_address_id = uri.match(/\/networks\/(\d+)\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/).captures + ip_address = self.data[:networks][network_id.to_i][:IpAddresses][:IpAddress].detect{|ip| ip[:name] == ip_address_id }.dup + if ip_address + response(:body => ip_address) + else response(:status => 404) # ? + end + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_ip_addresses.rb b/lib/fog/ecloud/requests/compute/get_ip_addresses.rb deleted file mode 100644 index 735ea65f8..000000000 --- a/lib/fog/ecloud/requests/compute/get_ip_addresses.rb +++ /dev/null @@ -1,11 +0,0 @@ -module Fog - module Compute - class Ecloud - - class Real - basic_request :get_ip_addresses - end - - end - end -end diff --git a/lib/fog/ecloud/requests/compute/get_layout.rb b/lib/fog/ecloud/requests/compute/get_layout.rb index e661db9ff..8e99ebe0b 100644 --- a/lib/fog/ecloud/requests/compute/get_layout.rb +++ b/lib/fog/ecloud/requests/compute/get_layout.rb @@ -1,11 +1,18 @@ module Fog module Compute class Ecloud - class Real basic_request :get_layout end + class Mock + def get_layout(uri) + environment_id = id_from_uri(uri) + layout = self.data[:layouts][environment_id] + + response(:body => layout) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_layouts.rb b/lib/fog/ecloud/requests/compute/get_layouts.rb index 5b035331d..904e40147 100644 --- a/lib/fog/ecloud/requests/compute/get_layouts.rb +++ b/lib/fog/ecloud/requests/compute/get_layouts.rb @@ -1,11 +1,18 @@ module Fog module Compute class Ecloud - class Real basic_request :get_layouts end + class Mock + def get_layouts(uri) + environment_id = id_from_uri(uri) + layout = self.data[:layouts][environment_id] + + response(:body => layout) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_network.rb b/lib/fog/ecloud/requests/compute/get_network.rb index ccbc78dba..5eb94ee0a 100644 --- a/lib/fog/ecloud/requests/compute/get_network.rb +++ b/lib/fog/ecloud/requests/compute/get_network.rb @@ -6,6 +6,17 @@ module Fog basic_request :get_network end + class Mock + def get_network(uri) + network_id = id_from_uri(uri) + network = self.data[:networks][network_id].dup + + if network + response(:body => Fog::Ecloud.slice(network, :id, :environment_id)) + else response(:status => 404) # ? + end + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_networks.rb b/lib/fog/ecloud/requests/compute/get_networks.rb index e5199ae54..810371e1d 100644 --- a/lib/fog/ecloud/requests/compute/get_networks.rb +++ b/lib/fog/ecloud/requests/compute/get_networks.rb @@ -6,6 +6,26 @@ module Fog basic_request :get_networks end + class Mock + def get_networks(uri) + environment_id = id_from_uri(uri) + environment = self.data[:environments][environment_id] + + networks = self.data[:networks].values.select{|n| n[:environment_id] == environment_id}.dup + networks = networks.map{|n| Fog::Ecloud.slice(n, :environment, :id)} + + body = { + :href => uri, + :type => "application/vnd.tmrk.cloud.network; type=collection", + :Links => { + :Link => Fog::Ecloud.keep(environment, :name, :href, :type) + }, + :Network => (networks.size > 1 ? networks : networks.first), + } + + response(:body => body) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_node.rb b/lib/fog/ecloud/requests/compute/get_node.rb index ee087c267..b1bb78226 100644 --- a/lib/fog/ecloud/requests/compute/get_node.rb +++ b/lib/fog/ecloud/requests/compute/get_node.rb @@ -6,6 +6,17 @@ module Fog basic_request :get_node end + class Mock + def get_node(uri) + node_service_id = id_from_uri(uri) + node_service = self.data[:node_services][node_service_id.to_i] + + if node_service + response(:body => Fog::Ecloud.slice(node_service, :id, :internet_service_id)) + else response(:status => 404) # ? + end + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_nodes.rb b/lib/fog/ecloud/requests/compute/get_nodes.rb index 2010e8f3d..1f7db7c82 100644 --- a/lib/fog/ecloud/requests/compute/get_nodes.rb +++ b/lib/fog/ecloud/requests/compute/get_nodes.rb @@ -6,6 +6,15 @@ module Fog basic_request :get_nodes end + class Mock + def get_nodes(uri) + + internet_service_id = id_from_uri(uri) + internet_service = self.data[:internet_services][internet_service_id] + + response(:body => internet_service) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_operating_system.rb b/lib/fog/ecloud/requests/compute/get_operating_system.rb index 054e24794..9f82d6ccc 100644 --- a/lib/fog/ecloud/requests/compute/get_operating_system.rb +++ b/lib/fog/ecloud/requests/compute/get_operating_system.rb @@ -6,6 +6,20 @@ module Fog basic_request :get_operating_system end + class Mock + def get_operating_system(uri) + os_name, compute_pool_id = uri.match(/operatingsystems\/(.*)\/computepools\/(\d+)$/).captures + compute_pool_id = compute_pool_id.to_i + + operating_systems = self.data[:operating_systems].values.select{|os| os[:compute_pool_id] == compute_pool_id} + operating_system = operating_systems.find{|os| os[:short_name] == os_name} + + if operating_system + response(:body => Fog::Ecloud.slice(operating_system, :id, :compute_pool_id, :short_name)) + else response(:status => 404) # ? + end + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_operating_system_families.rb b/lib/fog/ecloud/requests/compute/get_operating_system_families.rb index 1acb3f9e6..ddb9878f9 100644 --- a/lib/fog/ecloud/requests/compute/get_operating_system_families.rb +++ b/lib/fog/ecloud/requests/compute/get_operating_system_families.rb @@ -6,6 +6,26 @@ module Fog basic_request :get_operating_system_families end + class Mock + def get_operating_system_families(uri) + compute_pool_id = id_from_uri(uri) + compute_pool = self.data[:compute_pools][compute_pool_id] + + operating_system_families = self.data[:operating_system_families].values.select{|osf| osf[:compute_pool_id] == compute_pool_id} + operating_system_families = operating_system_families.map{|osf| Fog::Ecloud.slice(osf, :id, :compute_pool_id)}.map{|osf| osf[:OperatingSystemFamily]} + + operating_system_family_response = {:OperatingSystemFamily => (operating_system_families.size > 1 ? operating_system_families : operating_system_families.first)} # GAH + body = { + :href => uri, + :type => "application/vnd.tmrk.cloud.operatingSystemFamily; type=collection", + :Links => { + :Link => compute_pool, + } + }.merge(operating_system_family_response) + + response(:body => body) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_operating_system_family.rb b/lib/fog/ecloud/requests/compute/get_operating_system_family.rb deleted file mode 100644 index 054e24794..000000000 --- a/lib/fog/ecloud/requests/compute/get_operating_system_family.rb +++ /dev/null @@ -1,11 +0,0 @@ -module Fog - module Compute - class Ecloud - - class Real - basic_request :get_operating_system - end - - end - end -end diff --git a/lib/fog/ecloud/requests/compute/get_organization.rb b/lib/fog/ecloud/requests/compute/get_organization.rb index 95375dccd..5fba627a5 100644 --- a/lib/fog/ecloud/requests/compute/get_organization.rb +++ b/lib/fog/ecloud/requests/compute/get_organization.rb @@ -6,6 +6,20 @@ module Fog basic_request :get_organization end + class Mock + def get_organization(uri) + organization_id = id_from_uri(uri) + organization = self.data[:organizations][organization_id] + + body = { + :xmlns_i => "http://www.w3.org/2001/XMLSchema-instance", + :href => "/cloudapi/ecloud/organizations/", + :type => "application/vnd.tmrk.cloud.organization; type=collection" + }.merge(organization) + + response(:body => body) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_organizations.rb b/lib/fog/ecloud/requests/compute/get_organizations.rb index 841a45531..12fb90cd4 100644 --- a/lib/fog/ecloud/requests/compute/get_organizations.rb +++ b/lib/fog/ecloud/requests/compute/get_organizations.rb @@ -1,11 +1,23 @@ module Fog module Compute class Ecloud - class Real basic_request :get_organizations - end + end # Real + class Mock + def get_organizations(uri) + organizations = self.data[:organizations].values.dup + organizations.each{|org| org.delete(:id)} + body = { + :xmlns_i => "http://www.w3.org/2001/XMLSchema-instance", + :href => "/cloudapi/ecloud/organizations/", + :type => "application/vnd.tmrk.cloud.organization; type=collection" + }.merge(:Organization => (organizations.size > 1 ? organizations : organizations.first)) + + response(:body => body) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_public_ip.rb b/lib/fog/ecloud/requests/compute/get_public_ip.rb index 219b724b5..6699bb9d2 100644 --- a/lib/fog/ecloud/requests/compute/get_public_ip.rb +++ b/lib/fog/ecloud/requests/compute/get_public_ip.rb @@ -1,11 +1,21 @@ module Fog module Compute class Ecloud - class Real basic_request :get_public_ip end + class Mock + def get_public_ip(uri) + public_ip_id = id_from_uri(uri) + public_ip = self.data[:public_ips][public_ip_id] + + if public_ip + response(:body => Fog::Ecloud.slice(public_ip, :id, :environment_id)) + else response(:status => 404) # ? + end + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_public_ips.rb b/lib/fog/ecloud/requests/compute/get_public_ips.rb index 3260fc923..bff594082 100644 --- a/lib/fog/ecloud/requests/compute/get_public_ips.rb +++ b/lib/fog/ecloud/requests/compute/get_public_ips.rb @@ -6,6 +6,28 @@ module Fog basic_request :get_public_ips end + class Mock + def get_public_ips(uri) + + environment_id = id_from_uri(uri) + environment = self.data[:environments][environment_id] + + public_ips = self.data[:public_ips].values.select{|cp| cp[:environment_id] == environment_id} + + public_ips = public_ips.map{|pi| Fog::Ecloud.slice(pi, :id, :environment_id)} + + public_ip_response = {:PublicIp => (public_ips.size > 1 ? public_ips : public_ips.first)} # GAH + body = { + :href => uri, + :type => "application/vnd.tmrk.cloud.publicIp; type=collection", + :Links => { + :Link => environment, + } + }.merge(public_ip_response) + + response(:body => body) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_row.rb b/lib/fog/ecloud/requests/compute/get_row.rb index 69c32da35..ca7571c49 100644 --- a/lib/fog/ecloud/requests/compute/get_row.rb +++ b/lib/fog/ecloud/requests/compute/get_row.rb @@ -1,11 +1,18 @@ module Fog module Compute class Ecloud - class Real basic_request :get_row end + class Mock + def get_row(uri) + row_id = id_from_uri(uri) + row = self.data[:rows][row_id] + + response(:body => row) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_rows.rb b/lib/fog/ecloud/requests/compute/get_rows.rb index 97b9e25fa..3cb6168e5 100644 --- a/lib/fog/ecloud/requests/compute/get_rows.rb +++ b/lib/fog/ecloud/requests/compute/get_rows.rb @@ -1,11 +1,12 @@ module Fog module Compute class Ecloud - class Real basic_request :get_rows end + class Mock + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_server.rb b/lib/fog/ecloud/requests/compute/get_server.rb index f606bb0e8..22e4d3de6 100644 --- a/lib/fog/ecloud/requests/compute/get_server.rb +++ b/lib/fog/ecloud/requests/compute/get_server.rb @@ -6,6 +6,17 @@ module Fog basic_request :get_server end + class Mock + def get_server(uri) + server_id = uri.match(/(\d+)/) + server_id = server_id.nil? ? nil : server_id[1].to_i + server = self.data[:servers][server_id] + if server + response(:body => Fog::Ecloud.slice(server, :id, :compute_pool_id)) + else response(:status => 404) # ? + end + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_servers.rb b/lib/fog/ecloud/requests/compute/get_servers.rb index 9372ec9d6..67e044f1e 100644 --- a/lib/fog/ecloud/requests/compute/get_servers.rb +++ b/lib/fog/ecloud/requests/compute/get_servers.rb @@ -1,11 +1,42 @@ module Fog module Compute class Ecloud - class Real basic_request :get_servers end + class Mock + def get_servers(uri) + if uri =~ /layoutgroups/i + group_id = id_from_uri(uri) + group = self.data[:groups][group_id] + servers = group[:VirtualMachines][:VirtualMachine] + compute_pool_id = servers.first[:compute_pool_id] unless servers.empty? + compute_pool = self.data[:compute_pools][compute_pool_id] unless compute_pool_id.nil? + elsif uri =~ /computepool/i + compute_pool_id = id_from_uri(uri) + compute_pool = self.data[:compute_pools][compute_pool_id] + servers = self.data[:servers].values.select{|cp| cp[:compute_pool_id] == compute_pool_id} + servers = servers.map{|server| Fog::Ecloud.slice(server, :id, :compute_pool_id)} + end + links = if compute_pool.nil? + [] + else + [Fog::Ecloud.keep(compute_pool, :name, :href, :type),] + end + + server_response = {:VirtualMachine => (servers.size > 1 ? servers : servers.first)} # GAH + body = { + :href => uri, + :type => "application/vnd.tmrk.cloud.virtualMachine; type=collection", + :Links => { + :Link => links + } + }.merge(server_response) + + response(:body => body) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_ssh_key.rb b/lib/fog/ecloud/requests/compute/get_ssh_key.rb index e7df26b58..7543c983a 100644 --- a/lib/fog/ecloud/requests/compute/get_ssh_key.rb +++ b/lib/fog/ecloud/requests/compute/get_ssh_key.rb @@ -6,6 +6,17 @@ module Fog basic_request :get_ssh_key end + class Mock + def get_ssh_key(uri) + ssh_key_id = id_from_uri(uri).to_i + ssh_key = self.data[:ssh_keys][ssh_key_id.to_i] + + if ssh_key + response(:body => Fog::Ecloud.slice(ssh_key, :id, :admin_organization)) + else response(:status => 404) # ? + end + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_ssh_keys.rb b/lib/fog/ecloud/requests/compute/get_ssh_keys.rb index 8b919e3b4..2c61114fc 100644 --- a/lib/fog/ecloud/requests/compute/get_ssh_keys.rb +++ b/lib/fog/ecloud/requests/compute/get_ssh_keys.rb @@ -6,6 +6,26 @@ module Fog basic_request :get_ssh_keys end + class Mock + def get_ssh_keys(uri) + organization_id = id_from_uri(uri) + organization = self.data[:organizations][organization_id] + + ssh_keys = self.data[:ssh_keys].values.select{|key| key[:admin_organization_id] == organization_id} + ssh_keys = ssh_keys.map{|key| Fog::Ecloud.slice(key, :id, :admin_organization)} + + ssh_key_response = {:SshKey => (ssh_keys.size > 1 ? ssh_keys : ssh_keys.first)} # GAH + body = { + :href => "/cloudapi/ecloud/admin/organizations/#{organization_id}/sshKeys", + :type => "application/vnd.tmrk.cloud.sshKey; type=collection", + :Links => { + :Link => organization, + }, + }.merge(ssh_key_response) + + response(:body => body) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_task.rb b/lib/fog/ecloud/requests/compute/get_task.rb index 4e70b4dac..8c998164c 100644 --- a/lib/fog/ecloud/requests/compute/get_task.rb +++ b/lib/fog/ecloud/requests/compute/get_task.rb @@ -1,11 +1,13 @@ module Fog module Compute class Ecloud - class Real basic_request :get_task end - + class Mock + def get_task(uri) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_template.rb b/lib/fog/ecloud/requests/compute/get_template.rb index 77430a5f6..89ef02464 100644 --- a/lib/fog/ecloud/requests/compute/get_template.rb +++ b/lib/fog/ecloud/requests/compute/get_template.rb @@ -6,6 +6,17 @@ module Fog basic_request :get_template end + class Mock + def get_template(uri) + template_id, compute_pool_id = uri.match(/(\d+).*\/(\d+)$/).captures + template = self.data[:templates][template_id.to_i] + + if template + response(:body => Fog::Ecloud.slice(template, :id, :environment)) + else response(:status => 404) # ? + end + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_templates.rb b/lib/fog/ecloud/requests/compute/get_templates.rb index 189ece588..ef44b3cfe 100644 --- a/lib/fog/ecloud/requests/compute/get_templates.rb +++ b/lib/fog/ecloud/requests/compute/get_templates.rb @@ -6,6 +6,44 @@ module Fog basic_request :get_templates end + class Mock + def get_templates(uri) # /cloudapi/ecloud/computepools/compute_pools/534 + compute_pool_id = id_from_uri(uri) + compute_pool = self.data[:compute_pools][compute_pool_id] + + templates = self.data[:templates].values.select{|template| template[:compute_pool_id] == compute_pool_id} + templates = templates.map{|template| Fog::Ecloud.slice(template, :id, :compute_pool)} + + template_response = {:Template => (templates.size > 1 ? templates : templates.first)} # GAH + body = { + :href => uri, + :type => "application/vnd.tmrk.cloud.template; type=collection", + :Links => { + :Link => compute_pool, + }, + :Families => { + :Family => { + :Name => "Standard Templates", + :Categories => { + :Category => [ + { + :Name => "OS Only", + :OperatingSystems => { + :OperatingSystem => { + :Name => "Linux", + :Templates => template_response, + } + } + } + ] + } + } + } + } + + response(:body => body) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/get_virtual_machine_assigned_ips.rb b/lib/fog/ecloud/requests/compute/get_virtual_machine_assigned_ips.rb new file mode 100644 index 000000000..9b16e3c28 --- /dev/null +++ b/lib/fog/ecloud/requests/compute/get_virtual_machine_assigned_ips.rb @@ -0,0 +1,44 @@ +module Fog + module Compute + class Ecloud + + class Real + def get_virtual_machine_assigned_ips(virtual_machine_id) + request( + :uri => "/cloudapi/ecloud/virtualmachines/#{virtual_machine_id}/assignedips", + :parse => true + ) + end + end + + class Mock + def get_virtual_machine_assigned_ips(virtual_machine_id) + server = self.data[:servers][virtual_machine_id.to_i] + compute_pool = self.data[:compute_pools][server[:compute_pool_id]] + environment_id = compute_pool[:environment_id] + environment = self.data[:environments][environment_id] + + networks = self.data[:networks].values.select{|n| n[:environment_id] == environment_id} + networks = networks.map{|n| deep_copy(Fog::Ecloud.slice(n, :environment, :id))} + + + networks.each do |network| + address = network[:IpAddresses][:IpAddress].map{|ia| ia[:name]} + network[:IpAddresses][:IpAddress] = address.first + end + + body = { + :href => "/cloudapi/ecloud/virtualMachines/#{virtual_machine_id}/assignedIps", + :type => "application/vnd.tmrk.cloud.network", + :Links => { + :Link => Fog::Ecloud.keep(environment, :name, :href, :type) + }, + :Networks => {:Network => (networks.size > 1 ? networks : networks.first)}, + } + + response(:body => body) + end + end + end + end +end diff --git a/lib/fog/ecloud/requests/compute/groups_delete.rb b/lib/fog/ecloud/requests/compute/groups_delete.rb index e7bf555fe..c5611b64a 100644 --- a/lib/fog/ecloud/requests/compute/groups_delete.rb +++ b/lib/fog/ecloud/requests/compute/groups_delete.rb @@ -1,11 +1,26 @@ module Fog module Compute class Ecloud - class Real basic_request :groups_delete, 204, 'DELETE' end + class Mock + def groups_delete(uri) + group_id = id_from_uri(uri) + self.data[:groups].delete(group_id) + self.data[:rows].values.each do |row| + row[:Groups][:Group].delete_if { |g| g[:id] == group_id } + end + self.data[:layouts].values.each do |layout| + layout[:Rows][:Row].each do |row| + row[:Groups][:Group].delete_if { |g| g[:id] == group_id } + end + end + + response(:body => nil, :status => 204) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/internet_service_create.rb b/lib/fog/ecloud/requests/compute/internet_service_create.rb index d342c0ad5..9fccd41a9 100644 --- a/lib/fog/ecloud/requests/compute/internet_service_create.rb +++ b/lib/fog/ecloud/requests/compute/internet_service_create.rb @@ -62,6 +62,41 @@ module Fog end end end + + class Mock + def internet_service_create(service_data) + validate_internet_service_data(service_data) + public_ip_id = service_data[:uri].match(/(\d+)/)[1] + public_ip = self.data[:public_ips][public_ip_id.to_i].dup + service_id = Fog::Mock.random_numbers(6).to_i + service = { + :href => "/cloudapi/ecloud/internetServices/#{service_id}", + :name => service_data[:name], + :type => "application/vnd.tmrk.cloud.internetService", + :Links => { + :Link => [ + Fog::Ecloud.keep(public_ip, :href, :name, :type), + ], + }, + :Protocol => service_data[:protocol], + :Port => service_data[:port], + :Enabled => service_data[:enabled], + :Description => service_data[:description], + :PublicIp => Fog::Ecloud.keep(public_ip, :href, :name, :type), + :Persistence => { + :Type => service_data[:persistence][:type], + }, + } + + internet_service_response = response(:body => service) + + service.merge!(:public_ip => public_ip) + + self.data[:internet_services][service_id] = service + + internet_service_response + end + end end end -end +end diff --git a/lib/fog/ecloud/requests/compute/internet_service_delete.rb b/lib/fog/ecloud/requests/compute/internet_service_delete.rb index 5a72188e2..51410378e 100644 --- a/lib/fog/ecloud/requests/compute/internet_service_delete.rb +++ b/lib/fog/ecloud/requests/compute/internet_service_delete.rb @@ -6,6 +6,29 @@ module Fog basic_request :internet_service_delete, 202, 'DELETE' end + class Mock + def internet_service_delete(uri) + service_id = id_from_uri(uri) + + service = self.data[:internet_services][service_id].dup + self.data[:internet_services].delete(service_id) + + task_id = Fog::Mock.random_numbers(10).to_i + task = { + :id => task_id, + :href => "/cloudapi/ecloud/tasks/#{task_id}", + :type => "application/vnd.tmrk.cloud.task", + :Operation => "Delete Service", + :Status => "Complete", + :ImpactedItem => Fog::Ecloud.keep(service, :name, :href, :type), + :StartTime => Time.now.iso8601, + :CompletedTime => Time.now.iso8601, + :InitiatedBy => {}, + } + self.data[:tasks][task_id] = task + response(:body => task) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/node_service_create.rb b/lib/fog/ecloud/requests/compute/node_service_create.rb index 4547bce80..9adabde47 100644 --- a/lib/fog/ecloud/requests/compute/node_service_create.rb +++ b/lib/fog/ecloud/requests/compute/node_service_create.rb @@ -40,6 +40,54 @@ module Fog end end end + + class Mock + def node_service_create(service_data) + validate_node_service_data(service_data) + + internet_service_id = service_data[:uri].match(/(\d+)/)[1] + internet_service = self.data[:internet_services][internet_service_id.to_i].dup + network_id, ip_address_name = service_data[:ip_address].match(/\/(\d+)\/(.*)$/).captures + network = self.data[:networks][network_id.to_i] + ip_addresses = network[:IpAddresses][:IpAddress] + ip_addresses = ip_addresses.is_a?(Array) ? ip_addresses : [ip_addresses] + ip_address = ip_addresses.detect { |ip| ip[:name] == ip_address_name } + + service_id = Fog::Mock.random_numbers(6).to_i + service = { + :href => "/cloudapi/ecloud/nodeservices/#{service_id}", + :name => service_data[:name], + :type => "application/vnd.tmrk.cloud.nodeService", + :Links => { + :Link => [ + Fog::Ecloud.keep(internet_service, :href, :name, :type), + ], + }, + :Protocol => service_data[:protocol], + :Port => service_data[:port], + :Enabled => service_data[:enabled], + :Description => service_data[:description], + :IpAddress => { + :href => ip_address[:href], + :name => ip_address[:name], + :type => ip_address[:type], + :Network => { + :href => network[:href], + :name => network[:name], + :type => network[:type], + }, + }, + } + + node_service_response = response(:body => service) + + service.merge!(:internet_service => internet_service) + + self.data[:node_services][service_id] = service + + node_service_response + end + end end end -end +end diff --git a/lib/fog/ecloud/requests/compute/node_service_delete.rb b/lib/fog/ecloud/requests/compute/node_service_delete.rb index 651ad60e1..364a5eeaf 100644 --- a/lib/fog/ecloud/requests/compute/node_service_delete.rb +++ b/lib/fog/ecloud/requests/compute/node_service_delete.rb @@ -6,6 +6,30 @@ module Fog basic_request :node_service_delete, 202, 'DELETE' end + class Mock + def node_service_delete(uri) + + service_id = id_from_uri(uri) + + service = self.data[:node_services][service_id].dup + self.data[:node_services].delete(service_id) + + task_id = Fog::Mock.random_numbers(10).to_i + task = { + :id => task_id, + :href => "/cloudapi/ecloud/tasks/#{task_id}", + :type => "application/vnd.tmrk.cloud.task", + :Operation => "Delete Node Service", + :Status => "Complete", + :ImpactedItem => Fog::Ecloud.keep(service, :name, :href, :type), + :StartTime => Time.now.iso8601, + :CompletedTime => Time.now.iso8601, + :InitiatedBy => {}, + } + self.data[:tasks][task_id] = task + response(:body => task) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/rows_delete.rb b/lib/fog/ecloud/requests/compute/rows_delete.rb index 8b0e8e158..3bef1c47e 100644 --- a/lib/fog/ecloud/requests/compute/rows_delete.rb +++ b/lib/fog/ecloud/requests/compute/rows_delete.rb @@ -1,11 +1,21 @@ module Fog module Compute class Ecloud - class Real basic_request :rows_delete, 204, 'DELETE' end + class Mock + def rows_delete(uri) + row_id = id_from_uri(uri) + self.data[:rows].delete(row_id) + self.data[:layouts].values.each do |layout| + layout[:Rows][:Row].delete_if { |r| r[:id] == row_id } + end + + response(:body => nil, :status => 204) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/virtual_machine_add_ip.rb b/lib/fog/ecloud/requests/compute/virtual_machine_add_ip.rb deleted file mode 100644 index 52783b096..000000000 --- a/lib/fog/ecloud/requests/compute/virtual_machine_add_ip.rb +++ /dev/null @@ -1,40 +0,0 @@ -module Fog - module Compute - class Ecloud - module Shared - - def build_request_body_add_ip(networks) - xml = Builder::XmlMarkup.new - xml.AssignedIpAddresses do - xml.Networks do - networks.each do |network| - xml.Network(:href => network[:href], :type => network[:type]) do - xml.IpAddresses do - network[:ips].each do |ip| - xml.IpAddress ip - end - end - end - end - end - end - end - end - - class Real - - def virtual_machine_add_ip(href, options) - body = build_request_body_add_ip(options) - request( - :expects => 202, - :method => 'PUT', - :headers => {}, - :body => body, - :uri => href, - :parse => true - ) - end - end - end - end -end diff --git a/lib/fog/ecloud/requests/compute/virtual_machine_attach_disk.rb b/lib/fog/ecloud/requests/compute/virtual_machine_attach_disk.rb new file mode 100644 index 000000000..31886717f --- /dev/null +++ b/lib/fog/ecloud/requests/compute/virtual_machine_attach_disk.rb @@ -0,0 +1,62 @@ +module Fog + module Compute + class Ecloud + module Shared + + def build_request_body_attach_disk(options) + xml = Builder::XmlMarkup.new + xml.AttachDisks(:name => options[:name]) do + xml.DetachedDisks do + xml.DetachedDisk(:href => options[:href], :name => options[:name], :type => "application/vnd.tmrk.cloud.detachedDisk") + end + end + end + end + + class Real + def virtual_machine_attach_disk(href, options) + body = build_request_body_attach_disk(options) + request( + :expects => 202, + :method => 'POST', + :headers => {}, + :body => body, + :uri => href, + :parse => true + ) + end + end + + class Mock + def virtual_machine_attach_disk(href, options) + server_id = href.match(/(\d+)/)[1].to_i + server = self.data[:servers][server_id] + compute_pool_id = server[:compute_pool_id] + compute_pool = self.data[:compute_pools][compute_pool_id] + detached_disk_id = options[:href].match(/(\d+)/)[1].to_i + detached_disk = self.data[:detached_disks][detached_disk_id] + new_index = (server[:HardwareConfiguration][:Disks][:Disk].map { |h| h[:Index].to_i }.sort.last + 1).to_s + detached_disk[:Index] = new_index + server[:HardwareConfiguration][:Disks][:Disk] << Fog::Ecloud.keep(detached_disk, :Index, :Size, :Name) + + self.data[:detached_disks].delete(detached_disk_id) + + task_id = Fog::Mock.random_numbers(10).to_i + task = { + :id => task_id, + :href => "/cloudapi/ecloud/tasks/#{task_id}", + :type => "application/vnd.tmrk.cloud.task", + :Operation => "Attach Disk", + :Status => "Complete", + :ImpactedItem => Fog::Ecloud.keep(server, :href, :type), + :StartTime => Time.now.iso8601, + :CompletedTime => Time.now.iso8601, + :InitiatedBy => {}, + } + self.data[:tasks][task_id] = task + response(:body => task) + end + end + end + end +end diff --git a/lib/fog/ecloud/requests/compute/virtual_machine_create_from_template.rb b/lib/fog/ecloud/requests/compute/virtual_machine_create_from_template.rb index 6d87c6f09..db60f8f3f 100644 --- a/lib/fog/ecloud/requests/compute/virtual_machine_create_from_template.rb +++ b/lib/fog/ecloud/requests/compute/virtual_machine_create_from_template.rb @@ -14,12 +14,6 @@ module Fog raise ArgumentError.new("Required data missing: #{(required_opts - options.keys).map(&:inspect).join(", ")}") end - if template_uri.scan(/\/catalog\/\d+/)[0] - options[:template_type] = get_catalog_item(template_uri).body[:type] - elsif template_uri.scan(/\/templates\/\d+/)[0] - options[:template_type] = get_template(template_uri).body[:type] - end - options[:network_uri] = options[:network_uri].is_a?(String) ? [options[:network_uri]] : options[:network_uri] options[:network_uri].map! do |uri| network = get_network(uri).body @@ -27,7 +21,7 @@ module Fog ip = options[:ips][options[:network_uri].index(uri)] end {:href => uri, :name => network[:name], :ip => ip} - end + end options[:template_uri] = template_uri options end @@ -82,7 +76,7 @@ module Fog options[:network_uri] = options[:network_uri].is_a?(String) ? [options[:network_uri]] : options[:network_uri] options[:network_uri].each do |uri| xml.NetworkAdapter do - xml.Network(:href => uri[:href], :name => uri[:name], :type => "application/vnd.tmrk.cloud.network") + xml.Network(:href => uri[:href], :name => uri[:name], :type => "application/vnd.tmrk.cloud.network") xml.IpAddress uri[:ip] end end @@ -95,31 +89,157 @@ module Fog end end end - end + end end xml.SshKey(:href => options[:ssh_key_uri]) end xml.PoweredOn options[:powered_on] - xml.Template(:href => options[:template_uri], :type => options[:template_type]) + xml.Template(:href => options[:template_uri], :type => options[:template_type]) end end end class Real - def virtual_machine_create_from_template(template_uri, options) options = validate_create_server_options(template_uri, options) request( :expects => 201, - :method => 'POST', - :headers => {}, - :body => build_request_body(options), - :uri => options[:uri], - :parse => true + :method => 'POST', + :body => build_request_body(options), + :uri => options[:uri], + :parse => true ) end end + + class Mock + def virtual_machine_create_from_template(template_uri, options) + options = validate_create_server_options(template_uri, options) + server_id = Fog::Mock.random_numbers(7).to_i + row_id = Fog::Mock.random_numbers(6).to_i + group_id = Fog::Mock.random_numbers(6).to_i + template_id, compute_pool_id = template_uri.match(/\/templates\/(\d+)\/computepools\/(\d+)$/).captures + compute_pool = self.data[:compute_pools][compute_pool_id.to_i].dup + environment = self.data[:environments][compute_pool[:environment_id]] + layout = self.data[:layouts][environment[:id]] + networks = options[:network_uri] + nics = networks.each_with_index.map do |network, i| + { + :UnitNumber => i.to_s, + :Name => "Network adapter #{i}", + :MacAddress => Fog::Ecloud.mac_address, + :Network => Fog::Ecloud.keep(network, :name, :href, :type), + } + end + + links = [Fog::Ecloud.keep(compute_pool, :name, :href, :type), Fog::Ecloud.keep(environment, :name, :href, :type)] + networks.each do |network| + links << Fog::Ecloud.keep(network, :name, :href, :type) + network_id = id_from_uri(network[:href]) + ip = self.data[:networks][network_id][:IpAddresses][:IpAddress].detect { |ip| ip[:id] = network[:ip] } + ip[:DetectedOn] = {:href => "/cloudapi/ecloud/networkhosts/#{server_id}", :name => options[:name], :type => "application/vnd.tmrk.cloud.networkHost"} + ip[:Host] = {:href => "/cloudapi/ecloud/networkhosts/#{server_id}", :name => options[:name], :type => "application/vnd.tmrk.cloud.networkHost"} + end + + server = { + :href => "/cloudapi/ecloud/virtualmachines/#{server_id}", + :name => options[:name], + :type => "application/vnd.tmrk.cloud.virtualMachine", + :Description => options[:description], + :Status => "Deployed", + :HardwareConfiguration => { + :href => "/cloudapi/ecloud/virtualmachines/#{server_id}/hardwareconfiguration", + :type => "application/vnd.tmrk.cloud.virtualMachineHardware", + :Links => { + :Link => { + :href => "/cloudapi/ecloud/virtualmachines/#{server_id}", + :name => options[:name], + :type => "application/vnd.tmrk.cloud.virtualMachine", + :rel => "up", + } + }, + :ProcessorCount => options[:cpus], + :Memory => { + :Unit => "MB", + :Value => options[:memory], + }, + :Disks => { + :Disk => [{ + :Index => "0", + :Name => "Hard Disk 1", + :Size => { + :Unit => "GB", + :Value => "25", + }, + }], + }, + :Nics => { + :Nic => nics, + }, + }, + :IpAddresses => { + :AssignedIpAddresses => { + :Networks => { + :Network => self.data[:networks].dup.values, + } + } + }, + :Links => { :Link => links }, + } + + row = { + :id => row_id, + :name => options[:row], + :href => "/cloudapi/ecloud/layoutrows/#{row_id}", + :type => "application/vnd.tmrk.cloud.layoutRow", + :Links => { + :Link => [ + Fog::Ecloud.keep(environment, :name, :href, :type) + ], + }, + :Index => 0, + :Groups => { + :Group => [ + ], + }, + :environment_id => environment[:id], + } + + group = { + :id => group_id, + :name => options[:group], + :href => "/cloudapi/ecloud/layoutgroups/#{group_id}", + :type => "application/vnd.tmrk.cloud.layoutGroup", + :Links => { + :Link => [ + Fog::Ecloud.keep(row, :name, :href, :type), + ], + }, + :Index => 0, + :VirtualMachines => { + :VirtualMachine => [ + server, + ], + }, + :row_id => row_id, + } + row[:Groups][:Group].push(group) + layout[:Rows][:Row].push(row) + + server.merge!(:OperatingSystem => options[:operating_system].merge(:type => "application/vnd.tmrk.cloud.operatingSystem")) if options[:operating_system] + + server_response = response(:body => server) + + server.merge!(:compute_pool_id => compute_pool[:id]) + + self.data[:servers][server_id] = server + self.data[:rows][row_id] = row + self.data[:groups][group_id] = group + + server_response + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/virtual_machine_delete.rb b/lib/fog/ecloud/requests/compute/virtual_machine_delete.rb index 70365e01e..a93887a95 100644 --- a/lib/fog/ecloud/requests/compute/virtual_machine_delete.rb +++ b/lib/fog/ecloud/requests/compute/virtual_machine_delete.rb @@ -1,11 +1,46 @@ module Fog module Compute class Ecloud - class Real basic_request :virtual_machine_delete, 202, 'DELETE' end + class Mock + def virtual_machine_delete(uri) + server_id = id_from_uri(uri) + + server = self.data[:servers][server_id] + self.data[:servers].delete(server_id) + self.data[:groups].values.each do |group| + group[:VirtualMachines][:VirtualMachine].delete_if { |s| s[:name] == server[:name] } + end + self.data[:networks].values.each do |network| + network[:IpAddresses][:IpAddress].each do |ip| + unless ip[:Host].nil? + ip[:Host] = nil if ip[:Host][:name] == server[:name] + end + unless ip[:DetectedOn].nil? + ip[:DetectedOn] = nil if ip[:DetectedOn][:name] == server[:name] + end + end + end + task_id = Fog::Mock.random_numbers(10) + task = { + :id => task_id, + :href => "/cloudapi/ecloud/tasks/#{task_id}", + :type => "application/vnd.tmrk.cloud.task", + :Operation => "Delete Server", + :Status => "Complete", + :ImpactedItem => Fog::Ecloud.keep(server, :name, :href, :type), + :StartTime => Time.now.iso8601, + :CompletedTime => Time.now.iso8601, + :InitiatedBy => {}, + } + self.data[:tasks][task_id] = task + + response(:body => task) + end + end end end end diff --git a/lib/fog/ecloud/requests/compute/virtual_machine_detach_disk.rb b/lib/fog/ecloud/requests/compute/virtual_machine_detach_disk.rb new file mode 100644 index 000000000..df6d62d7b --- /dev/null +++ b/lib/fog/ecloud/requests/compute/virtual_machine_detach_disk.rb @@ -0,0 +1,71 @@ +module Fog + module Compute + class Ecloud + module Shared + + def build_request_body_detach_disk(options) + xml = Builder::XmlMarkup.new + xml.DetachDisk(:name => options[:name]) do + xml.Description options[:description] + xml.Disk do + xml.Index options[:disk][:Index] + end + end + end + end + + class Real + def virtual_machine_detach_disk(href, options) + body = build_request_body_detach_disk(options) + request( + :expects => 201, + :method => 'POST', + :headers => {}, + :body => body, + :uri => href, + :parse => true + ) + end + end + + class Mock + def virtual_machine_detach_disk(href, options) + server_id = href.match(/(\d+)/)[1].to_i + server = self.data[:servers][server_id] + compute_pool_id = server[:compute_pool_id] + compute_pool = self.data[:compute_pools][compute_pool_id] + detached_disk_id = Fog::Mock.random_numbers(6).to_i + detached_disk = { + :id => detached_disk_id, + :href => "/cloudapi/ecloud/detacheddisks/#{detached_disk_id}", + :name => options[:name], + :type => "application/vnd.tmrk.cloud.detachedDisk", + :Links => { + :Link => [ + Fog::Ecloud.keep(compute_pool, :href, :name, :type), + ], + }, + :Description => options[:description], + :LastKnownVirtualMachineConfiguration => Fog::Ecloud.keep(server, :name, :ProcessorCount, :Memory, :OperatingSystem), + :Type => "Data", + :Size => { + :Unit => "GB", + :Value => options[:disk][:Size][:Value], + }, + :Status => "Available", + } + + server[:HardwareConfiguration][:Disks][:Disk].delete_if { |disk| disk[:Index] == options[:disk][:Index] } + + detached_disk_response = response(:body => detached_disk) + + detached_disk.merge!(:compute_pool_id => compute_pool_id) + + self.data[:detached_disks][detached_disk_id] = detached_disk + + detached_disk_response + end + end + end + end +end diff --git a/lib/fog/ecloud/requests/compute/virtual_machine_edit_assigned_ips.rb b/lib/fog/ecloud/requests/compute/virtual_machine_edit_assigned_ips.rb new file mode 100644 index 000000000..510ae76f9 --- /dev/null +++ b/lib/fog/ecloud/requests/compute/virtual_machine_edit_assigned_ips.rb @@ -0,0 +1,82 @@ +module Fog + module Compute + class Ecloud + module Shared + + def build_request_body_edit_assigned_ips(networks) + xml = Builder::XmlMarkup.new + xml.AssignedIpAddresses do + xml.Networks do + networks.each do |network| + xml.Network(:href => network[:href], :type => network[:type]) do + xml.IpAddresses do + network[:ips].each do |ip| + xml.IpAddress ip + end + end + end + end + end + end + end + end + + class Real + + def virtual_machine_edit_assigned_ips(href, options) + body = build_request_body_edit_assigned_ips(options) + request( + :expects => 202, + :method => 'PUT', + :headers => {}, + :body => body, + :uri => href, + :parse => true + ) + end + end + + class Mock + def virtual_machine_edit_assigned_ips(href, options) + server_id = href.match(/(\d+)/)[1].to_i + server = self.data[:servers][server_id] + options.each do |network| + network_id = id_from_uri(network[:href]) + network = self.data[:networks][network_id] + options.each.each do |net| + net[:ips].each do |ip| + ip = network[:IpAddresses][:IpAddress].detect { |iph| iph[:name] == ip } + ip[:Host] = { + :href => "/clouapi/ecloud/networkhosts/#{server_id}", + :name => server[:name], + :type => "application/vnd.tmrk.cloud.networkHost" + } + ip[:DetectedOn] = { + :href => "/clouapi/ecloud/networkhosts/#{server_id}", + :name => server[:name], + :type => "application/vnd.tmrk.cloud.networkHost" + } + end + end + end + + task_id = Fog::Mock.random_numbers(10) + task = { + :id => task_id, + :href => "/cloudapi/ecloud/tasks/#{task_id}", + :type => "application/vnd.tmrk.cloud.task", + :Operation => "Delete Server", + :Status => "Complete", + :ImpactedItem => Fog::Ecloud.keep(server, :name, :href, :type), + :StartTime => Time.now.iso8601, + :CompletedTime => Time.now.iso8601, + :InitiatedBy => {}, + } + self.data[:tasks][task_id] = task + + response(:body => task) + end + end + end + end +end diff --git a/lib/fog/ecloud/requests/compute/virtual_machine_edit_hardware_configuration.rb b/lib/fog/ecloud/requests/compute/virtual_machine_edit_hardware_configuration.rb index 69fc487a8..0973ccdd4 100644 --- a/lib/fog/ecloud/requests/compute/virtual_machine_edit_hardware_configuration.rb +++ b/lib/fog/ecloud/requests/compute/virtual_machine_edit_hardware_configuration.rb @@ -21,8 +21,8 @@ module Fog xml.HardwareConfiguration do xml.ProcessorCount data[:cpus] xml.Memory do - xml.Unit data[:memory][:Unit] - xml.Value data[:memory][:Value] + xml.Unit "MB" + xml.Value data[:memory] end xml.Disks do data[:disks].each do |disk| @@ -45,7 +45,31 @@ module Fog end end end - end + end + end + end + + class Mock + def virtual_machine_edit_hardware_configuration(vm_uri, data) + + server_id = vm_uri.match(/(\d+)/)[1] + + server = self.data[:servers][server_id.to_i] + task_id = Fog::Mock.random_numbers(10) + task = { + :id => task_id, + :href => "/cloudapi/ecloud/tasks/#{task_id}", + :type => "application/vnd.tmrk.cloud.task", + :Operation => "Configure Server", + :Status => "Complete", + :ImpactedItem => Fog::Ecloud.keep(server, :name, :href, :type), + :StartTime => Time.now.iso8601, + :CompletedTime => Time.now.iso8601, + :InitiatedBy => {}, + } + self.data[:tasks][task_id] = task + + response(:body => task) end end end diff --git a/lib/fog/ecloud/requests/compute/virtual_machine_import.rb b/lib/fog/ecloud/requests/compute/virtual_machine_import.rb index b0a17bab1..aa2e44089 100644 --- a/lib/fog/ecloud/requests/compute/virtual_machine_import.rb +++ b/lib/fog/ecloud/requests/compute/virtual_machine_import.rb @@ -9,7 +9,7 @@ module Fog raise ArgumentError.new("Required data missing: #{(required_opts - options.keys).map(&:inspect).join(", ")}") end - options[:network_uri] = options[:network_uri].is_a?(String) ? [options[:network_uri]] : options[:network_uri] + options[:network_uri] = [*options[:network_uri]] options[:template_uri] = template_uri options end @@ -54,14 +54,128 @@ module Fog request( :expects => 201, - :method => 'POST', - :headers => {}, - :body => build_request_body_import(options), - :uri => options[:uri], - :parse => true + :method => 'POST', + :body => build_request_body_import(options), + :uri => options[:uri], + :parse => true ) end end + + class Mock + def virtual_machine_import(template_uri, options) + options = validate_import_server_options(template_uri, options) + + compute_pool_id = options[:uri].match(/computePools\/(\d+)/)[1].to_i + compute_pool = self.data[:compute_pools][compute_pool_id].dup + environment = self.data[:environments][compute_pool[:environment_id]] + networks = options[:network_uri].map{|nuri| self.data[:networks][id_from_uri(nuri)].dup} + server_id = Fog::Mock.random_numbers(6).to_i + row_id = Fog::Mock.random_numbers(6).to_i + group_id = Fog::Mock.random_numbers(6).to_i + nics = networks.each_with_index.map do |network, i| + { + :UnitNumber => i.to_s, + :Name => "Network adapter #{i}", + :MacAddress => Fog::Ecloud.mac_address, + :Network => Fog::Ecloud.keep(network, :name, :href, :type) + } + end + + links = [Fog::Ecloud.keep(compute_pool, :name, :href, :type), Fog::Ecloud.keep(environment, :name, :href, :type)] + networks.each{|network| links << Fog::Ecloud.keep(network, :name, :href, :type)} + server = { + :href => "/cloudapi/ecloud/virtualmachines/#{server_id}", + :name => options[:name], + :type => "application/vnd.tmrk.cloud.virtualMachine", + :Description => options[:description], + :Status => "Deployed", + :PoweredOn => "false", + :HardwareConfiguration => { + :href => "/cloudapi/ecloud/virtualmachines/#{server_id}/hardwareconfiguration", + :type => "application/vnd.tmrk.cloud.virtualMachineHardware", + :Links => { + :Link => { + :href => "/cloudapi/ecloud/virtualmachines/#{server_id}", + :name => options[:name], + :type => "application/vnd.tmrk.cloud.virtualMachine", + :rel => "up" + } + }, + :ProcessorCount => options[:cpus], + :Memory => { + :Unit => "MB", + :Value => options[:memory], + }, + :Disks => { # Default drive + :Disk => [{ + :Index => "0", + :Name => "Hard Disk 1", + :Size => { + :Unit => "GB", + :Value => "25" + }, + }], + }, + :Nics => { + :Nic => nics, + }, + }, + :Links => { :Link => links }, + } + + row = { + :id => row_id, + :name => options[:row], + :href => "/cloudapi/ecloud/layoutrows/#{row_id}", + :type => "application/vnd.tmrk.cloud.layoutRow", + :Links => { + :Link => [ + Fog::Ecloud.keep(environment, :name, :href, :type) + ], + }, + :Index => 0, + :Groups => { + :Group => [ + ], + }, + :environment_id => environment[:id], + } + + group = { + :id => group_id, + :name => options[:group], + :href => "/cloudapi/ecloud/layoutgroups/#{group_id}", + :type => "application/vnd.tmrk.cloud.layoutGroup", + :Links => { + :Link => [ + Fog::Ecloud.keep(row, :name, :href, :type), + ], + }, + :Index => 0, + :VirtualMachines => { + :VirtualMachine => [ + server, + ], + }, + :row_id => row_id, + } + row[:Groups][:Group].push(group) + layout[:Rows][:Row].push(row) + + server.merge!(:OperatingSystem => options[:operating_system].merge(:type => "application/vnd.tmrk.cloud.operatingSystem")) if options[:operating_system] + + server_response = response(:body => server) + + server.merge!(:compute_pool_id => compute_pool_id) + + self.data[:servers][server_id] = server + self.data[:rows][row_id] = row + self.data[:groups][group_id] = group + + server_response + end + end end end end diff --git a/tests/compute/helper.rb b/tests/compute/helper.rb index 13bfb56e2..b58735baa 100644 --- a/tests/compute/helper.rb +++ b/tests/compute/helper.rb @@ -19,13 +19,26 @@ def compute_providers }, :mocked => false }, - :openstack => { - :mocked => true, + :ecloud => { :server_attributes => { - :flavor_ref => 2, - :image_ref => "0e09fbd6-43c5-448a-83e9-0d3d05f9747e", - :name => "fog_#{Time.now.to_i}" - } + :name => "eugene", + :row => "eugene1", + :group => "eugene-104", + :catalog_network_name => "bridged", + :description => "blarg", + :operating_system => { + :name => "Red Hat Enterprise Linux 5 (64-bit)", + :href => "/cloudapi/ecloud/operatingsystems/rhel5_64guest/computepools/963", + }, + }.tap do |hash| + [:template_href, :network_uri, :environment_name].each do |k| + key = "ecloud_#{k}".to_sym + if Fog.credentials[key] + hash[k]= Fog.credentials[key] + end + end + end, + :mocked => true, }, :cloudstack => { :provider_attributes => { @@ -81,6 +94,14 @@ def compute_providers :ninefold => { :mocked => false }, + :openstack => { + :mocked => true, + :server_attributes => { + :flavor_ref => 2, + :image_ref => "0e09fbd6-43c5-448a-83e9-0d3d05f9747e", + :name => "fog_#{Time.now.to_i}" + } + }, :rackspace => { :provider_attributes => { :version => :v1 }, :server_attributes => { diff --git a/tests/compute/models/flavors_tests.rb b/tests/compute/models/flavors_tests.rb index a0702db37..beccc0946 100644 --- a/tests/compute/models/flavors_tests.rb +++ b/tests/compute/models/flavors_tests.rb @@ -1,6 +1,6 @@ for provider, config in compute_providers - next if [:glesys, :voxel, :ibm].include?(provider) + next if [:glesys, :voxel, :ibm, :ecloud].include?(provider) Shindo.tests("Fog::Compute[:#{provider}] | flavors", [provider.to_s]) do diff --git a/tests/compute/models/server_tests.rb b/tests/compute/models/server_tests.rb index 294b8269a..151feee93 100644 --- a/tests/compute/models/server_tests.rb +++ b/tests/compute/models/server_tests.rb @@ -1,5 +1,7 @@ for provider, config in compute_providers + next if [:ecloud].include?(provider) + Shindo.tests("Fog::Compute[:#{provider}] | server", [provider.to_s]) do provider_attributes = config[:provider_attributes] || {} diff --git a/tests/compute/models/servers_tests.rb b/tests/compute/models/servers_tests.rb index 4be329ca7..a48e1401f 100644 --- a/tests/compute/models/servers_tests.rb +++ b/tests/compute/models/servers_tests.rb @@ -1,6 +1,7 @@ - for provider, config in compute_providers + next if [:ecloud].include?(provider) + Shindo.tests("Fog::Compute[:#{provider}] | servers", [provider.to_s]) do provider_attributes = config[:provider_attributes] || {} diff --git a/tests/ecloud/compute/models/admin_organization_tests.rb b/tests/ecloud/compute/models/admin_organization_tests.rb new file mode 100644 index 000000000..2baf1f050 --- /dev/null +++ b/tests/ecloud/compute/models/admin_organization_tests.rb @@ -0,0 +1,16 @@ +provider, config = :ecloud, compute_providers[:ecloud] + +Shindo.tests("Fog::Compute[:#{provider}] | admin_organizations", [provider.to_s]) do + connection = Fog::Compute[provider] + @organization = connection.organizations.first + @admin_organization = @organization.admin + + tests('#get').succeeds do + fetched_admin_organization = connection.get_admin_organization(@admin_organization.href) + returns(true) { !fetched_admin_organization.nil? } + end + + tests('#ssh_keys').succeeds do + returns(true, "a list of SshKeys") { @admin_organization.ssh_keys.is_a?(Fog::Compute::Ecloud::SshKeys) } + end +end diff --git a/tests/ecloud/compute/models/compute_pool_tests.rb b/tests/ecloud/compute/models/compute_pool_tests.rb new file mode 100644 index 000000000..ea6b24c92 --- /dev/null +++ b/tests/ecloud/compute/models/compute_pool_tests.rb @@ -0,0 +1,17 @@ +provider, config = :ecloud, compute_providers[:ecloud] + +Shindo.tests("Fog::Compute[:#{provider}] | compute_pools", [provider.to_s]) do + connection = Fog::Compute[provider] + @organization = connection.organizations.first + @environment = @organization.environments.first + + tests('#all').succeeds do + returns(false) { @environment.compute_pools.all.empty? } + end + + tests('#get').succeeds do + compute_pool = @environment.compute_pools.all.first + fetched_compute_pool = connection.compute_pools.get(compute_pool.href) + returns(true) { !fetched_compute_pool.nil? } + end +end diff --git a/tests/ecloud/compute/models/detached_disk_tests.rb b/tests/ecloud/compute/models/detached_disk_tests.rb new file mode 100644 index 000000000..ae34a2d5a --- /dev/null +++ b/tests/ecloud/compute/models/detached_disk_tests.rb @@ -0,0 +1,26 @@ +provider, config = :ecloud, compute_providers[:ecloud] + +Shindo.tests("Fog::Compute[:#{provider}] | detached_disks", [provider.to_s]) do + connection = Fog::Compute[provider] + @organization = connection.organizations.first + @environment = @organization.environments.first + @compute_pool = @environment.compute_pools.first + @detached_disks = @compute_pool.detached_disks + + tests('#all').succeeds do + if @detached_disks.is_a?(Fog::Compute::Ecloud::DetachedDisks) && @detached_disks.reload.empty? + returns(true, "compute pool has no detached disks") { @detached_disks.all.empty? } + else + returns(false, "has detached disks") { @detached_disks.all.empty? } + end + end + + unless @detached_disks.empty? + tests('#get') do + disk = @detached_disks.first + fetched_disk = connection.detached_disks.get(disk.href) + returns(false, "disk is not nil") { fetched_disk.nil? } + returns(true, "is a DetachedDisk") { fetched_disk.is_a?(Fog::Compute::Ecloud::DetachedDisk) } + end + end +end diff --git a/tests/ecloud/compute/models/environment_tests.rb b/tests/ecloud/compute/models/environment_tests.rb new file mode 100644 index 000000000..1e59ec056 --- /dev/null +++ b/tests/ecloud/compute/models/environment_tests.rb @@ -0,0 +1,23 @@ +provider, config = :ecloud, compute_providers[:ecloud] + +Shindo.tests("Fog::Compute[:#{provider}] | environments", [provider.to_s]) do + connection = Fog::Compute[provider] + @organization = connection.organizations.first + + tests('#all').succeeds do + returns(false) { @organization.environments.all.empty? } + end + + tests('#get').succeeds do + environment = @organization.environments.all.first + fetched_environment = connection.environments.get(environment.href) + + returns(true) { !fetched_environment.nil? } + end + + tests("#organization").succeeds do + environment = @organization.environments.all.first + returns(false, "returns an organization") { environment.organization.nil? } + returns(true, "returns correct organization") { environment.organization.href == @organization.href } + end +end diff --git a/tests/ecloud/compute/models/internet_service_tests.rb b/tests/ecloud/compute/models/internet_service_tests.rb new file mode 100644 index 000000000..bb617d1d3 --- /dev/null +++ b/tests/ecloud/compute/models/internet_service_tests.rb @@ -0,0 +1,28 @@ +provider, config = :ecloud, compute_providers[:ecloud] +connection = Fog::Compute[provider] +organization = connection.organizations.first +environment = organization.environments.detect { |e| e.name == config[:ecloud_environment_name] } || organization.environments.first +public_ips = environment.public_ips +public_ip = public_ips.detect { |i| i.name == config[:ecloud_public_ip_name] } || public_ips.first + +Shindo.tests("Fog::Compute[:#{provider}] | internet_services", "queries") do + @internet_services = public_ip.internet_services + + tests('#all').succeeds do + returns(true, "is a collection") { @internet_services.is_a?(Fog::Compute::Ecloud::InternetServices) } + if Fog.mocking? + returns(false, "has services") { @internet_services.empty? } + else + true + end + end + + unless @internet_services.empty? + tests('#get').succeeds do + service = @internet_services.first + fetched_service = connection.internet_services.get(service.href) + returns(false, "service is not nil") { fetched_service.nil? } + returns(true, "is an InternetService") { fetched_service.is_a?(Fog::Compute::Ecloud::InternetService) } + end + end +end diff --git a/tests/ecloud/compute/models/ip_address_tests.rb b/tests/ecloud/compute/models/ip_address_tests.rb new file mode 100644 index 000000000..ea86bfe5d --- /dev/null +++ b/tests/ecloud/compute/models/ip_address_tests.rb @@ -0,0 +1,19 @@ +provider, config = :ecloud, compute_providers[:ecloud] + +Shindo.tests("Fog::Compute[:#{provider}] | ip_addresses", [provider.to_s]) do + connection = Fog::Compute[provider] + @organization = connection.organizations.first + @environment = @organization.environments.first + @network = @environment.networks.first + @ip_addresses = @network.ips + + tests('#all').succeeds do + returns(false) { @ip_addresses.all.empty? } + end + + tests('#get').succeeds do + address = @ip_addresses.first + fetched_network = connection.ip_addresses.get(address.href) + returns(false) { fetched_network.nil? } + end +end diff --git a/tests/ecloud/compute/models/network_tests.rb b/tests/ecloud/compute/models/network_tests.rb new file mode 100644 index 000000000..de9e3f9fc --- /dev/null +++ b/tests/ecloud/compute/models/network_tests.rb @@ -0,0 +1,17 @@ +provider, config = :ecloud, compute_providers[:ecloud] + +Shindo.tests("Fog::Compute[:#{provider}] | networks", [provider.to_s]) do + connection = Fog::Compute[provider] + @organization = connection.organizations.first + @environment = @organization.environments.first + + tests('#all').succeeds do + returns(false) { @environment.networks.all.empty? } + end + + tests('#get').succeeds do + network = @environment.networks.all.first + fetched_network = connection.networks.get(network.href) + returns(true) { !fetched_network.nil? } + end +end diff --git a/tests/ecloud/compute/models/operating_system_families_tests.rb b/tests/ecloud/compute/models/operating_system_families_tests.rb new file mode 100644 index 000000000..0ae127fe5 --- /dev/null +++ b/tests/ecloud/compute/models/operating_system_families_tests.rb @@ -0,0 +1,13 @@ +provider, config = :ecloud, compute_providers[:ecloud] + +Shindo.tests("Fog::Compute[:#{provider}] | operating_system_families", [provider.to_s]) do + connection = Fog::Compute[provider] + @organization = connection.organizations.first + @environment = @organization.environments.find{|e| e.name == config[:server_attributes][:environment_name]} || @organization.environments.first + @compute_pool = @environment.compute_pools.first + + tests('#all').succeeds do + operating_system_families = @compute_pool.operating_system_families + returns(false) { operating_system_families.empty? } + end +end diff --git a/tests/ecloud/compute/models/operating_system_tests.rb b/tests/ecloud/compute/models/operating_system_tests.rb new file mode 100644 index 000000000..7c46d3f1e --- /dev/null +++ b/tests/ecloud/compute/models/operating_system_tests.rb @@ -0,0 +1,22 @@ +provider, config = :ecloud, compute_providers[:ecloud] + +Shindo.tests("Fog::Compute[:#{provider}] | operating_system", [provider.to_s]) do + connection = Fog::Compute[provider] + @organization = connection.organizations.first + @environment = @organization.environments.find{|e| e.name == config[:server_attributes][:environment_name]} || @organization.environments.first + @compute_pool = @environment.compute_pools.first + + tests('#all').succeeds do + family = @compute_pool.operating_system_families.first + operating_systems = family.operating_systems + + returns(false) { operating_systems.empty? } + end + + tests('#get').succeeds do + family = @compute_pool.operating_system_families.first + operating_system = family.operating_systems.first + + returns(false) { family.operating_systems.get(operating_system.href).nil? } + end +end diff --git a/tests/ecloud/compute/models/organization_tests.rb b/tests/ecloud/compute/models/organization_tests.rb new file mode 100644 index 000000000..0663c0a43 --- /dev/null +++ b/tests/ecloud/compute/models/organization_tests.rb @@ -0,0 +1,21 @@ +provider, config = :ecloud, compute_providers[:ecloud] + +Shindo.tests("Fog::Compute[:#{provider}] | organizations", [provider.to_s]) do + connection = Fog::Compute[provider] + + tests('#all').succeeds do + returns(false) { connection.organizations.all.empty? } + end + + tests('#get').succeeds do + organization = connection.organizations.all.first + fetched_organization = connection.organizations.get(organization.href) + returns(true) { !fetched_organization.nil? } + end + + tests("#admin").succeeds do + organization = connection.organizations.all.first + + returns(true, "return AdminOrganization") { organization.admin.is_a?(Fog::Compute::Ecloud::AdminOrganization) } + end +end diff --git a/tests/ecloud/compute/models/public_ip_tests.rb b/tests/ecloud/compute/models/public_ip_tests.rb new file mode 100644 index 000000000..96c4aebcf --- /dev/null +++ b/tests/ecloud/compute/models/public_ip_tests.rb @@ -0,0 +1,19 @@ +provider, config = :ecloud, compute_providers[:ecloud] + +Shindo.tests("Fog::Compute[:#{provider}] | ip_addresses", [provider.to_s]) do + connection = Fog::Compute[provider] + @organization = connection.organizations.first + @environment = @organization.environments.first + @public_ips = @environment.public_ips + + tests('#all').succeeds do + returns(false, "has ips") { @public_ips.all.empty? } + end + + tests('#get').succeeds do + address = @public_ips.first + fetched_ip = connection.public_ips.get(address.href) + returns(false, "ip is not nil") { fetched_ip.nil? } + returns(true, "is a PublicIp") { fetched_ip.is_a?(Fog::Compute::Ecloud::PublicIp) } + end +end diff --git a/tests/ecloud/compute/models/server_tests.rb b/tests/ecloud/compute/models/server_tests.rb new file mode 100644 index 000000000..19f04975d --- /dev/null +++ b/tests/ecloud/compute/models/server_tests.rb @@ -0,0 +1,143 @@ +provider, config = :ecloud, compute_providers[:ecloud] + +connection = Fog::Compute[provider] +organization = connection.organizations.first +environment = organization.environments.find{|e| e.name == config[:server_attributes][:environment_name]} || organization.environments.first +public_ip = environment.public_ips.first +compute_pool = environment.compute_pools.first +image_href = Fog.credentials[:ecloud_image_href] || compute_pool.templates.first.href +ssh_key = organization.admin.ssh_keys.detect { |key| key.name == "root" } + +Shindo.tests("Fog::Compute[:#{provider}] | servers", "operations") do + @network = environment.networks.first + options = config[:server_attributes].merge(:network_uri => @network.href, :ssh_key_uri => ssh_key.href) + #if Fog.credentials[:ecloud_ssh_key_id] + # options = options.merge(:ssh_key_uri => "/cloudapi/ecloud/admin/sshkeys/#{Fog.credentials[:ecloud_ssh_key_id]}") + #end + + @server = compute_pool.servers.create(image_href, options) + + tests('#create_server').succeeds do + @server.wait_for { ready? } # server + end + + tests('#environment_has_a_row_and_group_with_the_right_names').succeeds do + row = environment.rows.detect { |r| r.name == options[:row] } + returns(false, "row is not nil") { row.nil? } + group = row.groups.detect { |g| g.name == options[:group] } + returns(false, "group is not nil") { group.nil? } + server = group.servers.detect { |s| s.name == @server.name } + returns(false, "group has server") { server.nil? } + end + + tests('#get_server_flavor').succeeds do + @server.flavor_id == {:ram => 512, :cpus => 1} + end + + @hwc = @server.hardware_configuration + tests('#add_disk_to_server').succeeds do + disk_count = @hwc.storage.is_a?(Hash) ? [@hwc.storage].count : @hwc.storage.count + @server.add_disk(25).wait_for { ready? } + @server.reload.wait_for { ready? } + returns(true, "disk count increased by 1") { @server.reload.hardware_configuration.storage.count == disk_count + 1 } + end + + tests('#detach_disk_from_server').succeeds do + disk_count = @hwc.reload.storage.is_a?(Hash) ? [@hwc.storage].count : @hwc.storage.count + @server.detach_disk(1).wait_for { ready? } + @server.reload.wait_for { ready? } + returns(true, "disk count decreased by 1") { @server.reload.hardware_configuration.storage.count == disk_count - 1 } + end + + @detached_disk = compute_pool.reload.detached_disks.detect { |d| d.name == @server.name } + tests('#attach_disk_to_server').succeeds do + disk_count = @hwc.reload.storage.is_a?(Hash) ? [@hwc.storage].count : @hwc.storage.count + @server.attach_disk(@detached_disk).wait_for { ready? } + @server.reload.wait_for { ready? } + returns(true, "disk count increased by 1") { @server.reload.hardware_configuration.storage.count == disk_count + 1 } + end + + tests('#delete_disk').succeeds do + disk_count = @hwc.reload.storage.is_a?(Hash) ? [@hwc.storage].count : @hwc.storage.count + @server.delete_disk(1).wait_for { ready? } + @server.reload.wait_for { ready? } + returns(true, "disk count decreased by 1") { @server.reload.hardware_configuration.storage.count == disk_count - 1 } + end + + @ip = @network.ips.reload.detect { |i| i.host.nil? && i.detected_on.nil? } + tests('#add_ip_to_server').succeeds do + @server.add_ip(:href => @network.href, :network_name => @network.name, :ip => @ip.name) + end + + service_name = Fog.credentials[:ecloud_internet_service_name] || Fog::Mock.random_letters(6) + service_port = Fog.credentials[:ecloud_internet_service_port] || Fog::Mock.random_numbers(3).to_i + service_protocol = Fog.credentials[:ecloud_internet_service_protocol] + @service = public_ip.internet_services.create(:name => service_name, :port => service_port, :protocol => service_protocol, :description => "", :enabled => true) + + tests('#create_internet_service').succeeds do + @service.wait_for { ready? } + returns(true, "is an internet service") { @service.is_a?(Fog::Compute::Ecloud::InternetService) } + end + + unless @server.ips.empty? + @ip_address = @server.ips.first + @ip = @server.ips.first.network.ips.detect { |i| i.name == @ip_address.address.name } + @node = @service.nodes.create(:name => @server.name, :port => service_port, :ip_address => @ip.href, :description => "", :enabled => true) + tests('#create_node_service').succeeds do + @node.wait_for { ready? } + returns(true, "is a node server") { @node.is_a?(Fog::Compute::Ecloud::Node) } + end + + tests('#destroy_node_service').succeeds do + @node.destroy.wait_for { ready? } + end + end + + #tests('#delete_ip_from_server').succeeds do + # @server.delete_ip(:href => @network.href, :network_name => @network.name, :ip => @ip.name) + #end + + tests('#destroy_internet_service').succeeds do + @service.destroy.wait_for { ready? } + end + + @server_count = environment.servers.count + tests('#destroy_server').succeeds do + @server.destroy.wait_for { ready? } # task + end + @new_server_count = environment.servers.reload.count + + tests('#server_count_reduced').succeeds do + returns(true, "server count is reduced") { @new_server_count < @server_count } + end + + @row = environment.rows.detect { |r| r.name == options[:row] } + @group = @row.groups.detect { |g| g.name == options[:group] } + if @group.servers.empty? + tests('#delete_group').succeeds do + @group.destroy + returns(true, "group no longer exists") { @group.reload.nil? } + end + end + if @row.groups.reload.empty? + tests("#delete_row").succeeds do + @row.destroy + returns(true, "row no longer exists") { @row.reload.nil? } + end + end +end + +Shindo.tests("Fog::Compute[:#{provider}] | server", "attributes") do + @network = environment.networks.first + options = config[:server_attributes].merge(:network_uri => @network.href, :ssh_key_uri => ssh_key.href) + #if Fog.credentials[:ecloud_ssh_key_id] + # options = options.merge(:ssh_key_uri => "/cloudapi/ecloud/admin/sshkeys/#{Fog.credentials[:ecloud_ssh_key_id]}") + #end + + @server = compute_pool.servers.first || compute_pool.servers.create(image_href, options).tap{|s| s.wait_for { ready? }} + + tests('#ip_addresses').succeeds do + returns(true, "is an array") { @server.ips.is_a?(Array) } + returns(true, "contains an VirtualMachineAssignedIp") { @server.ips.all?{|ip| ip.is_a?(Fog::Compute::Ecloud::VirtualMachineAssignedIp) } } + end +end diff --git a/tests/ecloud/compute/models/ssh_key_tests.rb b/tests/ecloud/compute/models/ssh_key_tests.rb new file mode 100644 index 000000000..3e4156304 --- /dev/null +++ b/tests/ecloud/compute/models/ssh_key_tests.rb @@ -0,0 +1,19 @@ +provider, config = :ecloud, compute_providers[:ecloud] + +Shindo.tests("Fog::Compute[:#{provider}] | ssh_keys", [provider.to_s]) do + connection = Fog::Compute[provider] + @organization = connection.organizations.first + @admin_organization = @organization.admin + @admin_organization.reload + @ssh_keys = @admin_organization.ssh_keys + + tests('#all').succeeds do + returns(false) { @ssh_keys.empty? } + end + + tests('#get').succeeds do + ssh_key = @ssh_keys.first + + returns(false) { @ssh_keys.get(ssh_key.href).nil? } + end +end diff --git a/tests/ecloud/compute/models/template_tests.rb b/tests/ecloud/compute/models/template_tests.rb new file mode 100644 index 000000000..675680f23 --- /dev/null +++ b/tests/ecloud/compute/models/template_tests.rb @@ -0,0 +1,20 @@ +provider, config = :ecloud, compute_providers[:ecloud] + +Shindo.tests("Fog::Compute[:#{provider}] | templates", [provider.to_s]) do + connection = Fog::Compute[provider] + @organization = connection.organizations.first + @environment = @organization.environments.find{|e| e.name == config[:server_attributes][:environment_name]} || @organization.environments.first + @compute_pool = @environment.compute_pools.first + + tests('#all').succeeds do + templates = @compute_pool.templates + returns(false) { templates.empty? } + end + + tests('#get').succeeds do + templates = @compute_pool.templates + template = templates.first + + returns(false) { @compute_pool.templates.get(template.href).nil? } + end +end