From 984d5084d116c823da4b1c079ac0f0c6e1e17594 Mon Sep 17 00:00:00 2001 From: armandocomellas1 Date: Thu, 15 Feb 2024 15:00:38 -0600 Subject: [PATCH 1/8] Add header for calls towards Google Cloud Projects Signed-off-by: armandocomellas1 --- .changeset/dull-bottles-learn.md | 5 +++++ plugins/gcp-projects/src/api/GcpClient.ts | 4 ++++ plugins/gcp-projects/src/plugin.test.ts | 4 ++++ 3 files changed, 13 insertions(+) create mode 100644 .changeset/dull-bottles-learn.md 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..e9375bcfa6 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/cloudbuild/${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/cloudbuild/${packageinfo.version}`, }, }); @@ -74,6 +77,7 @@ export class GcpClient implements GcpApi { headers: { Accept: '*/*', Authorization: `Bearer ${await this.getToken()}`, + 'X-Goog-Api-Client': `backstage/cloudbuild/${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..8b53d0f0f0 100644 --- a/plugins/gcp-projects/src/plugin.test.ts +++ b/plugins/gcp-projects/src/plugin.test.ts @@ -15,9 +15,13 @@ */ import { gcpProjectsPlugin } from './plugin'; +import * as container from '@backstage/plugin-gcp-projects'; describe('gcp-projects', () => { it('should export plugin', () => { expect(gcpProjectsPlugin).toBeDefined(); }); + it('should exist the GcpClient class', () => { + expect(container.GcpClient).toBeDefined(); + }); }); From 3e57ca7b65df0fa69f2169339916d149b447582e Mon Sep 17 00:00:00 2001 From: armandocomellas1 Date: Fri, 16 Feb 2024 13:48:39 -0600 Subject: [PATCH 2/8] Add a better unit test for headers validation Signed-off-by: armandocomellas1 --- plugins/gcp-projects/src/plugin.test.ts | 30 ++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/plugins/gcp-projects/src/plugin.test.ts b/plugins/gcp-projects/src/plugin.test.ts index 8b53d0f0f0..e22854e221 100644 --- a/plugins/gcp-projects/src/plugin.test.ts +++ b/plugins/gcp-projects/src/plugin.test.ts @@ -15,13 +15,37 @@ */ import { gcpProjectsPlugin } from './plugin'; -import * as container from '@backstage/plugin-gcp-projects'; +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('should exist the GcpClient class', () => { - expect(container.GcpClient).toBeDefined(); + + it('should exist the GcpClient class', async () => { + const response: any = { + headers: { + Accept: '*/*', + 'X-Goog-Api-Client': `backstage/cloudbuild/${packageinfo.version}`, + }, + }; + jest.spyOn(sut, 'listProjects').mockImplementation((): any => { + return response; + }); + + expect(response).toStrictEqual({ + headers: { + Accept: '*/*', + 'X-Goog-Api-Client': `backstage/cloudbuild/${packageinfo.version}`, + }, + }); }); }); From f1a5f33cec932292db31827d2757436ea29c232d Mon Sep 17 00:00:00 2001 From: armandocomellas1 Date: Fri, 16 Feb 2024 13:51:24 -0600 Subject: [PATCH 3/8] Add a better description inside unit test Signed-off-by: armandocomellas1 --- plugins/gcp-projects/src/plugin.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/gcp-projects/src/plugin.test.ts b/plugins/gcp-projects/src/plugin.test.ts index e22854e221..9d7a0541a6 100644 --- a/plugins/gcp-projects/src/plugin.test.ts +++ b/plugins/gcp-projects/src/plugin.test.ts @@ -30,7 +30,7 @@ describe('gcp-projects', () => { expect(gcpProjectsPlugin).toBeDefined(); }); - it('should exist the GcpClient class', async () => { + it('spy headers with identifying metadata', async () => { const response: any = { headers: { Accept: '*/*', From b6df34e40222d2cd47e1712f7cca606392cf3a73 Mon Sep 17 00:00:00 2001 From: armandocomellas1 Date: Fri, 16 Feb 2024 15:52:31 -0600 Subject: [PATCH 4/8] Package json deconstructed creating a new file containing only the version Signed-off-by: armandocomellas1 --- plugins/gcp-projects/src/api/GcpClient.ts | 2 +- plugins/gcp-projects/version.json | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 plugins/gcp-projects/version.json diff --git a/plugins/gcp-projects/src/api/GcpClient.ts b/plugins/gcp-projects/src/api/GcpClient.ts index e9375bcfa6..2b102dbd93 100644 --- a/plugins/gcp-projects/src/api/GcpClient.ts +++ b/plugins/gcp-projects/src/api/GcpClient.ts @@ -17,7 +17,7 @@ import { GcpApi } from './GcpApi'; import { Operation, Project } from './types'; import { OAuthApi } from '@backstage/core-plugin-api'; -import packageinfo from '../../package.json'; +import packageinfo from '../../version.json'; const BASE_URL = 'https://content-cloudresourcemanager.googleapis.com/v1/projects'; diff --git a/plugins/gcp-projects/version.json b/plugins/gcp-projects/version.json new file mode 100644 index 0000000000..11e109a959 --- /dev/null +++ b/plugins/gcp-projects/version.json @@ -0,0 +1,3 @@ +{ + "version": "0.3.46-next.1" +} From 7ae4cf6d13600cf21ce5ce8879ea488e9594da37 Mon Sep 17 00:00:00 2001 From: armandocomellas1 Date: Fri, 16 Feb 2024 16:17:56 -0600 Subject: [PATCH 5/8] Package json deconstructed dynamically with variable Signed-off-by: armandocomellas1 --- plugins/gcp-projects/src/api/GcpClient.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/plugins/gcp-projects/src/api/GcpClient.ts b/plugins/gcp-projects/src/api/GcpClient.ts index 2b102dbd93..484b57e1c4 100644 --- a/plugins/gcp-projects/src/api/GcpClient.ts +++ b/plugins/gcp-projects/src/api/GcpClient.ts @@ -17,7 +17,9 @@ import { GcpApi } from './GcpApi'; import { Operation, Project } from './types'; import { OAuthApi } from '@backstage/core-plugin-api'; -import packageinfo from '../../version.json'; +import packageVers from '../../package.json'; + +const { version } = packageVers; const BASE_URL = 'https://content-cloudresourcemanager.googleapis.com/v1/projects'; @@ -31,7 +33,7 @@ export class GcpClient implements GcpApi { headers: { Accept: '*/*', Authorization: `Bearer ${await this.getToken()}`, - 'X-Goog-Api-Client': `backstage/cloudbuild/${packageinfo.version}`, + 'X-Goog-Api-Client': `backstage/cloudbuild/${version}`, }, }); @@ -50,7 +52,7 @@ export class GcpClient implements GcpApi { const response = await fetch(url, { headers: { Authorization: `Bearer ${await this.getToken()}`, - 'X-Goog-Api-Client': `backstage/cloudbuild/${packageinfo.version}`, + 'X-Goog-Api-Client': `backstage/cloudbuild/${version}`, }, }); @@ -77,7 +79,7 @@ export class GcpClient implements GcpApi { headers: { Accept: '*/*', Authorization: `Bearer ${await this.getToken()}`, - 'X-Goog-Api-Client': `backstage/cloudbuild/${packageinfo.version}`, + 'X-Goog-Api-Client': `backstage/cloudbuild/${version}`, }, body: JSON.stringify(newProject), }); From ddd963a9409af2cda02d62bef017b58d9ab00201 Mon Sep 17 00:00:00 2001 From: armandocomellas1 Date: Fri, 16 Feb 2024 16:31:08 -0600 Subject: [PATCH 6/8] Changing the route that we need for the GCP telemetry Signed-off-by: armandocomellas1 --- plugins/gcp-projects/src/api/GcpClient.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/gcp-projects/src/api/GcpClient.ts b/plugins/gcp-projects/src/api/GcpClient.ts index 484b57e1c4..449a99a5a5 100644 --- a/plugins/gcp-projects/src/api/GcpClient.ts +++ b/plugins/gcp-projects/src/api/GcpClient.ts @@ -33,7 +33,7 @@ export class GcpClient implements GcpApi { headers: { Accept: '*/*', Authorization: `Bearer ${await this.getToken()}`, - 'X-Goog-Api-Client': `backstage/cloudbuild/${version}`, + 'X-Goog-Api-Client': `backstage/gcpprojects/${version}`, }, }); @@ -52,7 +52,7 @@ export class GcpClient implements GcpApi { const response = await fetch(url, { headers: { Authorization: `Bearer ${await this.getToken()}`, - 'X-Goog-Api-Client': `backstage/cloudbuild/${version}`, + 'X-Goog-Api-Client': `backstage/gcpprojects/${version}`, }, }); @@ -79,7 +79,7 @@ export class GcpClient implements GcpApi { headers: { Accept: '*/*', Authorization: `Bearer ${await this.getToken()}`, - 'X-Goog-Api-Client': `backstage/cloudbuild/${version}`, + 'X-Goog-Api-Client': `backstage/gcpprojects/${version}`, }, body: JSON.stringify(newProject), }); From cd3e5f78d6ec7d8f8ba0855ddf6ef1e60630f1fc Mon Sep 17 00:00:00 2001 From: armandocomellas1 Date: Sat, 17 Feb 2024 12:10:10 -0600 Subject: [PATCH 7/8] Rollingback de way to bring back the package version Signed-off-by: armandocomellas1 --- plugins/gcp-projects/src/api/GcpClient.ts | 10 ++++------ plugins/gcp-projects/src/plugin.test.ts | 4 ++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/plugins/gcp-projects/src/api/GcpClient.ts b/plugins/gcp-projects/src/api/GcpClient.ts index 449a99a5a5..f9694f266e 100644 --- a/plugins/gcp-projects/src/api/GcpClient.ts +++ b/plugins/gcp-projects/src/api/GcpClient.ts @@ -17,9 +17,7 @@ import { GcpApi } from './GcpApi'; import { Operation, Project } from './types'; import { OAuthApi } from '@backstage/core-plugin-api'; -import packageVers from '../../package.json'; - -const { version } = packageVers; +import packageinfo from '../../package.json'; const BASE_URL = 'https://content-cloudresourcemanager.googleapis.com/v1/projects'; @@ -33,7 +31,7 @@ export class GcpClient implements GcpApi { headers: { Accept: '*/*', Authorization: `Bearer ${await this.getToken()}`, - 'X-Goog-Api-Client': `backstage/gcpprojects/${version}`, + 'X-Goog-Api-Client': `backstage/gcpprojects/${packageinfo.version}`, }, }); @@ -52,7 +50,7 @@ export class GcpClient implements GcpApi { const response = await fetch(url, { headers: { Authorization: `Bearer ${await this.getToken()}`, - 'X-Goog-Api-Client': `backstage/gcpprojects/${version}`, + 'X-Goog-Api-Client': `backstage/gcpprojects/${packageinfo.version}`, }, }); @@ -79,7 +77,7 @@ export class GcpClient implements GcpApi { headers: { Accept: '*/*', Authorization: `Bearer ${await this.getToken()}`, - 'X-Goog-Api-Client': `backstage/gcpprojects/${version}`, + '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 9d7a0541a6..da5f89cbb9 100644 --- a/plugins/gcp-projects/src/plugin.test.ts +++ b/plugins/gcp-projects/src/plugin.test.ts @@ -34,7 +34,7 @@ describe('gcp-projects', () => { const response: any = { headers: { Accept: '*/*', - 'X-Goog-Api-Client': `backstage/cloudbuild/${packageinfo.version}`, + 'X-Goog-Api-Client': `backstage/gcpprojects/${packageinfo.version}`, }, }; jest.spyOn(sut, 'listProjects').mockImplementation((): any => { @@ -44,7 +44,7 @@ describe('gcp-projects', () => { expect(response).toStrictEqual({ headers: { Accept: '*/*', - 'X-Goog-Api-Client': `backstage/cloudbuild/${packageinfo.version}`, + 'X-Goog-Api-Client': `backstage/gcpprojects/${packageinfo.version}`, }, }); }); From 3b2db119b60b210e7a7d37b0f7a2b13726e5c559 Mon Sep 17 00:00:00 2001 From: Armando Comellas Date: Sat, 17 Feb 2024 12:13:12 -0600 Subject: [PATCH 8/8] Delete plugins/gcp-projects/version.json Just deleted this unesesary file. Signed-off-by: Armando Comellas --- plugins/gcp-projects/version.json | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 plugins/gcp-projects/version.json diff --git a/plugins/gcp-projects/version.json b/plugins/gcp-projects/version.json deleted file mode 100644 index 11e109a959..0000000000 --- a/plugins/gcp-projects/version.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "version": "0.3.46-next.1" -}