Skip to content

Commit ad605d8

Browse files
authored
Merge pull request #13 from donoghuc/BOLT-519
(BOLT-519) Set User-Agent header for analytics use
2 parents 4fba666 + 552dfbc commit ad605d8

File tree

6 files changed

+36
-12
lines changed

6 files changed

+36
-12
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ expected to be at `~/.puppetlabs/token` which is the default location used by
2222
### initialization Settings
2323

2424
* `service-url` **[required]** - Base URL for the location of the Orchestrator API service
25-
* `ca_cert` **[required]** - Path to the CA certificate file needed to verify the SSL connection to the API.
26-
* `token_path`- Path to a file with the RBAC token in it (defaults to `~/.puppetlabs/token`)
25+
* `cacert` **[required]** - Path to the CA certificate file needed to verify the SSL connection to the API.
26+
* `token-file`- Path to a file with the RBAC token in it (defaults to `~/.puppetlabs/token`)
2727
* `token` - Pass directly the RBAC token, if specified the token will be used instead of a token from file.
28+
* `User-Agent`- Set `User-Agent` header for HTTP requests. Defaults to `OrchestratorRubyClient/[VERSION]`
2829

2930
### Example
3031

@@ -36,7 +37,7 @@ require 'orchestrator_client'
3637

3738
client = OrchestratorClient.new({
3839
'service-url' => 'https://orchestrator.example.lan:8143/orchestrator/v1',
39-
'ca_cert' => '/path/to/cert'
40+
'cacert' => '/path/to/cert'
4041
})
4142

4243
## Access endpoints through the client object

lib/orchestrator_client.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def get(location)
3535
https = create_http(uri)
3636
req = Net::HTTP::Get.new(uri)
3737
req['Content-Type'] = "application/json"
38+
req['User-Agent'] = @config['User-Agent']
3839
req.add_field('X-Authentication', @config.token)
3940
res = https.request(req)
4041

@@ -51,6 +52,7 @@ def post(location, body)
5152

5253
req = Net::HTTP::Post.new(uri)
5354
req['Content-Type'] = "application/json"
55+
req['User-Agent'] = @config['User-Agent']
5456
req.add_field('X-Authentication', @config.token)
5557
req.body = body.to_json
5658
res = https.request(req)

lib/orchestrator_client/config.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def cacert
3434
def defaults
3535
{ 'cacert' => cacert,
3636
'token-file' => File.join(user_root, 'token'),
37+
'User-Agent' => "OrchestratorRubyClient/#{OrchestratorClient::VERSION}"
3738
}
3839
end
3940

spec/unit/command_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
stub_request(:post, "https://orchestrator.example.lan:8143/orchestrator/v1/command/deploy").
3434
with(:body => @deploy_opts.to_json,
35-
:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby', 'X-Authentication'=>'myfaketoken'}).
35+
:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>"OrchestratorRubyClient/#{OrchestratorClient::VERSION}", 'X-Authentication'=>'myfaketoken'}).
3636
to_return(:status => 202, :body => response, :headers => {})
3737

3838
expect(@orchestrator.command.deploy(@deploy_opts)).to be_an_instance_of Hash
@@ -45,7 +45,7 @@
4545

4646
stub_request(:post, "https://orchestrator.example.lan:8143/orchestrator/v1/command/task").
4747
with(:body => @task_opts.to_json,
48-
:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby', 'X-Authentication'=>'myfaketoken'}).
48+
:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>"OrchestratorRubyClient/#{OrchestratorClient::VERSION}", 'X-Authentication'=>'myfaketoken'}).
4949
to_return(:status => 202, :body => response, :headers => {})
5050

5151
expect(@orchestrator.command.task(@task_opts)).to be_an_instance_of Hash
@@ -58,7 +58,7 @@
5858

5959
stub_request(:post, "https://orchestrator.example.lan:8143/orchestrator/v1/command/stop").
6060
with(:body => "{\"job\":\"1\"}",
61-
:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby', 'X-Authentication'=>'myfaketoken'}).
61+
:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>"OrchestratorRubyClient/#{OrchestratorClient::VERSION}", 'X-Authentication'=>'myfaketoken'}).
6262
to_return(:status => 202, :body => response, :headers => {})
6363

6464
expect(@orchestrator.command.stop(1)).to be_an_instance_of Hash

spec/unit/jobs_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
it 'optionally accepts a number to limit results' do
2525
stub_request(:get, "https://orchestrator.example.lan:8143/orchestrator/v1/jobs?limit=1").
26-
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby', 'X-Authentication'=>'myfaketoken'}).
26+
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>"OrchestratorRubyClient/#{OrchestratorClient::VERSION}", 'X-Authentication'=>'myfaketoken'}).
2727
to_return(:status => 200, :body => response, :headers => {})
2828

