Class: Fog::Compute::Vsphere::Mock

Inherits:
Object
  • Object
show all
Includes:
Shared
Defined in:
lib/fog/vsphere/compute.rb,
lib/fog/vsphere/requests/compute/vm_clone.rb,
lib/fog/vsphere/requests/compute/create_vm.rb,
lib/fog/vsphere/requests/compute/vm_reboot.rb,
lib/fog/vsphere/requests/compute/get_folder.rb,
lib/fog/vsphere/requests/compute/vm_destroy.rb,
lib/fog/vsphere/requests/compute/vm_migrate.rb,
lib/fog/vsphere/requests/compute/get_cluster.rb,
lib/fog/vsphere/requests/compute/get_network.rb,
lib/fog/vsphere/requests/compute/vm_power_on.rb,
lib/fog/vsphere/requests/compute/current_time.rb,
lib/fog/vsphere/requests/compute/get_template.rb,
lib/fog/vsphere/requests/compute/vm_power_off.rb,
lib/fog/vsphere/requests/compute/get_datastore.rb,
lib/fog/vsphere/requests/compute/list_clusters.rb,
lib/fog/vsphere/requests/compute/list_networks.rb,
lib/fog/vsphere/requests/compute/vm_config_vnc.rb,
lib/fog/vsphere/requests/compute/get_datacenter.rb,
lib/fog/vsphere/requests/compute/list_templates.rb,
lib/fog/vsphere/requests/compute/get_server_type.rb,
lib/fog/vsphere/requests/compute/list_datastores.rb,
lib/fog/vsphere/requests/compute/list_vm_volumes.rb,
lib/fog/vsphere/requests/compute/list_datacenters.rb,
lib/fog/vsphere/requests/compute/modify_vm_volume.rb,
lib/fog/vsphere/requests/compute/vm_reconfig_cpus.rb,
lib/fog/vsphere/requests/compute/get_resource_pool.rb,
lib/fog/vsphere/requests/compute/list_customfields.rb,
lib/fog/vsphere/requests/compute/list_server_types.rb,
lib/fog/vsphere/requests/compute/list_vm_interfaces.rb,
lib/fog/vsphere/requests/compute/set_vm_customvalue.rb,
lib/fog/vsphere/requests/compute/vm_reconfig_memory.rb,
lib/fog/vsphere/requests/compute/get_virtual_machine.rb,
lib/fog/vsphere/requests/compute/list_resource_pools.rb,
lib/fog/vsphere/requests/compute/modify_vm_interface.rb,
lib/fog/vsphere/requests/compute/list_vm_customvalues.rb,
lib/fog/vsphere/requests/compute/vm_reconfig_hardware.rb,
lib/fog/vsphere/requests/compute/list_virtual_machines.rb,
lib/fog/vsphere/requests/compute/get_vm_first_scsi_controller.rb

Instance Attribute Summary

Attributes included from Shared

#vsphere_is_vcenter, #vsphere_rev, #vsphere_server, #vsphere_username

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



316
317
318
319
320
321
322
323
324
# File 'lib/fog/vsphere/compute.rb', line 316

def initialize(options={})
  require 'rbvmomi'
  @vsphere_username = options[:vsphere_username]
  @vsphere_password = 'REDACTED'
  @vsphere_server   = options[:vsphere_server]
  @vsphere_expected_pubkey_hash = options[:vsphere_expected_pubkey_hash]
  @vsphere_is_vcenter = true
  @vsphere_rev = '4.0'
end

Class Method Details

.dataObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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
# File 'lib/fog/vsphere/compute.rb', line 196

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      :servers => {
        "5032c8a5-9c5e-ba7a-3804-832a03e16381" => {
         "resource_pool"    => "Resources",
         "memory_mb"        => 2196,
         "mac_addresses"    => { "Network adapter 1" => "00:50:56:a9:00:28" },
         "power_state"      => "poweredOn",
         "cpus"             => 1,
         "hostname"         => "dhcp75-197.virt.bos.redhat.com",
         "mo_ref"           => "vm-562",
         "connection_state" => "connected",
         "overall_status"   => "green",
         "datacenter"       => "Solutions",
         "volumes"          =>
            [{
              "id"        => "6000C29c-a47d-4cd9-5249-c371de775f06",
              "datastore" => "Storage1",
              "mode"      => "persistent",
              "size"      => 8388608,
              "thin"      => true,
              "name"      => "Hard disk 1",
              "filename"  => "[Storage1] rhel6-mfojtik/rhel6-mfojtik.vmdk",
              "size_gb"   => 8
             }],
         "interfaces"       =>
            [{"mac"     => "00:50:56:a9:00:28",
              "network" => "VM Network",
              "name"    => "Network adapter 1",
              "status"  => "ok",
              "summary" => "VM Network",
             }],
         "hypervisor"       => "gunab.puppetlabs.lan",
         "guest_id"         => "rhel6_64Guest",
         "tools_state"      => "toolsOk",
         "cluster"          => "Solutionscluster",
         "name"             => "rhel64",
         "operatingsystem"  => "Red Hat Enterprise Linux 6 (64-bit)",
         "path"             => "/Datacenters/Solutions/vm",
         "uuid"             => "4229f0e9-bfdc-d9a7-7bac-12070772e6dc",
         "instance_uuid"    => "5032c8a5-9c5e-ba7a-3804-832a03e16381",
         "id"               => "5032c8a5-9c5e-ba7a-3804-832a03e16381",
         "tools_version"    => "guestToolsUnmanaged",
         "ipaddress"        => "192.168.100.184",
         "template"         => false
        },
        "502916a3-b42e-17c7-43ce-b3206e9524dc" => {
         "resource_pool"    => "Resources",
         "memory_mb"        => 512,
         "power_state"      => "poweredOn",
         "mac_addresses"    => { "Network adapter 1" => "00:50:56:a9:00:00" },
         "hostname"         => nil,
         "cpus"             => 1,
         "connection_state" => "connected",
         "mo_ref"           => "vm-621",
         "overall_status"   => "green",
         "datacenter"       => "Solutions",
         "volumes"          =>
            [{"thin"      => false,
              "size_gb"   => 10,
              "datastore" => "datastore1",
              "filename"  => "[datastore1] i-1342439683/i-1342439683.vmdk",
              "size"      => 10485762,
              "name"      => "Hard disk 1",
              "mode"      => "persistent",
              "id"        => "6000C29b-f364-d073-8316-8e98ac0a0eae" }],
         "interfaces"       =>
            [{ "summary" => "VM Network",
              "mac"     => "00:50:56:a9:00:00",
              "status"  => "ok",
              "network" => "VM Network",
              "name"    => "Network adapter 1" }],
         "hypervisor"       => "gunab.puppetlabs.lan",
         "guest_id"         => nil,
         "cluster"          => "Solutionscluster",
         "tools_state"      => "toolsNotInstalled",
         "name"             => "i-1342439683",
         "operatingsystem"  => nil,
         "path"             => "/",
         "tools_version"    => "guestToolsNotInstalled",
         "uuid"             => "4229e0de-30cb-ceb2-21f9-4d8d8beabb52",
         "instance_uuid"    => "502916a3-b42e-17c7-43ce-b3206e9524dc",
         "id"               => "502916a3-b42e-17c7-43ce-b3206e9524dc",
         "ipaddress"        => nil,
         "template"         => false
        },
        "5029c440-85ee-c2a1-e9dd-b63e39364603" => {
         "resource_pool"    => "Resources",
         "memory_mb"        => 2196,
         "power_state"      => "poweredOn",
         "mac_addresses"    => { "Network adapter 1" => "00:50:56:b2:00:af" },
         "hostname"         => "centos56gm.localdomain",
         "cpus"             => 1,
         "connection_state" => "connected",
         "mo_ref"           => "vm-715",
         "overall_status"   => "green",
         "datacenter"       => "Solutions",
         "hypervisor"       => "gunab.puppetlabs.lan",
         "guest_id"         => "rhel6_64Guest",
         "cluster"          => "Solutionscluster",
         "tools_state"      => "toolsOk",
         "name"             => "jefftest",
         "operatingsystem"  => "Red Hat Enterprise Linux 6 (64-bit)",
         "path"             => "/",
         "tools_version"    => "guestToolsUnmanaged",
         "ipaddress"        => "192.168.100.187",
         "uuid"             => "42329da7-e8ab-29ec-1892-d6a4a964912a",
         "instance_uuid"    => "5029c440-85ee-c2a1-e9dd-b63e39364603",
         "id"               => "5029c440-85ee-c2a1-e9dd-b63e39364603",
         "template"         => false
        }
      },
      :datacenters => {
        "Solutions" => {:name => "Solutions", :status => "grey"}
      }
    }
  end
end

Instance Method Details

#add_vm_interface(vmid, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
# File 'lib/fog/vsphere/requests/compute/modify_vm_interface.rb', line 47

def add_vm_interface(vmid, options = {})
  raise ArgumentError, "instance id is a required parameter" unless vmid
  raise ArgumentError, "interface is a required parameter" unless options and options[:interface]
  true
end

#add_vm_volume(volume) ⇒ Object



16
17
18
# File 'lib/fog/vsphere/requests/compute/modify_vm_volume.rb', line 16

def add_vm_volume(volume)
  true
end

#create_vm(attributes = { }) ⇒ Object



156
157
# File 'lib/fog/vsphere/requests/compute/create_vm.rb', line 156

def create_vm attributes = { }
end

#current_timeObject



15
16
17
# File 'lib/fog/vsphere/requests/compute/current_time.rb', line 15

def current_time
  { 'current_time' => Time.now.utc }
end

#dataObject



326
327
328
# File 'lib/fog/vsphere/compute.rb', line 326

def data
  self.class.data[@vsphere_username]
end

#destroy_vm_interface(vmid, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


53
54
55
56
57
# File 'lib/fog/vsphere/requests/compute/modify_vm_interface.rb', line 53

def destroy_vm_interface(vmid, options = {})
  raise ArgumentError, "instance id is a required parameter" unless vmid
  raise ArgumentError, "interface is a required parameter" unless options and options[:interface]
  true
end

#destroy_vm_volume(volume) ⇒ Object



20
21
22
# File 'lib/fog/vsphere/requests/compute/modify_vm_volume.rb', line 20

def destroy_vm_volume(volume)
  true
end

#get_cluster(id) ⇒ Object



20
21
# File 'lib/fog/vsphere/requests/compute/get_cluster.rb', line 20

def get_cluster(id)
end

#get_datacenter(name) ⇒ Object



23
24
25
26
27
# File 'lib/fog/vsphere/requests/compute/get_datacenter.rb', line 23

def get_datacenter name
  dc = self.data[:datacenters][name]
  raise(Fog::Compute::Vsphere::NotFound) unless dc
  dc
end

#get_datastore(name, datacenter_name) ⇒ Object



25
26
# File 'lib/fog/vsphere/requests/compute/get_datastore.rb', line 25

def get_datastore(name, datacenter_name)
end

#get_folder(path, filters = { }) ⇒ Object



69
70
# File 'lib/fog/vsphere/requests/compute/get_folder.rb', line 69

def get_folder(path, filters = { })
end

#get_folder_path(folder, root = nil) ⇒ Object



59
60
61
# File 'lib/fog/vsphere/requests/compute/list_virtual_machines.rb', line 59

def get_folder_path(folder, root = nil)
  nil
end

#get_network(id) ⇒ Object



25
26
# File 'lib/fog/vsphere/requests/compute/get_network.rb', line 25

def get_network(id)
end

#get_resource_pool(name, cluster_name, datacenter_name) ⇒ Object



21
22
# File 'lib/fog/vsphere/requests/compute/get_resource_pool.rb', line 21

def get_resource_pool(name, cluster_name, datacenter_name)
end

#get_server_type(id) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/fog/vsphere/requests/compute/get_server_type.rb', line 22

def get_server_type(id)
  {:id=>"rhel6Guest", 
    :name=>"rhel6Guest", 
    :family=>"linuxGuest", 
    :fullname=>"Red Hat Enterprise Linux 6 (32-Bit)", 
    :datacenter=>"Solutions"}          
end

#get_template(id, datacenter_name = nil) ⇒ Object



11
12
# File 'lib/fog/vsphere/requests/compute/get_template.rb', line 11

def get_template(id, datacenter_name = nil)
end

#get_virtual_machine(id, datacenter_name = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/fog/vsphere/requests/compute/get_virtual_machine.rb', line 32

def get_virtual_machine(id, datacenter_name = nil)
  if is_uuid?(id)
    vm = list_virtual_machines({ 'instance_uuid' => id, 'datacenter' => datacenter_name }).first
  else
    # try to find based on VM name. May need to handle the path of the VM
    vm = list_virtual_machines({ 'name' => id, 'datacenter' => datacenter_name }).first
  end
  vm ? vm : raise(Fog::Compute::Vsphere::NotFound, "#{id} was not found")
end

#get_vm_first_scsi_controller(vm_id) ⇒ Object



