Module: CemAcpt::Goss::Api

Defined in:
lib/cem_acpt/goss/api.rb,
lib/cem_acpt/goss/api/action_response.rb

Overview

Holds methods for interacting with the Goss API running on a test node.

Defined Under Namespace

Modules: DurationHandler Classes: ActionResponse, ActionResponseResult, ActionResponseSummary

Constant Summary collapse

ACTIONS =

The actions that can be run against the Goss API. The key is the action name and the value is the port/endpoint of the action.

{
  acpt: '8080/acpt',
  idempotent: '8081/idempotent',
  noop: '8082/noop',
}.freeze

Class Method Summary collapse

Class Method Details

.action_uri(host, action) ⇒ URI

Create a URI for the specified action against the specified host.

Parameters:

  • host (String)

    The host to run the action against. This should be a public IP address or a DNS-resolvable name.

  • action (Symbol)

    The action to run.

Returns:

  • (URI)

    The URI for the action.



27
28
29
# File 'lib/cem_acpt/goss/api.rb', line 27

def action_uri(host, action)
  URI("http://#{host}:#{ACTIONS[action.to_sym]}")
end

.run_action(internet, host, action) ⇒ ActionResponse

Run the specified action against the specified host.

Parameters:

  • internet (Async::HTTP::Internet)

    The Async::HTTP::Internet object to use for the request.

  • host (String)

    The host to run the action against. This should be a public IP address or a DNS-resolvable name.

  • action (Symbol)

    The action to run.

Returns:



74
75
76
77
78
79
# File 'lib/cem_acpt/goss/api.rb', line 74

def run_action(internet, host, action)
  uri = action_uri(host, action)
  response = internet.get(uri.to_s)
  body = JSON.parse(response.read)
  ActionResponse.new(host, action, response.status, body)
end

.run_actions_async(hosts, results: Queue.new, only: [], except: []) ⇒ Queue

Run the specified actions against the specified hosts asynchronously.

Parameters:

  • hosts (Array<String>)

    The hosts to run the actions against. Each host should be a public IP address or a DNS-resolvable name.

  • results (Queue) (defaults to: Queue.new)

    The queue to push the results to.

  • only (Array<Symbol>) (defaults to: [])

    The actions to run.

  • except (Array<Symbol>) (defaults to: [])

    The actions to skip.

Returns:

  • (Queue)

    The queue of results.

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cem_acpt/goss/api.rb', line 38

def run_actions_async(hosts, results: Queue.new, only: [], except: [])
  raise ArgumentError, 'hosts must be an Array' unless hosts.is_a?(Array)
  raise ArgumentError, 'results must be a Queue-like object implementing #<<' unless results.respond_to?(:<<)
  raise ArgumentError, 'only must be an Array' unless except.is_a?(Array)
  raise ArgumentError, 'except must be an Array' unless except.is_a?(Array)
  only.map!(&:to_sym)
  except.map!(&:to_sym)
  only_specified = !only.empty?
  except_specified = !except.empty?
  Async do
    internet = Async::HTTP::Internet.new
    barrier = Async::Barrier.new
    barrier.async do
      hosts.each do |host|
        ACTIONS.keys.each do |action, _|
          next if only_specified && !only.include?(action)
          next if except_specified && except.include?(action)

          results << run_action(internet, host, action)
        end
      end
    end
    barrier.wait
  ensure
    internet&.close
    results.close if results.respond_to?(:close)
  end
  results
end