2929
expect(@orchestrator.jobs.all(1)).to be_an_instance_of Hash
@@ -33,7 +33,7 @@
3333
response = "{\n \"items\" : [ {\n \"report\" : {\n \"id\" : \"https://orchestrator.example.lan:8143/orchestrator/v1/jobs/58/report\"\n },\n \"name\" : \"58\",\n \"events\" : {\n \"id\" : \"https://orchestrator.example.lan:8143/orchestrator/v1/jobs/58/events\"\n },\n \"state\" : \"failed\",\n \"nodes\" : {\n \"id\" : \"https://orchestrator.example.lan:8143/orchestrator/v1/jobs/58/nodes\"\n },\n \"id\" : \"https://orchestrator.example.lan:8143/orchestrator/v1/jobs/58\",\n \"environment\" : {\n \"name\" : \"production\"\n },\n \"options\" : {\n \"concurrency\" : null,\n \"noop\" : false,\n \"trace\" : false,\n \"debug\" : false,\n \"scope\" : {\n \"whole_environment\" : true\n },\n \"enforce_environment\" : true,\n \"environment\" : \"production\",\n \"evaltrace\" : false,\n \"target\" : \"whole environment\"\n },\n \"timestamp\" : \"2016-09-20T19:01:18Z\",\n \"owner\" : {\n \"id\" : \"80f8475c-d8b5-4dfd-b567-3cae4ef7a3a7\",\n \"login\" : \"tom\"\n },\n \"node_count\" : 2\n } ]\n}"
3434

3535
stub_request(:get, "https://orchestrator.example.lan:8143/orchestrator/v1/jobs").
36-
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby', 'X-Authentication'=>'myfaketoken'}).
36+
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>"OrchestratorRubyClient/#{OrchestratorClient::VERSION}", 'X-Authentication'=>'myfaketoken'}).
3737
to_return(:status => 200, :body => response, :headers => {})
3838

3939
expect(@orchestrator.jobs.all).to be_an_instance_of Hash
@@ -54,7 +54,7 @@
5454

5555
it 'takes a job number and accespts a start number to retrieve latest results' do
5656
stub_request(:get, "https://orchestrator.example.lan:8143/orchestrator/v1/jobs/1/events?start=10").
57-
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby', 'X-Authentication'=>'myfaketoken'}).
57+
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>"OrchestratorRubyClient/#{OrchestratorClient::VERSION}", 'X-Authentication'=>'myfaketoken'}).
5858
to_return(:status => 200, :body => response, :headers => {})
5959

6060
expect(@orchestrator.jobs.events(1,10)).to be_an_instance_of Hash
@@ -63,7 +63,7 @@
6363
response = "{\n \"next-events\" : {\n \"id\" : \"https://orchestrator.example.lan:8143/orchestrator/v1/jobs/1/events?start=7\"\n },\n \"items\" : [ {\n \"type\" : \"node_running\",\n \"timestamp\" : \"2016-08-15T21:34:34Z\",\n \"details\" : {\n \"node\" : \"orchestrator.example.lan\",\n \"detail\" : {\n \"noop\" : false\n }\n },\n \"message\" : \"Started puppet run on orchestrator.example.lan ...\",\n \"id\" : \"1\"\n }, {\n \"type\" : \"node_running\",\n \"timestamp\" : \"2016-08-15T21:34:34Z\",\n \"details\" : {\n \"node\" : \"agent2.puppetlabs.vm\",\n \"detail\" : {\n \"noop\" : false\n }\n },\n \"message\" : \"Started puppet run on agent2.puppetlabs.vm ...\",\n \"id\" : \"2\"\n }, {\n \"type\" : \"node_running\",\n \"timestamp\" : \"2016-08-15T21:34:34Z\",\n \"details\" : {\n \"node\" : \"agent1.puppetlabs.vm\",\n \"detail\" : {\n \"noop\" : false\n }\n },\n \"message\" : \"Started puppet run on agent1.puppetlabs.vm ...\",\n \"id\" : \"3\"\n }, {\n \"type\" : \"node_finished\",\n \"timestamp\" : \"2016-08-15T21:34:47Z\",\n \"details\" : {\n \"node\" : \"agent1.puppetlabs.vm\",\n \"detail\" : {\n \"hash\" : \"20d60265af8bafc0ac1b99c1eb4b8bde411a81a3\",\n \"noop\" : false,\n \"status\" : \"unchanged\",\n \"metrics\" : {\n \"corrective_change\" : 0,\n \"out_of_sync\" : 0,\n \"restarted\" : 0,\n \"skipped\" : 0,\n \"total\" : 171,\n \"changed\" : 0,\n \"scheduled\" : 0,\n \"failed_to_restart\" : 0,\n \"failed\" : 0\n },\n \"report-url\" : \"https://orchestrator.example.lan/#/cm/report/20d60265af8bafc0ac1b99c1eb4b8bde411a81a3\",\n \"environment\" : \"production\",\n \"configuration_version\" : \"1471296882\"\n }\n },\n \"message\" : \"Finished puppet run on agent1.puppetlabs.vm - Success! \",\n \"id\" : \"4\"\n }, {\n \"type\" : \"node_finished\",\n \"timestamp\" : \"2016-08-15T21:34:47Z\",\n \"details\" : {\n \"node\" : \"agent2.puppetlabs.vm\",\n \"detail\" : {\n \"hash\" : \"d460f0a513b1e18d43f19867b269cf105772fc41\",\n \"noop\" : false,\n \"status\" : \"unchanged\",\n \"metrics\" : {\n \"corrective_change\" : 0,\n \"out_of_sync\" : 0,\n \"restarted\" : 0,\n \"skipped\" : 0,\n \"total\" : 171,\n \"changed\" : 0,\n \"scheduled\" : 0,\n \"failed_to_restart\" : 0,\n \"failed\" : 0\n },\n \"report-url\" : \"https://orchestrator.example.lan/#/cm/report/d460f0a513b1e18d43f19867b269cf105772fc41\",\n \"environment\" : \"production\",\n \"configuration_version\" : \"1471296882\"\n }\n },\n \"message\" : \"Finished puppet run on agent2.puppetlabs.vm - Success! \",\n \"id\" : \"5\"\n }, {\n \"type\" : \"node_finished\",\n \"timestamp\" : \"2016-08-15T21:35:26Z\",\n \"details\" : {\n \"node\" : \"orchestrator.example.lan\",\n \"detail\" : {\n \"hash\" : \"c02e060e07da2a0e3ecdbad0e46a265ca22f433d\",\n \"noop\" : false,\n \"status\" : \"unchanged\",\n \"metrics\" : {\n \"corrective_change\" : 0,\n \"out_of_sync\" : 3,\n \"restarted\" : 0,\n \"skipped\" : 0,\n \"total\" : 789,\n \"changed\" : 0,\n \"scheduled\" : 0,\n \"failed_to_restart\" : 0,\n \"failed\" : 0\n },\n \"report-url\" : \"https://orchestrator.example.lan/#/cm/report/c02e060e07da2a0e3ecdbad0e46a265ca22f433d\",\n \"environment\" : \"production\",\n \"configuration_version\" : \"1471296883\"\n }\n },\n \"message\" : \"Finished puppet run on orchestrator.example.lan - Success! \",\n \"id\" : \"6\"\n } ]\n}"
6464

