diff --git a/app/models/manageiq/providers/embedded_ansible/provider.rb b/app/models/manageiq/providers/embedded_ansible/provider.rb index 854ba6a6c05..224d411cb39 100644 --- a/app/models/manageiq/providers/embedded_ansible/provider.rb +++ b/app/models/manageiq/providers/embedded_ansible/provider.rb @@ -1,6 +1,8 @@ class ManageIQ::Providers::EmbeddedAnsible::Provider < ::Provider include ManageIQ::Providers::AnsibleTower::Shared::Provider + include_concern 'DefaultAnsibleObjects' + has_one :automation_manager, :foreign_key => "provider_id", :class_name => "ManageIQ::Providers::EmbeddedAnsible::AutomationManager", diff --git a/app/models/manageiq/providers/embedded_ansible/provider/default_ansible_objects.rb b/app/models/manageiq/providers/embedded_ansible/provider/default_ansible_objects.rb new file mode 100644 index 00000000000..d4fb7f75db9 --- /dev/null +++ b/app/models/manageiq/providers/embedded_ansible/provider/default_ansible_objects.rb @@ -0,0 +1,55 @@ +module ManageIQ::Providers::EmbeddedAnsible::Provider::DefaultAnsibleObjects + extend ActiveSupport::Concern + + ANSIBLE_OBJECT_SOURCE = "MIQ_ANSIBLE".freeze + + included do + has_many :default_ansible_objects, -> { where(:source => ANSIBLE_OBJECT_SOURCE) }, :as => :resource, :dependent => :destroy, :class_name => "CustomAttribute" + end + + def default_organization + get_default_ansible_object("organization") + end + + def default_credential + get_default_ansible_object("credential") + end + + def default_inventory + get_default_ansible_object("inventory") + end + + def default_host + get_default_ansible_object("host") + end + + def default_organization=(org) + set_default_ansible_object("organization", org) + end + + def default_credential=(cred) + set_default_ansible_object("credential", cred) + end + + def default_inventory=(inv) + set_default_ansible_object("inventory", inv) + end + + def default_host=(host) + set_default_ansible_object("host", host) + end + + def delete_ansible_object(name) + default_ansible_objects.find_by(:name => name).try(:delete) + end + + private + + def get_default_ansible_object(name) + default_ansible_objects.find_by(:name => name).try(:value).try(:to_i) + end + + def set_default_ansible_object(name, value) + default_ansible_objects.find_or_initialize_by(:name => name).update_attributes(:value => value) + end +end diff --git a/spec/models/manageiq/providers/embedded_ansible/provider_spec.rb b/spec/models/manageiq/providers/embedded_ansible/provider_spec.rb index c399fa76485..9534228531c 100644 --- a/spec/models/manageiq/providers/embedded_ansible/provider_spec.rb +++ b/spec/models/manageiq/providers/embedded_ansible/provider_spec.rb @@ -2,4 +2,44 @@ describe ManageIQ::Providers::EmbeddedAnsible::Provider do it_behaves_like 'ansible provider' + + subject { FactoryGirl.build(:provider_embedded_ansible) } + + context "DefaultAnsibleObjects concern" do + before do + subject.save + end + + context "with no attributes" do + %w(organization credential inventory host).each do |obj_name| + it "#default_#{obj_name} returns nil" do + expect(subject.public_send("default_#{obj_name}")).to be_nil + end + + it "#default_#{obj_name}= creates a new custom attribute" do + subject.public_send("default_#{obj_name}=", obj_name.length) + expect(subject.default_ansible_objects.find_by(:name => obj_name).value.to_i).to eq(obj_name.length) + end + end + end + + context "with attributes saved" do + before do + %w(organization credential inventory host).each do |obj_name| + subject.default_ansible_objects.create(:name => obj_name, :value => obj_name.length) + end + end + + %w(organization credential inventory host).each do |obj_name| + it "#default_#{obj_name} returns the saved value" do + expect(subject.public_send("default_#{obj_name}")).to eq(obj_name.length) + end + + it "#default_#{obj_name}= doesn't create a second object if we pass the same value" do + subject.public_send("default_#{obj_name}=", obj_name.length) + expect(subject.default_ansible_objects.where(:name => obj_name).count).to eq(1) + end + end + end + end end