orchestrator_client 0.3.1 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 544692bcfb8a25aa745a2b8b6d04bb3dfc5d9aea
4
- data.tar.gz: 3d478262f860e470b6a759fb898b1f049bb268c8
3
+ metadata.gz: edb40acfb0113456ba1b3999c6f9e093d0999501
4
+ data.tar.gz: 39980ed67308f0dd7c3ae9daf7f9636e78c4d22b
5
5
  SHA512:
6
- metadata.gz: 0b9ebba6fd6d909f0c85e7ee3b18b74a32cf338f3fc493732024accc045c56d21cc0aac648ad51adaf14d3de7ee0023d851525cff4f42da9ef5a333a871f848b
7
- data.tar.gz: 2b2e156ad03c89a559c34df15be723bb0e0d69021ed0721737fbfce857524d3ddc6ce22d2d0f29a616adcee85614d5416bc0ea86b6b87aa8e7bbde2498ce7060
6
+ metadata.gz: 63087f62bdcfedbb2e49d3e6579d5f047fff242fbd1d8291b84e6efcfe398b3f7e82b72ccd05bac3f423c832dce0b99cbf040954b82b9f97d2d6d65665c1d95c
7
+ data.tar.gz: 784cf2dd3a28b00cd09b790e0b7a0ee9c70a433c98fc213c4647a706ccba09295b83ba767b4f74dac676cabefb2b6cb51081377987acfe0572547ea726722e87
data/README.md CHANGED
@@ -26,6 +26,8 @@ expected to be at `~/.puppetlabs/token` which is the default location used by
26
26
  * `token-file`- Path to a file with the RBAC token in it (defaults to `~/.puppetlabs/token`)
27
27
  * `token` - Pass directly the RBAC token, if specified the token will be used instead of a token from file.
28
28
  * `User-Agent`- Set `User-Agent` header for HTTP requests. Defaults to `OrchestratorRubyClient/[VERSION]`
29
+ * `job-poll-interval`- Set the default amount of time to sleep when polling in Orchestrator::Job#wait and #each\_event
30
+ * `job-poll-timeout`- Set the default maximum amount of time to wait in Orchestrator::Job#wait
29
31
 
30
32
  ### Example
31
33
 
@@ -2,6 +2,8 @@ require 'net/https'
2
2
  require 'uri'
3
3
  require 'json'
4
4
  require 'openssl'
5
+ require 'faraday'
6
+ require 'net/http/persistent'
5
7
 
6
8
  class OrchestratorClient
7
9
  require 'orchestrator_client/error'
@@ -11,35 +13,31 @@ class OrchestratorClient
11
13
  require 'orchestrator_client/config'
12
14
 
13
15
  attr_accessor :config
16
+ attr_reader :http
14
17
 
15
18
  def initialize(overrides, load_files=false)
16
19
  @config = Config.new(overrides, load_files)
17
20
  @config.validate
21
+ @http = create_http(config.root_url)
18
22
  end
19
23
 
20
- def make_uri(path)
21
- URI.parse("#{config.root_url}#{path}")
22
- end
23
-
24
- def create_http(uri)
25
- http = Net::HTTP.new(uri.host, uri.port)
26
- http.use_ssl = true
27
- http.ssl_version = :TLSv1_2
28
- http.ca_file = config['cacert']
29
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
30
- http
24
+ def create_http(root_url)
25
+ Faraday.new(url: root_url) do |f|
26
+ f.headers['Content-Type'] = 'application/json'
27
+ f.headers['X-Authentication'] = config.token
28
+ f.headers['User-Agent'] = config['User-Agent']
29
+ f.ssl['ca_file'] = config['cacert']
30
+ f.ssl['version'] = :TLSv1_2
31
+ f.adapter :net_http_persistent, pool_size: 5 do |http|
32
+ http.idle_timeout = 30
33
+ end
34
+ end
31
35
  end
32
36
 
33
37
  def get(location)
34
- uri = make_uri(location)
35
- https = create_http(uri)
36
- req = Net::HTTP::Get.new(uri)
37
- req['Content-Type'] = "application/json"
38
- req['User-Agent'] = @config['User-Agent']
39
- req.add_field('X-Authentication', @config.token)
40
- res = https.request(req)
38
+ res = http.get(location)
41
39
 
42
- if res.code != "200"
40
+ if res.status != 200
43
41
  raise OrchestratorClient::ApiError.make_error_from_response(res)
44
42
  end
45
43
 
@@ -47,17 +45,9 @@ class OrchestratorClient
47
45
  end
