Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring available_resource_groups method for miq_ae_orchestrations #12212

Merged
merged 2 commits into from
Nov 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,25 +1,61 @@
#
# Description: provide the dynamic list content from available resource groups
#
rg_list = {nil => "<New resource group>"}
service = $evm.root.attributes["service_template"] || $evm.root.attributes["service"]
if service.respond_to?(:orchestration_manager) && service.orchestration_manager
service.orchestration_manager.resource_groups.each { |t| rg_list[t.name] = t.name }
end
module ManageIQ
module Automate
module Cloud
module Orchestration
module Operations
class AvailableResoureceGroups
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"]
rs_groups = service.try(:orchestration_manager).try(:resource_groups)

rs_list = {}
rs_groups.each { |rs| rs_list[rs.name] = rs.name } if rs_groups

return nil => "<none>" if rs_list.blank?

dialog_field = $evm.object
rs_list[nil] = "<select>" if rs_list.length > 1
rs_list
end

# sort_by: value / description / none
dialog_field["sort_by"] = "description"
def fill_dialog_field(list)
dialog_field = @handle.object

# sort_order: ascending / descending
dialog_field["sort_order"] = "ascending"
# sort_by: value / description / none
dialog_field["sort_by"] = "description"

# data_type: string / integer
dialog_field["data_type"] = "string"
# sort_order: ascending / descending
dialog_field["sort_order"] = "ascending"

# required: true / false
dialog_field["required"] = "false"
# data_type: string / integer
dialog_field["data_type"] = "string"

dialog_field["values"] = rg_list
dialog_field["default_value"] = nil
# required: true / false
dialog_field["required"] = "false"

dialog_field["values"] = list

dialog_field["default_value"] = list.length == 1 ? list.keys.first : nil
end
end
end
end
end
end
end

if __FILE__ == $PROGRAM_NAME
ManageIQ::Automate::Cloud::Orchestration::Operations::AvailableResoureceGroups.new.main
end

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
require Rails.root.join('db/fixtures/ae_datastore/ManageIQ/Cloud/Orchestration/Operations' \
'/Methods.class/__methods__/available_resource_groups.rb').to_s

describe ManageIQ::Automate::Cloud::Orchestration::Operations::AvailableResoureceGroups do
let(:root_hash) do
{ 'service_template' => MiqAeMethodService::MiqAeServiceServiceTemplate.find(service_template.id) }
end
let(:root_object) { Spec::Support::MiqAeMockObject.new(root_hash) }
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 resource group list" do
described_class.new(ae_service).main

expect(ae_service["values"]).to eq(nil => default_desc_blank)
end
end

shared_examples_for "#having all of the resource groups" do
let(:default_desc) { "<select>" }
let(:rgroup1) { FactoryGirl.create(:resource_group) }
let(:rgroup2) { FactoryGirl.create(:resource_group) }
let(:ems) do
FactoryGirl.create(:ems_azure, :resource_groups => [rgroup1, rgroup2])
end

it "finds all the resource groups and populates the list" do
described_class.new(ae_service).main

expect(ae_service["values"]).to include(
nil => default_desc,
rgroup1.name => rgroup1.name,
rgroup2.name => rgroup2.name)
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 of the resource groups"
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_template' => MiqAeMethodService::MiqAeServiceService.find(service_template.id) }
end

context 'with orchestration_manager' do
let(:service_template) do
FactoryGirl.create(:service_orchestration, :orchestration_manager => ems)
end

it_behaves_like "#having all of the resource groups"
end

context 'without orchestration_manager' do
let(:service_template) do
FactoryGirl.create(:service_orchestration)
end

it_behaves_like "#having only default value"
end
end
end