From c9aff8c3e3c80cb2e776d9d37b28cd808a5663e7 Mon Sep 17 00:00:00 2001 From: Roman Lazoryshchak Date: Thu, 9 Jul 2020 21:19:10 +0300 Subject: [PATCH] Add ability to create update and delete Resource Pools #253 --- lib/fog/vsphere/compute.rb | 3 ++ .../requests/compute/create_resource_pool.rb | 49 +++++++++++++++++++ .../requests/compute/destroy_resource_pool.rb | 15 ++++++ .../requests/compute/update_resource_pool.rb | 31 ++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 lib/fog/vsphere/requests/compute/create_resource_pool.rb create mode 100644 lib/fog/vsphere/requests/compute/destroy_resource_pool.rb create mode 100644 lib/fog/vsphere/requests/compute/update_resource_pool.rb diff --git a/lib/fog/vsphere/compute.rb b/lib/fog/vsphere/compute.rb index babc1a6e..7c2fbe8f 100644 --- a/lib/fog/vsphere/compute.rb +++ b/lib/fog/vsphere/compute.rb @@ -71,6 +71,9 @@ class Compute < Fog::Service request :get_cluster request :list_resource_pools request :get_resource_pool + request :create_resource_pool + request :update_resource_pool + request :destroy_resource_pool request :list_networks request :get_network request :list_datastores diff --git a/lib/fog/vsphere/requests/compute/create_resource_pool.rb b/lib/fog/vsphere/requests/compute/create_resource_pool.rb new file mode 100644 index 00000000..a382150a --- /dev/null +++ b/lib/fog/vsphere/requests/compute/create_resource_pool.rb @@ -0,0 +1,49 @@ +module Fog + module Vsphere + class Compute + class Real + def create_resource_pool(attributes = {}) + cluster = get_raw_cluster(attributes[:cluster], attributes[:datacenter]) + + root_resource_pool = if attributes[:root_resource_pool_name] + cluster.resourcePool.find attributes[:root_resource_pool_name] + else + cluster.resourcePool + end + + root_resource_pool.CreateResourcePool( + name: attributes[:name], + spec: get_resource_pool_spec(attributes) + ) + + get_resource_pool(attributes[:name], attributes[:cluster], attributes[:datacenter]) + end + + private + + def get_resource_pool_spec(attributes = {}) + RbVmomi::VIM.ResourceConfigSpec( + cpuAllocation: get_resource_pool_allocation_spec(attributes.fetch(:cpu, {})), + memoryAllocation: get_resource_pool_allocation_spec(attributes.fetch(:memory, {})) + ) + end + + def get_resource_pool_allocation_spec(attributes = {}) + RbVmomi::VIM.ResourceAllocationInfo( + reservation: attributes.fetch(:reservation, 0), + limit: attributes.fetch(:limit, -1), + expandableReservation: attributes.fetch(:expandable_reservation, false), + shares: RbVmomi::VIM.SharesInfo( + level: RbVmomi::VIM.SharesLevel(attributes.fetch(:shares_level, 'normal')), + shares: attributes.fetch(:shares, 0) + ) + ) + end + end + + class Mock + def create_resource_pool(attributes = {}); end + end + end + end +end diff --git a/lib/fog/vsphere/requests/compute/destroy_resource_pool.rb b/lib/fog/vsphere/requests/compute/destroy_resource_pool.rb new file mode 100644 index 00000000..841096ef --- /dev/null +++ b/lib/fog/vsphere/requests/compute/destroy_resource_pool.rb @@ -0,0 +1,15 @@ +module Fog + module Vsphere + class Compute + class Real + def destroy_resource_pool(attributes = {}) + get_raw_resource_pool_by_ref(attributes).Destroy_Task().wait_for_completion + end + end + + class Mock + def destroy_resource_pool(attributes = {}); end + end + end + end +end diff --git a/lib/fog/vsphere/requests/compute/update_resource_pool.rb b/lib/fog/vsphere/requests/compute/update_resource_pool.rb new file mode 100644 index 00000000..bc61596a --- /dev/null +++ b/lib/fog/vsphere/requests/compute/update_resource_pool.rb @@ -0,0 +1,31 @@ +module Fog + module Vsphere + class Compute + class Real + def update_resource_pool(attributes = {}) + raw_resource_pool = get_raw_resource_pool_by_ref(attributes) + + raw_resource_pool.UpdateConfig( + name: attributes[:name], + config: get_resource_pool_spec(attributes) + ) + + get_resource_pool(attributes[:name], attributes[:cluster], attributes[:datacenter]) + end + + private + + def get_raw_resource_pool_by_ref(attributes = {}) + dc = find_raw_datacenter(attributes[:datacenter]) + cluster = dc.find_compute_resource(attributes[:cluster]) + + list_raw_resource_pools(cluster).detect { |rp| rp._ref == attributes[:ref] } + end + end + + class Mock + def update_resource_pool(attributes = {}); end + end + end + end +end