Skip to content

Commit

Permalink
Associate job with credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
bzwei committed Mar 3, 2017
1 parent 92b5071 commit 054598e
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 23 deletions.
7 changes: 6 additions & 1 deletion app/models/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ def self.new(*args, &block)
belongs_to :resource, :polymorphic => true

has_many :authentication_configuration_script_bases,
:dependent => :destroy
:dependent => :destroy
has_many :configuration_script_bases,
:through => :authentication_configuration_script_bases

has_many :authentication_orchestration_stacks,
:dependent => :destroy
has_many :orchestration_stacks,
:through => :authentication_orchestration_stacks

has_many :configuration_script_sources

before_save :set_credentials_changed_on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,25 @@ def refresh_ems
end

def update_with_provider_object(raw_job)
self.ems_ref ||= raw_job.id
self.status = raw_job.status
self.start_time ||= raw_job.started
self.finish_time ||= raw_job.finished
self.verbosity ||= raw_job.verbosity
update_attributes(
:ems_ref => raw_job.id,
:status => raw_job.status,
:start_time => raw_job.started,
:finish_time => raw_job.finished,
:verbosity => raw_job.verbosity
)

if parameters.empty?
self.parameters = raw_job.extra_vars_hash.collect do |para_key, para_val|
OrchestrationStackParameter.new(:name => para_key, :value => para_val, :ems_ref => "#{raw_job.id}_#{para_key}")
end
end
save!

if authentications.empty?
credential_types = %w(credential_id cloud_credential_id network_credential_id)
credential_refs = credential_types.collect { |attr| raw_job.try(attr) }.delete_blanks
self.authentications = ext_management_system.credentials.where(:manager_ref => credential_refs)
end
end
private :update_with_provider_object

Expand Down
3 changes: 3 additions & 0 deletions app/models/orchestration_stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class OrchestrationStack < ApplicationRecord
has_many :outputs, :dependent => :destroy, :foreign_key => :stack_id, :class_name => "OrchestrationStackOutput"
has_many :resources, :dependent => :destroy, :foreign_key => :stack_id, :class_name => "OrchestrationStackResource"

has_many :authentication_orchestration_stacks, :dependent => :destroy
has_many :authentications, :through => :authentication_orchestration_stacks

has_many :direct_vms, :class_name => "ManageIQ::Providers::CloudManager::Vm"
has_many :direct_security_groups, :class_name => "SecurityGroup"
has_many :direct_cloud_networks, :class_name => "CloudNetwork"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,24 @@

let(:manager) { FactoryGirl.create(:automation_manager_ansible_tower, :provider) }
let(:mock_api) { AnsibleTowerClient::Api.new(faraday_connection) }

let(:machine_credential) { FactoryGirl.create(:ansible_machine_credential, :manager_ref => '1', :resource => manager) }
let(:cloud_credential) { FactoryGirl.create(:ansible_cloud_credential, :manager_ref => '2', :resource => manager) }
let(:network_credential) { FactoryGirl.create(:ansible_network_credential, :manager_ref => '3', :resource => manager) }

let(:the_raw_job) do
AnsibleTowerClient::Job.new(
mock_api,
'id' => '1',
'name' => template.name,
'status' => 'Successful',
'extra_vars' => {'param1' => 'val1'}.to_json,
'verbosity' => 3,
'started' => Time.current,
'finished' => Time.current,
'id' => '1',
'name' => template.name,
'status' => 'Successful',
'extra_vars' => {'param1' => 'val1'}.to_json,
'verbosity' => 3,
'started' => Time.current,
'finished' => Time.current,
'credential_id' => machine_credential.manager_ref,
'cloud_credential_id' => cloud_credential.manager_ref,
'network_credential_id' => network_credential.manager_ref
).tap { |rjob| allow(rjob).to receive(:stdout).and_return('job stdout') }
end

Expand Down Expand Up @@ -62,9 +70,11 @@
:finish_time => the_raw_job.finished,
:verbosity => the_raw_job.verbosity
)
subject.reload
expect(subject.ems_ref).to eq(the_raw_job.id)
expect(subject.status).to eq(the_raw_job.status)
expect(subject.parameters.first).to have_attributes(:name => 'param1', :value => 'val1')
expect(subject.authentications).to match_array([machine_credential, cloud_credential, network_credential])
end

it 'catches errors from provider' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,24 @@

let(:manager) { FactoryGirl.create(:embedded_automation_manager_ansible, :provider) }
let(:mock_api) { AnsibleTowerClient::Api.new(faraday_connection) }

let(:machine_credential) { FactoryGirl.create(:ansible_machine_credential, :manager_ref => '1', :resource => manager) }
let(:cloud_credential) { FactoryGirl.create(:ansible_cloud_credential, :manager_ref => '2', :resource => manager) }
let(:network_credential) { FactoryGirl.create(:ansible_network_credential, :manager_ref => '3', :resource => manager) }

let(:the_raw_job) do
AnsibleTowerClient::Job.new(
mock_api,
'id' => '1',
'name' => template.name,
'status' => 'Successful',
'extra_vars' => {'param1' => 'val1'}.to_json,
'verbosity' => 3,
'started' => Time.current,
'finished' => Time.current,
'id' => '1',
'name' => template.name,
'status' => 'Successful',
'extra_vars' => {'param1' => 'val1'}.to_json,
'verbosity' => 3,
'started' => Time.current,
'finished' => Time.current,
'credential_id' => machine_credential.manager_ref,
'cloud_credential_id' => cloud_credential.manager_ref,
'network_credential_id' => network_credential.manager_ref
).tap { |rjob| allow(rjob).to receive(:stdout).and_return('job stdout') }
end

Expand Down Expand Up @@ -65,6 +73,7 @@
expect(subject.ems_ref).to eq(the_raw_job.id)
expect(subject.status).to eq(the_raw_job.status)
expect(subject.parameters.first).to have_attributes(:name => 'param1', :value => 'val1')
expect(subject.authentications).to match_array([machine_credential, cloud_credential, network_credential])
end

it 'catches errors from provider' do
Expand Down
5 changes: 3 additions & 2 deletions spec/models/service_ansible_tower_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
describe ServiceAnsibleTower do
let(:template_by_dialog) { FactoryGirl.create(:configuration_script) }
let(:template_by_setter) { FactoryGirl.create(:configuration_script) }
let(:tower) { FactoryGirl.create(:automation_manager_ansible_tower) }
let(:template_by_dialog) { FactoryGirl.create(:configuration_script, :manager => tower) }
let(:template_by_setter) { FactoryGirl.create(:configuration_script, :manager => tower) }

let(:dialog_options) do
{
Expand Down

0 comments on commit 054598e

Please sign in to comment.