Class: AnsibleTowerClient::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/ansible_tower_client/connection.rb

Direct Known Subclasses

Api

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Connection

Returns a new instance of Connection.



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
# File 'lib/ansible_tower_client/connection.rb', line 7

def initialize(options = nil)
  raise ":username and :password are required" if options.nil? || !options[:username] || !options[:password]
  raise ":base_url is required" unless options[:base_url]

  logger     = options[:logger] || AnsibleTowerClient.logger
  verify_ssl = options[:verify_ssl] || OpenSSL::SSL::VERIFY_PEER
  verify_ssl = verify_ssl == OpenSSL::SSL::VERIFY_NONE ? false : true

  require 'faraday'
  require 'faraday_middleware'
  require 'ansible_tower_client/middleware/raise_tower_error'
  Faraday::Response.register_middleware :raise_tower_error => -> { Middleware::RaiseTowerError }

  connection_opts = { :ssl => {:verify => verify_ssl} }
  connection_opts[:proxy] = options[:proxy] if options[:proxy].present?
  connection_opts[:headers] = options[:headers] if options[:headers].present?
  connection_opts[:request] = options[:request] if options[:request].present?

  @connection = Faraday.new(options[:base_url], connection_opts) do |f|
    f.use(FaradayMiddleware::EncodeJson)
    f.use(FaradayMiddleware::FollowRedirects, :limit => 3, :standards_compliant => true)
    f.request(:url_encoded)
    f.response(:raise_tower_error)
    f.response(:logger, logger)
    f.adapter(Faraday.default_adapter)
    f.basic_auth(options[:username], options[:password])
  end
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



5
6
7
# File 'lib/ansible_tower_client/connection.rb', line 5

def connection
  @connection
end

Instance Method Details

#apiObject



36
37
38
# File 'lib/ansible_tower_client/connection.rb', line 36

def api
  @api ||= Api.new(connection)
end