Skip to content

Commit 3c6c473

Browse files
committed
fix(functions): stop-billing - Apply feedback from glasnt #4085 (review)
1 parent b279adf commit 3c6c473

File tree

5 files changed

+21
-46
lines changed

5 files changed

+21
-46
lines changed
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
{
2-
"rules": {
3-
"node/no-missing-import": ["off"],
4-
"node/no-missing-require": ["off"],
5-
"node/no-unpublished-import": ["off"],
6-
"node/no-unpublished-require": ["off"],
7-
"node/no-unsupported-features/es-syntax": ["off"]
8-
},
92
"parserOptions": {
103
"ecmaVersion": 2020
114
}
12-
}
5+
}

functions/billing/stop-billing/.gcloudignore

Lines changed: 0 additions & 16 deletions
This file was deleted.

functions/billing/stop-billing/index.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,23 @@ const gcpMetadata = require('gcp-metadata');
1919

2020
const billing = new CloudBillingClient();
2121

22-
const projectIdEnv = process.env.GOOGLE_CLOUD_PROJECT;
22+
let projectId = process.env.GOOGLE_CLOUD_PROJECT;
2323

24-
functions.cloudEvent('StopBillingCloudEvent', async cloudEvent => {
25-
// TODO(developer): As stopping billing is a destructive action
26-
// for your project, change the following constant to `false`
27-
// after you validate with a test budget.
28-
const simulateDeactivation = true;
29-
30-
let projectId = projectIdEnv;
24+
// TODO(developer): Since stopping billing is a destructive action
25+
// for your project, first validate a test budget with a dry run enabled.
26+
const dryRun = true;
3127

28+
functions.cloudEvent('StopBillingCloudEvent', async cloudEvent => {
3229
if (projectId === undefined) {
33-
console.log(
34-
'Project ID not found in env variables. Getting GCP metadata...'
35-
);
3630
try {
3731
projectId = await gcpMetadata.project('project-id');
3832
} catch (error) {
3933
console.error('project-id metadata not found:', error);
34+
35+
console.error(
36+
'Project ID could not be found in environment variables ' +
37+
'or Cloud Run metadata server. Stopping execution.'
38+
);
4039
return;
4140
}
4241
}
@@ -50,6 +49,7 @@ functions.cloudEvent('StopBillingCloudEvent', async cloudEvent => {
5049
console.error('Invalid CloudEvent: missing data.message.data');
5150
return;
5251
}
52+
5353
const eventData = Buffer.from(messageData, 'base64').toString();
5454

5555
let eventObject;
@@ -75,7 +75,7 @@ functions.cloudEvent('StopBillingCloudEvent', async cloudEvent => {
7575

7676
const billingEnabled = await _isBillingEnabled(projectName);
7777
if (billingEnabled) {
78-
await _disableBillingForProject(projectName, simulateDeactivation);
78+
await _disableBillingForProject(projectName);
7979
} else {
8080
console.log('Billing is already disabled.');
8181
}
@@ -106,14 +106,12 @@ const _isBillingEnabled = async projectName => {
106106
/**
107107
* Disable billing for a project by removing its billing account
108108
* @param {string} projectName The name of the project to disable billing
109-
* @param {boolean} simulateDeactivation
110-
* If true, it won't actually disable billing.
111-
* Useful to validate with test budgets.
112109
* @returns {void}
113110
*/
114-
const _disableBillingForProject = async (projectName, simulateDeactivation) => {
115-
if (simulateDeactivation) {
116-
console.log('Billing disabled. (Simulated)');
111+
const _disableBillingForProject = async projectName => {
112+
if (dryRun) {
113+
console.log('** DRY RUN: simulating billing deactivation');
114+
console.log('Billing disabled.');
117115
return;
118116
}
119117

@@ -128,7 +126,7 @@ const _disableBillingForProject = async (projectName, simulateDeactivation) => {
128126
resource: requestBody,
129127
});
130128

131-
console.log(`Billing disabled: ${JSON.stringify(response)}`);
129+
console.log(`Billing disabled. Response: ${JSON.stringify(response)}`);
132130
} catch (e) {
133131
console.error('Failed to disable billing, check permissions.', e);
134132
}

functions/billing/stop-billing/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"scripts": {
1111
"test": "c8 mocha -p -j 2 test/index.test.js --timeout=5000 --exit"
1212
},
13-
"author": "Emmanuel Parada <[email protected]>",
13+
"author": "Google Inc.",
1414
"license": "Apache-2.0",
1515
"dependencies": {
1616
"@google-cloud/billing": "^5.1.0",
@@ -21,4 +21,4 @@
2121
"devDependencies": {
2222
"c8": "^10.1.3"
2323
}
24-
}
24+
}

functions/billing/stop-billing/test/index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ describe('index.test.js', () => {
113113

114114
assert.ok(consoleOutput.includes('Getting billing info'));
115115
assert.ok(consoleOutput.includes('Disabling billing for project'));
116-
assert.ok(consoleOutput.includes('Billing disabled. (Simulated)'));
116+
assert.ok(consoleOutput.includes('Billing disabled.'));
117117
});
118118
});
119119
});

0 commit comments

Comments
 (0)