Class: Orchestrator_api

Inherits:
Object
  • Object
show all
Defined in:
lib/orchestrator_api.rb

Defined Under Namespace

Classes: Command, Environments, Error, Jobs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ Orchestrator_api

Returns a new instance of Orchestrator_api.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/orchestrator_api.rb', line 14

def initialize(settings = {})

  @config = { 'token_path'  => File.join(Dir.home, '.puppetlabs', 'token'),
  }.merge(settings)

  if @config['token']
    @token = @config['token']
  else
    @token = File.read(@config['token_path'])
  end

  if @config['service-url'].nil?
    raise "Configuration error: 'service-url' must specify the server running the Orchestration services and cannot be empty"
  end
  if @config['ca_cert'].nil?
    raise "Configuration error: 'ca_cert' must specify a path to the CA certificate used for communications with the server and cannot be empty"
  end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



12
13
14
# File 'lib/orchestrator_api.rb', line 12

def config
  @config
end

#tokenObject

Returns the value of attribute token.



12
13
14
# File 'lib/orchestrator_api.rb', line 12

def token
  @token
end

Instance Method Details

#commandObject



79
80
81
# File 'lib/orchestrator_api.rb', line 79

def command
  @command ||= Orchestrator_api::Command.new(self, url)
end

#create_http(uri) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/orchestrator_api.rb', line 37

def create_http(uri)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.ssl_version = :TLSv1
  http.ca_file = config['ca_cert']
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  http
end

#environmentsObject



83
84
85
# File 'lib/orchestrator_api.rb', line 83

def environments
  @environments ||= Orchestrator_api::Environments.new(self, url)
end

#get(location) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/orchestrator_api.rb', line 46

def get(location)
  uri = URI.parse(location)
  https = create_http(uri)

  req = Net::HTTP::Get.new(uri.request_uri)
  req['Content-Type'] = "application/json"
  req.add_field('X-Authentication', token)
  res = https.request(req)

  if res.code != "200"
    raise Orchestrator_api::Error.make_error_from_response(res)
  end

  JSON.parse(res.body)
end

#jobsObject



87
88
89
# File 'lib/orchestrator_api.rb', line 87

def jobs
  @jobs ||= Orchestrator_api::Jobs.new(self, url)
end

#post(location, body) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/orchestrator_api.rb', line 62

def post(location, body)
  uri = URI.parse(location)
  https = create_http(uri)

  req = Net::HTTP::Post.new(uri.request_uri)
  req['Content-Type'] = "application/json"
  req.add_field('X-Authentication', token)
  req.body = body.to_json
  res = https.request(req)

  if res.code != "202"
    raise Orchestrator_api::Error.make_error_from_response(res)
  end

  JSON.parse(res.body)
end

#rootObject



91
92
93
# File 'lib/orchestrator_api.rb', line 91

def root
  get(url)
end

#urlObject



33
34
35
# File 'lib/orchestrator_api.rb', line 33

def url
  config['service-url']
end