Skip to content

Commit bb58b19

Browse files
committed
fix(functions): stop-billing - Apply feedback from gemini-code-assist review
#4085 (review)
1 parent 8505f6a commit bb58b19

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

functions/billing/stop-billing/index.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ functions.cloudEvent('StopBillingCloudEvent', async cloudEvent => {
3030
let projectId = projectIdEnv;
3131

3232
if (projectId === undefined) {
33-
console.log('Project ID not found in Env variables. Reading metadata...');
33+
console.log('Project ID not found in env variables. Getting GCP metadata...');
3434
try {
3535
projectId = await gcpMetadata.project('project-id');
3636
} catch (error) {
@@ -43,12 +43,20 @@ functions.cloudEvent('StopBillingCloudEvent', async cloudEvent => {
4343

4444
// Find more information about the notification format here:
4545
// https://cloud.google.com/billing/docs/how-to/budgets-programmatic-notifications#notification-format
46-
const eventData = Buffer.from(
47-
cloudEvent.data['message']['data'],
48-
'base64'
49-
).toString();
46+
const messageData = cloudEvent.data?.message?.data;
47+
if (!messageData) {
48+
console.error('Invalid CloudEvent: missing data.message.data');
49+
return;
50+
}
51+
const eventData = Buffer.from(messageData, 'base64').toString();
5052

51-
const eventObject = JSON.parse(eventData);
53+
let eventObject;
54+
try {
55+
eventObject = JSON.parse(eventData);
56+
} catch (e) {
57+
console.error('Error parsing event data:', e);
58+
return;
59+
}
5260

5361
console.log(
5462
`Project ID: ${projectId} ` +
@@ -63,7 +71,7 @@ functions.cloudEvent('StopBillingCloudEvent', async cloudEvent => {
6371

6472
console.log(`Disabling billing for project '${projectName}'...`);
6573

66-
const billingEnabled = await _isBillingEnabled(projectName);
74+
const billingEnabled = _isBillingEnabled(projectName);
6775
if (billingEnabled) {
6876
_disableBillingForProject(projectName, simulateDeactivation);
6977
} else {

functions/billing/stop-billing/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cloud-functions-stop-billing",
3-
"private": "true",
3+
"private": true,
44
"version": "0.0.1",
55
"description": "Disable billing with a budget notification.",
66
"main": "index.js",

0 commit comments

Comments
 (0)