fog-vsphere 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +23 -0
  3. data/.travis.yml +32 -0
  4. data/CONTRIBUTING.md +18 -0
  5. data/CONTRIBUTORS.md +59 -0
  6. data/Gemfile +5 -0
  7. data/LICENSE.md +20 -0
  8. data/README.md +31 -0
  9. data/Rakefile +8 -0
  10. data/fog-vsphere.gemspec +32 -0
  11. data/gemfiles/Gemfile.1.9.2+ +8 -0
  12. data/gemfiles/Gemfile.1.9.2- +11 -0
  13. data/lib/fog/vsphere.rb +41 -0
  14. data/lib/fog/vsphere/compute.rb +473 -0
  15. data/lib/fog/vsphere/models/compute/cluster.rb +28 -0
  16. data/lib/fog/vsphere/models/compute/clusters.rb +22 -0
  17. data/lib/fog/vsphere/models/compute/customfield.rb +16 -0
  18. data/lib/fog/vsphere/models/compute/customfields.rb +23 -0
  19. data/lib/fog/vsphere/models/compute/customvalue.rb +14 -0
  20. data/lib/fog/vsphere/models/compute/customvalues.rb +33 -0
  21. data/lib/fog/vsphere/models/compute/datacenter.rb +44 -0
  22. data/lib/fog/vsphere/models/compute/datacenters.rb +19 -0
  23. data/lib/fog/vsphere/models/compute/datastore.rb +21 -0
  24. data/lib/fog/vsphere/models/compute/datastores.rb +21 -0
  25. data/lib/fog/vsphere/models/compute/folder.rb +24 -0
  26. data/lib/fog/vsphere/models/compute/folders.rb +23 -0
  27. data/lib/fog/vsphere/models/compute/interface.rb +91 -0
  28. data/lib/fog/vsphere/models/compute/interfaces.rb +66 -0
  29. data/lib/fog/vsphere/models/compute/interfacetype.rb +22 -0
  30. data/lib/fog/vsphere/models/compute/interfacetypes.rb +34 -0
  31. data/lib/fog/vsphere/models/compute/network.rb +18 -0
  32. data/lib/fog/vsphere/models/compute/networks.rb +22 -0
  33. data/lib/fog/vsphere/models/compute/process.rb +17 -0
  34. data/lib/fog/vsphere/models/compute/resource_pool.rb +19 -0
  35. data/lib/fog/vsphere/models/compute/resource_pools.rb +22 -0
  36. data/lib/fog/vsphere/models/compute/scsicontroller.rb +16 -0
  37. data/lib/fog/vsphere/models/compute/server.rb +325 -0
  38. data/lib/fog/vsphere/models/compute/servers.rb +36 -0
  39. data/lib/fog/vsphere/models/compute/servertype.rb +36 -0
  40. data/lib/fog/vsphere/models/compute/servertypes.rb +23 -0
  41. data/lib/fog/vsphere/models/compute/snapshot.rb +35 -0
  42. data/lib/fog/vsphere/models/compute/snapshots.rb +27 -0
  43. data/lib/fog/vsphere/models/compute/template.rb +11 -0
  44. data/lib/fog/vsphere/models/compute/templates.rb +19 -0
  45. data/lib/fog/vsphere/models/compute/volume.rb +99 -0
  46. data/lib/fog/vsphere/models/compute/volumes.rb +53 -0
  47. data/lib/fog/vsphere/requests/compute/cloudinit_to_customspec.rb +65 -0
  48. data/lib/fog/vsphere/requests/compute/create_folder.rb +22 -0
  49. data/lib/fog/vsphere/requests/compute/create_vm.rb +169 -0
  50. data/lib/fog/vsphere/requests/compute/current_time.rb +18 -0
  51. data/lib/fog/vsphere/requests/compute/get_cluster.rb +25 -0
  52. data/lib/fog/vsphere/requests/compute/get_compute_resource.rb +41 -0
  53. data/lib/fog/vsphere/requests/compute/get_datacenter.rb +31 -0
  54. data/lib/fog/vsphere/requests/compute/get_datastore.rb +30 -0
  55. data/lib/fog/vsphere/requests/compute/get_folder.rb +74 -0
  56. data/lib/fog/vsphere/requests/compute/get_interface_type.rb +15 -0
  57. data/lib/fog/vsphere/requests/compute/get_network.rb +59 -0
  58. data/lib/fog/vsphere/requests/compute/get_resource_pool.rb +26 -0
  59. data/lib/fog/vsphere/requests/compute/get_server_type.rb +32 -0
  60. data/lib/fog/vsphere/requests/compute/get_template.rb +16 -0
  61. data/lib/fog/vsphere/requests/compute/get_virtual_machine.rb +57 -0
  62. data/lib/fog/vsphere/requests/compute/get_vm_first_scsi_controller.rb +26 -0
  63. data/lib/fog/vsphere/requests/compute/list_child_snapshots.rb +71 -0
  64. data/lib/fog/vsphere/requests/compute/list_clusters.rb +72 -0
  65. data/lib/fog/vsphere/requests/compute/list_compute_resources.rb +92 -0
  66. data/lib/fog/vsphere/requests/compute/list_customfields.rb +21 -0
  67. data/lib/fog/vsphere/requests/compute/list_datacenters.rb +53 -0
  68. data/lib/fog/vsphere/requests/compute/list_datastores.rb +40 -0
  69. data/lib/fog/vsphere/requests/compute/list_folders.rb +44 -0
  70. data/lib/fog/vsphere/requests/compute/list_interface_types.rb +25 -0
  71. data/lib/fog/vsphere/requests/compute/list_networks.rb +38 -0
  72. data/lib/fog/vsphere/requests/compute/list_processes.rb +40 -0
  73. data/lib/fog/vsphere/requests/compute/list_resource_pools.rb +38 -0
  74. data/lib/fog/vsphere/requests/compute/list_server_types.rb +54 -0
  75. data/lib/fog/vsphere/requests/compute/list_templates.rb +48 -0
  76. data/lib/fog/vsphere/requests/compute/list_virtual_machines.rb +80 -0
  77. data/lib/fog/vsphere/requests/compute/list_vm_customvalues.rb +20 -0
  78. data/lib/fog/vsphere/requests/compute/list_vm_interfaces.rb +63 -0
  79. data/lib/fog/vsphere/requests/compute/list_vm_snapshots.rb +66 -0
  80. data/lib/fog/vsphere/requests/compute/list_vm_volumes.rb +52 -0
  81. data/lib/fog/vsphere/requests/compute/modify_vm_interface.rb +59 -0
  82. data/lib/fog/vsphere/requests/compute/modify_vm_volume.rb +25 -0
  83. data/lib/fog/vsphere/requests/compute/revert_to_snapshot.rb +30 -0
  84. data/lib/fog/vsphere/requests/compute/set_vm_customvalue.rb +17 -0
  85. data/lib/fog/vsphere/requests/compute/vm_clone.rb +727 -0
  86. data/lib/fog/vsphere/requests/compute/vm_config_vnc.rb +45 -0
  87. data/lib/fog/vsphere/requests/compute/vm_destroy.rb +23 -0
  88. data/lib/fog/vsphere/requests/compute/vm_execute.rb +47 -0
  89. data/lib/fog/vsphere/requests/compute/vm_migrate.rb +33 -0
  90. data/lib/fog/vsphere/requests/compute/vm_power_off.rb +39 -0
  91. data/lib/fog/vsphere/requests/compute/vm_power_on.rb +26 -0
  92. data/lib/fog/vsphere/requests/compute/vm_reboot.rb +31 -0
  93. data/lib/fog/vsphere/requests/compute/vm_reconfig_cpus.rb +23 -0
  94. data/lib/fog/vsphere/requests/compute/vm_reconfig_hardware.rb +24 -0
  95. data/lib/fog/vsphere/requests/compute/vm_reconfig_memory.rb +23 -0
  96. data/lib/fog/vsphere/requests/compute/vm_take_snapshot.rb +37 -0
  97. data/lib/fog/vsphere/version.rb +5 -0
  98. data/tests/compute_tests.rb +53 -0
  99. data/tests/helper.rb +8 -0
  100. data/tests/helpers/mock_helper.rb +9 -0
  101. data/tests/helpers/succeeds_helper.rb +9 -0
  102. data/tests/models/compute/server_tests.rb +70 -0
  103. data/tests/models/compute/servers_tests.rb +15 -0
  104. data/tests/requests/compute/current_time_tests.rb +12 -0
  105. data/tests/requests/compute/get_network_tests.rb +50 -0
  106. data/tests/requests/compute/list_child_snapshots_tests.rb +10 -0
  107. data/tests/requests/compute/list_clusters_tests.rb +11 -0
  108. data/tests/requests/compute/list_virtual_machines_tests.rb +38 -0
  109. data/tests/requests/compute/list_vm_snapshots_tests.rb +10 -0
  110. data/tests/requests/compute/revert_to_snapshot_tests.rb +15 -0
  111. data/tests/requests/compute/set_vm_customvalue_tests.rb +20 -0
  112. data/tests/requests/compute/vm_clone_tests.rb +50 -0
  113. data/tests/requests/compute/vm_config_vnc_tests.rb +19 -0
  114. data/tests/requests/compute/vm_destroy_tests.rb +17 -0
  115. data/tests/requests/compute/vm_migrate_tests.rb +16 -0
  116. data/tests/requests/compute/vm_power_off_tests.rb +26 -0
  117. data/tests/requests/compute/vm_power_on_tests.rb +17 -0
  118. data/tests/requests/compute/vm_reboot_tests.rb +26 -0
  119. data/tests/requests/compute/vm_reconfig_cpus_tests.rb +19 -0
  120. data/tests/requests/compute/vm_reconfig_hardware_tests.rb +19 -0
  121. data/tests/requests/compute/vm_reconfig_memory_tests.rb +19 -0
  122. data/tests/requests/compute/vm_take_snapshot_tests.rb +19 -0
  123. metadata +289 -0
