From 586af43e48cc1c65882185db93f0322dd23baa44 Mon Sep 17 00:00:00 2001 From: Brandon Dunne Date: Tue, 31 Jan 2017 10:04:15 -0500 Subject: [PATCH] Raise UnlicensedFeatureError when we receive a HTTP 402 Rescue UnlicensedFeatureError when trying to fetch survey_data https://bugzilla.redhat.com/show_bug.cgi?id=1370251 --- lib/ansible_tower_client/base_models/job_template.rb | 1 + lib/ansible_tower_client/exception.rb | 3 ++- lib/ansible_tower_client/middleware/raise_tower_error.rb | 2 ++ spec/middleware/raise_tower_error_spec.rb | 7 ++++++- spec/support/shared_examples/job_template.rb | 7 +++++++ 5 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/ansible_tower_client/base_models/job_template.rb b/lib/ansible_tower_client/base_models/job_template.rb index fffb1f5..32c29b1 100644 --- a/lib/ansible_tower_client/base_models/job_template.rb +++ b/lib/ansible_tower_client/base_models/job_template.rb @@ -11,6 +11,7 @@ def survey_spec spec_url = related['survey_spec'] return nil unless spec_url api.get(spec_url).body + rescue AnsibleTowerClient::UnlicensedFeatureError end def survey_spec_hash diff --git a/lib/ansible_tower_client/exception.rb b/lib/ansible_tower_client/exception.rb index 3df29f2..7a19a17 100644 --- a/lib/ansible_tower_client/exception.rb +++ b/lib/ansible_tower_client/exception.rb @@ -1,6 +1,7 @@ module AnsibleTowerClient class Error < Exception; end + class ClientError < Error; end class ConnectionError < Error; end class NoMethodError < Error; end - class ClientError < Error; end + class UnlicensedFeatureError < Error; end end diff --git a/lib/ansible_tower_client/middleware/raise_tower_error.rb b/lib/ansible_tower_client/middleware/raise_tower_error.rb index 72c13ad..98b23b9 100644 --- a/lib/ansible_tower_client/middleware/raise_tower_error.rb +++ b/lib/ansible_tower_client/middleware/raise_tower_error.rb @@ -5,6 +5,8 @@ class RaiseTowerError < Faraday::Response::Middleware def on_complete(env) case env[:status] + when 402 + raise AnsibleTowerClient::UnlicensedFeatureError when 404 raise Faraday::Error::ResourceNotFound, response_values(env) when 407 diff --git a/spec/middleware/raise_tower_error_spec.rb b/spec/middleware/raise_tower_error_spec.rb index 788d3d6..ded35bb 100644 --- a/spec/middleware/raise_tower_error_spec.rb +++ b/spec/middleware/raise_tower_error_spec.rb @@ -5,15 +5,20 @@ describe AnsibleTowerClient::Middleware::RaiseTowerError do context "Faraday::Error" do let(:env_400) { MockEnv.new('missing these attributes', 400) } + let(:env_402) { MockEnv.new('missing these attributes', 402) } let(:env_404) { MockEnv.new('missing these attributes', 404) } let(:env_407) { MockEnv.new('missing these attributes', 407) } let(:error) { AnsibleTowerClient::Middleware::RaiseTowerError.new } - it "raises ClientError and returns the body message with a status 404" do + it "raises ClientError and returns the body message with a status 400" do expect { error.on_complete(env_400) }.to raise_error(AnsibleTowerClient::ClientError, "missing these attributes") end + it "raises UnlicensedFeatureError with a status 402" do + expect { error.on_complete(env_402) }.to raise_error(AnsibleTowerClient::UnlicensedFeatureError) + end + it "raises ResourceNotFound exception with a status 404" do expect { error.on_complete(env_404) }.to raise_error(Faraday::Error::ResourceNotFound) end diff --git a/spec/support/shared_examples/job_template.rb b/spec/support/shared_examples/job_template.rb index 8e24137..00edb69 100644 --- a/spec/support/shared_examples/job_template.rb +++ b/spec/support/shared_examples/job_template.rb @@ -25,6 +25,13 @@ expect(described_class.new(api, raw_instance_no_survey).survey_spec).to be_nil end end + + describe "unlicensed" do + it "returns a survey spec" do + expect(api).to receive(:get).and_raise(AnsibleTowerClient::UnlicensedFeatureError) + expect(described_class.new(api, raw_instance).survey_spec).to be_nil + end + end end shared_examples_for "JobTemplate#survey_spec_hash" do