From 50c8bd6350ebfd8d35deb2a1b41eb36e193d1a14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Mon, 4 Dec 2017 00:53:04 +0100 Subject: Generate user agent header for GCP Client --- lib/google_api/cloud_platform/client.rb | 4 ++++ spec/lib/google_api/cloud_platform/client_spec.rb | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/google_api/cloud_platform/client.rb b/lib/google_api/cloud_platform/client.rb index 9242cbe840c..615cd7dc60a 100644 --- a/lib/google_api/cloud_platform/client.rb +++ b/lib/google_api/cloud_platform/client.rb @@ -82,6 +82,10 @@ module GoogleApi def token_life_time(expires_at) DateTime.strptime(expires_at, '%s').to_time.utc - Time.now.utc end + + def user_agent_header + { 'User-Agent': "GitLab/#{Gitlab::VERSION.match('(\d+\.\d+)').captures.first} (GPN:GitLab;)" } + end end end end diff --git a/spec/lib/google_api/cloud_platform/client_spec.rb b/spec/lib/google_api/cloud_platform/client_spec.rb index fac23dce44d..41964034062 100644 --- a/spec/lib/google_api/cloud_platform/client_spec.rb +++ b/spec/lib/google_api/cloud_platform/client_spec.rb @@ -125,4 +125,13 @@ describe GoogleApi::CloudPlatform::Client do it { is_expected.to be_nil } end end + + describe '#user_agent_header' do + subject { client.instance_eval { user_agent_header } } + + it 'returns the correct major and minor GitLab version ' do + stub_const('Gitlab::VERSION', '10.3.0-pre') + expect(subject).to eq({ 'User-Agent': 'GitLab/10.3 (GPN:GitLab;)' }) + end + end end -- cgit v1.2.1 From 04c6d102616b48c95b09656efc720c7dfdc99d8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Mon, 4 Dec 2017 01:59:29 +0100 Subject: Use RequestOptions in GCP Client user_agent_header --- lib/google_api/cloud_platform/client.rb | 4 +++- spec/lib/google_api/cloud_platform/client_spec.rb | 9 +++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/google_api/cloud_platform/client.rb b/lib/google_api/cloud_platform/client.rb index 615cd7dc60a..15401057903 100644 --- a/lib/google_api/cloud_platform/client.rb +++ b/lib/google_api/cloud_platform/client.rb @@ -84,7 +84,9 @@ module GoogleApi end def user_agent_header - { 'User-Agent': "GitLab/#{Gitlab::VERSION.match('(\d+\.\d+)').captures.first} (GPN:GitLab;)" } + options = Google::Apis::RequestOptions.new + options.header = { 'User-Agent': "GitLab/#{Gitlab::VERSION.match('(\d+\.\d+)').captures.first} (GPN:GitLab;)" } + options end end end diff --git a/spec/lib/google_api/cloud_platform/client_spec.rb b/spec/lib/google_api/cloud_platform/client_spec.rb index 41964034062..ee8f7afc285 100644 --- a/spec/lib/google_api/cloud_platform/client_spec.rb +++ b/spec/lib/google_api/cloud_platform/client_spec.rb @@ -129,9 +129,14 @@ describe GoogleApi::CloudPlatform::Client do describe '#user_agent_header' do subject { client.instance_eval { user_agent_header } } - it 'returns the correct major and minor GitLab version ' do + it 'returns a RequestOptions object' do + expect(subject).to be_instance_of(Google::Apis::RequestOptions) + end + + it 'has the correct GitLab version in User-Agent header' do stub_const('Gitlab::VERSION', '10.3.0-pre') - expect(subject).to eq({ 'User-Agent': 'GitLab/10.3 (GPN:GitLab;)' }) + + expect(subject.header).to eq({ 'User-Agent': 'GitLab/10.3 (GPN:GitLab;)' }) end end end -- cgit v1.2.1 From 68b43f4d9c5dbc2d8264e5cadb7417e21ac0ed27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Mon, 4 Dec 2017 02:19:02 +0100 Subject: Test usage of custom user agent in GCP Client --- lib/google_api/cloud_platform/client.rb | 6 +++--- spec/lib/google_api/cloud_platform/client_spec.rb | 10 +++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/google_api/cloud_platform/client.rb b/lib/google_api/cloud_platform/client.rb index 15401057903..36be6b7e97a 100644 --- a/lib/google_api/cloud_platform/client.rb +++ b/lib/google_api/cloud_platform/client.rb @@ -44,7 +44,7 @@ module GoogleApi service = Google::Apis::ContainerV1::ContainerService.new service.authorization = access_token - service.get_zone_cluster(project_id, zone, cluster_id) + service.get_zone_cluster(project_id, zone, cluster_id, options: user_agent_header) end def projects_zones_clusters_create(project_id, zone, cluster_name, cluster_size, machine_type:) @@ -62,14 +62,14 @@ module GoogleApi } } ) - service.create_cluster(project_id, zone, request_body) + service.create_cluster(project_id, zone, request_body, options: user_agent_header) end def projects_zones_operations(project_id, zone, operation_id) service = Google::Apis::ContainerV1::ContainerService.new service.authorization = access_token - service.get_zone_operation(project_id, zone, operation_id) + service.get_zone_operation(project_id, zone, operation_id, options: user_agent_header) end def parse_operation_id(self_link) diff --git a/spec/lib/google_api/cloud_platform/client_spec.rb b/spec/lib/google_api/cloud_platform/client_spec.rb index ee8f7afc285..ecb4034ec8b 100644 --- a/spec/lib/google_api/cloud_platform/client_spec.rb +++ b/spec/lib/google_api/cloud_platform/client_spec.rb @@ -3,6 +3,7 @@ require 'spec_helper' describe GoogleApi::CloudPlatform::Client do let(:token) { 'token' } let(:client) { described_class.new(token, nil) } + let(:user_agent_options) { client.instance_eval { user_agent_header } } describe '.session_key_for_redirect_uri' do let(:state) { 'random_string' } @@ -55,7 +56,8 @@ describe GoogleApi::CloudPlatform::Client do before do allow_any_instance_of(Google::Apis::ContainerV1::ContainerService) - .to receive(:get_zone_cluster).and_return(gke_cluster) + .to receive(:get_zone_cluster).with(any_args, options: user_agent_options) + .and_return(gke_cluster) end it { is_expected.to eq(gke_cluster) } @@ -74,7 +76,8 @@ describe GoogleApi::CloudPlatform::Client do before do allow_any_instance_of(Google::Apis::ContainerV1::ContainerService) - .to receive(:create_cluster).and_return(operation) + .to receive(:create_cluster).with(any_args, options: user_agent_options) + .and_return(operation) end it { is_expected.to eq(operation) } @@ -102,7 +105,8 @@ describe GoogleApi::CloudPlatform::Client do before do allow_any_instance_of(Google::Apis::ContainerV1::ContainerService) - .to receive(:get_zone_operation).and_return(operation) + .to receive(:get_zone_operation).with(any_args, options: user_agent_options) + .and_return(operation) end it { is_expected.to eq(operation) } -- cgit v1.2.1 From bb80d439f4bbb37032a2ee2d35ee5eab7594de7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Mon, 4 Dec 2017 13:43:45 +0100 Subject: Refactor GCP Client#user_agent_header to use #tap --- lib/google_api/cloud_platform/client.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/google_api/cloud_platform/client.rb b/lib/google_api/cloud_platform/client.rb index 36be6b7e97a..b0563fb2d69 100644 --- a/lib/google_api/cloud_platform/client.rb +++ b/lib/google_api/cloud_platform/client.rb @@ -84,9 +84,9 @@ module GoogleApi end def user_agent_header - options = Google::Apis::RequestOptions.new - options.header = { 'User-Agent': "GitLab/#{Gitlab::VERSION.match('(\d+\.\d+)').captures.first} (GPN:GitLab;)" } - options + Google::Apis::RequestOptions.new.tap do |options| + options.header = { 'User-Agent': "GitLab/#{Gitlab::VERSION.match('(\d+\.\d+)').captures.first} (GPN:GitLab;)" } + end end end end -- cgit v1.2.1 From 9710015651351725b9cb84bfe658f293e413bf06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Mon, 4 Dec 2017 13:50:34 +0100 Subject: Add CHANGELOG entry --- changelogs/unreleased/user-agent-gke-api.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelogs/unreleased/user-agent-gke-api.yml diff --git a/changelogs/unreleased/user-agent-gke-api.yml b/changelogs/unreleased/user-agent-gke-api.yml new file mode 100644 index 00000000000..1abdbadd53b --- /dev/null +++ b/changelogs/unreleased/user-agent-gke-api.yml @@ -0,0 +1,5 @@ +--- +title: Use custom user agent header in all GCP API requests. +merge_request: 15705 +author: +type: changed -- cgit v1.2.1