From 8d197e6c46c8a0e005a150b1c3e8ecc7c913807f 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 | 65 +++++++++++++++++++ .../requests/compute/destroy_resource_pool.rb | 15 +++++ .../requests/compute/update_resource_pool.rb | 31 +++++++++ .../compute/create_resource_pool_tests.rb | 35 ++++++++++ .../compute/update_resource_pool_tests.rb | 28 ++++++++ 6 files changed, 177 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 create mode 100644 tests/requests/compute/create_resource_pool_tests.rb create mode 100644 tests/requests/compute/update_resource_pool_tests.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..f3adb24e --- /dev/null +++ b/lib/fog/vsphere/requests/compute/create_resource_pool.rb @@ -0,0 +1,65 @@ +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 = {}) + cpu_shares_level = attributes.fetch(:cpu_shares_level, 'normal') + cpu_reservation = attributes.fetch(:cpu_reservation, 0) + cpu_expandable_reservation = attributes.fetch(:cpu_expandable_reservation, false) + cpu_limit = attributes.fetch(:cpu_limit, -1) + cpu_shares = attributes.fetch(:cpu_shares, 0) + + memory_shares_level = attributes.fetch(:memory_shares_level, 'normal') + memory_reservation = attributes.fetch(:memory_reservation, 0) + memory_expandable_reservation = attributes.fetch(:memory_expandable_reservation, false) + memory_limit = attributes.fetch(:memory_limit, -1) + memory_shares = attributes.fetch(:memory_shares, 0) + + RbVmomi::VIM.ResourceConfigSpec( + cpuAllocation: RbVmomi::VIM.ResourceAllocationInfo( + reservation: cpu_reservation, + limit: cpu_limit, + expandableReservation: cpu_expandable_reservation, + shares: RbVmomi::VIM.SharesInfo( + level: RbVmomi::VIM.SharesLevel(cpu_shares_level), + shares: cpu_shares + ) + ), + memoryAllocation: RbVmomi::VIM.ResourceAllocationInfo( + reservation: memory_reservation, + limit: memory_limit, + expandableReservation: memory_expandable_reservation, + shares: RbVmomi::VIM.SharesInfo( + level: RbVmomi::VIM.SharesLevel(memory_shares_level), + shares: memory_shares + ) + ) + ) + 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..6a90cd2c --- /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 diff --git a/tests/requests/compute/create_resource_pool_tests.rb b/tests/requests/compute/create_resource_pool_tests.rb new file mode 100644 index 00000000..7f05f3bc --- /dev/null +++ b/tests/requests/compute/create_resource_pool_tests.rb @@ -0,0 +1,35 @@ +require_relative '../../test_helper' + +describe Fog::Vsphere::Compute::Real do + include Fog::Vsphere::TestHelper + + before { Fog.unmock! } + after { Fog.mock! } + + let(:compute) { prepare_compute } + + describe '#create_resource_pool' do + let(:resource_pool_name) { 'ResourcePool' } + + it 'creates resource pool' do + with_webmock_cassette('create_resource_pool') do + resource_pool = compute.create_resource_pool( + datacenter: 'DC1', + cluster: 'vc-qa-DC1-cluster1', + name: resource_pool_name, + cpu_reservation: '2000', + cpu_shares_level: 'normal', + cpu_expandable_reservation: false, + cpu_limit: -1, + cpu_shares: 0, + memory_reservation: '512', + memory_shares_level: 'normal', + memory_expandable_reservation: false, + memory_limit: -1, + memory_shares: 0 + ) + assert_equal(resource_pool[:name], resource_pool_name) + end + end + end +end diff --git a/tests/requests/compute/update_resource_pool_tests.rb b/tests/requests/compute/update_resource_pool_tests.rb new file mode 100644 index 00000000..ff172349 --- /dev/null +++ b/tests/requests/compute/update_resource_pool_tests.rb @@ -0,0 +1,28 @@ +require_relative '../../test_helper' + +describe Fog::Vsphere::Compute::Real do + include Fog::Vsphere::TestHelper + + before { Fog.unmock! } + after { Fog.mock! } + + let(:compute) { prepare_compute } + + describe '#update_resource_pool' do + let(:resource_pool_name) { 'ResourcePool1' } + + it 'updates resource pool' do + with_webmock_cassette('update_resource_pool') do + resource_pool = compute.update_resource_pool( + datacenter: 'DC1', + cluster: 'vc-qa-DC1-cluster1', + name: resource_pool_name, + cpu_reservation: '3000', + memory_reservation: '1024', + ref: 'resgroup-5764' + ) + assert_equal(resource_pool[:name], resource_pool_name) + end + end + end +end