Class: AnsibleTowerClient::Api

Inherits:
Connection show all
Includes:
Logging
Defined in:
lib/ansible_tower_client/api.rb

Constant Summary collapse

DEFAULT_ERROR_MSG =
"An unknown error was returned from the provider".freeze

Instance Attribute Summary collapse

Attributes inherited from Connection

#connection

Instance Method Summary collapse

Methods inherited from Connection

#api

Constructor Details

#initialize(connection) ⇒ Api

Returns a new instance of Api.



10
11
12
# File 'lib/ansible_tower_client/api.rb', line 10

def initialize(connection)
  @instance = connection
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ansible_tower_client/api.rb', line 86

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

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



9
10
11
# File 'lib/ansible_tower_client/api.rb', line 9

def instance
  @instance
end

Instance Method Details

#activity_streamObject



26
27
28
# File 'lib/ansible_tower_client/api.rb', line 26

def activity_stream
  Collection.new(self, activity_stream_class)
end

#activity_stream_classObject

Object class accessors patched for the appropriate version of the API



111
112
113
# File 'lib/ansible_tower_client/api.rb', line 111

def activity_stream_class
  @activity_stream_class ||= AnsibleTowerClient::ActivityStream
end

#ad_hoc_command_classObject



115
116
117
# File 'lib/ansible_tower_client/api.rb', line 115

def ad_hoc_command_class
  @ad_hoc_command_class ||= AnsibleTowerClient::AdHocCommand
end

#ad_hoc_commandsObject



30
31
32
# File 'lib/ansible_tower_client/api.rb', line 30

def ad_hoc_commands
  Collection.new(self, ad_hoc_command_class)
end

#configObject



14
15
16
# File 'lib/ansible_tower_client/api.rb', line 14

def config
  JSON.parse(get("config").body)
end

#credential_classObject



119
120
121
# File 'lib/ansible_tower_client/api.rb', line 119

def credential_class
  @credential_class ||= AnsibleTowerClient::Credential
end

#credentialsObject



34
35
36
# File 'lib/ansible_tower_client/api.rb', line 34

def credentials
  Collection.new(self, credential_class)
end

#group_classObject



123
124
125
# File 'lib/ansible_tower_client/api.rb', line 123

def group_class
  @group_class ||= AnsibleTowerClient::Group
end

#groupsObject



38
39
40
# File 'lib/ansible_tower_client/api.rb', line 38

def groups
  Collection.new(self, group_class)
end

#host_classObject



127
128
129
# File 'lib/ansible_tower_client/api.rb', line 127

def host_class
  @host_class ||= AnsibleTowerClient::Host
end

#hostsObject



42
43
44
# File 'lib/ansible_tower_client/api.rb', line 42

def hosts
  Collection.new(self, host_class)
end

#inventoriesObject



46
47
48
# File 'lib/ansible_tower_client/api.rb', line 46

def inventories
  Collection.new(self, inventory_class)
end

#inventory_classObject



131
132
133
# File 'lib/ansible_tower_client/api.rb', line 131

def inventory_class
  @inventory_class ||= AnsibleTowerClient::Inventory
end

#inventory_source_classObject



135
136
137
# File 'lib/ansible_tower_client/api.rb', line 135

def inventory_source_class
  @inventory_source_class ||= AnsibleTowerClient::InventorySource
end

#inventory_sourcesObject



50
51
52
# File 'lib/ansible_tower_client/api.rb', line 50

def inventory_sources
  Collection.new(self, inventory_source_class)
end

#inventory_update_classObject



139
140
141
# File 'lib/ansible_tower_client/api.rb', line 139

def inventory_update_class
  @inventory_update_class ||= AnsibleTowerClient::InventoryUpdate
end

#inventory_updatesObject



54
55
56
# File 'lib/ansible_tower_client/api.rb', line 54

def inventory_updates
  Collection.new(self, inventory_update_class)
end

#job_classObject



143
144
145
# File 'lib/ansible_tower_client/api.rb', line 143

def job_class
  @job_class ||= AnsibleTowerClient::Job
end

#job_event_classObject



147
148
149
# File 'lib/ansible_tower_client/api.rb', line 147

def job_event_class
  @job_event_class ||= AnsibleTowerClient::JobEvent
end

#job_eventsObject



62
63
64
# File 'lib/ansible_tower_client/api.rb', line 62

def job_events
  Collection.new(self, job_event_class)
end

#job_play_classObject



151
152
153
# File 'lib/ansible_tower_client/api.rb', line 151

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

#job_playsObject



66
67
68
# File 'lib/ansible_tower_client/api.rb', line 66

def job_plays
  Collection.new(self, job_play_class)
end

#job_template_classObject



155
156
157
158
159
160
161
162
163
# File 'lib/ansible_tower_client/api.rb', line 155

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

#job_templatesObject



70
71
72
# File 'lib/ansible_tower_client/api.rb', line 70

def job_templates
  Collection.new(self, job_template_class)
end

#jobsObject



58
59
60
# File 'lib/ansible_tower_client/api.rb', line 58

def jobs
  Collection.new(self, job_class)
end

#organization_classObject



165
166
167
# File 'lib/ansible_tower_client/api.rb', line 165

def organization_class
  @organization_class ||= AnsibleTowerClient::Organization
end

#organizationsObject



74
75
76
# File 'lib/ansible_tower_client/api.rb', line 74

def organizations
  Collection.new(self, organization_class)
end

#project_classObject



169
170
171
# File 'lib/ansible_tower_client/api.rb', line 169

def project_class
  @project_class ||= AnsibleTowerClient::Project
end

#project_update_classObject



173
174
175
# File 'lib/ansible_tower_client/api.rb', line 173

def project_update_class
  @project_update_class ||= AnsibleTowerClient::ProjectUpdate
end

#project_updatesObject



82
83
84
# File 'lib/ansible_tower_client/api.rb', line 82

def project_updates
  Collection.new(self, project_update_class)
end

#projectsObject



78
79
80
# File 'lib/ansible_tower_client/api.rb', line 78

def projects
  Collection.new(self, project_class)
end

#respond_to_missing?(method, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/ansible_tower_client/api.rb', line 105

def respond_to_missing?(method, _include_private = false)
  instance.respond_to?(method)
end

#verify_credentialsObject



22
23
24
# File 'lib/ansible_tower_client/api.rb', line 22

def verify_credentials
  JSON.parse(get("me").body).fetch_path("results", 0, "username")
end

#versionObject



18
19
20
# File 'lib/ansible_tower_client/api.rb', line 18

def version
  @version ||= config["version"]
end