ansible_tower_client 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b2dc19710dcd49dc3f01afcb0c098c05b493ada
4
- data.tar.gz: 2b4c93dc99dd27fe22da7772d84f4b525705a575
3
+ metadata.gz: b745ac89684afcf6633ca64b8a8e8e16615c22c9
4
+ data.tar.gz: 5bad31a0e9ad53ff33c2e25d59d474638dae7a16
5
5
  SHA512:
6
- metadata.gz: 9f83694a37572b9605ef8c8ab3427beac58c12d568c4d535dbe1b751b18a4206a9f3912b6ade318ed6b2ac0e0e6dbebc1d3a7d95bdeec613cb160c5d5467c1d4
7
- data.tar.gz: 36e5f3499c43259246444b49f65a8b89328c6f811c54c0303ccf85d097692da1841252c0a2fa8e4a2c1ea6f579e111e586745c5f90cc3627671e34ec6fb48685
6
+ metadata.gz: 0eb05bf1f05c2091be7034346c0b4d400348b21adbafa6ede16e13ca1d7b8c43c6fdc59c7fba23299db3aae90f574cf298ca6f2fd8ba61a921d7a992388f90f9
7
+ data.tar.gz: de27338f770e7153fe5926a2824c187143ae6d6b672ba6d68e48a5af1fa5bca82b660ddf183fc3cc0d9d182138b08c399043ce0170afef1dd34f78a9c9166d70
data/.gitignore CHANGED
@@ -1,6 +1,7 @@
1
1
  /.bundle/
2
2
  /.yardoc
3
3
  /Gemfile.lock
4
+ /Gemfile.dev.rb
4
5
  /_yardoc/
5
6
  /coverage/
6
7
  /doc/
File without changes
data/Gemfile CHANGED
@@ -3,6 +3,9 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in ansible_tower_client.gemspec
4
4
  gemspec
5
5
 
6
+ # Pull in a development Gemfile if one exists
7
+ eval_gemfile('Gemfile.dev.rb') if File.exists?('Gemfile.dev.rb')
8
+
6
9
  # HACK: Rails 5 dropped support for Ruby < 2.2.2
7
10
  active_support_version = "< 5" if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.2.2")
8
11
  gem "activesupport", active_support_version
@@ -25,7 +25,6 @@ Gem::Specification.new do |spec|
25
25
  spec.add_runtime_dependency "more_core_extensions", "~> 3.0"
26
26
 
27
27
  spec.add_development_dependency "bundler", "~> 1.10"
28
- spec.add_development_dependency "byebug"
29
28
  spec.add_development_dependency "factory_girl"
30
29
  spec.add_development_dependency "rake", "~> 10.0"
31
30
  spec.add_development_dependency "rspec"
@@ -11,7 +11,9 @@ require "ansible_tower_client/hash_model"
11
11
  require "ansible_tower_client/base_model"
12
12
  require "ansible_tower_client/collection"
13
13
 
14
+ require "ansible_tower_client/base_models/activity_stream"
14
15
  require "ansible_tower_client/base_models/ad_hoc_command"
16
+ require "ansible_tower_client/base_models/credential"
15
17
  require "ansible_tower_client/base_models/group"
16
18
  require "ansible_tower_client/base_models/host"
17
19
  require "ansible_tower_client/base_models/inventory"
@@ -19,6 +21,7 @@ require "ansible_tower_client/base_models/inventory_source"
19
21
  require "ansible_tower_client/base_models/inventory_update"
20
22
  require "ansible_tower_client/base_models/job"
21
23
  require "ansible_tower_client/base_models/job_template"
24
+ require "ansible_tower_client/base_models/project"
22
25
 
23
26
  require "ansible_tower_client/v2/job_template_v2"
24
27
 
@@ -4,6 +4,8 @@ module AnsibleTowerClient
4
4
  class Api < Connection
5
5
  include Logging
6
6
 
7
+ DEFAULT_ERROR_MSG = "An unknown error was returned from the provider".freeze
8
+
7
9
  attr_reader :instance
8
10
  def initialize(connection)
9
11
  @instance = connection
