Class: Kitchen::Driver::Vcenter
- Inherits:
-
Base
- Object
- Base
- Kitchen::Driver::Vcenter
- Defined in:
- lib/kitchen/driver/vcenter.rb
Overview
Extends the Base class for vCenter
Defined Under Namespace
Classes: ResourceAmbiguousError, ResourceMissingError, UnauthenticatedError
Constant Summary collapse
- UNAUTH_CLASSES =
[ VSphereAutomation::CIS::VapiStdErrorsUnauthenticated, VSphereAutomation::VCenter::VapiStdErrorsUnauthenticated, ].freeze
Instance Attribute Summary collapse
-
#api_client ⇒ Object
Returns the value of attribute api_client.
-
#connection_options ⇒ Object
Returns the value of attribute connection_options.
-
#ipaddress ⇒ Object
Returns the value of attribute ipaddress.
Instance Method Summary collapse
-
#create(state) ⇒ Object
The main create method.
-
#destroy(state) ⇒ Object
The main destroy method.
Instance Attribute Details
#api_client ⇒ Object
Returns the value of attribute api_client.
37 38 39 |
# File 'lib/kitchen/driver/vcenter.rb', line 37 def api_client @api_client end |
#connection_options ⇒ Object
Returns the value of attribute connection_options.
37 38 39 |
# File 'lib/kitchen/driver/vcenter.rb', line 37 def @connection_options end |
#ipaddress ⇒ Object
Returns the value of attribute ipaddress.
37 38 39 |
# File 'lib/kitchen/driver/vcenter.rb', line 37 def ipaddress @ipaddress end |
Instance Method Details
#create(state) ⇒ Object
The main create method
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 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 |
# File 'lib/kitchen/driver/vcenter.rb', line 107 def create(state) debug format("Starting kitchen-vcenter %s", ::KitchenVcenter::VERSION) save_and_validate_parameters connect # Use the root resource pool of a specified cluster, if any if config[:cluster].nil? # Find the first resource pool on any cluster config[:resource_pool] = get_resource_pool(config[:resource_pool]) else cluster = get_cluster(config[:cluster]) root_pool = cluster.resource_pool if config[:resource_pool].nil? config[:resource_pool] = root_pool else rp_api = VSphereAutomation::VCenter::ResourcePoolApi.new(api_client) raise_if_unauthenticated rp_api, "checking for resource pools" found_pool = nil pools = rp_api.get(root_pool).value.resource_pools pools.each do |pool| name = rp_api.get(pool).value.name found_pool = pool if name == config[:resource_pool] end raise_if_missing found_pool, format("Resource pool `%s` not found on cluster `%s`", config[:resource_pool], config[:cluster]) config[:resource_pool] = found_pool end end # Check that the datacenter exists dc_folder = File.dirname(config[:datacenter]) dc_folder = nil if dc_folder == "." dc_name = File.basename(config[:datacenter]) datacenter_exists?(dc_folder, dc_name) # Get datacenter and cluster information datacenter = get_datacenter(dc_folder, dc_name) cluster_id = get_cluster_id(config[:cluster]) # Find the identifier for the targethost to pass to rbvmomi config[:targethost] = get_host(config[:targethost], datacenter, cluster_id) # Check if network exists, if to be changed config[:networks].each { |network| network_exists?(network[:name]) } # Same thing needs to happen with the folder name if it has been set unless config[:folder].nil? config[:folder] = { name: config[:folder], id: get_folder(config[:folder], "VIRTUAL_MACHINE", datacenter), } end # Check for valid tags before cloning = (config[:tags]) # Allow different clone types config[:clone_type] = :linked if config[:clone_type] == "linked" config[:clone_type] = :instant if config[:clone_type] == "instant" # Create a hash of options that the clone requires = { vm_name: config[:vm_name], targethost: config[:targethost], poweron: config[:poweron], template: config[:template], datacenter: config[:datacenter], folder: config[:folder], resource_pool: config[:resource_pool], clone_type: config[:clone_type].to_sym, networks: config[:networks], interface: config[:interface], wait_timeout: config[:vm_wait_timeout], wait_interval: config[:vm_wait_interval], vm_customization: config[:vm_customization], guest_customization: config[:guest_customization], active_discovery: config[:active_discovery], active_discovery_command: config[:active_discovery_command], vm_os: config[:vm_os], vm_username: config[:vm_username], vm_password: config[:vm_password], vm_win_network: config[:vm_win_network], transform_ip: config[:transform_ip], benchmark: config[:benchmark], benchmark_file: config[:benchmark_file], } begin # Create an object from which the clone operation can be called new_vm = Support::CloneVm.new(, ) new_vm.clone state[:hostname] = new_vm.ip state[:vm_name] = new_vm.vm_name rescue # Kitchen::ActionFailed => e if config[:vm_rollback] == true error format("Rolling back VM `%s` after critical error", config[:vm_name]) # Inject name of failed VM for destroy to work state[:vm_name] = config[:vm_name] destroy(state) end raise end if debug format("Setting tags on machine: `%s`", .keys.join("`, `")) tag_service = VSphereAutomation::CIS::TaggingTagAssociationApi.new(api_client) raise_if_unauthenticated tag_service, "connecting to tagging service" request_body = { object_id: { id: get_vm(config[:vm_name]).vm, type: "VirtualMachine", }, tag_ids: .values, } tag_service.(request_body) end end |
#destroy(state) ⇒ Object
The main destroy method
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 |
# File 'lib/kitchen/driver/vcenter.rb', line 239 def destroy(state) return if state[:vm_name].nil? # Reset resource pool, as it's not needed for the destroy action but might be a remnant of earlier calls to "connect" # Temporary fix until setting cluster + resource_pool at the same time is implemented (lines #64/#187) config[:resource_pool] = nil save_and_validate_parameters connect vm = get_vm(state[:vm_name]) unless vm.nil? vm_api = VSphereAutomation::VCenter::VMApi.new(api_client) raise_if_unauthenticated vm_api, "connecting to VM API" # shut the machine down if it is running if vm.power_state == "POWERED_ON" power = VSphereAutomation::VCenter::VmPowerApi.new(api_client) power.stop(vm.vm) end # delete the vm vm_api.delete(vm.vm) end end |