Sha256: 83d0b9cc91d56be23f4756dc9976a8d5f64b8ab4a3569d2ba7eb59fe67499a8d

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

module AnsibleTowerClient
  class Connection
    attr_reader :connection

    def initialize(options = nil)
      raise "Credentials are required" unless options[:username] && options[:password]
      raise ":base_url is required" unless options[:base_url]
      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 = Faraday.new(options[:base_url], :ssl => {:verify => verify_ssl}) do |f|
        f.use FaradayMiddleware::FollowRedirects, :limit => 3, :standards_compliant => true
        f.request(:url_encoded)
        f.response(:raise_tower_error)
        f.adapter(Faraday.default_adapter)
        f.basic_auth(options[:username], options[:password])
      end
    end

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

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

    def version
      config["version"]
    end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ansible_tower_client-0.3.0 lib/ansible_tower_client/connection.rb
ansible_tower_client-0.2.0 lib/ansible_tower_client/connection.rb