class Fog::Compute::Cloudstack::Mock

Public Class Methods

data() click to toggle source
# File lib/fog/cloudstack/compute.rb, line 128
def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {}
  end
end
new(options={}) click to toggle source
# File lib/fog/cloudstack/compute.rb, line 138
def initialize(options={})
  @cloudstack_api_key = options[:cloudstack_api_key]
end
reset() click to toggle source
# File lib/fog/cloudstack/compute.rb, line 134
def self.reset
  @data = nil
end

Public Instance Methods

attach_volume(options={}) click to toggle source
# File lib/fog/cloudstack/requests/compute/attach_volume.rb, line 21
def attach_volume(options={})
  volume_id = options['id']
  server_id = options['virtualmachineid']

  volume = self.data[:volumes][volume_id]
  unless volume
    raise Fog::Compute::Cloudstack::BadRequest.new("Unable to execute API command attachvolume due to invalid value. Object volumes(uuid: #{volume_id}) does not exist.")
  end

  server = self.data[:servers][server_id]
  unless server
    raise Fog::Compute::Cloudstack::BadRequest.new("Unable to execute API command attachvolume due to invalid value. Object vm_instance(uuid: #{server_id}) does not exist.")
  end

  volume['virtualmachineid']= server['id']
  volume['vmname']= server['name']
  volume['vmdisplayname']= server['displayname']

  job_id = Fog::Cloudstack.uuid

  # FIXME: need to determine current user
  account_id = self.data[:accounts].first
  user_id = self.data[:users].first

  job = {
    "accountid"     => account_id,
    "userid"        => user_id,
    "cmd"           => "com.cloud.api.commands.AttachVolumeCmd",
    "created"       => Time.now.iso8601,
    "jobid"         => job_id,
    "jobstatus"     => 1,
    "jobprocstatus" => 0,
    "jobresultcode" => 0,
    "jobresulttype" => "object",
    "jobresult"     =>
      {"volume"     => volume}
  }

  self.data[:jobs][job_id]= job

  {
    "attachvolumeresponse" => {
      "jobid" => job_id
    }
  }
end
create_volume(options={}) click to toggle source
# File lib/fog/cloudstack/requests/compute/create_volume.rb, line 20
def create_volume(options={})
  volume_id = Fog::Cloudstack.uuid

  unless volume_name = options['name']
    raise Fog::Compute::Cloudstack::BadRequest.new('Unable to execute API command createvolume due to missing parameter name')
  end

  unless zone_id = options['zoneid']
    raise Fog::Compute::Cloudstack::BadRequest.new('Unable to execute API command createvolume due to missing parameter zoneid')
  end

  unless disk_offering_id = options['diskofferingid']
    raise Fog::Compute::Cloudstack::BadRequest.new('Unable to execute API command createvolume due to missing parameter diskofferingid')
  end

  volume = {
     "id"                      => volume_id,
     "name"                    => volume_name,
     "zoneid"                  => zone_id,
     "zonename"                => "ey-wdc-00",
     "type"                    => "DATADISK",
     "size"                    => 5368709120,
     "created"                 => "2012-05-22T14:52:55-0500",
     "state"                   => "Allocated",
     "account"                 => "accountname",
     "domainid"                => "6023b6fe-5bef-4358-bc76-9f4e75afa52f",
     "domain"                  => "ROOT",
     "storagetype"             => "shared",
     "hypervisor"              => "None",
     "diskofferingid"          => disk_offering_id,
     "diskofferingname"        => "Small",
     "diskofferingdisplaytext" => "Small Disk, 5 GB",
     "storage"                 => "none",
     "destroyed"               => false,
     "isextractable"           => false
  }

  self.data[:volumes][volume_id]= volume
  {'createvolumeresponse' => volume}
end
data() click to toggle source
# File lib/fog/cloudstack/compute.rb, line 142
def data
  self.class.data[@cloudstack_api_key]
end
delete_volume(options={}) click to toggle source
# File lib/fog/cloudstack/requests/compute/delete_volume.rb, line 19
def delete_volume(options={})
  volume_id = options['id']
  if self.data[:volumes][volume_id]
    self.data[:volumes].delete(volume_id)
    {
      "deletevolumeresponse" => {
        "success" => "true"
      }
    }
  else # FIXME: mayhaps
    self.data[:volumes].delete(volume_id)
    {
      "deletevolumeresponse" => {
        "success" => "false"
      }
    }
  end
