Class: PuppetX::Relay::Util::HTTP::RelayAPI

Inherits:
Client
  • Object
show all
Defined in:
lib/puppet_x/relay/util/http/relay_api.rb

Defined Under Namespace

Classes: MissingTokenError

Instance Method Summary collapse

Methods inherited from Client

#get, #post, #put, #request

Constructor Details

#initialize(base_url, token, settings = nil) ⇒ RelayAPI

Returns a new instance of RelayAPI.

Raises:



15
16
17
18
19
20
# File 'lib/puppet_x/relay/util/http/relay_api.rb', line 15

def initialize(base_url, token, settings = nil)
  super(base_url, settings)
  @token = token

  raise MissingTokenError unless @token
end

Instance Method Details

#emit_event(data) ⇒ Object

Parameters:

  • data (Hash)


58
59
60
61
62
# File 'lib/puppet_x/relay/util/http/relay_api.rb', line 58

def emit_event(data)
  resp = post('api/events', body: { 'data' => data })
  resp.value
  resp
end

#get_runsArray<Agent::Model::Stateful>

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/puppet_x/relay/util/http/relay_api.rb', line 23

def get_runs
  resp = get('_puppet/runs')
  resp.value

  data = JSON.parse(resp.body)
  data['runs'].map { |run|
    case run['type']
    when 'run', nil
      Agent::Model::Run.from_h(run)
    when 'task-run'
      Agent::Model::TaskRun.from_h(run)
    when 'plan-run'
      Agent::Model::PlanRun.from_h(run)
    end
  }.compact
end

#post_accept_run(run) ⇒ Object

Parameters:



52
53
54
55
# File 'lib/puppet_x/relay/util/http/relay_api.rb', line 52

def post_accept_run(run)
  resp = post("_puppet/runs/#{run.id}/accept")
  resp.instance_of? Net::HTTPAccepted
end

#put_run_state(run) ⇒ Agent::Model::Stateful

Parameters:

Returns:



42
43
44
45
46
47
48
49
# File 'lib/puppet_x/relay/util/http/relay_api.rb', line 42

def put_run_state(run)
  Puppet.debug(_('Updating run %{id} state to %{state}') % { id: run.id, state: run.state.inspect })
  resp = put("_puppet/runs/#{run.id}/state", body: run.state)
  resp.value

  data = JSON.parse(resp.body)
  run.with_state(Agent::Model::State.from_h(data))
end