@@ -21,10 +23,18 @@ module AnsibleTowerClient
21
23
  JSON.parse(get("me").body).fetch_path("results", 0, "username")
22
24
  end
23
25
 
26
+ def activity_stream
27
+ Collection.new(self, activity_stream_class)
28
+ end
29
+
24
30
  def ad_hoc_commands
25
31
  Collection.new(self, ad_hoc_command_class)
26
32
  end
27
33
 
34
+ def credentials
35
+ Collection.new(self, credential_class)
36
+ end
37
+
28
38
  def groups
29
39
  Collection.new(self, group_class)
30
40
  end
@@ -53,6 +63,10 @@ module AnsibleTowerClient
53
63
  Collection.new(self, job_template_class)
54
64
  end
55
65
 
66
+ def projects
67
+ Collection.new(self, project_class)
68
+ end
69
+
56
70
  def method_missing(method_name, *args, &block)
57
71
  if instance.respond_to?(method_name)
58
72
  logger.debug { "#{self.class.name} Sending <#{method_name}> with <#{args.inspect}>" }
@@ -65,10 +79,11 @@ module AnsibleTowerClient
65
79
  rescue Faraday::ConnectionFailed, Faraday::SSLError => err
66
80
  raise
67
81
  rescue Faraday::ClientError => err
82
+ raise if err.response.nil?
68
83
  response = err.response
69
84
  logger.debug { "#{self.class.name} #{err.class.name} #{response.pretty_inspect}" }
70
85
  message = JSON.parse(response[:body])['detail'] rescue nil
71
- message ||= "An unknown error was returned from the provider"
86
+ message ||= DEFAULT_ERROR_MSG
72
87
  logger.error("#{self.class.name} #{err.class.name} #{message}")
73
88
  raise AnsibleTowerClient::ConnectionError, message
74
89
  end
@@ -79,10 +94,18 @@ module AnsibleTowerClient
79
94
 
80
95
  # Object class accessors patched for the appropriate version of the API
81
96
 
97
+ def activity_stream_class
98
+ @activity_stream_class ||= AnsibleTowerClient::ActivityStream
99
+ end
100
+
82
101
  def ad_hoc_command_class
83
102
  @ad_hoc_command_class ||= AnsibleTowerClient::AdHocCommand
84
103
  end
85
104
 
105
+ def credential_class
106
+ @credential_class ||= AnsibleTowerClient::Credential
107
+ end
108
+
86
109
  def group_class
87
110
  @group_class ||= AnsibleTowerClient::Group
88
111
  end
@@ -116,5 +139,9 @@ module AnsibleTowerClient
116
139
  end
117
140
  end
118
141
  end
142
+
143
+ def project_class
144
+ @project_class ||= AnsibleTowerClient::Project
145
+ end
119
146
  end
120
147
  end
@@ -37,6 +37,110 @@ module AnsibleTowerClient
37
37
  super(raw_hash)
38
38
  end
39
39
 
