Class: Fog::Compute::Softlayer::Mock

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Softlayer::Compute::Shared

valid_request?

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 = []
  @bare_metal_servers = []
  super(args)
end

Instance Attribute Details

#bare_metal_serversObject

Returns the value of attribute bare_metal_servers.



50
51
52
# File 'lib/fog/softlayer/compute.rb', line 50

def bare_metal_servers
  @bare_metal_servers
end

#default_domainObject

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_guestsObject

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

#_requestObject

Raises:

  • (Fog::Errors::MockNotImplemented)


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.

Parameters:

  • opts (Array<Hash>)

Options Hash (opts):

  • :body (Array<Hash>)

    HTTP request body parameters

  • "hostname" (String)

    VM hostname, should be unique within the domain.

  • "domain" (String)

    VM domain.

  • "processorCoreAmount" (Integer)

    Number of CPU cores provisioned for the VM.

  • "memoryCapacity" (Integer)

    Available RAM for the server in GB. Valid arguments are 1, 2, 4, 6, 8, 12, 16, 32, 48, 64

  • "hourlyBillingFlag" (Boolean)

    Should the VM be billed hourly or monthly (monthly is less expensive, minimum charge of 1 month).

  • "localDiskFlag" (Boolean)

    Should the root volume be on the machine or on the SAN

  • "operatingSystemReferenceCode" (String)

    A valid SoftLayer operatingSystemReferenceCode string

  • "dedicatedAccountHostOnlyFlag" (Boolean)

    Defaults to false, pass true for a single-tenant VM.

Returns:

  • (Excon::Response)

Raises:

  • (ArgumentError)


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 create_bare_metal_server(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.,
        "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
  @bare_metal_servers.push(response.body).flatten!
  response
end

#create_vm(opts) ⇒ Excon::Response

Launch a single SoftLayer VM.

Parameters:

Options Hash (opts):

  • :body (<Hash>)

    HTTP request body parameters

  • "hostname" (String)

    VM hostname, should be unique within the domain.

  • "domain" (String)

    VM domain.

  • "startCpus" (Integer)

    Number of CPU cores provisioned for the VM.

  • "maxMemory" (Integer)

    Available RAM for the VM in MB. Valid arguments are 1024, 2048, 4096, 6144, 8192, 12288, 16384, 32768, 49152, 65536

  • "hourlyBillingFlag" (Boolean)

    Should the VM be billed hourly or monthly (monthly is less expensive, minimum charge of 1 month).

  • "localDiskFlag" (Boolean)

    Should the root volume be on the machine or on the SAN

  • "operatingSystemReferenceCode" (String)

    A valid SoftLayer operatingSystemReferenceCode string

  • "dedicatedAccountHostOnlyFlag" (Boolean)

    Defaults to false, pass true for a single-tenant VM.

Returns:

  • (Excon::Response)

Raises:

  • (ArgumentError)


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.

Parameters:

  • opts (Array<Hash>)

Options Hash (opts):

  • :body (Array<Hash>)

    HTTP request body parameters

  • "hostname" (String)

    VM hostname, should be unique within the domain.

  • "domain" (String)

    VM domain.

  • "startCpus" (Integer)

    Number of CPU cores provisioned for the VM.

  • "maxMemory" (Integer)

    Available RAM for the VM in MB. Valid arguments are 1024, 2048, 4096, 6144, 8192, 12288, 16384, 32768, 49152, 65536

  • "hourlyBillingFlag" (Boolean)

    Should the VM be billed hourly or monthly (monthly is less expensive, minimum charge of 1 month).

  • "localDiskFlag" (Boolean)

    Should the root volume be on the machine or on the SAN

  • "operatingSystemReferenceCode" (String)

    A valid SoftLayer operatingSystemReferenceCode string

  • "dedicatedAccountHostOnlyFlag" (Boolean)

    Defaults to false, pass true for a single-tenant VM.

Returns:

  • (Excon::Response)

Raises:

  • (ArgumentError)


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.,
        :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

Parameters:

  • id (Integer)

Returns:

  • (Excon::Response)


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 delete_bare_metal_server(id)
  response = Excon::Response.new

  # Found it and deleted it.
  response.status = 200
  response.body = self.get_bare_metal_servers.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

Parameters:

  • id (Integer)

Returns:

  • (Excon::Response)


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 get_bare_metal_server(identifier)
  response = Excon::Response.new
  response.body = @bare_metal_servers.map {|vm| vm if vm['id'] == identifier}.compact.reduce
  response.status = 200
  response
end

#get_bare_metal_serversObject



12
13
14
15
16
17
# File 'lib/fog/softlayer/requests/compute/get_bare_metal_servers.rb', line 12

def get_bare_metal_servers
  response = Excon::Response.new
  response.body = @bare_metal_servers
  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_vmsObject



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_serversObject



70
71
72
# File 'lib/fog/softlayer/compute.rb', line 70

def list_servers
  (self.get_vms.body << self.get_bare_metal_servers.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