diff --git a/.changeset/dull-bottles-learn.md b/.changeset/dull-bottles-learn.md new file mode 100644 index 0000000000..e279f9e0ad --- /dev/null +++ b/.changeset/dull-bottles-learn.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-gcp-projects': patch +--- + +Add a `x-google-api-client` header for calls towards Google Cloud Projects diff --git a/plugins/gcp-projects/src/api/GcpClient.ts b/plugins/gcp-projects/src/api/GcpClient.ts index 10dcb5a4c8..f9694f266e 100644 --- a/plugins/gcp-projects/src/api/GcpClient.ts +++ b/plugins/gcp-projects/src/api/GcpClient.ts @@ -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), }); diff --git a/plugins/gcp-projects/src/plugin.test.ts b/plugins/gcp-projects/src/plugin.test.ts index 7b644d1400..da5f89cbb9 100644 --- a/plugins/gcp-projects/src/plugin.test.ts +++ b/plugins/gcp-projects/src/plugin.test.ts @@ -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}`, + }, + }); + }); });