Skip to content

Commit

Permalink
Add ability to create update and delete Resource Pools fog#253
Browse files Browse the repository at this point in the history
  • Loading branch information
rlazoryshchak committed Jul 13, 2020
1 parent 1e3bd2a commit 8d197e6
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/fog/vsphere/compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 65 additions & 0 deletions lib/fog/vsphere/requests/compute/create_resource_pool.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions lib/fog/vsphere/requests/compute/destroy_resource_pool.rb
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions lib/fog/vsphere/requests/compute/update_resource_pool.rb
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions tests/requests/compute/create_resource_pool_tests.rb
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions tests/requests/compute/update_resource_pool_tests.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8d197e6

Please sign in to comment.