Class: Chef::Knife::Cloud::VcenterService
- Inherits:
-
Service
- Object
- Service
- Chef::Knife::Cloud::VcenterService
- Includes:
- VcenterServiceHelpers
- Defined in:
- lib/chef/knife/cloud/vcenter_service.rb
Overview
Extends the Service method, this is the bulk of the integration
Instance Attribute Summary collapse
-
#api_client ⇒ Object
readonly
Returns the value of attribute api_client.
-
#connection_options ⇒ Object
readonly
Returns the value of attribute connection_options.
-
#ipaddress ⇒ Object
readonly
Returns the value of attribute ipaddress.
-
#session_api ⇒ Object
readonly
Returns the value of attribute session_api.
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
Instance Method Summary collapse
-
#create_server(options = {}) ⇒ Object
Creates the server.
-
#datacenter_exists?(name) ⇒ Boolean
Checks to see if the datacenter exists in the vCenter.
-
#delete_vm(name) ⇒ Object
Deletes the VM.
-
#get_datastore(name) ⇒ Object
Gets the datastore.
-
#get_folder(name) ⇒ Object
Gets the folder.
-
#get_host(name) ⇒ Object
Gets the host.
-
#get_resource_pool(name) ⇒ Object
Gets the resource_pool.
-
#get_server(name) ⇒ Object
Gets the server.
-
#initialize(options = {}) ⇒ VcenterService
constructor
A new instance of VcenterService.
-
#list_clusters ⇒ Object
Return a list of the clusters in the vCenter.
-
#list_datacenters ⇒ Object
Return a list of the datacenters in the vCenter.
-
#list_hosts ⇒ Object
Return a list of the hosts in the vCenter.
-
#list_servers ⇒ Object
Get a list of vms from the API.
-
#server_summary(server, _coloumns_with_inf = nil) ⇒ Object
Gets some server information.
Methods included from VcenterServiceHelpers
#check_for_missing_config_values!, #create_service_instance, #validate!, #verify_ssl?
Constructor Details
#initialize(options = {}) ⇒ VcenterService
Returns a new instance of VcenterService.
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 |
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 44 def initialize( = {}) super() configuration = VSphereAutomation::Configuration.new.tap do |c| c.host = [:host] c.username = [:username] c.password = [:password] c.scheme = "https" c.verify_ssl = [:verify_ssl] c.verify_ssl_host = [:verify_ssl] end Base.log.warn("SSL Verification is turned OFF") if [:logs] && ![:verify_ssl] @api_client = VSphereAutomation::ApiClient.new(configuration) api_client.default_headers["Authorization"] = configuration.basic_auth_token session_api = VSphereAutomation::CIS::SessionApi.new(api_client) session_id = session_api.create("").value api_client.default_headers["vmware-api-session-id"] = session_id # Set the class properties for the rbvmomi connections = { user: [:username], password: [:password], insecure: [:verify_ssl] ? false : true, host: [:host], } end |
Instance Attribute Details
#api_client ⇒ Object (readonly)
Returns the value of attribute api_client.
41 42 43 |
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 41 def api_client @api_client end |
#connection_options ⇒ Object (readonly)
Returns the value of attribute connection_options.
42 43 44 |
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 42 def end |
#ipaddress ⇒ Object (readonly)
Returns the value of attribute ipaddress.
42 43 44 |
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 42 def ipaddress @ipaddress end |
#session_api ⇒ Object (readonly)
Returns the value of attribute session_api.
41 42 43 |
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 41 def session_api @session_api end |
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id.
41 42 43 |
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 41 def session_id @session_id end |
Instance Method Details
#create_server(options = {}) ⇒ Object
Creates the server
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 126 127 128 129 130 131 132 133 134 |
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 78 def create_server( = {}) # Create the vm object vm_api = VSphereAutomation::VCenter::VMApi.new(api_client) # Use the option to determine now a new machine is being created case [:type] when "clone" datacenter_exists?([:datacenter]) # Some of the options need to be the ID of the component in VMware # Update these using the REST API so that they can be passed to the support library [:targethost] = get_host([:targethost]).host [:resource_pool] = get_resource_pool([:resource_pool]) # Configure the folder option as a has with the name an the id unless [:folder].nil? [:folder] = { name: [:folder], id: get_folder([:folder]), } end # Clone the machine using the support library clone_obj = ::Support::CloneVm.new(, ) @ipaddress = clone_obj.clone # return an object from the restapi get_server([:name]) when "create" # Create the placement object placement_spec = VSphereAutomation::VCenter::VcenterVMPlacementSpec.new( ### folder: get_folder([:folder]), host: get_host([:targethost]).host, datastore: get_datastore([:datastore]), resource_pool: get_resource_pool([:resource_pool]) ) # Create the CreateSpec object create_spec = VSphereAutomation::VCenter::VcenterVMCreateSpec.new( name: [:name], guest_OS: VSphereAutomation::VCenter::VcenterVmGuestOS::OTHER, placement: placement_spec ) # Create the new machine begin create_model = VSphereAutomation::VCenter::VcenterVMCreate.new(spec: create_spec) vm_api.create(create_model).value rescue StandardError => e puts e. end end end |
#datacenter_exists?(name) ⇒ Boolean
Checks to see if the datacenter exists in the vCenter
166 167 168 169 170 171 |
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 166 def datacenter_exists?(name) dc_api = VSphereAutomation::VCenter::DatacenterApi.new(api_client) dcs = dc_api.list({ filter_names: name }).value raise format("Unable to find data center: %s", name) if dcs.empty? end |
#delete_vm(name) ⇒ Object
Deletes the VM
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 250 def delete_vm(name) vm = get_server(name) server_summary(vm) ui.msg("") ui.confirm("Do you really want to be delete this virtual machine") vm_api = VSphereAutomation::VCenter::VMApi.new(api_client) # check the power state of the machine, if it is powered on turn it off if vm.power_state == "POWERED_ON" power_api = VSphereAutomation::VCenter::VmPowerApi.new(api_client) ui.msg("Shutting down machine") power_api.stop(vm.vm) end vm_api.delete(vm.vm) end |
#get_datastore(name) ⇒ Object
Gets the datastore
206 207 208 209 210 211 212 213 |
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 206 def get_datastore(name) ds_api = VSphereAutomation::VCenter::DatastoreApi.new(api_client) ds = ds_api.list({ filter_names: name }).value raise format("Unable to find data store: %s", name) if ds.empty? ds.first.datastore end |
#get_folder(name) ⇒ Object
Gets the folder
176 177 178 179 180 181 182 183 |
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 176 def get_folder(name) folder_api = VSphereAutomation::VCenter::FolderApi.new(api_client) folders = folder_api.list({ filter_names: name }).value raise format("Unable to find folder: %s", name) if folders.empty? folders.first.folder end |
#get_host(name) ⇒ Object
Gets the host
188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 188 def get_host(name) # create a host object to work with host_api = VSphereAutomation::VCenter::HostApi.new(api_client) if name.nil? hosts = host_api.list.value else hosts = host_api.list({ filter_names: name }).value end raise format("Unable to find target host: %s", name) if hosts.empty? hosts.first end |
#get_resource_pool(name) ⇒ Object
Gets the resource_pool
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 218 def get_resource_pool(name) rp_api = VSphereAutomation::VCenter::ResourcePoolApi.new(api_client) if name.nil? # Remove default pool for first pass (<= 1.2.1 behavior to pick first user-defined pool found) resource_pools = rp_api.list.value.delete_if { |pool| pool.name == "Resources" } puts "Search of all resource pools found: " + resource_pools.map(&:name).to_s # Revert to default pool, if no user-defined pool found (> 1.2.1 behavior) # (This one might not be found under some circumstances by the statement above) return get_resource_pool("Resources") if resource_pools.empty? else resource_pools = rp_api.list({ filter_names: name }).value puts "Search for resource pools found: " + resource_pools.map(&:name).to_s end raise format("Unable to find Resource Pool: %s", name) if resource_pools.empty? resource_pools.first.resource_pool end |
#get_server(name) ⇒ Object
Gets the server
242 243 244 245 |
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 242 def get_server(name) vm_api = VSphereAutomation::VCenter::VMApi.new(api_client) vm_api.list({ filter_names: name }).value.first end |
#list_clusters ⇒ Object
Return a list of the clusters in the vCenter
159 160 161 |
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 159 def list_clusters VSphereAutomation::VCenter::ClusterApi.new(api_client).list.value end |
#list_datacenters ⇒ Object
Return a list of the datacenters in the vCenter
153 154 155 |
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 153 def list_datacenters VSphereAutomation::VCenter::DatacenterApi.new(api_client).list.value end |
#list_hosts ⇒ Object
Return a list of the hosts in the vCenter
147 148 149 |
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 147 def list_hosts VSphereAutomation::VCenter::HostApi.new(api_client).list.value end |
#list_servers ⇒ Object
Get a list of vms from the API
138 139 140 141 142 143 |
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 138 def list_servers vms = VSphereAutomation::VCenter::VMApi.new(api_client).list.value # list_resource_command uses .send(:name) syntax, so convert to OpenStruct to keep it compatible vms.map { |vmsummary| OpenStruct.new(vmsummary.to_hash) } end |
#server_summary(server, _coloumns_with_inf = nil) ⇒ Object
Gets some server information
272 273 274 275 276 |
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 272 def server_summary(server, _coloumns_with_inf = nil) msg_pair("ID", server.vm) msg_pair("Name", server.name) msg_pair("Power State", server.power_state) end |