21
22
# File 'lib/fog/vsphere/requests/compute/get_vm_first_scsi_controller.rb', line 21

def get_vm_first_scsi_controller(vm_id)
end

#list_clusters(filters = { }) ⇒ Object



32
33
# File 'lib/fog/vsphere/requests/compute/list_clusters.rb', line 32

def list_clusters(filters = { })
end

#list_datacenters(filters = {}) ⇒ Object



48
49
50
# File 'lib/fog/vsphere/requests/compute/list_datacenters.rb', line 48

def list_datacenters filters = {}
  [ {:name => "Solutions", :status => "grey"}, {:name => "Solutions2", :status => "green" }]
end

#list_datastores(datacenter_name) ⇒ Object



35
36
37
# File 'lib/fog/vsphere/requests/compute/list_datastores.rb', line 35

def list_datastores(datacenter_name)
  []
end

#list_networks(datacenter_name) ⇒ Object



32
33
34
# File 'lib/fog/vsphere/requests/compute/list_networks.rb', line 32

def list_networks(datacenter_name)
  []
end

#list_resource_pools(filters = { }) ⇒ Object



34
35
# File 'lib/fog/vsphere/requests/compute/list_resource_pools.rb', line 34

def list_resource_pools(filters = { })
end

#list_server_types(datacenter_name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fog/vsphere/requests/compute/list_server_types.rb', line 39

def list_server_types(datacenter_name)
  [{:id=>"rhel6Guest", 
    :name=>"rhel6Guest", 
    :family=>"linuxGuest", 
    :fullname=>"Red Hat Enterprise Linux 6 (32-Bit)", 
    :datacenter=>"Solutions"}, 
   {:id=>"rhel5_64Guest", 
    :name=>"rhel5_64Guest", 
    :family=>"linuxGuest", 
    :fullname=>"Red Hat Enterprise Linux 5 (64-Bit)", 
    :datacenter=>"Solutions"}]
end

#list_templates(filters = { }) ⇒ Object



44
45
# File 'lib/fog/vsphere/requests/compute/list_templates.rb', line 44

def list_templates(filters = { })
end

#list_virtual_machines(options = { }) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/fog/vsphere/requests/compute/list_virtual_machines.rb', line 63

def list_virtual_machines(options = { })
  if options['instance_uuid'].nil? and options['mo_ref'].nil?
    self.data[:servers].values
  elsif !options['instance_uuid'].nil?
    server = self.data[:servers][options['instance_uuid']]
    server.nil? ? [] : [server]
  else
    self.data[:servers].values.select{|vm| vm['mo_ref'] == options['mo_ref']}
  end
end

#list_vm_customfields(vm_id) ⇒ Object



17
18
# File 'lib/fog/vsphere/requests/compute/list_customfields.rb', line 17

def list_vm_customfields()
end

#list_vm_interfaces(vm_id) ⇒ Object



60
61
# File 'lib/fog/vsphere/requests/compute/list_vm_interfaces.rb', line 60

def list_vm_interfaces(vm_id)
end

#list_vm_volumes(vm_id) ⇒ Object



48
49
# File 'lib/fog/vsphere/requests/compute/list_vm_volumes.rb', line 48

def list_vm_volumes(vm_id)
end

#reset_dataObject



330
331
332
# File 'lib/fog/vsphere/compute.rb', line 330

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

#set_vm_customvalue(vm_id, key, value) ⇒ Object



12
13
14
# File 'lib/fog/vsphere/requests/compute/set_vm_customvalue.rb', line 12

def set_vm_customvalue(vm_id, key, value)
  nil
end

#vm_clone(options = {}) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/fog/vsphere/requests/compute/vm_clone.rb', line 280

def vm_clone(options = {})
  # Option handling TODO Needs better method of checking
  options = vm_clone_check_options(options)
  notfound = lambda { raise Fog::Compute::Vsphere::NotFound, "Could not find VM template" }
  template = list_virtual_machines.find(notfound) do |vm|
    vm['name'] == options['template_path'].split("/")[-1]
  end

  # generate a random id
  id = [8,4,4,4,12].map{|i| Fog::Mock.random_hex(i)}.join("-")
  new_vm = template.clone.merge({
    "name" => options['name'],
    "id" => id,
    "instance_uuid" => id,
    "path" => "/Datacenters/#{options['datacenter']}/#{options['dest_folder'] ? options['dest_folder']+"/" : ""}#{options['name']}"
  })
  self.data[:servers][id] = new_vm

  {
    'vm_ref'   => "vm-#{Fog::Mock.random_numbers(3)}",
    'new_vm'   => new_vm,
    'task_ref' => "task-#{Fog::Mock.random_numbers(4)}",
  }
