Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #78 from syncrou/override_type_when_appropriate
Browse files Browse the repository at this point in the history
Override type when appropriate
  • Loading branch information
gmcculloug authored Mar 2, 2017
2 parents 38393bd + be7f540 commit d4e0738
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/ansible_tower_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
require "ansible_tower_client/base_models/inventory_update"
require "ansible_tower_client/base_models/job"
require "ansible_tower_client/base_models/job_event"
require "ansible_tower_client/base_models/job_play"
require "ansible_tower_client/base_models/job_template"
require "ansible_tower_client/base_models/organization"
require "ansible_tower_client/base_models/project"
Expand Down
8 changes: 8 additions & 0 deletions lib/ansible_tower_client/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ def job_events
Collection.new(self, job_event_class)
end

def job_plays
Collection.new(self, job_play_class)
end

def job_templates
Collection.new(self, job_template_class)
end
Expand Down Expand Up @@ -140,6 +144,10 @@ def job_event_class
@job_event_class ||= AnsibleTowerClient::JobEvent
end

def job_play_class
@job_play_class ||= AnsibleTowerClient::JobPlay
end

def job_template_class
@job_template_class ||= begin
if Gem::Version.new(version).between?(Gem::Version.new(2), Gem::Version.new(3))
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible_tower_client/base_models/host.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module AnsibleTowerClient
class Host < BaseModel
def groups
Collection.new(api).find_all_by_url(related["groups"])
Collection.new(api, api.group_class).find_all_by_url(related["groups"])
end
end
end
2 changes: 1 addition & 1 deletion lib/ansible_tower_client/base_models/inventory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module AnsibleTowerClient
class Inventory < BaseModel
def inventory_sources
Collection.new(api).find_all_by_url(related['inventory_sources'])
Collection.new(api, api.inventory_source_class).find_all_by_url(related['inventory_sources'])
end

def update_all_inventory_sources
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible_tower_client/base_models/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def extra_vars_hash
end

def job_plays
Collection.new(api).find_all_by_url(related["job_plays"])
Collection.new(api, api.job_play_class).find_all_by_url(related["job_plays"])
end

def stdout
Expand Down
4 changes: 4 additions & 0 deletions lib/ansible_tower_client/base_models/job_play.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module AnsibleTowerClient
class JobPlay < BaseModel
end
end
2 changes: 1 addition & 1 deletion lib/ansible_tower_client/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def parse_result_set(body)
end

def build_object(result)
class_from_type(result["type"]).new(api, result)
(klass || class_from_type(result['type'])).new(api, result)
end
end
end
5 changes: 3 additions & 2 deletions spec/factories/responses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
klass ""
type { AnsibleTowerClient::FactoryHelper.underscore_string(klass.namespace.last) }
name { "#{type}-#{id}" }
url { "/api/v1/#{klass.endpoint}/#{id}/" }
url { "/api/v1/endpoint/" }
related { {"survey_spec" => "example.com/api", 'inventory' => 'inventory link', 'stdout' => 'example.com/api'} }
limit { "" }

Expand All @@ -35,7 +35,7 @@
trait(:inventory_id) { inventory { rand(500) } }
trait(:kind) { kind { "machine" } }
trait(:organization) { organization { rand(500) } }
trait(:url) { "/api/v1/#{klass}/#{rand(10)}" }
trait(:url) { url { "api/v1/endpoint/#{rand(10)}" } }
trait(:username) { username { "random username" } }

trait(:credential) { [description, kind, username] }
Expand All @@ -45,6 +45,7 @@
trait(:job) { [description, extra_vars] }
trait(:project) { [description, organization] }
trait(:job_event) { [url] }
trait(:job_play) { [url] }

initialize_with { AnsibleTowerClient::FactoryHelper.stringify_attribute_keys(attributes) }
end
Expand Down
2 changes: 1 addition & 1 deletion spec/job_event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
let(:api) { AnsibleTowerClient::Api.new(instance_double("Faraday::Connection")) }
let(:collection) { api.job_events }
let(:raw_url_collection) { build(:response_url_collection, :klass => described_class, :url => url) }
let(:raw_instance) { build(:response_instance, :group, :klass => described_class) }
let(:raw_instance) { build(:response_instance, :job_event, :klass => described_class) }

include_examples "Collection Methods"
include_examples "Api Methods"
Expand Down
21 changes: 21 additions & 0 deletions spec/job_play_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'json'

describe AnsibleTowerClient::JobPlay do
let(:url) { "example.com/api/v1/job_plays" }
let(:api) { AnsibleTowerClient::Api.new(instance_double("Faraday::Connection")) }
let(:collection) { api.job_plays }
let(:raw_collection) { build(:response_collection, :klass => described_class) }
let(:raw_url_collection) { build(:response_url_collection, :klass => described_class, :url => url) }
let(:raw_instance) { build(:response_instance, :job_play, :klass => described_class) }

include_examples "Collection Methods"
include_examples "Crud Methods"
include_examples "Api Methods"

it "#initialize instantiates an #{described_class} from a hash" do
obj = described_class.new(instance_double("AnsibleTowerClient::Api"), raw_instance)

expect(obj).to be_a described_class
expect(obj.url).to be_a String
end
end
10 changes: 5 additions & 5 deletions spec/job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@
context '#job_plays' do
let(:url) { "example.com/api/v1/job_plays" }
let(:job_collection) { build(:response_collection, :klass => described_class) }
let(:job_events) { build(:response_url_collection, :klass => AnsibleTowerClient::JobEvent, :url => url) }
it "returns a collection of AnsibleTowerClient::JobEvents" do
expect(AnsibleTowerClient::Collection).to receive(:new).with(api).and_return(job_collection)
expect(job_collection).to receive(:find_all_by_url).and_return(job_events['results'])
let(:job_plays) { build(:response_url_collection, :klass => AnsibleTowerClient::JobPlay, :url => url) }
it "returns a collection of AnsibleTowerClient::JobPlays" do
expect(AnsibleTowerClient::Collection).to receive(:new).with(api, api.job_play_class).and_return(job_collection)
expect(job_collection).to receive(:find_all_by_url).and_return(job_plays['results'])
results = described_class.new(api, raw_instance).job_plays.first
expect(results).to include(
"type" => "job_event",
"type" => "job_play",
"url" => "example.com/api/v1/job_plays",
)
end
Expand Down

0 comments on commit d4e0738

Please sign in to comment.