Class: Fog::Compute::Softlayer::Mock
- Inherits:
-
Object
- Object
- Fog::Compute::Softlayer::Mock
- Includes:
- Softlayer::Compute::Shared
- Defined in:
- lib/fog/softlayer/compute.rb,
lib/fog/softlayer/requests/compute/get_vm.rb,
lib/fog/softlayer/requests/compute/get_vms.rb,
lib/fog/softlayer/requests/compute/create_vm.rb,
lib/fog/softlayer/requests/compute/delete_vm.rb,
lib/fog/softlayer/requests/compute/create_vms.rb,
lib/fog/softlayer/requests/compute/get_bare_metal_server.rb,
lib/fog/softlayer/requests/compute/get_bare_metal_servers.rb,
lib/fog/softlayer/requests/compute/create_bare_metal_server.rb,
lib/fog/softlayer/requests/compute/delete_bare_metal_server.rb
Overview
The Mock Service allows you to run a fake instance of the Service which makes no real connections.
Instance Attribute Summary collapse
-
#bare_metal_servers ⇒ Object
Returns the value of attribute bare_metal_servers.
-
#default_domain ⇒ Object
Returns the value of attribute default_domain.
-
#virtual_guests ⇒ Object
Returns the value of attribute virtual_guests.
Instance Method Summary collapse
- #_request ⇒ Object
-
#create_bare_metal_server(opts) ⇒ Excon::Response
Launch a SoftLayer BMC server.
-
#create_vm(opts) ⇒ Excon::Response
Launch a single SoftLayer VM.
-
#create_vms(opts) ⇒ Excon::Response
Launch one or more SoftLayer VMs.
-
#delete_bare_metal_server(id) ⇒ Excon::Response
Delete a BMC server.
-
#delete_vm(id) ⇒ Excon::Response
Delete a VM.
- #get_bare_metal_server(identifier) ⇒ Object
- #get_bare_metal_servers ⇒ Object
- #get_vm(identifier) ⇒ Object
- #get_vms ⇒ Object
-
#initialize(args) ⇒ Mock
constructor
A new instance of Mock.
- #list_servers ⇒ Object
- #request(method, path, parameters = {}) ⇒ Object
- #request_access_token(connection, credentials) ⇒ Object
Methods included from Softlayer::Compute::Shared
Constructor Details
#initialize(args) ⇒ Mock
Returns a new instance of Mock.
52 53 54 55 56 |
# File 'lib/fog/softlayer/compute.rb', line 52 def initialize(args) @virtual_guests = [] = [] super(args) end |
Instance Attribute Details
#bare_metal_servers ⇒ Object
Returns the value of attribute bare_metal_servers.
50 51 52 |
# File 'lib/fog/softlayer/compute.rb', line 50 def end |
#default_domain ⇒ Object
Returns the value of attribute default_domain.
48 49 50 |
# File 'lib/fog/softlayer/compute.rb', line 48 def default_domain @default_domain end |
#virtual_guests ⇒ Object
Returns the value of attribute virtual_guests.
50 51 52 |
# File 'lib/fog/softlayer/compute.rb', line 50 def virtual_guests @virtual_guests end |
Instance Method Details
#_request ⇒ Object
66 67 68 |
# File 'lib/fog/softlayer/compute.rb', line 66 def _request raise Fog::Errors::MockNotImplemented end |
#create_bare_metal_server(opts) ⇒ Excon::Response
Launch a SoftLayer BMC server.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/fog/softlayer/requests/compute/create_bare_metal_server.rb', line 35 def (opts) raise ArgumentError, "Fog::Compute::Softlayer#create_bare_metal_server expects argument of type Hash" unless opts.kind_of?(Hash) response = Excon::Response.new required = %w{hostname domain processorCoreAmount memoryCapacity hourlyBillingFlag operatingSystemReferenceCode} begin Fog::Softlayer.valid_request?(required, opts) or raise MissingRequiredParameter response.status = 200 # a real response comes back with lots of nil values like this too, it takes 1 - 2 hours for a real BMC server to provision response.body = { "accountId" => Fog::Softlayer.mock_account_id, "createDate" => Time.now.iso8601, "dedicatedAccountHostOnlyFlag" => false, "domain" => nil, "fullyQualifiedDomainName" => nil, "hostname" => nil, "id" => Fog::Softlayer.mock_vm_id, "lastPowerStateId" => nil, "lastVerifiedDate" => nil, "maxCpu" => nil, "maxCpuUnits" => "CORE", "maxMemory" => nil, "metricPollDate" => nil, "modifyDate" => nil, "startCpus" => nil, "statusId" => 1001, "globalIdentifier" => Fog::Softlayer.mock_global_identifier } rescue MissingRequiredParameter response.status = 500 response.body = { "code" => "SoftLayer_Exception_MissingCreationProperty", "error" => "Properties #{required.join(', ')} ALL must be set to create an instance of 'SoftLayer_Hardware'." } end .push(response.body).flatten! response end |
#create_vm(opts) ⇒ Excon::Response
Launch a single SoftLayer VM.
35 36 37 38 39 |
# File 'lib/fog/softlayer/requests/compute/create_vm.rb', line 35 def create_vm(opts) raise ArgumentError, "Fog::Compute::Softlayer#create_vm expects argument of type Hash" unless opts.kind_of?(Hash) opts = [opts] self.create_vms(opts) end |
#create_vms(opts) ⇒ Excon::Response
Launch one or more SoftLayer VMs.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/fog/softlayer/requests/compute/create_vms.rb', line 35 def create_vms(opts) raise ArgumentError, "Fog::Compute::Softlayer#create_vms expects argument of type Array" unless opts.kind_of?(Array) response = Excon::Response.new required = %w{hostname domain startCpus maxMemory hourlyBillingFlag localDiskFlag} begin opts.each {|vm| Fog::Softlayer.valid_request?(required, vm) or raise MissingRequiredParameter} response.status = 200 response.body = [] ## stub some responses fields = { :accountId => Fog::Softlayer.mock_account_id, :createDate => Time.now.iso8601, :dedicatedAccountHostOnlyFlag => false, :domain => nil, :fullyQualifiedDomainName => nil, :hostname => nil, :id => Fog::Softlayer.mock_vm_id, :lastPowerStateId => nil, :lastVerifiedDate => nil, :maxCpu => nil, :maxCpuUnits => "CORE", :maxMemory => nil, :metricPollDate => nil, :modifyDate => nil, :startCpus => nil, :statusId => 1001, :globalIdentifier => Fog::Softlayer.mock_global_identifier } # clobber stubbed values where applicable response.body = opts.each_with_index.map do |vm,i| fields.reduce({}) do |result,(field,default)| result[field] = vm[field] || default result end end rescue MissingRequiredParameter response.status = 500 response.body = { "code" => "SoftLayer_Exception_MissingCreationProperty", "error" => "Properties #{required.join(', ')} ALL must be set to create an instance of 'SoftLayer_Virtual_Guest'." } end @virtual_guests.push(response.body).flatten! response end |
#delete_bare_metal_server(id) ⇒ Excon::Response
Delete a BMC server
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/fog/softlayer/requests/compute/delete_bare_metal_server.rb', line 16 def (id) response = Excon::Response.new # Found it and deleted it. response.status = 200 response.body = self..body.map{|server| server['id']}.include?(id) # Didn't find it, give the error that the API would give. unless response.body response.body = Fog::JSON.encode({:error => "A billing item is required to process a cancellation.", :code => "SoftLayer_Exception_NotFound"}) response.status = 500 end response end |
#delete_vm(id) ⇒ Excon::Response
Delete a VM
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/fog/softlayer/requests/compute/delete_vm.rb', line 16 def delete_vm(id) response = Excon::Response.new # Found it and deleted it. response.status = 200 response.body = self.get_vms.body.map{|server| server['id']}.include?(id) # Didn't find it, give the error that the API would give. unless response.body response.body = Fog::JSON.encode({:error => "A billing item is required to process a cancellation.", :code => "SoftLayer_Exception_NotFound"}) response.status = 500 end response end |
#get_bare_metal_server(identifier) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/fog/softlayer/requests/compute/get_bare_metal_server.rb', line 12 def (identifier) response = Excon::Response.new response.body = .map {|vm| vm if vm['id'] == identifier}.compact.reduce response.status = 200 response end |
#get_bare_metal_servers ⇒ Object
12 13 14 15 16 17 |
# File 'lib/fog/softlayer/requests/compute/get_bare_metal_servers.rb', line 12 def response = Excon::Response.new response.body = response.status = 200 response end |
#get_vm(identifier) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/fog/softlayer/requests/compute/get_vm.rb', line 12 def get_vm(identifier) response = Excon::Response.new response.body = @virtual_guests.map {|vm| vm if vm['id'] == identifier}.compact.reduce response.status = 200 response end |
#get_vms ⇒ Object
12 13 14 15 16 17 |
# File 'lib/fog/softlayer/requests/compute/get_vms.rb', line 12 def get_vms response = Excon::Response.new response.body = @virtual_guests response.status = 200 response end |
#list_servers ⇒ Object
70 71 72 |
# File 'lib/fog/softlayer/compute.rb', line 70 def list_servers (self.get_vms.body << self..body).flatten end |
#request(method, path, parameters = {}) ⇒ Object
58 59 60 |
# File 'lib/fog/softlayer/compute.rb', line 58 def request(method, path, parameters = {}) _request end |
#request_access_token(connection, credentials) ⇒ Object
62 63 64 |
# File 'lib/fog/softlayer/compute.rb', line 62 def request_access_token(connection, credentials) _request end |