Merge pull request #22995 from armandocomellas1/add-useragent-gcs

[Google Cloud Platform Projects] Add X-Google-Api-Client as Header to identify Backstage use case for GCP telemetry
This commit is contained in:
Patrik Oldsberg
2024-02-20 01:18:42 +01:00
committed by GitHub
3 changed files with 37 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-gcp-projects': patch
---
Add a `x-google-api-client` header for calls towards Google Cloud Projects
@@ -17,6 +17,7 @@
import { GcpApi } from './GcpApi';
import { Operation, Project } from './types';
import { OAuthApi } from '@backstage/core-plugin-api';
import packageinfo from '../../package.json';
const BASE_URL =
'https://content-cloudresourcemanager.googleapis.com/v1/projects';
@@ -30,6 +31,7 @@ export class GcpClient implements GcpApi {
headers: {
Accept: '*/*',
Authorization: `Bearer ${await this.getToken()}`,
'X-Goog-Api-Client': `backstage/gcpprojects/${packageinfo.version}`,
},
});
@@ -48,6 +50,7 @@ export class GcpClient implements GcpApi {
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${await this.getToken()}`,
'X-Goog-Api-Client': `backstage/gcpprojects/${packageinfo.version}`,
},
});
@@ -74,6 +77,7 @@ export class GcpClient implements GcpApi {
headers: {
Accept: '*/*',
Authorization: `Bearer ${await this.getToken()}`,
'X-Goog-Api-Client': `backstage/gcpprojects/${packageinfo.version}`,
},
body: JSON.stringify(newProject),
});
+28
View File
@@ -15,9 +15,37 @@
*/
import { gcpProjectsPlugin } from './plugin';
import { GcpClient } from './api';
import { OAuthApi } from '@backstage/core-plugin-api';
import packageinfo from '../package.json';
describe('gcp-projects', () => {
let sut: GcpClient;
let googleAuthApi: OAuthApi;
beforeEach(() => {
sut = new GcpClient(googleAuthApi);
});
it('should export plugin', () => {
expect(gcpProjectsPlugin).toBeDefined();
});
it('spy headers with identifying metadata', async () => {
const response: any = {
headers: {
Accept: '*/*',
'X-Goog-Api-Client': `backstage/gcpprojects/${packageinfo.version}`,
},
};
jest.spyOn(sut, 'listProjects').mockImplementation((): any => {
return response;
});
expect(response).toStrictEqual({
headers: {
Accept: '*/*',
'X-Goog-Api-Client': `backstage/gcpprojects/${packageinfo.version}`,
},
});
});
});