48
46
 
49
47
  def post(location, body)
50
- uri = make_uri(location)
51
- https = create_http(uri)
52
-
53
- req = Net::HTTP::Post.new(uri)
54
- req['Content-Type'] = "application/json"
55
- req['User-Agent'] = @config['User-Agent']
56
- req.add_field('X-Authentication', @config.token)
57
- req.body = body.to_json
58
- res = https.request(req)
48
+ res = http.post(location, body.to_json)
59
49
 
60
- unless Set.new(["202", "200"]).include? res.code
50
+ unless Set.new([202, 200]).include? res.status
61
51
  raise OrchestratorClient::ApiError.make_error_from_response(res)
62
52
  end
63
53
 
@@ -35,7 +35,9 @@ class OrchestratorClient::Config
35
35
  def defaults
36
36
  { 'cacert' => cacert,
37
37
  'token-file' => File.join(user_root, 'token'),
38
- 'User-Agent' => "OrchestratorRubyClient/#{OrchestratorClient::VERSION}"
38
+ 'User-Agent' => "OrchestratorRubyClient/#{OrchestratorClient::VERSION}",
39
+ 'job-poll-interval' => 1,
40
+ 'job-poll-timeout' => 1000,
39
41
  }
40
42
  end
41
43
 
@@ -75,7 +77,16 @@ class OrchestratorClient::Config
75
77
  end
76
78
 
77
79
  def load_token
78
- @config['token'] || File.open(config['token-file']) { |f| f.read.strip }
80
+ if @config['token']
81
+ @config['token']
82
+ else
83
+ token = File.open(config['token-file']) { |f| f.read.strip }
84
+ if token != URI.escape(token)
85
+ raise OrchestratorClient::ConfigError.new("'#{config['token-file']}' contains illegal characters")
86
+ end
87
+ @config['token'] = token
88
+ @config['token']
89
+ end
79
90
  end
80
91
 
81
92
  def token
@@ -17,7 +17,7 @@ class OrchestratorClient::ApiError < RuntimeError
17
17
  rescue
18
18
  return OrchestratorClient::BadResponse.new("Response body was not valid JSON: #{res.body}")
19
19
  end
20
- code = res.code
20
+ code = res.status
21
21
 
22
22
  case data['kind']
23
23
  when 'puppetlabs.validators/validation-error'
@@ -16,6 +16,8 @@ class OrchestratorClient::Job
16
16
 
17
17
  def initialize(client, options = {}, type=:deploy)
18
18
  @client = client
19
+ @poll_interval = options.delete(:_poll_interval) || client.config['job-poll-interval']
20
+ @poll_timeout = options.delete(:_poll_timeout) || client.config['job-poll-timeout']
19
21
  @options = options
20
22
  @type = type
21
23
  end
@@ -71,7 +73,7 @@ class OrchestratorClient::Job
71
73
  events = @client.jobs.events(@job_name)
72
74
  start = events['next-events']['event']
73
75
  if events['items'].empty?
74
- sleep 1
76
+ sleep @poll_interval
75
77
  else
76
78
  events['items'].each do |event|
77
79
  finished = true if DONE_EVENTS.include?(event['type'])
@@ -81,15 +83,15 @@ class OrchestratorClient::Job
81
83
  end
82
84
  end
83
85
 
84
- def wait(timeout=1000)
86
+ def wait(timeout=@poll_timeout)
85
87
  counter = 0
86
88
  while counter < timeout
87
89
  get_details
88
90
  if DONE_STATES.include?(details['state'])
89
91
  return report
90
92
  end
91
- sleep 1
92
- counter += 1
93
+ sleep @poll_interval
94
+ counter += @poll_interval
93
95
  end
94
96
  end
95
97
  end
@@ -1,3 +1,3 @@
1
1
  class OrchestratorClient
2
- VERSION = '0.3.1'.freeze
2
+ VERSION = '0.4.1'.freeze
3
3
  end
@@ -14,4 +14,7 @@ Gem::Specification.new do |s|
14
14
  s.homepage = 'https://github.com/puppetlabs/orchestrator_client-ruby'
15
15
  s.license = "Apache-2.0"
16
16
  s.require_paths = ["lib"]
17
+
18
+ s.add_dependency 'faraday'
19
+ s.add_dependency 'net-http-persistent'
17
20
  end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: orchestrator_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Puppet
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-14 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2019-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: net-http-persistent
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  description:
14
42
  email: info@puppetlabs.com
15
43
  executables: []