Parent

Class/Module Index [+]

Quicksearch

Fog::Compute::Softlayer::Mock

The Mock Service allows you to run a fake instance of the Service which makes no real connections.

Attributes

bare_metal_servers[RW]
default_domain[RW]
softlayer_default_datacenter[RW]
softlayer_default_domain[RW]
virtual_guests[RW]

Public Class Methods

new(args) click to toggle source
# File lib/fog/softlayer/compute.rb, line 71
def initialize(args)
  @virtual_guests = []
  @bare_metal_servers = []
  @tags = []
  @key_pairs = []
  super(args)
end

Public Instance Methods

_request() click to toggle source
# File lib/fog/softlayer/compute.rb, line 87
def _request
  raise Fog::Errors::MockNotImplemented
end
create_bare_metal_server(opts) click to toggle source

Launch a SoftLayer BMC server.

@param [Array<Hash>] opts @option opts [Array<Hash>] :body

HTTP request body parameters

@option opts [String] “hostname”

VM hostname, should be unique within the domain.

@option opts [String] “domain”

VM domain.

@option opts [Integer] “processorCoreAmount”

Number of CPU cores provisioned for the VM.

@option opts [Integer] “memoryCapacity”

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

@option opts [Boolean] “hourlyBillingFlag”

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

@option opts [Boolean] “localDiskFlag”

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

@option opts [String] “operatingSystemReferenceCode”

A valid SoftLayer operatingSystemReferenceCode string

@option opts [Boolean] “dedicatedAccountHostOnlyFlag”

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

@return [Excon::Response]

# 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 = %{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
  @bare_metal_servers.push(response.body).flatten!
  response