40
+ # Persist a brand new record and return a
41
+ # representation of that object to the caller.
42
+ # Pass in the api connection and a JSON string.
43
+ #
44
+ # Example:
45
+ # project = AnsibleTowerClient::Project.create!(connection.api, {:name => 'test'}.to_json)
46
+ #
47
+ # # The values passed to create! are available in the resulting object
48
+ # project.name # => 'test'
49
+ # Errors:
50
+ # Any error raised by the API will be returned and logged
51
+ #
52
+ def self.create!(api, attributes)
53
+ response = api.post("#{endpoint}/", attributes).body
54
+ new(api, JSON.parse(response))
55
+ end
56
+
57
+ # Just like create! except a false
58
+ # is returned if the object is not saved.
59
+ #
60
+ def self.create(*args)
61
+ create!(*args)
62
+ rescue AnsibleTowerClient::Error
63
+ false
64
+ end
65
+
66
+ # Persist changes passed in as a Hash and return a
67
+ # representation of that object to the caller.
68
+ #
69
+ # Example:
70
+ # project = connection.api.projects.find 2
71
+ # project.update_attributes!(:name => 'test')
72
+ #
73
+ # # The values passed to update_attributes! are available in calling object
74
+ # project.name # => 'test'
75
+ # Errors:
76
+ # Any error raised by the API will be returned and logged
77
+ #
78
+ def update_attributes!(attributes)
79
+ @api.patch(url, attributes.to_json)
80
+ attributes.each do |method_name, value|
81
+ send("#{method_name}=", value)
82
+ end
83
+ true
84
+ end
85
+
86
+ # Just like update_attributes! except a true or false
87
+ # is returned if the object is saved or not.
88
+ #
89
+ def update_attributes(attributes)
90
+ update_attributes!(attributes)
91
+ rescue AnsibleTowerClient::Error
92
+ false
93
+ end
94
+
95
+ # Persist in memory changes.
96
+ #
97
+ # Example:
98
+ # project = connection.api.projects.find 2
99
+ # project.name = 'test'
100
+ # project.save!
101
+ #
102
+ # # The in memory values are persisted.
103
+ # project.name # => 'test'
104
+ # Errors:
105
+ # Any error raised by the API will be returned and logged
106
+ #
107
+ def save!
108
+ @api.patch(url, to_h.to_json)
109
+ true
110
+ end
111
+
112
+ # Just like save! except a true or false
113
+ # is returned if the object is saved or not.
114
+ #
115
+ def save
116
+ save!
117
+ rescue AnsibleTowerClient::Error
118
+ false
119
+ end
120
+
121
+ # Delete the current object and
122
+ # return the original instance.
123
+ #
124
+ # Example
125
+ # project = connection.api.projects.find 2
126
+ # project.destroy!
127
+ # Errors:
128
+ # Any error raised by the API will be returned and logged
129
+ #
130
+ def destroy!
131
+ @api.delete(url)
132
+ self
133
+ end
134
+
135
+ # Just like destroy! except a false
136
+ # is returned if the object is not deleted.
137
+ #
138
+ def destroy
139
+ destroy!
140
+ rescue AnsibleTowerClient::Error
141
+ false
142
+ end
143
+
40
144
  def hashify(attribute)
41
145
  YAML.safe_load(send(attribute))
42
146
  end
@@ -71,10 +175,6 @@ module AnsibleTowerClient
71
175
  key = "#{key}_id" if !key.end_with?('_id') && id_attrs.include?(key)
72
176
  key.underscore
73
177
  end
74
-
75
- def generate_writer?
76
- false
77
- end
78
178
  end
79
179
  end
80
180
  end
@@ -0,0 +1,7 @@
1
+ module AnsibleTowerClient
2
+ class ActivityStream < BaseModel
3
+ def self.endpoint
4
+ "activity_stream".freeze
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module AnsibleTowerClient
2
+ class Credential < BaseModel
3
+ def self.endpoint
4
+ "credentials".freeze
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module AnsibleTowerClient
2
+ class Project < BaseModel
3
+ def self.endpoint
4
+ "projects".freeze
5
+ end
6
+ end
7
+ end
@@ -6,17 +6,20 @@ module AnsibleTowerClient
6
6
  @klass = klass
7
7
  end
8
8
 
9
- def all
10
- find_all_by_url(klass.endpoint)
9
+ # @param get_options [Hash] a hash of http GET params to pass to the api request
10
+ # e.g. { :order_by => 'timestamp', :name__contains => 'foo' }
11
+ def all(get_options = nil)
12
+ find_all_by_url(klass.endpoint, get_options)
11
13
  end
12
14
 
13
- def find_all_by_url(url)
15
+ def find_all_by_url(url, get_options = nil)
14
16
  Enumerator.new do |yielder|
15
17
  @collection = []
16
18
  next_page = url
17
19
 
18
20
  loop do
19
- next_page = fetch_more_results(next_page) if @collection.empty?
21
+ next_page = fetch_more_results(next_page, get_options) if @collection.empty?
22
+ get_options = nil
20
23
  raise StopIteration if @collection.empty?
21
24
  yielder.yield(@collection.shift)
22
25
  end
@@ -27,15 +30,23 @@ module AnsibleTowerClient
27
30
  build_object(parse_response(api.get("#{klass.endpoint}/#{id}/")))
28
31
  end
29
32
 
