This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathapi.rb
185 lines (144 loc) · 4.29 KB
/
api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
require 'pp'
module AnsibleTowerClient
class Api < Connection
include Logging
DEFAULT_ERROR_MSG = "An unknown error was returned from the provider".freeze
attr_reader :instance
def initialize(connection)
@instance = connection
end
def config
JSON.parse(get("config").body)
end
def version
@version ||= config["version"]
end
def verify_credentials
JSON.parse(get("me").body).fetch_path("results", 0, "username")
end
def activity_stream
Collection.new(self, activity_stream_class)
end
def ad_hoc_commands
Collection.new(self, ad_hoc_command_class)
end
def credentials
Collection.new(self, credential_class)
end
def groups
Collection.new(self, group_class)
end
def hosts
Collection.new(self, host_class)
end
def inventories
Collection.new(self, inventory_class)
end
def inventory_sources
Collection.new(self, inventory_source_class)
end
def inventory_updates
Collection.new(self, inventory_update_class)
end
def jobs
Collection.new(self, job_class)
end
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
def organizations
Collection.new(self, organization_class)
end
def projects
Collection.new(self, project_class)
end
def project_updates
Collection.new(self, project_update_class)
end
def method_missing(method_name, *args, &block)
if instance.respond_to?(method_name)
path = build_path_to_resource(args.shift)
args.unshift(path)
logger.debug { "#{self.class.name} Sending <#{method_name}> with <#{args.inspect}>" }
instance.send(method_name, *args, &block).tap do |response|
logger.debug { "#{self.class.name} Response:\n#{JSON.parse(response.body).pretty_inspect}" }
end
else
super
end
rescue Faraday::ConnectionFailed => err
raise AnsibleTowerClient::ConnectionError, err
rescue Faraday::SSLError => err
raise AnsibleTowerClient::SSLError, err
rescue Faraday::ClientError => err
raise AnsibleTowerClient::ConnectionError, err
end
def respond_to_missing?(method, _include_private = false)
instance.respond_to?(method)
end
# Object class accessors patched for the appropriate version of the API
def activity_stream_class
@activity_stream_class ||= AnsibleTowerClient::ActivityStream
end
def ad_hoc_command_class
@ad_hoc_command_class ||= AnsibleTowerClient::AdHocCommand
end
def credential_class
@credential_class ||= AnsibleTowerClient::Credential
end
def group_class
@group_class ||= AnsibleTowerClient::Group
end
def host_class
@host_class ||= AnsibleTowerClient::Host
end
def inventory_class
@inventory_class ||= AnsibleTowerClient::Inventory
end
def inventory_source_class
@inventory_source_class ||= AnsibleTowerClient::InventorySource
end
def inventory_update_class
@inventory_update_class ||= AnsibleTowerClient::InventoryUpdate
end
def job_class
@job_class ||= AnsibleTowerClient::Job
end
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))
AnsibleTowerClient::JobTemplateV2
else
AnsibleTowerClient::JobTemplate
end
end
end
def organization_class
@organization_class ||= AnsibleTowerClient::Organization
end
def project_class
@project_class ||= AnsibleTowerClient::Project
end
def project_update_class
@project_update_class ||= AnsibleTowerClient::ProjectUpdate
end
private
def build_path_to_resource(original)
return original unless %r{\/?api\/v1\/(.*)} =~ original
return original if instance.url_prefix.path == "/"
File.join(instance.url_prefix.path, Regexp.last_match[1])
end
end
end