-
Notifications
You must be signed in to change notification settings - Fork 900
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 #12523 from pkomanek/refactoring_available_flavors
Refactoring available flavors
- Loading branch information
Showing
3 changed files
with
170 additions
and
100 deletions.
There are no files selected for viewing
68 changes: 51 additions & 17 deletions
68
...re/ManageIQ/Cloud/Orchestration/Operations/Methods.class/__methods__/available_flavors.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,26 +1,60 @@ | ||
# | ||
# Description: provide the dynamic list content from available flavors | ||
# | ||
flavor_list = {} | ||
service = $evm.root.attributes["service_template"] || $evm.root.attributes["service"] | ||
if service.respond_to?(:orchestration_manager) && service.orchestration_manager | ||
service.orchestration_manager.flavors.each { |f| flavor_list[f.name] = f.name } | ||
end | ||
flavor_list[nil] = flavor_list.empty? ? "<None>" : "<Choose>" | ||
module ManageIQ | ||
module Automate | ||
module Cloud | ||
module Orchestration | ||
module Operations | ||
class AvailableFlavors | ||
def initialize(handle = $evm) | ||
@handle = handle | ||
end | ||
|
||
def main | ||
fill_dialog_field(fetch_list_data) | ||
end | ||
|
||
private | ||
|
||
def fetch_list_data | ||
service = @handle.root.attributes["service_template"] || @handle.root.attributes["service"] | ||
flavors = service.try(:orchestration_manager).try(:flavors) | ||
|
||
flavor_list = {} | ||
flavors.each { |f| flavor_list[f.name] = f.name } if flavors | ||
|
||
dialog_field = $evm.object | ||
return nil => "<none>" if flavor_list.blank? | ||
|
||
# sort_by: value / description / none | ||
dialog_field["sort_by"] = "description" | ||
flavor_list[nil] = "<select>" if flavor_list.length > 1 | ||
flavor_list | ||
end | ||
|
||
# sort_order: ascending / descending | ||
dialog_field["sort_order"] = "ascending" | ||
def fill_dialog_field(list) | ||
dialog_field = @handle.object | ||
|
||
# data_type: string / integer | ||
dialog_field["data_type"] = "string" | ||
# sort_by: value / description / none | ||
dialog_field["sort_by"] = "description" | ||
|
||
# required: true / false | ||
dialog_field["required"] = "true" | ||
# sort_order: ascending / descending | ||
dialog_field["sort_order"] = "ascending" | ||
|
||
dialog_field["values"] = flavor_list | ||
dialog_field["default_value"] = nil | ||
# data_type: string / integer | ||
dialog_field["data_type"] = "string" | ||
|
||
# required: true / false | ||
dialog_field["required"] = "true" | ||
|
||
dialog_field["values"] = list | ||
dialog_field["default_value"] = nil | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
if __FILE__ == $PROGRAM_NAME | ||
ManageIQ::Automate::Cloud::Orchestration::Operations::AvailableFlavors.new.main | ||
end |
83 changes: 0 additions & 83 deletions
83
spec/automation/unit/method_validation/available_flavors_spec.rb
This file was deleted.
Oops, something went wrong.
119 changes: 119 additions & 0 deletions
119
...nageIQ/Cloud/Orchestration/Operations/Methods.class/__methods__/available_flavors_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,119 @@ | ||
require Rails.root.join('db/fixtures/ae_datastore/ManageIQ/Cloud/Orchestration/Operations' \ | ||
'/Methods.class/__methods__/available_flavors.rb').to_s | ||
|
||
describe ManageIQ::Automate::Cloud::Orchestration::Operations::AvailableFlavors do | ||
let(:root_object) { Spec::Support::MiqAeMockObject.new(root_hash) } | ||
let(:root_hash) do | ||
{ 'service_template' => MiqAeMethodService::MiqAeServiceServiceTemplate.find(service_template.id) } | ||
end | ||
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 | ||
|
||
shared_examples_for "#having only default value" do | ||
let(:default_desc_blank) { "<none>" } | ||
|
||
it "provides only default value to the flavor list" do | ||
described_class.new(ae_service).main | ||
|
||
expect(ae_service["values"]).to eq(nil => default_desc_blank) | ||
expect(ae_service["default_value"]).to be_nil | ||
end | ||
end | ||
|
||
shared_examples_for "#having all flavors" do |service_type| | ||
let(:default_desc) { "<select>" } | ||
let(:flavor1) { FactoryGirl.create(:flavor, :name => 'flavor1') } | ||
let(:flavor2) { FactoryGirl.create(:flavor, :name => 'flavor2') } | ||
let(:ems) { FactoryGirl.create(:ems_openstack, :flavors => [flavor1, flavor2]) } | ||
|
||
let(:svc_model_flavor1) do | ||
MiqAeMethodService::MiqAeServiceFlavor.find(flavor1.id) | ||
end | ||
|
||
let(:svc_model_flavor2) do | ||
MiqAeMethodService::MiqAeServiceFlavor.find(flavor2.id) | ||
end | ||
|
||
let(:svc_model_orchestration_manager) do | ||
MiqAeMethodService::MiqAeServiceExtManagementSystem.find(ems.id) | ||
end | ||
|
||
let(:svc_model_service) do | ||
root_hash["service_template"] || root_hash["service"] | ||
end | ||
|
||
it "finds all the flavors and populates the list" do | ||
allow(ae_service.root).to receive(:attributes) | ||
.and_return(service_type => svc_model_service) | ||
allow(svc_model_service).to receive(:orchestration_manager) | ||
.and_return(svc_model_orchestration_manager) | ||
allow(svc_model_orchestration_manager).to receive(:flavors) | ||
.and_return([svc_model_flavor1, svc_model_flavor2]) | ||
described_class.new(ae_service).main | ||
|
||
expect(ae_service["values"]).to include( | ||
nil => default_desc, | ||
flavor1.name => flavor1.name, | ||
flavor2.name => flavor2.name | ||
) | ||
expect(ae_service["default_value"]).to be_nil | ||
end | ||
end | ||
|
||
context "workspace has no service template" do | ||
let(:root_hash) { {} } | ||
|
||
it_behaves_like "#having only default value" | ||
end | ||
|
||
context "workspace has service template other than orchestration" do | ||
let(:service_template) { FactoryGirl.create(:service_template) } | ||
|
||
it_behaves_like "#having only default value" | ||
end | ||
|
||
context "workspace has orchestration service template" do | ||
context 'with orchestration_manager' do | ||
let(:service_template) do | ||
FactoryGirl.create(:service_template_orchestration, :orchestration_manager => ems) | ||
end | ||
|
||
it_behaves_like "#having all flavors", "service_template" | ||
end | ||
|
||
context 'without orchestration_manager' do | ||
let(:service_template) do | ||
FactoryGirl.create(:service_template_orchestration) | ||
end | ||
|
||
it_behaves_like "#having only default value" | ||
end | ||
end | ||
|
||
context "workspace has orchestration service" do | ||
let(:root_hash) do | ||
{ 'service' => MiqAeMethodService::MiqAeServiceService.find(service.id) } | ||
end | ||
|
||
context 'with orchestration_manager' do | ||
let(:service) do | ||
FactoryGirl.create(:service_orchestration, :orchestration_manager => ems) | ||
end | ||
|
||
it_behaves_like "#having all flavors", "service" | ||
end | ||
|
||
context 'without orchestration_manager' do | ||
let(:service) do | ||
FactoryGirl.create(:service_orchestration) | ||
end | ||
|
||
it_behaves_like "#having only default value" | ||
end | ||
end | ||
end |