Skip to content

Commit

Permalink
Add a concern for storing and accessing default embedded ansible obje…
Browse files Browse the repository at this point in the history
…ct ids

This will allow us to determine the ansible side object to relate
new objects to.

This also frees us from having to store an extra flag in our inventory
to identify which objects are the "system created" ones and which
are the other objects that may be artifacts of running jobs.

https://www.pivotaltracker.com/story/show/140098929
  • Loading branch information
carbonin committed Mar 17, 2017
1 parent a143dcd commit 5921219
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/models/manageiq/providers/embedded_ansible/provider.rb
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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
40 changes: 40 additions & 0 deletions spec/models/manageiq/providers/embedded_ansible/provider_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5921219

Please sign in to comment.