end
create_bare_metal_tags(id, tags = []) click to toggle source
# File lib/fog/softlayer/requests/compute/create_bare_metal_tags.rb, line 13
def create_bare_metal_tags(id, tags = [])
  raise ArgumentError, "Tags argument for #{self.class.name}##{__method__} must be Array." unless tags.is_a?(Array)
  response = Excon::Response.new
  response.status = self.get_bare_metal_server(id).status

  if response.status != 404
    tags.each do |tag|
      @tags << {
          '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_key_pair(opts) click to toggle source
# File lib/fog/softlayer/requests/compute/create_key_pair.rb, line 13
def create_key_pair(opts)
  response = Excon::Response.new

  response.status = 200
  response.body = []

  response.body = {
      "createDate" => Time.now.iso8601,
      "fingerprint" => "1a:1a:1a:1a:1a:1a:1a:1a:1a:1a:1a:1a:1a:1a:1a:1a",
      "id" => Fog::Mock.random_numbers(5).to_i,
      "key" => opts[:key],
      "label" => opts[:label],
      "modifyDate" => nil
  }

  @key_pairs.push(response.body)
  response
end
create_vm(opts) click to toggle source

Launch a single SoftLayer VM.

@param [<Hash>] opts @option opts [<Hash>] :body

HTTP request body parameters

@option opts [String] “hostname”

VM hostname, should be unique within the domain.

@option opts [String] “domain”

VM domain.

@option opts [Integer] “startCpus”

Number of CPU cores provisioned for the VM.

@option opts [Integer] “maxMemory”

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

@option opts [Boolean] “hourlyBillingFlag”

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

@option opts [Boolean] “localDiskFlag”

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

@option opts [String] “operatingSystemReferenceCode”

A valid SoftLayer operatingSystemReferenceCode string

@option opts [Boolean] “dedicatedAccountHostOnlyFlag”

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

@return [Excon::Response]

# 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 = []) click to toggle source
# File lib/fog/softlayer/requests/compute/create_vm_tags.rb, line 13
def create_vm_tags(id, tags = [])
  raise ArgumentError, "Tags argument for #{self.class.name}##{__method__} must be Array." unless tags.is_a?(Array)
  response = Excon::Response.new
  response.status = self.get_vm(id).status

  if response.status != 404
    tags.each do |tag|
      @tags << {
          '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) click to toggle source

Launch one or more SoftLayer VMs.

@param [Array<Hash>] opts @option opts [Array<Hash>] :body

HTTP request body parameters

@option opts [String] “hostname”

VM hostname, should be unique within the domain.

@option opts [String] “domain”

VM domain.

@option opts [Integer] “startCpus”

Number of CPU cores provisioned for the VM.

@option opts [Integer] “maxMemory”

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

@option opts [Boolean] “hourlyBillingFlag”

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

@option opts [Boolean] “localDiskFlag”

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

@option opts [String] “operatingSystemReferenceCode”

A valid SoftLayer operatingSystemReferenceCode string

@option opts [Boolean] “dedicatedAccountHostOnlyFlag”

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

@return [Excon::Response]

# 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 = %{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) click to toggle source

Delete a BMC server @param [Integer] id @return [Excon::Response]

# 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_bare_metal_tags(id, tags = []) click to toggle source
# File lib/fog/softlayer/requests/compute/delete_bare_metal_tags.rb, line 14
def delete_bare_metal_tags(id, tags = [])
  raise ArgumentError, "Tags argument for #{self.class.name}##{__method__} must be Array." unless tags.is_a?(Array)
  response = Excon::Response.new
  response.status = self.get_bare_metal_server(id).status

  if response.status != 404
    @tags = @tags.reject do |tag|
      tag['resourceTableId'] == id and tags.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_key_pair(id) click to toggle source
# File lib/fog/softlayer/requests/compute/delete_key_pair.rb, line 13
def delete_key_pair(id)
  response = Excon::Response.new
  response.status = 200
  if @key_pairs.reject! { |kp| kp['id'] == id }.nil?
    response.status = 404
    response.body = {
      "error" => "Unable to find object with id of '#{id}'.",
      "code" => "SoftLayer_Exception_ObjectNotFound"
    }
  else
    response.body = true
  end
  response
end
delete_vm(id) click to toggle source

Delete a VM @param [Integer] id @return [Excon::Response]

# 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 = []) click to toggle source
# File lib/fog/softlayer/requests/compute/delete_vm_tags.rb, line 14
def delete_vm_tags(id, tags = [])
  raise ArgumentError, "Tags argument for #{self.class.name}##{__method__} must be Array." unless tags.is_a?(Array)
  response = Excon::Response.new
  response.status = self.get_vm(id).status

  if response.status != 404
    @tags = @tags.reject do |tag|
      tag['resourceTableId'] == id and tags.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() click to toggle source
# File lib/fog/softlayer/requests/compute/describe_tags.rb, line 14
def describe_tags
  response = Excon::Response.new
  response.body = @tags
  response.status = 200
  response
end
get_bare_metal_server(identifier) click to toggle source
# 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.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() click to toggle source
# 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_bare_metal_tags(id) click to toggle source
# File lib/fog/softlayer/requests/compute/get_bare_metal_tags.rb, line 14
def get_bare_metal_tags(id)
  response = Excon::Response.new

  response.status = self.get_bare_metal_server(id).status

  bmc = self.get_bare_metal_server(id).body
  unless bmc['error']
    tags = @tags.map do |tag|
      tag if tag['resourceTableId'] == id
    end.compact
  end

  bmc['tagReferences'] = tags
  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_key_pair(id) click to toggle source
# File lib/fog/softlayer/requests/compute/get_key_pair.rb, line 12
def get_key_pair(id)
  response = Excon::Response.new
  response.status = 200
  response.body = key_pair = @key_pairs.select { |kp| kp['id'] == id }.first

  if key_pair.nil?
    response.body = {
        "error"=>"Unable to find object with id of '#{id}'.",
        "code"=>"SoftLayer_Exception_ObjectNotFound"
    }
    response.status = 404
  end
  response
end
get_key_pairs() click to toggle source
# File lib/fog/softlayer/requests/compute/get_key_pairs.rb, line 13
def get_key_pairs
  response = Excon::Response.new
  response.body = @key_pairs
  response.status = 200
  response
end
get_references_by_tag_name(tag_list) click to toggle source
# 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 = @tags.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) click to toggle source
# File lib/fog/softlayer/requests/compute/get_tag.rb, line 14
def get_tag(id)
  # TODO: Implement
end
get_vm(identifier) click to toggle source
# 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) click to toggle source
# File lib/fog/softlayer/requests/compute/get_vm_tags.rb, line 14
def get_vm_tags(id)
  response = Excon::Response.new

  response.status = self.get_vm(id).status

  vm = self.get_vm(id).body
  unless vm['error']
    tags = @tags.map do |tag|
      tag if tag['resourceTableId'] == id
    end.compact
  end

  vm['tagReferences'] = tags
  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() click to toggle source
# 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() click to toggle source
# File lib/fog/softlayer/compute.rb, line 91
def list_servers
  vms = self.get_vms.body
  bare_metals = self.get_bare_metal_servers.body
  vms.map! { |server| server['bare_metal'] = false }
  bare_metals.map! { |server| server['bare_metal'] = true }
  (vms << bare_metals).flatten
end
request(method, path, options = {}) click to toggle source
# File lib/fog/softlayer/compute.rb, line 79
def request(method, path, options = {})
  _request
end
request_access_token(connection, credentials) click to toggle source
# File lib/fog/softlayer/compute.rb, line 83
def request_access_token(connection, credentials)
  _request
end
update_key_pair(id, opts) click to toggle source
# File lib/fog/softlayer/requests/compute/update_key_pair.rb, line 13
def update_key_pair(id, opts)
  response = Excon::Response.new
  response.status = 200

  key_pair, index = @key_pairs.each_with_index.map { |kp, i| [kp, i] if kp['id'] == id }.compact.flatten

  if key_pair.nil?
    response.status = 404
    response.body = {
        "error" => "Unable to find object with id of '#{id}'.",
        "code" => "SoftLayer_Exception_ObjectNotFound"
    }
  else

    @key_pairs[index] = key_pair.merge(opts)
    response.body = true
  end
  response
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.