From 2f239dc4b7e6d1383a719af4f0a04d697ca2d3a9 Mon Sep 17 00:00:00 2001 From: Brandon Dunne Date: Mon, 17 Apr 2017 15:54:42 -0400 Subject: [PATCH] Reformat Ansible Tower error messages https://bugzilla.redhat.com/show_bug.cgi?id=1439135 --- .../automation_manager/configuration_script.rb | 6 ++++++ .../automation_manager/configuration_script.rb | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/models/manageiq/providers/ansible_tower/shared/automation_manager/configuration_script.rb b/app/models/manageiq/providers/ansible_tower/shared/automation_manager/configuration_script.rb index ef6e62e235b..c0c93d29c2a 100644 --- a/app/models/manageiq/providers/ansible_tower/shared/automation_manager/configuration_script.rb +++ b/app/models/manageiq/providers/ansible_tower/shared/automation_manager/configuration_script.rb @@ -21,6 +21,12 @@ def create_in_provider(manager_id, params) task_ids.each { |tid| MiqTask.wait_for_taskid(tid) } find_by!(:manager_id => manager.id, :manager_ref => job_template.id) + rescue AnsibleTowerClient::Error => err + begin + raise err.class, JSON.parse(err.message).values.flatten.join(", ") + rescue JSON::ParserError + raise err + end end def update_in_provider(manager_id, params) diff --git a/spec/support/ansible_shared/automation_manager/configuration_script.rb b/spec/support/ansible_shared/automation_manager/configuration_script.rb index d803f72aff4..78189429316 100644 --- a/spec/support/ansible_shared/automation_manager/configuration_script.rb +++ b/spec/support/ansible_shared/automation_manager/configuration_script.rb @@ -130,6 +130,22 @@ expect { described_class.create_in_provider(manager.id, params) }.to raise_error(ActiveRecord::RecordNotFound) end + + context "provider raises on create" do + it "with a hash" do + expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc) + expect(job_templates).to receive(:create!).and_raise(AnsibleTowerClient::Error, {"name"=>["Job template with this Name already exists."]}.to_json) + + expect { described_class.create_in_provider(manager.id, params) }.to raise_error(AnsibleTowerClient::Error, "Job template with this Name already exists.") + end + + it "with a string" do + expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc) + expect(job_templates).to receive(:create!).and_raise(AnsibleTowerClient::Error, "Job template with this Name already exists.") + + expect { described_class.create_in_provider(manager.id, params) }.to raise_error(AnsibleTowerClient::Error, "Job template with this Name already exists.") + end + end end it ".create_in_provider_queue" do