Class: ForemanApiClient::Connection
- Inherits:
-
Object
- Object
- ForemanApiClient::Connection
- Includes:
- Logging
- Defined in:
- lib/foreman_api_client/connection.rb
Constant Summary collapse
- ALLOW_404 =
some foreman servers don’t have locations or organizations, just return nil
[:locations, :organizations]
Instance Attribute Summary collapse
-
#connection_attrs ⇒ Object
Returns the value of attribute connection_attrs.
Instance Method Summary collapse
- #all(resource, filter = {}) ⇒ Object
-
#all_with_details(resource, filter = {}) ⇒ Object
ala n+1.
-
#api_cached? ⇒ Boolean
used for tests to manually invoke loading api from server this keeps http calls consistent.
- #ensure_api_cached ⇒ Object
-
#fetch(resource, action = :index, filter = {}) ⇒ Object
filter: “page” => 2, “per_page” => 50, “search” => “field=value”, “value”.
- #host(manager_ref) ⇒ Object
-
#initialize(attrs) ⇒ Connection
constructor
A new instance of Connection.
- #load_details(resources, resource) ⇒ Object
- #verify? ⇒ Boolean
Methods included from Logging
Constructor Details
#initialize(attrs) ⇒ Connection
Returns a new instance of Connection.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/foreman_api_client/connection.rb', line 8 def initialize(attrs) self.connection_attrs = attrs.dup connection_attrs[:uri] = connection_attrs.delete(:base_url) connection_attrs[:api_version] ||= 2 connection_attrs[:apidoc_cache_dir] ||= tmpdir = { verify_ssl: connection_attrs.delete(:verify_ssl), ssl_ca_file: connection_attrs.delete(:ssl_ca_file) }.compact @api = ApipieBindings::API.new(connection_attrs, ) end |
Instance Attribute Details
#connection_attrs ⇒ Object
Returns the value of attribute connection_attrs.
6 7 8 |
# File 'lib/foreman_api_client/connection.rb', line 6 def connection_attrs @connection_attrs end |
Instance Method Details
#all(resource, filter = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/foreman_api_client/connection.rb', line 25 def all(resource, filter = {}) page = 0 all = [] loop do page_params = {:page => (page += 1), :per_page => 50}.merge(filter) small = fetch(resource, :index, page_params) return if small.nil? # 404 all += small.to_a break if small.empty? || all.size >= small.total end PagedResponse.new(all) end |
#all_with_details(resource, filter = {}) ⇒ Object
ala n+1
40 41 42 |
# File 'lib/foreman_api_client/connection.rb', line 40 def all_with_details(resource, filter = {}) load_details(all(resource, filter), resource) end |
#api_cached? ⇒ Boolean
used for tests to manually invoke loading api from server this keeps http calls consistent
65 66 67 |
# File 'lib/foreman_api_client/connection.rb', line 65 def api_cached? File.exist?(@api.apidoc_cache_file) end |
#ensure_api_cached ⇒ Object
69 70 71 |
# File 'lib/foreman_api_client/connection.rb', line 69 def ensure_api_cached @api.apidoc end |
#fetch(resource, action = :index, filter = {}) ⇒ Object
filter: “page” => 2, “per_page” => 50, “search” => “field=value”, “value”
49 50 51 52 53 54 55 56 |
# File 'lib/foreman_api_client/connection.rb', line 49 def fetch(resource, action = :index, filter = {}) action, filter = :index, action if action.kind_of?(Hash) logger.info("#{self.class.name}##{__method__} Calling Apipie Resource: #{resource.inspect} Action: #{action.inspect} Params: #{dump_hash(filter)}") PagedResponse.new(@api.resource(resource).action(action).call(filter)) rescue RestClient::ResourceNotFound raise unless ALLOW_404.include?(resource) nil end |
#host(manager_ref) ⇒ Object
58 59 60 |
# File 'lib/foreman_api_client/connection.rb', line 58 def host(manager_ref) ::ForemanApiClient::Host.new(self, manager_ref) end |
#load_details(resources, resource) ⇒ Object
44 45 46 |
# File 'lib/foreman_api_client/connection.rb', line 44 def load_details(resources, resource) resources.map! { |os| fetch(resource, :show, "id" => os["id"]).first } if resources end |
#verify? ⇒ Boolean
20 21 22 23 |
# File 'lib/foreman_api_client/connection.rb', line 20 def verify? results = Array(fetch(:home).try(:results)).first results.respond_to?(:key?) && results.key?("links") end |