Class: Fog::Compute::Cloudstack::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/rackspace-fog/cloudstack/compute.rb,
lib/rackspace-fog/cloudstack/requests/compute/list_zones.rb,
lib/rackspace-fog/cloudstack/requests/compute/list_volumes.rb,
lib/rackspace-fog/cloudstack/requests/compute/attach_volume.rb,
lib/rackspace-fog/cloudstack/requests/compute/create_volume.rb,
lib/rackspace-fog/cloudstack/requests/compute/delete_volume.rb,
lib/rackspace-fog/cloudstack/requests/compute/detach_volume.rb,
lib/rackspace-fog/cloudstack/requests/compute/list_templates.rb,
lib/rackspace-fog/cloudstack/requests/compute/list_async_jobs.rb,
lib/rackspace-fog/cloudstack/requests/compute/list_virtual_machines.rb,
lib/rackspace-fog/cloudstack/requests/compute/deploy_virtual_machine.rb,
lib/rackspace-fog/cloudstack/requests/compute/list_service_offerings.rb,
lib/rackspace-fog/cloudstack/requests/compute/reboot_virtual_machine.rb,
lib/rackspace-fog/cloudstack/requests/compute/destroy_virtual_machine.rb

Overview

Real

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



244
245
246
# File 'lib/rackspace-fog/cloudstack/compute.rb', line 244

def initialize(options={})
  @cloudstack_api_key = options[:cloudstack_api_key]
end

Class Method Details

.dataObject



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/rackspace-fog/cloudstack/compute.rb', line 248

