Skip to content

Commit

Permalink
Add multiple_value option for expose_eligible_resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
lfu committed Feb 10, 2017
1 parent 6c41fcc commit 951d518
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
Expand Up @@ -2,7 +2,7 @@ module MiqAeMethodService
class MiqAeServiceManageIQ_Providers_CloudManager_Provision < MiqAeServiceMiqProvision
expose_eligible_resources :availability_zones
expose_eligible_resources :instance_types
expose_eligible_resources :security_groups
expose_eligible_resources :security_groups, :multiple_value => true
expose_eligible_resources :floating_ip_addresses
expose_eligible_resources :cloud_networks
expose_eligible_resources :cloud_subnets
Expand Down
Expand Up @@ -2,7 +2,7 @@ module MiqAeServiceMiqProvisionMixin
extend ActiveSupport::Concern

module ClassMethods
def expose_eligible_resources(resource)
def expose_eligible_resources(resource, options = {})
method_name = "eligible_#{resource}"
define_method(method_name) do
ar_method do
Expand All @@ -16,6 +16,12 @@ def expose_eligible_resources(resource)
set_resource(rsc)
end
end

if options[:multiple_value]
define_method("set_#{resource.to_s.pluralize}") do |rscs|
ar_method { set_resources(rscs) }
end
end
end
end

Expand Down Expand Up @@ -44,6 +50,10 @@ def set_resource(rsc)
object_send(:set_resource, rsc)
end

def set_resources(rscs)
object_send(:set_resources, rscs)
end

def set_nic_settings(idx, nic_hash, value = nil)
object_send(:set_nic_settings, idx, nic_hash, value)
end
Expand Down
Expand Up @@ -140,28 +140,35 @@ module MiqAeServiceManageIQ_Providers_CloudManager_ProvisionSpec

if t != "google"
context "security_groups" do
before do
@ci = FactoryGirl.create("security_group_#{t}")
@c2 = FactoryGirl.create("security_group_#{t}")
allow_any_instance_of(workflow_klass).to receive(:allowed_security_groups).and_return(@ci.id => @ci.name, @c2.id => @c2.name)
end

it "workflow exposes allowed_security_groups" do
expect(workflow_klass.instance_methods).to include(:allowed_security_groups)
end

context "with a security_group" do
before do
@ci = FactoryGirl.create("security_group_#{t}")
allow_any_instance_of(workflow_klass).to receive(:allowed_security_groups).and_return(@ci.id => @ci.name)
end
it "#eligible_security_groups" do
result = ae_svc_prov.eligible_security_groups

expect(result).to be_kind_of(Array)
expect(result.first.class).to eq("MiqAeMethodService::MiqAeService#{@ci.class.name.gsub(/::/, '_')}".constantize)
end

it "#eligible_security_groups" do
result = ae_svc_prov.eligible_security_groups
it "#set_security_group" do
rsc = ae_svc_prov.eligible_security_groups.first
ae_svc_prov.set_security_group(rsc)

expect(result).to be_kind_of(Array)
expect(result.first.class).to eq("MiqAeMethodService::MiqAeService#{@ci.class.name.gsub(/::/, '_')}".constantize)
end
expect(@miq_provision.reload.options[:security_groups]).to eq([@ci.id])
end

it "#set_security_group" do
ae_svc_prov.eligible_security_groups.each { |rsc| ae_svc_prov.set_security_group(rsc) }
it "#set_security_groups" do
rscs = ae_svc_prov.eligible_security_groups
ae_svc_prov.set_security_groups(rscs)

expect(@miq_provision.reload.options[:security_groups]).to eq([@ci.id])
end
expect(@miq_provision.reload.options[:security_groups]).to match_array([@ci.id, @c2.id])
end
end
end
Expand Down

0 comments on commit 951d518

Please sign in to comment.