-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #353 from pkomanek/refactoring_cloud_vm_provisioni…
…ng_placement_best_fit_methods Refactoring cloud vm provisioning placement best fit methods
- Loading branch information
Showing
7 changed files
with
294 additions
and
62 deletions.
There are no files selected for viewing
84 changes: 58 additions & 26 deletions
84
...ent/automate/ManageIQ/Cloud/VM/Provisioning/Placement.class/__methods__/best_fit_azure.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,69 @@ | ||
################################################################### | ||
# | ||
# Description: Select the cloud network and availability zone for Azure | ||
# | ||
# # | ||
# Description: Select the cloud network, availability zone # | ||
# and resource group for Azure # | ||
# # | ||
################################################################### | ||
module ManageIQ | ||
module Automate | ||
module Cloud | ||
module VM | ||
module Provisioning | ||
module Placement | ||
class BestFitAzure | ||
def initialize(handle = $evm) | ||
@handle = handle | ||
end | ||
|
||
$evm.log("info", "Using Auto Placement for Azure Cloud Provider") | ||
prov = $evm.root["miq_provision"] | ||
image = prov.vm_template | ||
raise "Image not specified" if image.nil? | ||
def main | ||
@handle.log("info", "Using Auto Placement for Azure Cloud Provider") | ||
raise "Image not specified" if prov.try(:vm_template).nil? | ||
|
||
if prov.get_option(:cloud_network).nil? | ||
cloud_network = prov.eligible_cloud_networks.first | ||
prov.get_option(:cloud_network) || default_cloud_network | ||
prov.get_option(:cloud_subnet) || default_cloud_subnet | ||
prov.get_option(:resource_group) || default_resource_group | ||
end | ||
|
||
if cloud_network | ||
prov.set_cloud_network(cloud_network) | ||
$evm.log("info", "Selected Cloud Network: #{cloud_network.name}") | ||
end | ||
end | ||
private | ||
|
||
if prov.get_option(:cloud_subnet).nil? | ||
cloud_subnet = prov.eligible_cloud_subnets.first | ||
def prov | ||
@prov ||= @handle.root["miq_provision"].tap do |req| | ||
raise "miq_provision not provided" unless req | ||
end | ||
end | ||
|
||
if cloud_subnet | ||
prov.set_cloud_subnet(cloud_subnet) | ||
$evm.log("info", "Selected Cloud Subnet: #{cloud_subnet.name}") | ||
end | ||
end | ||
def default_cloud_network | ||
cloud_network = prov.eligible_cloud_networks.first | ||
|
||
if prov.get_option(:resource_group).nil? | ||
resource_group = prov.eligible_resource_groups.first | ||
if cloud_network | ||
prov.set_cloud_network(cloud_network) | ||
@handle.log("info", "Selected Cloud Network: #{cloud_network.name}") | ||
end | ||
end | ||
|
||
if resource_group | ||
prov.set_resource_group(resource_group) | ||
$evm.log("info", "Selected Resource Group: #{resource_group.name}") | ||
def default_cloud_subnet | ||
cloud_subnet = prov.eligible_cloud_subnets.first | ||
|
||
if cloud_subnet | ||
prov.set_cloud_subnet(cloud_subnet) | ||
@handle.log("info", "Selected Cloud Subnet: #{cloud_subnet.name}") | ||
end | ||
end | ||
|
||
def default_resource_group | ||
resource_group = prov.eligible_resource_groups.first | ||
|
||
if resource_group | ||
prov.set_resource_group(resource_group) | ||
@handle.log("info", "Selected Resource Group: #{resource_group.name}") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
ManageIQ::Automate::Cloud::VM::Provisioning::Placement::BestFitAzure.new.main |
76 changes: 53 additions & 23 deletions
76
...nt/automate/ManageIQ/Cloud/VM/Provisioning/Placement.class/__methods__/best_fit_google.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,57 @@ | ||
################################################################### | ||
# | ||
# Description: Select the cloud network and availability zone for Google | ||
# | ||
################################################################### | ||
|
||
prov = $evm.root["miq_provision"] | ||
image = prov.vm_template | ||
raise "Image not specified" if image.nil? | ||
|
||
if prov.get_option(:availability_zone).nil? | ||
availability_zone = prov.eligible_availability_zones.first | ||
|
||
if availability_zone | ||
prov.set_availability_zone(availability_zone) | ||
$evm.log("info", "Image=[#{image.name}] Availability Zone=[#{availability_zone.name}]") | ||
end | ||
end | ||
########################################################################## | ||
# # | ||
# Description: Select the cloud network and availability zone for Google # | ||
# # | ||
########################################################################## | ||
module ManageIQ | ||
module Automate | ||
module Cloud | ||
module VM | ||
module Provisioning | ||
module Placement | ||
class BestFitGoogle | ||
def initialize(handle = $evm) | ||
@handle = handle | ||
end | ||
|
||
def main | ||
raise "Image not specified" if prov.try(:vm_template).nil? | ||
|
||
set_availability_zone if prov.get_option(:availability_zone).nil? | ||
set_cloud_network if prov.get_option(:cloud_network).nil? | ||
end | ||
|
||
private | ||
|
||
if prov.get_option(:cloud_network).nil? | ||
cloud_network = prov.eligible_cloud_networks.first | ||
def prov | ||
@prov ||= @handle.root["miq_provision"].tap do |req| | ||
raise "miq_provision not provided" unless req | ||
end | ||
end | ||
|
||
if cloud_network | ||
prov.set_cloud_network(cloud_network) | ||
$evm.log("info", "Image=[#{image.name}] Cloud Network=[#{cloud_network.name}]") | ||
def set_availability_zone | ||
availability_zone = prov.eligible_availability_zones.first | ||
|
||
if availability_zone | ||
prov.set_availability_zone(availability_zone) | ||
@handle.log("info", "Image=[#{prov.vm_template.name}] Availability Zone=[#{availability_zone.name}]") | ||
end | ||
end | ||
|
||
def set_cloud_network | ||
cloud_network = prov.eligible_cloud_networks.first | ||
|
||
if cloud_network | ||
prov.set_cloud_network(cloud_network) | ||
@handle.log("info", "Image=[#{prov.vm_template.name}] Cloud Network=[#{cloud_network.name}]") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
ManageIQ::Automate::Cloud::VM::Provisioning::Placement::BestFitGoogle.new.main |
54 changes: 41 additions & 13 deletions
54
...automate/ManageIQ/Cloud/VM/Provisioning/Placement.class/__methods__/best_fit_openstack.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,47 @@ | ||
################################################################### | ||
# | ||
# Description: select the cloud network | ||
# Default availability zone is provided by Openstack | ||
# | ||
# # | ||
# Description: Select the cloud network # | ||
# Default availability zone is provided by Openstack # | ||
# # | ||
################################################################### | ||
module ManageIQ | ||
module Automate | ||
module Cloud | ||
module VM | ||
module Provisioning | ||
module Placement | ||
class BestFitOpenStack | ||
def initialize(handle = $evm) | ||
@handle = handle | ||
end | ||
|
||
# Get variables | ||
prov = $evm.root["miq_provision"] | ||
image = prov.vm_template | ||
raise "Image not specified" if image.nil? | ||
def main | ||
raise "Image not specified" if prov.try(:vm_template).nil? | ||
set_cloud_network if prov.get_option(:cloud_network).nil? | ||
end | ||
|
||
if prov.get_option(:cloud_network).nil? | ||
cloud_network = prov.eligible_cloud_networks.first | ||
if cloud_network | ||
prov.set_cloud_network(cloud_network) | ||
$evm.log("info", "Image=[#{image.name}] Cloud Network=[#{cloud_network.name}]") | ||
private | ||
|
||
def prov | ||
@prov ||= @handle.root["miq_provision"].tap do |req| | ||
raise "miq_provision not provided" unless req | ||
end | ||
end | ||
|
||
def set_cloud_network | ||
cloud_network = prov.eligible_cloud_networks.first | ||
|
||
if cloud_network | ||
prov.set_cloud_network(cloud_network) | ||
@handle.log("info", "Image=[#{prov.vm_template.name}] Cloud Network=[#{cloud_network.name}]") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
ManageIQ::Automate::Cloud::VM::Provisioning::Placement::BestFitOpenStack.new.main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...utomate/ManageIQ/Cloud/VM/Provisioning/Placement.class/__methods__/best_fit_azure_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require_domain_file | ||
|
||
describe ManageIQ::Automate::Cloud::VM::Provisioning::Placement::BestFitAzure do | ||
let(:root_object) { Spec::Support::MiqAeMockObject.new.tap { |ro| ro["miq_provision"] = svc_provision } } | ||
let(:ems) { FactoryGirl.create(:ems_azure_with_authentication) } | ||
let(:network) { FactoryGirl.create(:cloud_network) } | ||
let(:subnet) { FactoryGirl.create(:cloud_subnet) } | ||
let(:prov_options) { { :src_vm_id => vm_template.id } } | ||
let(:miq_provision) { FactoryGirl.create(:miq_provision, :options => prov_options) } | ||
let(:vm_template) { FactoryGirl.create(:template_azure, :ext_management_system => ems) } | ||
let(:resource_group) { FactoryGirl.create(:resource_group) } | ||
|
||
let(:ae_service) do | ||
Spec::Support::MiqAeMockService.new(root_object).tap do |service| | ||
current_object = Spec::Support::MiqAeMockObject.new | ||
current_object.parent = root_object | ||
service.object = current_object | ||
end | ||
end | ||
|
||
let(:svc_provision) { MiqAeMethodService::MiqAeServiceMiqProvision.find(miq_provision.id) } | ||
let(:svc_network) { MiqAeMethodService::MiqAeServiceCloudNetwork.find(network.id) } | ||
let(:svc_subnet) { MiqAeMethodService::MiqAeServiceCloudSubnet.find(subnet.id) } | ||
let(:svc_resource_group) { MiqAeMethodService::MiqAeServiceResourceGroup.find(resource_group.id) } | ||
|
||
it "sets properties" do | ||
expect(svc_provision).to(receive(:eligible_cloud_networks) { [svc_network] }) | ||
expect(svc_provision).to(receive(:eligible_cloud_subnets) { [svc_subnet] }) | ||
expect(svc_provision).to(receive(:eligible_resource_groups) { [svc_resource_group] }) | ||
expect(svc_provision).to(receive(:set_cloud_network).with(svc_network)) | ||
expect(svc_provision).to(receive(:set_cloud_subnet).with(svc_subnet)) | ||
expect(svc_provision).to(receive(:set_resource_group).with(svc_resource_group)) | ||
described_class.new(ae_service).main | ||
end | ||
|
||
context 'raises error' do | ||
it "#'miq_provision not provided'" do | ||
root_object['miq_provision'] = nil | ||
expect { described_class.new(ae_service).main }.to(raise_error('miq_provision not provided')) | ||
end | ||
|
||
it "#'Image not specified'" do | ||
allow(svc_provision).to(receive(:vm_template) { nil }) | ||
expect { described_class.new(ae_service).main }.to(raise_error('Image not specified')) | ||
end | ||
end | ||
end |
44 changes: 44 additions & 0 deletions
44
...tomate/ManageIQ/Cloud/VM/Provisioning/Placement.class/__methods__/best_fit_google_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require_domain_file | ||
|
||
describe ManageIQ::Automate::Cloud::VM::Provisioning::Placement::BestFitGoogle do | ||
let(:root_object) { Spec::Support::MiqAeMockObject.new.tap { |ro| ro["miq_provision"] = svc_provision } } | ||
let(:ems) { FactoryGirl.create(:ems_google_with_authentication) } | ||
let(:network) { FactoryGirl.create(:cloud_network) } | ||
let(:prov_options) { { :src_vm_id => vm_template.id } } | ||
let(:miq_provision) { FactoryGirl.create(:miq_provision, :options => prov_options) } | ||
let(:vm_template) { FactoryGirl.create(:template_google, :ext_management_system => ems) } | ||
let(:availability_zone) { FactoryGirl.create(:availability_zone) } | ||
|
||
let(:ae_service) do | ||
Spec::Support::MiqAeMockService.new(root_object).tap do |service| | ||
current_object = Spec::Support::MiqAeMockObject.new | ||
current_object.parent = root_object | ||
service.object = current_object | ||
end | ||
end | ||
|
||
let(:svc_provision) { MiqAeMethodService::MiqAeServiceMiqProvision.find(miq_provision.id) } | ||
let(:svc_network) { MiqAeMethodService::MiqAeServiceCloudNetwork.find(network.id) } | ||
let(:svc_zone) { MiqAeMethodService::MiqAeServiceAvailabilityZone.find(availability_zone.id) } | ||
|
||
it "sets properties" do | ||
expect(svc_provision).to receive(:eligible_cloud_networks) { [svc_network] } | ||
expect(svc_provision).to receive(:eligible_availability_zones) { [svc_zone] } | ||
expect(svc_provision).to receive(:set_cloud_network).with(svc_network) | ||
expect(svc_provision).to receive(:set_availability_zone).with(svc_zone) | ||
|
||
described_class.new(ae_service).main | ||
end | ||
|
||
context 'raises exception' do | ||
it '#miq_provision not provided' do | ||
ae_service.root["miq_provision"] = nil | ||
expect { described_class.new(ae_service).main }.to(raise_error('miq_provision not provided')) | ||
end | ||
|
||
it '#Image not specified' do | ||
allow(svc_provision).to(receive(:vm_template) { nil }) | ||
expect { described_class.new(ae_service).main }.to(raise_error('Image not specified')) | ||
end | ||
end | ||
end |
40 changes: 40 additions & 0 deletions
40
...ate/ManageIQ/Cloud/VM/Provisioning/Placement.class/__methods__/best_fit_openstack_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require_domain_file | ||
|
||
describe ManageIQ::Automate::Cloud::VM::Provisioning::Placement::BestFitOpenStack do | ||
let(:root_object) { Spec::Support::MiqAeMockObject.new.tap { |ro| ro["miq_provision"] = svc_provision } } | ||
let(:ems) { FactoryGirl.create(:ems_google_with_authentication) } | ||
let(:network) { FactoryGirl.create(:cloud_network) } | ||
let(:prov_options) { { :src_vm_id => vm_template.id } } | ||
let(:miq_provision) { FactoryGirl.create(:miq_provision, :options => prov_options) } | ||
let(:vm_template) { FactoryGirl.create(:template_google, :ext_management_system => ems) } | ||
|
||
let(:ae_service) do | ||
Spec::Support::MiqAeMockService.new(root_object).tap do |service| | ||
current_object = Spec::Support::MiqAeMockObject.new | ||
current_object.parent = root_object | ||
service.object = current_object | ||
end | ||
end | ||
|
||
let(:svc_provision) { MiqAeMethodService::MiqAeServiceMiqProvision.find(miq_provision.id) } | ||
let(:svc_network) { MiqAeMethodService::MiqAeServiceCloudNetwork.find(network.id) } | ||
|
||
it "sets cloud_network" do | ||
expect(svc_provision).to(receive(:eligible_cloud_networks) { [svc_network] }) | ||
expect(svc_provision).to(receive(:set_cloud_network).with(svc_network)) | ||
|
||
described_class.new(ae_service).main | ||
end | ||
|
||
context 'raises exception' do | ||
it '#miq_provision not provided' do | ||
ae_service.root["miq_provision"] = nil | ||
expect { described_class.new(ae_service).main }.to(raise_error('miq_provision not provided')) | ||
end | ||
|
||
it '#Image not specified' do | ||
allow(svc_provision).to(receive(:vm_template) { nil }) | ||
expect { described_class.new(ae_service).main }.to(raise_error('Image not specified')) | ||
end | ||
end | ||
end |