end
deploy_virtual_machine(options={}) click to toggle source
# File lib/fog/cloudstack/requests/compute/deploy_virtual_machine.rb, line 32
def deploy_virtual_machine(options={})
  zone_id = options['zoneid']
  unless zone_id
    raise Fog::Compute::Cloudstack::BadRequest.new('Unable to execute API command deployvirtualmachine due to missing parameter zoneid')
  end
  unless zone = self.data[:zones][zone_id]
    raise Fog::Compute::Cloudstack::BadRequest.new("Unable to execute API command deployvirtualmachine due to invalid value. Object zone(uuid: #{zone_id}) does not exist.")
  end
  zone_name = zone[:name]

  template_id = options['templateid']
  unless template = self.data[:images][template_id]
    raise Fog::Compute::Cloudstack::BadRequest.new('Unable to execute API command deployvirtualmachine due to missing parameter templateid')
  end
  template_name = template[:name]
  template_display_text = template[:display_text]

  service_offering_id = options['serviceofferingid']
  unless service_offering = self.data[:flavors][service_offering_id]
    raise Fog::Compute::Cloudstack::BadRequest.new('Unable to execute API command deployvirtualmachine due to missing parameter serviceofferingid')
  end

  service_offering_name = service_offering[:name]
  service_offering_cpu_number = service_offering[:cpunumber]
  service_offering_cpu_speed = service_offering[:cpuspeed]
  service_offering_memory = service_offering[:cpumemory]

  identity = Fog::Cloudstack.uuid
  name = options['name'] || Fog::Cloudstack.uuid
  display_name = options['displayname'] || name
  account_name = options['account'] || self.data[:accounts].first[1]["name"]

  domain = options['domainid'] ? self.data[:domains][options['domainid']] : self.data[:domains].first[1]
  domain_id = domain[:id]
  domain_name = domain[:name]

  # how is this setup
  password = nil
  password_enabled = false

  guest_os_id = Fog::Cloudstack.uuid

  security_group_ids = options['securitygroupids'] || [] # TODO: for now

  network_ids = Array(options['networkids']) || [self.data[:networks].first[1]["id"]]
  networks = network_ids.map{|nid| self.data[:networks][nid]}
  nic = networks.map do |network|
    {
      "id" => Fog::Cloudstack.uuid,
      "networkid" => network["id"],
      "netmask" => Fog::Cloudstack.ip_address,
      "gateway" => network["gateway"],
      "ipaddress" => Fog::Cloudstack.ip_address,
      "traffictype" => "Guest", # TODO: ?
      "type" => network["type"],
      "isdefault" => true, # TODO: ?
      "macaddress" => Fog::Cloudstack.mac_address
    }
  end

  virtual_machine = {
    "id" => identity,
    "name" => name,
    "displayname" => display_name,
    "account" => account_name,
    "domainid" => domain_id,
    "domain" => domain_name,
    "created" => Time.now.to_s,
    "state" => "Running",
    "haenable" => false,
    "zoneid" => zone_id,
    "zonename" => zone_name,
    "templateid" => template_id,
    "templatename" => template_name,
    "templatedisplaytext" => template_display_text,
    "passwordenabled" => false,
    "serviceofferingid" => service_offering_id,
    "serviceofferingname" => service_offering_name,
    "cpunumber" => service_offering_cpu_number,
    "cpuspeed" => service_offering_cpu_speed,
    "memory" => service_offering_memory,
    "cpuused" => "0%",
    "networkkbsread" => 0,
    "networkkbswrite" => 0,
    "guestosid" => guest_os_id,
    "rootdeviceid" => 0,
    "rootdevicetype" => "NetworkFilesystem",
    "securitygroup" => security_group_ids, # TODO: mayhaps?
    "nic" => nic
  }

  self.data[:servers][identity]= virtual_machine
  {'deployvirtualmachineresponse' => virtual_machine}