6565
stub_request(:get, "https://orchestrator.example.lan:8143/orchestrator/v1/jobs/1/events").
66-
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby', 'X-Authentication'=>'myfaketoken'}).
66+
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>"OrchestratorRubyClient/#{OrchestratorClient::VERSION}", 'X-Authentication'=>'myfaketoken'}).
6767
to_return(:status => 200, :body => response, :headers => {})
6868

6969
expect(@orchestrator.jobs.events(1)).to be_an_instance_of Hash

spec/unit/orchestrator_client_spec.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,17 @@
3737
describe "#get" do
3838
it 'takes an endpoint and parses the response as JSON' do
3939
stub_request(:get, "https://orchestrator.example.lan:8143/orchestrator/v1/endpoint").
40-
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby', 'X-Authentication'=>'myfaketoken'}).
40+
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>"OrchestratorRubyClient/#{OrchestratorClient::VERSION}", 'X-Authentication'=>'myfaketoken'}).
41+
to_return(:status => 200, :body => "{}", :headers => {})
42+
43+
expect(@orchestrator_api.get('endpoint')).to be_an_instance_of Hash
44+
end
45+
46+
it 'updates User-Agent header' do
47+
@config['User-Agent'] = 'foo'
48+
@orchestrator_api = OrchestratorClient.new(@config)
49+
stub_request(:get, "https://orchestrator.example.lan:8143/orchestrator/v1/endpoint").
50+
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>"foo", 'X-Authentication'=>'myfaketoken'}).
4151
to_return(:status => 200, :body => "{}", :headers => {})
4252

4353
expect(@orchestrator_api.get('endpoint')).to be_an_instance_of Hash
@@ -48,10 +58,20 @@
4858
it 'takes an endpoint, endpoint, and parses the response as JSON' do
4959
stub_request(:post, "https://orchestrator.example.lan:8143/orchestrator/v1/endpoint").
5060
with( :body => "{\"data\":\"atad\"}",
51-
:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby', 'X-Authentication'=>'myfaketoken'}).
61+
:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>"OrchestratorRubyClient/#{OrchestratorClient::VERSION}", 'X-Authentication'=>'myfaketoken'}).
5262
to_return(:status => 202, :body => "{}", :headers => {})
5363

5464
expect(@orchestrator_api.post('endpoint',{'data' => 'atad'})).to be_an_instance_of Hash
5565
end
66+
67+
it 'updates User-Agent header' do
68+
@config['User-Agent'] = 'foo'
69+
@orchestrator_api = OrchestratorClient.new(@config)
70+
stub_request(:get, "https://orchestrator.example.lan:8143/orchestrator/v1/endpoint").
71+
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>"foo", 'X-Authentication'=>'myfaketoken'}).
72+
to_return(:status => 200, :body => "{}", :headers => {})
73+
74+
expect(@orchestrator_api.get('endpoint')).to be_an_instance_of Hash
75+
end
5676
end
5777
end

0 commit comments

Comments
 (0)