33
+ def create!(*args)
34
+ klass.create!(api, *args)
35
+ end
36
+
37
+ def create(*args)
38
+ klass.create(api, *args)
39
+ end
40
+
30
41
  private
31
42
 
32
43
  def class_from_type(type)
33
44
  api.send("#{type}_class")
34
45
  end
35
46
 
36
- def fetch_more_results(next_page)
47
+ def fetch_more_results(next_page, get_options)
37
48
  return if next_page.nil?
38
- body = parse_response(api.get(next_page))
49
+ body = parse_response(api.get(next_page, get_options))
39
50
  parse_result_set(body["results"])
40
51
 
41
52
  body["next"]
@@ -1,3 +1,6 @@
1
1
  module AnsibleTowerClient
2
- class ConnectionError < Exception; end
2
+ class Error < Exception; end
3
+ class ConnectionError < Error; end
4
+ class NoMethodError < Error; end
5
+ class ClientError < Error; end
3
6
  end
@@ -1,3 +1,4 @@
1
+ require 'forwardable'
1
2
  module AnsibleTowerClient
2
3
  module Logging
3
4
  extend Forwardable
@@ -11,7 +11,7 @@ module AnsibleTowerClient
11
11
  # mimic the behavior that we get with proxy requests with HTTPS
12
12
  raise Faraday::Error::ConnectionFailed, %(407 "Proxy Authentication Required ")
13
13
  when CLIENT_ERROR_STATUSES
14
- raise Faraday::Error::ClientError, env.body
14
+ raise AnsibleTowerClient::ClientError, env.body
15
15
  end
16
16
  end
17
17
 
@@ -1,3 +1,3 @@
1
1
  module AnsibleTowerClient
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5.0".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ansible_tower_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Dunne
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-12-05 00:00:00.000000000 Z
12
+ date: 2017-01-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -81,20 +81,6 @@ dependencies:
81
81
  - - "~>"
82
82
  - !ruby/object:Gem::Version
83
83
  version: '1.10'
84
- - !ruby/object:Gem::Dependency
85
- name: byebug
86
- requirement: !ruby/object:Gem::Requirement
87
- requirements:
88
- - - ">="
89
- - !ruby/object:Gem::Version
90
- version: '0'
91
- type: :development
92
- prerelease: false
93
- version_requirements: !ruby/object:Gem::Requirement
94
- requirements:
95
- - - ">="
96
- - !ruby/object:Gem::Version
97
- version: '0'
98
84
  - !ruby/object:Gem::Dependency
99
85
  name: factory_girl
100
86
  requirement: !ruby/object:Gem::Requirement
@@ -147,6 +133,8 @@ extra_rdoc_files: []
147
133
  files:
148
134
  - ".gitignore"
149
135
  - ".rspec"
136
+ - ".rubocop.yml"
137
+ - ".rubocop_local.yml"
150
138
  - ".travis.yml"
151
139
  - Gemfile
152
140
  - LICENSE.txt
@@ -158,7 +146,9 @@ files:
158
146
  - lib/ansible_tower_client.rb
159
147
  - lib/ansible_tower_client/api.rb
160
148
  - lib/ansible_tower_client/base_model.rb
149
+ - lib/ansible_tower_client/base_models/activity_stream.rb
161
150
  - lib/ansible_tower_client/base_models/ad_hoc_command.rb
151
+ - lib/ansible_tower_client/base_models/credential.rb
162
152
  - lib/ansible_tower_client/base_models/group.rb
163
153
  - lib/ansible_tower_client/base_models/host.rb
164
154
  - lib/ansible_tower_client/base_models/inventory.rb
@@ -166,6 +156,7 @@ files:
166
156
  - lib/ansible_tower_client/base_models/inventory_update.rb
167
157
  - lib/ansible_tower_client/base_models/job.rb
168
158
  - lib/ansible_tower_client/base_models/job_template.rb
159
+ - lib/ansible_tower_client/base_models/project.rb
169
160
  - lib/ansible_tower_client/collection.rb
170
161
  - lib/ansible_tower_client/connection.rb
171
162
  - lib/ansible_tower_client/exception.rb