kitchen-vcenter 1.4.2 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7f49e8fc98358fba0a3d791d90cd7114dca3ebea36114c78243bd8290198660c
4
- data.tar.gz: d495c5cac6372f7afa189c27d6e7079c2bfc69b73bf5ff4ec82d6f4d02fdf4b8
3
+ metadata.gz: 31833a2dfb60dba469a3c03aa8261cc4639760770cb33b913909950009e5603b
4
+ data.tar.gz: c0c663f9b4ac33fdc505dab121ba33112d22b7cc9df59213a1aa1858c1191aa4
5
5
  SHA512:
6
- metadata.gz: 79182163a7adc1acf518959674e4c07833ba83e5e7de7ce46805887c68b998d9987a087c0e0897b14c0d6042df66620445091d742569e56de122fde30c770a23
7
- data.tar.gz: bdd5b03a192e3e71ff84017ffcd7ffa2314e4eef3bd82042c8822f18688061aba854951f0ab524ab5468756e2e87a63f9eed0996f5ac8aee5e3c4d3ae7e343b0
6
+ metadata.gz: 4c5ef3e005c6f281b651c197a46a1bb84d38c3261973d5dc909d1924afd398a8c4debc2746f0de3f84d5e2dcd294a3e0537fb8df4210733abd9e7eb94ff9a40c
7
+ data.tar.gz: 66bc5bcb9263cbd8ab91757555f32daa26b0b2148c792ab1dd62613053bab7efdfa83673d6afbcc0e97621b393d7fb2f3339b853856f958d158f718cc16f43af
@@ -20,5 +20,5 @@
20
20
  # The main kitchen-vcenter module
21
21
  module KitchenVcenter
22
22
  # The version of this version of test-kitchen we assume enterprises want.
23
- VERSION = "1.4.2"
23
+ VERSION = "1.5.0"
24
24
  end
@@ -24,6 +24,7 @@ require "base"
24
24
  require "lookup_service_helper"
25
25
  require "vapi"
26
26
  require "com/vmware/cis"
27
+ require "com/vmware/cis/tagging"
27
28
  require "com/vmware/vcenter"
28
29
  require "com/vmware/vcenter/vm"
29
30
  require "support/clone_vm"
@@ -53,27 +54,14 @@ module Kitchen
53
54
  default_config :clone_type, :full
54
55
  default_config :cluster, nil
55
56
  default_config :lookup_service_host, nil
57
+ default_config :network_name, nil
58
+ default_config :tags, nil
56
59
 
57
60
  # The main create method
58
61
  #
59
62
  # @param [Object] state is the state of the vm
60
63
  def create(state)
61
- # Configure the hash for use when connecting for cloning a machine
62
- @connection_options = {
63
- user: config[:vcenter_username],
64
- password: config[:vcenter_password],
65
- insecure: config[:vcenter_disable_ssl_verify] ? true : false,
66
- host: config[:vcenter_host],
67
- rev: config[:clone_type] == "instant" ? "6.7" : nil,
68
- }
69
-
70
- # If the vm_name has not been set then set it now based on the suite, platform and a random number
71
- if config[:vm_name].nil?
72
- config[:vm_name] = format("%s-%s-%s", instance.suite.name, instance.platform.name, SecureRandom.hex(4))
73
- end
74
-
75
- raise format("Cannot specify both cluster and resource_pool") if !config[:cluster].nil? && !config[:resource_pool].nil?
76
-
64
+ save_and_validate_parameters
77
65
  connect
78
66
 
79
67
  # Using the clone class, create a machine for TK
@@ -94,6 +82,9 @@ module Kitchen
94
82
  # Check that the datacenter exists
95
83
  datacenter_exists?(config[:datacenter])
96
84
 
85
+ # Check if network exists, if to be changed
86
+ network_exists?(config[:network_name]) unless config[:network_name].nil?
87
+
97
88
  # Same thing needs to happen with the folder name if it has been set
98
89
  unless config[:folder].nil?
99
90
  config[:folder] = {
@@ -116,12 +107,35 @@ module Kitchen
116
107
  folder: config[:folder],
117
108
  resource_pool: config[:resource_pool],
118
109
  clone_type: config[:clone_type],
110
+ network_name: config[:network_name],
119
111
  }
120
112
 
121
113
  # Create an object from which the clone operation can be called
122
114
  clone_obj = Support::CloneVm.new(connection_options, options)
123
115
  state[:hostname] = clone_obj.clone
124
116
  state[:vm_name] = config[:vm_name]
117
+
118
+ unless config[:tags].nil? || config[:tags].empty?
119
+ valid_tags = {}
120
+ vm_tags = Com::Vmware::Cis::Tagging::Tag.new(vapi_config)
121
+ vm_tags.list.each do |uid|
122
+ tag = vm_tags.get(uid)
123
+ valid_tags[tag.name] = tag.id
124
+ end
125
+
126
+ # Error out on undefined tags
127
+ invalid = config[:tags] - valid_tags.keys
128
+ raise format("Specified tag(s) %s not valid", invalid.join(",")) unless invalid.empty?
129
+
130
+ tag_service = Com::Vmware::Cis::Tagging::TagAssociation.new(vapi_config)
131
+
132
+ # calls needs a DynamicID object which we construct from type and mobID
133
+ mobid = get_vm(config[:vm_name]).vm
134
+ dynamic_id = Com::Vmware::Vapi::Std::DynamicID.new(type: "VirtualMachine", id: mobid)
135
+
136
+ tag_ids = config[:tags].map { |name| valid_tags[name] }
137
+ tag_service.attach_multiple_tags_to_object(dynamic_id, tag_ids)
138
+ end
125
139
  end
