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 |
# 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)} @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
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/foreman_api_client/connection.rb', line 22 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
37 38 39 |
# File 'lib/foreman_api_client/connection.rb', line 37 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
62 63 64 |
# File 'lib/foreman_api_client/connection.rb', line 62 def api_cached? File.exist?(@api.apidoc_cache_file) end |
#ensure_api_cached ⇒ Object
66 67 68 |
# File 'lib/foreman_api_client/connection.rb', line 66 def ensure_api_cached @api.apidoc end |
#fetch(resource, action = :index, filter = {}) ⇒ Object
filter: “page” => 2, “per_page” => 50, “search” => “field=value”, “value”
46 47 48 49 50 51 52 53 |
# File 'lib/foreman_api_client/connection.rb', line 46 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
55 56 57 |
# File 'lib/foreman_api_client/connection.rb', line 55 def host(manager_ref) ::ForemanApiClient::Host.new(self, manager_ref) end |
#load_details(resources, resource) ⇒ Object
41 42 43 |
# File 'lib/foreman_api_client/connection.rb', line 41 def load_details(resources, resource) resources.map! { |os| fetch(resource, :show, "id" => os["id"]).first } if resources end |
#verify? ⇒ Boolean
17 18 19 20 |
# File 'lib/foreman_api_client/connection.rb', line 17 def verify? results = Array(fetch(:home).try(:results)).first results.respond_to?(:key?) && results.key?("links") end |