end

#vm_config_vnc(options = { }) ⇒ Object

Raises:

  • (ArgumentError)


36
37
38
39
# File 'lib/fog/vsphere/requests/compute/vm_config_vnc.rb', line 36

def vm_config_vnc(options = { })
  raise ArgumentError, "instance_uuid is a required parameter" unless options.has_key? 'instance_uuid'
  { 'task_state' => 'success' }
end

#vm_destroy(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
# File 'lib/fog/vsphere/requests/compute/vm_destroy.rb', line 19

def vm_destroy(options = {})
  raise ArgumentError, "instance_uuid is a required parameter" unless options.has_key? 'instance_uuid'
  { 'task_state' => 'success' }
end

#vm_get_vnc(uuid) ⇒ Object



41
42
43
# File 'lib/fog/vsphere/requests/compute/vm_config_vnc.rb', line 41

def vm_get_vnc uuid
  {:password => 'secret', :port => '5900', :enabled => 'true'}
end

#vm_migrate(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


28
29
30
31
32
# File 'lib/fog/vsphere/requests/compute/vm_migrate.rb', line 28

def vm_migrate(options = {})
  priority = options['priority'].nil? ? 'defaultPriority' : options["priority"]
  raise ArgumentError, "instance_uuid is a required parameter" unless options.has_key? 'instance_uuid'
  { 'task_state' => 'success' }
end

#vm_power_off(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
# File 'lib/fog/vsphere/requests/compute/vm_power_off.rb', line 30

def vm_power_off(options = {})
  raise ArgumentError, "instance_uuid is a required parameter" unless options.has_key? 'instance_uuid'
  vm = get_virtual_machine(options['instance_uuid'])
  vm["power_state"] = "poweredOff"
  {
    'task_state'     => "running",
    'power_off_type' => options['force'] ? 'cut_power' : 'shutdown_guest',
  }
end

#vm_power_on(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
# File 'lib/fog/vsphere/requests/compute/vm_power_on.rb', line 22

def vm_power_on(options = {})
  raise ArgumentError, "instance_uuid is a required parameter" unless options.has_key? 'instance_uuid'
  { 'task_state' => 'success' }
end

#vm_reboot(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
# File 'lib/fog/vsphere/requests/compute/vm_reboot.rb', line 27

def vm_reboot(options = {})
  raise ArgumentError, "instance_uuid is a required parameter" unless options.has_key? 'instance_uuid'
  { 'task_state'     => "running", 'reboot_type' => options['force'] ? 'reset_power' : 'reboot_guest' }
end

#vm_reconfig_cpus(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
# File 'lib/fog/vsphere/requests/compute/vm_reconfig_cpus.rb', line 14

def vm_reconfig_cpus(options = {})
  raise ArgumentError, "cpus is a required parameter" unless options.has_key? 'cpus'
  raise ArgumentError, "instance_uuid is a required parameter" unless options.has_key? 'instance_uuid'
  hardware_spec={'numCPUs' => options['cpus'], 'numCoresPerSocket' => options['corespersocket']}
  vm_reconfig_hardware('instance_uuid' => options['instance_uuid'], 'hardware_spec' => hardware_spec )
end

#vm_reconfig_hardware(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
# File 'lib/fog/vsphere/requests/compute/vm_reconfig_hardware.rb', line 16

def vm_reconfig_hardware(options = {})
  raise ArgumentError, "hardware_spec is a required parameter" unless options.has_key? 'hardware_spec'
  raise ArgumentError, "instance_uuid is a required parameter" unless options.has_key? 'instance_uuid'
  { 'task_state' => 'success' }
end

#vm_reconfig_memory(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
# File 'lib/fog/vsphere/requests/compute/vm_reconfig_memory.rb', line 14

def vm_reconfig_memory(options = {})
  raise ArgumentError, "memory is a required parameter" unless options.has_key? 'memory'
  raise ArgumentError, "instance_uuid is a required parameter" unless options.has_key? 'instance_uuid'
  hardware_spec={'memoryMB' => options['memory']}
  vm_reconfig_hardware('instance_uuid' => options['instance_uuid'], 'hardware_spec' => hardware_spec )
end