end
destroy_virtual_machine(options={}) click to toggle source
# File lib/fog/cloudstack/requests/compute/destroy_virtual_machine.rb, line 21
def destroy_virtual_machine(options={})
  identity = options[:id]

  self.data[:servers].delete(identity)
end
detach_volume(options={}) click to toggle source
# File lib/fog/cloudstack/requests/compute/detach_volume.rb, line 21
def detach_volume(options={})
  volume_id = options['id']

  volume = self.data[:volumes][volume_id]
  unless volume
    raise Fog::Compute::Cloudstack::BadRequest.new("Unable to execute API command attachvolume due to invalid value. Object volumes(uuid: #{volume_id}) does not exist.")
  end

  volume['virtualmachineid']= volume['vmname']= volume['vmdisplayname']= nil

  job_id = Fog::Cloudstack.uuid

  # FIXME: need to determine current user
  account_id = self.data[:accounts].first
  user_id = self.data[:users].first

  job = {
    "accountid"     => account_id,
    "userid"        => user_id,
    "cmd"           => "com.cloud.api.commands.DetachVolumeCmd",
    "created"       => Time.now.iso8601,
    "jobid"         => job_id,
    "jobstatus"     => 1,
    "jobprocstatus" => 0,
    "jobresultcode" => 0,
    "jobresulttype" => "object",
    "jobresult"     =>
      {"volume"     => volume}
  }

  self.data[:jobs][job_id]= job

  {
    "detachvolumeresponse" => {
      "jobid" => job_id
    }
  }
end
list_async_jobs(options={}) click to toggle source
# File lib/fog/cloudstack/requests/compute/list_async_jobs.rb, line 19
def list_async_jobs(options={})
  # FIXME: support paging
  jobs = self.data[:jobs]
  {
    'listasyncjobsresponse' => {
      'count' => jobs.size,
      'asyncjobs' => jobs
    }
  }
end
list_service_offerings(options={}) click to toggle source
# File lib/fog/cloudstack/requests/compute/list_service_offerings.rb, line 21
def list_service_offerings(options={})
  flavors = []
  if service_offering_id = options['id']
    flavor = self.data[:flavors][service_offering_id]
    raise Fog::Compute::Cloudstack::BadRequest unless flavor
    flavors = [flavor]
  else
    flavors = self.data[:flavors].values
  end

  {
    "listserviceofferingsresponse" =>
    {
      "count" => flavors.size,
      "serviceoffering"=> flavors
    }
  }
end
list_templates(options={}) click to toggle source
# File lib/fog/cloudstack/requests/compute/list_templates.rb, line 21
def list_templates(options={})
  templates = self.data[:images].values

  {
    "listtemplatesresponse" =>
      {
        "count" => templates.size,
        "template"=> templates
      }
  }
end
list_virtual_machines(options={}) click to toggle source
# File lib/fog/cloudstack/requests/compute/list_virtual_machines.rb, line 19
def list_virtual_machines(options={})
  {"listvirtualmachinesresponse" =>
    {"count" => self.data[:servers].values.size, "virtualmachine" => self.data[:servers].values}}
end
list_volumes(options={}) click to toggle source
# File lib/fog/cloudstack/requests/compute/list_volumes.rb, line 20
def list_volumes(options={})
  volume_id = options.delete('id')
  if volume_id
    volumes = [self.data[:volumes][volume_id]]
  else
    volumes = self.data[:volumes].values
  end

  {
    'listvolumesresponse' => {
      'count' => volumes.size,
      'volume' => volumes
    }
  }
end
list_zones(options={}) click to toggle source
# File lib/fog/cloudstack/requests/compute/list_zones.rb, line 20
def list_zones(options={})
  zones = self.data[:zones].values

  {
    "listzonesresponse"=>
    {
      "count" => zones.size,
      "zone" => zones
    }
  }
end
reboot_virtual_machine(options={}) click to toggle source
# File lib/fog/cloudstack/requests/compute/reboot_virtual_machine.rb, line 20
def reboot_virtual_machine(options={})
  job_id = Fog::Cloudstack.uuid
  {
    "rebootvirtualmachineresponse" => {
      "jobid" => job_id
    }
  }
end
reset_data() click to toggle source
# File lib/fog/cloudstack/compute.rb, line 146
def reset_data
  self.class.data.delete(@cloudstack_api_key)
end