126
140
 
127
141
  # The main destroy method
@@ -130,6 +144,7 @@ module Kitchen
130
144
  def destroy(state)
131
145
  return if state[:vm_name].nil?
132
146
 
147
+ save_and_validate_parameters
133
148
  connect
134
149
  vm = get_vm(state[:vm_name])
135
150
 
@@ -147,6 +162,26 @@ module Kitchen
147
162
 
148
163
  private
149
164
 
165
+ # Helper method for storing and validating configuration parameters
166
+ #
167
+ def save_and_validate_parameters
168
+ # Configure the hash for use when connecting for cloning a machine
169
+ @connection_options = {
170
+ user: config[:vcenter_username],
171
+ password: config[:vcenter_password],
172
+ insecure: config[:vcenter_disable_ssl_verify] ? true : false,
173
+ host: config[:vcenter_host],
174
+ rev: config[:clone_type] == "instant" ? "6.7" : nil,
175
+ }
176
+
177
+ # If the vm_name has not been set then set it now based on the suite, platform and a random number
178
+ if config[:vm_name].nil?
179
+ config[:vm_name] = format("%s-%s-%s", instance.suite.name, instance.platform.name, SecureRandom.hex(4))
180
+ end
181
+
182
+ raise format("Cannot specify both cluster and resource_pool") if !config[:cluster].nil? && !config[:resource_pool].nil?
183
+ end
184
+
150
185
  # A helper method to validate the state
151
186
  #
152
187
  # @param [Object] state is the state of the vm
@@ -167,6 +202,17 @@ module Kitchen
167
202
  raise format("Unable to find data center: %s", name) if dc.empty?
168
203
  end
169
204
 
205
+ # Checks if a network exists or not
206
+ #
207
+ # @param [name] name is the name of the Network
208
+ def network_exists?(name)
209
+ net_obj = Com::Vmware::Vcenter::Network.new(vapi_config)
210
+ filter = Com::Vmware::Vcenter::Network::FilterSpec.new(names: Set.new([name]))
211
+ net = net_obj.list(filter)
212
+
213
+ raise format("Unable to find target network: %s", name) if net.empty?
214
+ end
215
+
170
216
  # Validates the host name of the server you can connect to
171
217
  #
172
218
  # @param [name] name is the name of the host
@@ -14,8 +14,11 @@ class Support
14
14
  def clone
15
15
  # set the datacenter name
16
16
  dc = vim.serviceInstance.find_datacenter(options[:datacenter])
17
- src_vm = dc.find_vm(options[:template])
18
17
 
18
+ # reference template using full inventory path
19
+ root_folder = @vim.serviceInstance.content.rootFolder
20
+ inventory_path = format("/%s/vm/%s", options[:datacenter], options[:template])
21
+ src_vm = root_folder.findByInventoryPath(inventory_path)
19
22
  raise format("Unable to find template: %s", options[:template]) if src_vm.nil?
20
23
 
21
24
  # Specify where the machine is going to be created
@@ -30,8 +33,28 @@ class Support
30
33
  # Set the resource pool
31
34
  relocate_spec.pool = options[:resource_pool]
32
35
 
36
+ # Change network, if wanted
37
+ unless options[:network_name].nil?
38
+ all_network_devices = src_vm.config.hardware.device.select do |device|
39
+ device.is_a?(RbVmomi::VIM::VirtualEthernetCard)
40
+ end
41
+
42
+ # Only support for first NIC so far
43
+ network_device = all_network_devices.first
44
+ network_device.backing = RbVmomi::VIM.VirtualEthernetCardNetworkBackingInfo(
45
+ deviceName: options[:network_name]
46
+ )
47
+
48
+ relocate_spec.deviceChange = [
49
+ RbVmomi::VIM.VirtualDeviceConfigSpec(
50
+ operation: RbVmomi::VIM::VirtualDeviceConfigSpecOperation("edit"),
51
+ device: network_device
52
+ )
53
+ ]
54
+ end
55
+
33
56
  # Set the folder to use
34
- dest_folder = options[:folder].nil? ? src_vm.parent : options[:folder][:id]
57
+ dest_folder = options[:folder].nil? ? dc.vmFolder : options[:folder][:id]
35
58
 
36
59
  puts "Cloning '#{options[:template]}' to create the VM..."
37
60
  if options[:clone_type] == :instant
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kitchen-vcenter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
- - Russell Seymour
7
+ - Chef Software
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-04 00:00:00.000000000 Z
11
+ date: 2019-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rbvmomi
@@ -96,7 +96,7 @@ dependencies:
96
96
  version: '0'
97
97
  description: Test Kitchen driver for VMware vCenter using SDK
98
98
  email:
99
- - russell@chef.io
99
+ - oss@chef.io
100
100
  executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []