Class: Fog::Compute::Softlayer::Mock
- Inherits:
-
Object
- Object
- Fog::Compute::Softlayer::Mock
- Includes:
- Softlayer::Compute::Shared, Softlayer::Slapi
- Defined in:
- lib/fog/softlayer/compute.rb,
lib/fog/softlayer/requests/compute/get_vm.rb,
lib/fog/softlayer/requests/compute/get_tag.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_vm_tags.rb,
lib/fog/softlayer/requests/compute/describe_tags.rb,
lib/fog/softlayer/requests/compute/create_vm_tags.rb,
lib/fog/softlayer/requests/compute/delete_vm_tags.rb,
lib/fog/softlayer/requests/compute/get_bare_metal_tags.rb,
lib/fog/softlayer/requests/compute/get_bare_metal_server.rb,
lib/fog/softlayer/requests/compute/create_bare_metal_tags.rb,
lib/fog/softlayer/requests/compute/delete_bare_metal_tags.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,
lib/fog/softlayer/requests/compute/get_references_by_tag_name.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_bare_metal_tags(id, tags = []) ⇒ Object
-
#create_vm(opts) ⇒ Excon::Response
Launch a single SoftLayer VM.
- #create_vm_tags(id, tags = []) ⇒ Object
-
#create_vms(opts) ⇒ Excon::Response
Launch one or more SoftLayer VMs.
-
#delete_bare_metal_server(id) ⇒ Excon::Response
Delete a BMC server.
- #delete_bare_metal_tags(id, tags = []) ⇒ Object
-
#delete_vm(id) ⇒ Excon::Response
Delete a VM.
- #delete_vm_tags(id, tags = []) ⇒ Object
- #describe_tags ⇒ Object
- #get_bare_metal_server(identifier) ⇒ Object
- #get_bare_metal_servers ⇒ Object
- #get_bare_metal_tags(id) ⇒ Object
- #get_references_by_tag_name(tag_list) ⇒ Object
- #get_tag(id) ⇒ Object
- #get_vm(identifier) ⇒ Object
- #get_vm_tags(id) ⇒ Object
- #get_vms ⇒ Object
-
#initialize(args) ⇒ Mock
constructor
A new instance of Mock.
- #list_servers ⇒ Object
- #request(method, path, options = {}) ⇒ Object
- #request_access_token(connection, credentials) ⇒ Object
Methods included from Softlayer::Compute::Shared
Methods included from Softlayer::Slapi
Constructor Details
#initialize(args) ⇒ Mock
Returns a new instance of Mock.
64 65 66 67 68 69 |
# File 'lib/fog/softlayer/compute.rb', line 64 def initialize(args) @virtual_guests = [] = [] = [] super(args) end |
Instance Attribute Details
#bare_metal_servers ⇒ Object
Returns the value of attribute bare_metal_servers.
62 63 64 |
# File 'lib/fog/softlayer/compute.rb', line 62 def end |
#default_domain ⇒ Object
Returns the value of attribute default_domain.
59 60 61 |
# File 'lib/fog/softlayer/compute.rb', line 59 def default_domain @default_domain end |
#virtual_guests ⇒ Object
Returns the value of attribute virtual_guests.
62 63 64 |
# File 'lib/fog/softlayer/compute.rb', line 62 def virtual_guests @virtual_guests end |
Instance Method Details
#_request ⇒ Object
79 80 81 |
# File 'lib/fog/softlayer/compute.rb', line 79 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 = 201 # 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_bare_metal_tags(id, tags = []) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/fog/softlayer/requests/compute/create_bare_metal_tags.rb', line 13 def (id, = []) raise ArgumentError, "Tags argument for #{self.class.name}##{__method__} must be Array." unless .is_a?(Array) response = Excon::Response.new response.status = self.(id).status if response.status != 404 .each do |tag| << { 'empRecordId'=>nil, 'id'=>Fog::Mock.random_numbers(7), 'resourceTableId'=>id, 'tagId'=> tagId = Fog::Mock.random_numbers(5), 'tagTypeId'=>1, 'usrRecordId'=>123456, 'tag'=>{'accountId'=>987654, 'id'=>tagId, 'internal'=>0, 'name'=>tag}, 'tagType'=>{'description'=>'Hardware', 'keyName'=>'HARDWARE'} } end response.body = true else response.body = { "error"=>"Unable to find object with id of '#{id}'.", "code"=>"SoftLayer_Exception_ObjectNotFound" } end 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_vm_tags(id, tags = []) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/fog/softlayer/requests/compute/create_vm_tags.rb', line 13 def (id, = []) raise ArgumentError, "Tags argument for #{self.class.name}##{__method__} must be Array." unless .is_a?(Array) response = Excon::Response.new response.status = self.get_vm(id).status if response.status != 404 .each do |tag| << { 'empRecordId'=>nil, 'id'=>Fog::Mock.random_numbers(7), 'resourceTableId'=>id, 'tagId'=> tagId = Fog::Mock.random_numbers(5), 'tagTypeId'=>1, 'usrRecordId'=>123456, 'tag'=>{'accountId'=>987654, 'id'=>tagId, 'internal'=>0, 'name'=>tag}, 'tagType'=>{'description'=>'CCI', 'keyName'=>'GUEST'} } end response.body = true else response.body = { "error"=>"Unable to find object with id of '#{id}'.", "code"=>"SoftLayer_Exception_ObjectNotFound" } end response 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 83 |
# 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, 'datacenter' => nil, '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, 'operatingSystem' => {}, 'tagReferences' => [] } # clobber stubbed values where applicable response.body = opts.map do |vm| fields.deep_merge(Fog::Softlayer.stringify_keys(opts.first)) # stringify in case :symbols were passed. 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_bare_metal_tags(id, tags = []) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/fog/softlayer/requests/compute/delete_bare_metal_tags.rb', line 14 def (id, = []) raise ArgumentError, "Tags argument for #{self.class.name}##{__method__} must be Array." unless .is_a?(Array) response = Excon::Response.new response.status = self.(id).status if response.status != 404 = .reject do |tag| tag['resourceTableId'] == id and .include?(tag['tag']['name']) end response.body = true else response.body = { "error"=>"Unable to find object with id of '#{id}'.", "code"=>"SoftLayer_Exception_ObjectNotFound" } 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 |
#delete_vm_tags(id, tags = []) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/fog/softlayer/requests/compute/delete_vm_tags.rb', line 14 def (id, = []) raise ArgumentError, "Tags argument for #{self.class.name}##{__method__} must be Array." unless .is_a?(Array) response = Excon::Response.new response.status = self.get_vm(id).status if response.status != 404 = .reject do |tag| tag['resourceTableId'] == id and .include?(tag['tag']['name']) end response.body = true else response.body = { "error"=>"Unable to find object with id of '#{id}'.", "code"=>"SoftLayer_Exception_ObjectNotFound" } end response end |
#describe_tags ⇒ Object
14 15 16 17 18 19 |
# File 'lib/fog/softlayer/requests/compute/describe_tags.rb', line 14 def response = Excon::Response.new response.body = response.status = 200 response end |
#get_bare_metal_server(identifier) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# 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.to_s }.compact.first || {} response.status = response.body.empty? ? 404 : 200 if response.status == 404 response.body = { "error"=>"Unable to find object with id of '#{identifier}'.", "code"=>"SoftLayer_Exception_ObjectNotFound" } end 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_bare_metal_tags(id) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/fog/softlayer/requests/compute/get_bare_metal_tags.rb', line 14 def (id) response = Excon::Response.new response.status = self.(id).status bmc = self.(id).body unless bmc['error'] = .map do |tag| tag if tag['resourceTableId'] == id end.compact end bmc['tagReferences'] = response.body = bmc if response.status == 404 response.body = { "error"=>"Unable to find object with id of '#{id}'.", "code"=>"SoftLayer_Exception_ObjectNotFound" } end response end |
#get_references_by_tag_name(tag_list) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/fog/softlayer/requests/compute/get_references_by_tag_name.rb', line 14 def get_references_by_tag_name(tag_list) response = Excon::Response.new response.status = 200 response.body = tag_list.split(',').map do |tag| refs = .select { |ref| ref['tag']['name'] == tag } unless refs.empty? { 'accountId' => Fog::Softlayer.mock_account_id, 'id'=>Fog::Mock.random_numbers(7), 'internal' => 0, 'name' => tag, 'references' => refs } end end.compact response end |
#get_tag(id) ⇒ Object
14 15 16 |
# File 'lib/fog/softlayer/requests/compute/get_tag.rb', line 14 def get_tag(id) # TODO: Implement end |
#get_vm(identifier) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# 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.to_s }.compact.first || {} response.status = response.body.empty? ? 404 : 200 if response.status == 404 response.body = { "error"=>"Unable to find object with id of '#{identifier}'.", "code"=>"SoftLayer_Exception_ObjectNotFound" } end response end |
#get_vm_tags(id) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/fog/softlayer/requests/compute/get_vm_tags.rb', line 14 def (id) response = Excon::Response.new response.status = self.get_vm(id).status vm = self.get_vm(id).body unless vm['error'] = .map do |tag| tag if tag['resourceTableId'] == id end.compact end vm['tagReferences'] = response.body = vm if response.status == 404 response.body = { "error"=>"Unable to find object with id of '#{id}'.", "code"=>"SoftLayer_Exception_ObjectNotFound" } end 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
83 84 85 86 87 88 89 |
# File 'lib/fog/softlayer/compute.rb', line 83 def list_servers vms = self.get_vms.body = self..body vms.map! { |server| server['bare_metal'] = false } .map! { |server| server['bare_metal'] = true } (vms << ).flatten end |
#request(method, path, options = {}) ⇒ Object
71 72 73 |
# File 'lib/fog/softlayer/compute.rb', line 71 def request(method, path, = {}) _request end |
#request_access_token(connection, credentials) ⇒ Object
75 76 77 |
# File 'lib/fog/softlayer/compute.rb', line 75 def request_access_token(connection, credentials) _request end |