def self.data
  @data ||= begin
    rc_options = Fog.credentials[:cloudstack] || {}
    zone_id = rc_options[:zone_id] ||"c554c592-e09c-9df5-7688-4a32754a4305"
    image_id = rc_options[:image_id] || "8a31cf9c-f248-0588-256e-9dbf58785216"
    flavor_id =  rc_options[:flavor_id] || "4437ac6c-9fe3-477a-57ec-60a5a45896a4"
     = "8bec6f15-e2b8-44fc-a8f3-a022b2873440"
    user_id = Fog::Cloudstack.uuid
    domain_id = Fog::Cloudstack.uuid
    network_id = (Array(rc_options[:network_ids]) || [Fog::Cloudstack.uuid]).first
    domain_name = "exampleorg"
    domain = {
      "id" => domain_id,
      "name" => domain_name,
      "level" => 1,
      "parentdomainid" => Fog::Cloudstack.uuid,
      "parentdomainname" => "ROOT",
      "haschild" => false,
      "path" => "ROOT/accountname"
    }
    user = {
      "id"          => user_id,
      "username"    => "username",
      "firstname"   => "Bob",
      "lastname"    => "Lastname",
      "email"       => "[email protected]",
      "created"     => "2012-05-14T16:25:17-0500",
      "state"       => "enabled",
      "account"     => "accountname",
      "accounttype" => 2,
      "domainid"    => domain_id,
      "domain"      => domain_name,
      "apikey"      => Fog::Cloudstack.uuid,
      "secretkey"   => Fog::Cloudstack.uuid
    }
    {
      :users    => { user_id    => user },
      :networks => { network_id => {
        "id"                          => network_id,
        "name"                        => "10.56.23.0/26",
        "displaytext"                 => "10.56.23.0/26",
        "broadcastdomaintype"         => "Vlan",
        "traffictype"                 => "Guest",
        "gateway"                     => "10.56.23.1",
        "netmask"                     => "255.255.255.192",
        "cidr"                        => "10.56.23.0/26",
        "zoneid"                      => zone_id,
        "zonename"                    => "zone-00",
        "networkofferingid"           => "af0c9bd5-a1b2-4ad0-bf4b-d6fa9b1b9d5b",
        "networkofferingname"         => "DefaultSharedNetworkOffering",
        "networkofferingdisplaytext"  => "Offering for Shared networks",
        "networkofferingavailability" => "Optional",
        "issystem"                    => false,
        "state"                       => "Setup",
        "related"                     => "86bbc9fc-d92e-49db-9fdc-296189090017",
        "broadcasturi"                => "vlan://800",
        "dns1"                        => "10.0.80.11",
        "type"                        => "Shared",
        "vlan"                        => "800",
        "acltype"                     => "Domain",
        "subdomainaccess"             => true,
        "domainid"                    => domain_id,
        "domain"                      => "ROOT",
        "service" => [
          {"name" => "UserData"},
          {"name" => "Dhcp"},
          {
            "name"       => "Dns",
            "capability" => [
              {
                "name"                       => "AllowDnsSuffixModification",
                "value"                      => "true",
                "canchooseservicecapability" => false
              }
            ]
        }],
        "networkdomain"     => "cs1cloud.internal",
        "physicalnetworkid" => "8f4627c5-1fdd-4504-8a92-f61b4e9cb3e3",
        "restartrequired"   => false,
        "specifyipranges"   => true}
      },
      :zones => { zone_id => {
        "id"                    => zone_id,
        "name"                  => "zone-00",
        "domainid"              => 1,
        "domainname"            => "ROOT",
        "networktype"           => "Advanced",
        "securitygroupsenabled" => false,
        "allocationstate"       => "Enabled",
        "zonetoken"             => Fog::Cloudstack.uuid,
        "dhcpprovider"          => "VirtualRouter"}},
      :images => { image_id => {
        "id"              => image_id,
        "name"            => "CentOS 5.6(64-bit) no GUI (XenServer)",
        "displaytext"     => "CentOS 5.6(64-bit) no GUI (XenServer)",
        "ispublic"        => true,
        "created"         => "2012-05-09T15:29:33-0500",
        "isready"         => true,
        "passwordenabled" => false,
        "format"          => "VHD",
        "isfeatured"      => true,
        "crossZones"      => true,
        "ostypeid"        => "a6a6694a-18f5-4765-8418-2b7a5f37cd0f",
        "ostypename"      => "CentOS 5.3 (64-bit)",
        "account"         => "system",
        "zoneid"          => zone_id,
        "zonename"        => "zone-00",
        "status"          => "Download Complete",
        "size"            => 21474836480,
        "templatetype"    => "BUILTIN",
        "domain"          => "ROOT",
        "domainid"        => "6023b6fe-5bef-4358-bc76-9f4e75afa52f",
        "isextractable"   => true,
        "checksum"        => "905cec879afd9c9d22ecc8036131a180"}},
      :flavors => { flavor_id => {
        "id"          => flavor_id,
        "name"        => "Medium Instance",
        "displaytext" => "Medium Instance",
        "cpunumber"   => 1,
        "cpuspeed"    => 1000,
        "memory"      => 1024,
        "created"     => "2012-05-09T14:48:36-0500",
        "storagetype" => "shared",
        "offerha"     => false,
        "limitcpuuse" => false,
        "issystem"    => false,
        "defaultuse"  => false}},
      :accounts => {  => {
        "id"                => ,
        "name"              => "accountname",
        "accounttype"       => 2,
        "domainid"          => domain_id,
        "domain"            => domain_name,
        "receivedbytes"     => 0,
        "sentbytes"         => 0,
        "vmlimit"           => "Unlimited",
        "vmtotal"           => 0,
        "vmavailable"       => "Unlimited",
        "iplimit"           => "Unlimited",
        "iptotal"           => 0,
        "ipavailable"       => "Unlimited",
        "volumelimit"       => "Unlimited",
        "volumetotal"       => 0,
        "volumeavailable"   => "Unlimited",
        "snapshotlimit"     => "Unlimited",
        "snapshottotal"     => 0,
        "snapshotavailable" => "Unlimited",
        "templatelimit"     => "Unlimited",
        "templatetotal"     => 0,
        "templateavailable" => "Unlimited",
        "vmstopped"         => 0,
        "vmrunning"         => 0,
        "projectlimit"      => "Unlimited",
        "projecttotal"      => 1,
        "projectavailable"  => "Unlimited",
        "networklimit"      => "Unlimited",
        "networktotal"      => 0,
        "networkavailable"  => "Unlimited",
        "state"             => "enabled",
        "user"              => [user]}
      },
      :domains => { domain_id => domain },
      :servers => {},
      :jobs    => {},
      :volumes => {}
    }
  end
end

.resetObject



417
418
419
# File 'lib/rackspace-fog/cloudstack/compute.rb', line 417

def self.reset
  @data = nil
end

Instance Method Details

#attach_volume(options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
# File 'lib/rackspace-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
   = self.data[:accounts].first
  user_id = self.data[:users].first

  job = {
    "accountid"     => ,
    "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 = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
# File 'lib/rackspace-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

#dataObject



421
422
423
# File 'lib/rackspace-fog/cloudstack/compute.rb', line 421

def data
  self.class.data
end

#delete_volume(options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rackspace-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 = {}) ⇒ Object



32
33
34
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rackspace-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
   = 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" => ,
    "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 = {}) ⇒ Object



21
22
23
24
25
# File 'lib/rackspace-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 = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rackspace-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
   = self.data[:accounts].first
  user_id = self.data[:users].first

  job = {
    "accountid"     => ,
    "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 = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/rackspace-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 = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rackspace-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 = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rackspace-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 = {}) ⇒ Object



19
20
21
22
# File 'lib/rackspace-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 = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rackspace-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 = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rackspace-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 = {}) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/rackspace-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_dataObject



425
426
427
# File 'lib/rackspace-fog/cloudstack/compute.rb', line 425

def reset_data
  self.class.data.delete(@cloudstack_api_key)
end