@@ -0,0 +1,28 @@
1
+ module Fog
2
+ module Compute
3
+ class Vsphere
4
+ class Cluster < Fog::Model
5
+ identity :id
6
+
7
+ attribute :name
8
+ attribute :datacenter
9
+ attribute :num_host
10
+ attribute :num_cpu_cores
11
+ attribute :overall_status
12
+ attribute :full_path
13
+
14
+ def resource_pools(filters = { })
15
+ self.attributes[:resource_pools] ||= id.nil? ? [] : service.resource_pools({
16
+ :service => service,
17
+ :cluster => name,
18
+ :datacenter => datacenter
19
+ }.merge(filters))
20
+ end
21
+
22
+ def to_s
23
+ name
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ module Fog
2
+ module Compute
3
+ class Vsphere
4
+ class Clusters < Fog::Collection
5
+ autoload :Cluster, File.expand_path('../cluster', __FILE__)
6
+
7
+ model Fog::Compute::Vsphere::Cluster
8
+ attr_accessor :datacenter
9
+
10
+ def all(filters = {})
11
+ requires :datacenter
12
+ load service.list_clusters(filters.merge(:datacenter => datacenter))
13
+ end
14
+
15
+ def get(id)
16
+ requires :datacenter
17
+ new service.get_cluster(id, datacenter)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ module Fog
2
+ module Compute
3
+ class Vsphere
4
+ class Customfield < Fog::Model
5
+ identity :key
6
+
7
+ attribute :name
8
+ attribute :type
9
+
10
+ def to_s
11
+ name
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ module Fog
2
+ module Compute
3
+ class Vsphere
4
+ class Customfields < Fog::Collection
5
+ autoload :Customfield, File.expand_path('../customfield', __FILE__)
6
+
7
+ model Fog::Compute::Vsphere::Customfield
8
+
9
+ attr_accessor :vm
10
+
11
+ def all(filters = {})
12
+ load service.list_customfields()
13
+ end
14
+
15
+ def get(key)
16
+ load(service.list_customfields()).find do | cv |
17
+ cv.key == ((key.is_a? String) ? key.to_i : key)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,14 @@
1
+ module Fog
2
+ module Compute
3
+ class Vsphere
4
+ class Customvalue < Fog::Model
5
+ attribute :value
6
+ attribute :key
7
+
8
+ def to_s
9
+ value
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,33 @@
1
+ module Fog
2
+ module Compute
3
+ class Vsphere
4
+ class Customvalues < Fog::Collection
5
+ autoload :Customvalue, File.expand_path('../customvalue', __FILE__)
6
+
7
+ model Fog::Compute::Vsphere::Customvalue
8
+
9
+ attr_accessor :vm
10
+
11
+ def all(filters = {})
12
+ requires :vm
13
+ case vm
14
+ when Fog::Compute::Vsphere::Server
15
+ load service.list_vm_customvalues(vm.id)
16
+ else
17
+ raise 'customvalues should have vm'
18
+ end
19
+ end
20
+
21
+ def get(key)
22
+ requires :vm
23
+ case vm
24
+ when Fog::Compute::Vsphere::Server
25
+ load service.list_vm_customvalues(vm.id)
26
+ else
27
+ raise 'customvalues should have vm'
28
+ end.find { | cv | cv.key == key }
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,44 @@
1
+ module Fog
2
+ module Compute
3
+ class Vsphere
4
+ class Datacenter < Fog::Model
5
+ identity :id
6
+ attribute :name
7
+ attribute :path
8
+ attribute :status
9
+
10
+ def clusters filters = { }
11
+ service.clusters({ :datacenter => path.join("/") }.merge(filters))
12
+ end
13
+
14
+ def networks filters = { }
15
+ service.networks({ :datacenter => path.join("/") }.merge(filters))
16
+ end
17
+
18
+ def datastores filters = { }
19
+ service.datastores({ :datacenter => path.join("/") }.merge(filters))
20
+ end
21
+
22
+ def vm_folders filters = { }
23
+ service.folders({ :datacenter => path.join("/"), :type => :vm }.merge(filters))
24
+ end
25
+
26
+ def virtual_machines filters = {}
27
+ service.servers({ :datacenter => path.join("/") }.merge(filters))
28
+ end
29
+
30
+ def servertypes filters={}
31
+ service.servertypes({:datacenter => name }.merge(filters))
32
+ end
33
+
34
+ def customfields filters = {}
35
+ service.customfields({ :datacenter => path.join("/")}.merge(filters))
36
+ end
37
+
38
+ def to_s
39
+ name
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,19 @@
1
+ module Fog
2
+ module Compute
3
+ class Vsphere
4
+ class Datacenters < Fog::Collection
5
+ autoload :Datacenter, File.expand_path('../datacenter', __FILE__)
6
+
7
+ model Fog::Compute::Vsphere::Datacenter
8
+
9
+ def all(filters = {})
10
+ load service.list_datacenters(filters)
11
+ end
12
+
13
+ def get(name)
14
+ new service.get_datacenter(name)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ module Fog
2
+ module Compute
3
+ class Vsphere
4
+ class Datastore < Fog::Model
5
+ identity :id
6
+
7
+ attribute :name
8
+ attribute :datacenter
9
+ attribute :type
10
+ attribute :freespace
11
+ attribute :accessible # reachable by at least one hypervisor
12
+ attribute :capacity
13
+ attribute :uncommitted
14
+
15
+ def to_s
16
+ name
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module Fog
2
+ module Compute
3
+ class Vsphere
4
+ class Datastores < Fog::Collection
5
+ autoload :Datastore, File.expand_path('../datastore', __FILE__)
6
+
7
+ model Fog::Compute::Vsphere::Datastore
8
+ attr_accessor :datacenter
9
+
10
+ def all(filters = {})
11
+ load service.list_datastores(filters.merge(:datacenter => datacenter))
12
+ end
13
+
14
+ def get(id)
15
+ requires :datacenter
16
+ new service.get_datastore(id, datacenter)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ module Fog
2
+ module Compute
3
+ class Vsphere
4
+ class Folder < Fog::Model
5
+ identity :id
6
+
7
+ attribute :name
8
+ attribute :parent
9
+ attribute :datacenter
10
+ attribute :path
11
+ attribute :type
12
+
13
+ def vms
14
+ return [] if type.to_s != 'vm'
15
+ service.servers(:folder => path, :datacenter => datacenter)
16
+ end
17
+
18
+ def to_s
19
+ name
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ module Fog
2
+ module Compute
3
+ class Vsphere
4
+ class Folders < Fog::Collection
5
+ autoload :Folder, File.expand_path('../folder', __FILE__)
6
+
7
+ model Fog::Compute::Vsphere::Folder
8
+ attr_accessor :datacenter, :type, :path
9
+
10
+ def all(filters = { })
11
+ requires :datacenter
12
+ requires :type
13
+ load service.list_folders(filters.merge(:datacenter => datacenter, :type => type, :path => path))
14
+ end
15
+
16
+ def get(id)
17
+ requires :datacenter
18
+ new service.get_folder(id, datacenter, type)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,91 @@
1
+ module Fog
2
+ module Compute
3
+ class Vsphere
4
+ class Interface < Fog::Model
5
+ SAVE_MUTEX = Mutex.new
6
+
7
+ identity :mac
8
+ alias_method :id, :mac
9
+
10
+ attribute :network
11
+ attribute :name
12
+ attribute :status
13
+ attribute :summary
14
+ attribute :type
15
+ attribute :key
16
+ attribute :virtualswitch
17
+ attribute :server_id
18
+
19
+ def initialize(attributes = {})
20
+ # Assign server first to prevent race condition with persisted?
21
+ self.server_id = attributes.delete(:server_id)
22
+
23
+ if attributes.key? :type then
24
+ if attributes[:type].is_a? String then
25
+ attributes[:type] = Fog::Vsphere.class_from_string(attributes[:type], "RbVmomi::VIM")
26
+ end
27
+ else
28
+ attributes[:type] = Fog::Vsphere.class_from_string("VirtualE1000", "RbVmomi::VIM")
29
+ end
30
+
31
+ super defaults.merge(attributes)
32
+ end
33
+
34
+ def to_s
35
+ name
36
+ end
37
+
38
+ def server
39
+ requires :server_id
40
+ service.servers.get(server_id)
41
+ end
42
+
43
+ def destroy
44
+ requires :server_id, :key, :type
45
+
46
+ service.destroy_vm_interface(server_id, :key => key, :type => type)
47
+ end
48
+
49
+ def save
50
+ raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
51
+ requires :server_id, :type, :network
52
+
53
+ # Our approach of finding the newly created interface is rough. We assume that the :key value always increments
54
+ # and thus the highest :key value must correspond to the created interface. Since this has an inherent race
55
+ # condition we need to gate the saves.
56
+ SAVE_MUTEX.synchronize do
57
+ data = service.add_vm_interface(server_id, attributes)
58
+
59
+ if data['task_state'] == 'success'
60
+ # We have to query vSphere to get the interface attributes since the task handle doesn't include that info.
61
+ created = server.interfaces.all.sort_by(&:key).last
62
+
63
+ self.mac = created.mac
64
+ self.name = created.name
65
+ self.status = created.status
66
+ self.summary = created.summary
67
+ self.key = created.key
68
+ self.virtualswitch = created.virtualswitch
69
+
70
+ true
71
+ else
72
+ false
73
+ end
74
+ end
75
+ end
76
+
77
+ private
78
+
79
+ def defaults
80
+ default_type=Fog.credentials[:default_nic_type] || RbVmomi::VIM::VirtualE1000
81
+ {
82
+ :name=>"Network adapter",
83
+ :network=>"VM Network",
84
+ :summary=>"VM Network",
85
+ :type=> Fog::Vsphere.class_from_string(default_type, "RbVmomi::VIM"),
86
+ }
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,66 @@
1
+ module Fog
2
+ module Compute
3
+ class Vsphere
4
+ class Interfaces < Fog::Collection
5
+ autoload :Interface, File.expand_path('../interface', __FILE__)
6
+
7
+ model Fog::Compute::Vsphere::Interface
8
+
9
+ attribute :server_id
10
+
11
+ def all(filters = {})
12
+ requires :server_id
13
+
14
+ case server
15
+ when Fog::Compute::Vsphere::Server
16
+ load service.list_vm_interfaces(server.id)
17
+ when Fog::Compute::Vsphere::Template
18
+ load service.list_template_interfaces(server.id)
19
+ else
20
+ raise 'interfaces should have vm or template'
21
+ end
22
+
23
+ self.each { |interface| interface.server_id = server.id }
24
+ self
25
+ end
26
+
27
+ def get(id)
28
+ requires :server_id
29
+
30
+ case server
31
+ when Fog::Compute::Vsphere::Server
32
+ interface = service.get_vm_interface(server.id, :key => id, :mac=> id, :name => id)
33
+ when Fog::Compute::Vsphere::Template
34
+ interface = service.get_template_interfaces(server.id, :key => id, :mac=> id, :name => id)
35
+ else
36
+
37
+ raise 'interfaces should have vm or template'
38
+ end
39
+
40
+ if interface
41
+ Fog::Compute::Vsphere::Interface.new(interface.merge(:server_id => server.id, :service => service))
42
+ else
43
+ nil
44
+ end
45
+ end
46
+
47
+ def new(attributes = {})
48
+ if server_id
49
+ super({ :server_id => server_id }.merge(attributes))
50
+ else
51
+ super
52
+ end
53
+ end
54
+
55
+ def server
56
+ return nil if server_id.nil?
57
+ service.servers.get(server_id)
58
+ end
59
+
60
+ def server=(new_server)
61
+ server_id = new_server.id
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end