Skip to content

Commit 5863455

Browse files
committed
fix(functions): add a variable for the developer to simulate disabling billing
- Style fixes - Fix comments
1 parent 24c71f9 commit 5863455

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

functions/billing/stop_billing/index.js

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,21 @@ const gcpMetadata = require('gcp-metadata');
2020
const billing = new CloudBillingClient();
2121

2222
functions.cloudEvent('stopBilling', async cloudEvent => {
23-
let PROJECT_ID;
23+
// TODO(developer): As stopping billing is a destructive action
24+
// for your project, change the following constant to false
25+
// after you validate with a test budget.
26+
const simulateDeactivation = true;
27+
28+
let projectId;
2429

2530
try {
26-
PROJECT_ID = await gcpMetadata.project('project-id');
31+
projectId = await gcpMetadata.project('project-id');
2732
} catch (error) {
28-
console.error('PROJECT_ID not found:', error);
33+
console.error('project-id metadata not found:', error);
2934
return;
3035
}
3136

32-
const PROJECT_NAME = `projects/${PROJECT_ID}`;
37+
const projectName = `projects/${projectId}`;
3338

3439
const eventData = Buffer.from(
3540
cloudEvent.data['message']['data'],
@@ -47,18 +52,20 @@ functions.cloudEvent('stopBilling', async cloudEvent => {
4752
return;
4853
}
4954

50-
const billingEnabled = await _isBillingEnabled(PROJECT_NAME);
55+
console.log(`Disabling billing for project '${projectName}'...`);
56+
57+
const billingEnabled = await _isBillingEnabled(projectName);
5158
if (billingEnabled) {
52-
_disableBillingForProject(PROJECT_NAME);
59+
_disableBillingForProject(projectName, simulateDeactivation);
5360
} else {
5461
console.log('Billing is already disabled.');
5562
}
5663
});
5764

5865
/**
5966
* Determine whether billing is enabled for a project
60-
* @param {string} projectName Name of project to check if billing is enabled
61-
* @return {bool} Whether project has billing enabled or not
67+
* @param {string} projectName The name of the project to check
68+
* @returns {boolean} Whether the project has billing enabled or not
6269
*/
6370
const _isBillingEnabled = async projectName => {
6471
try {
@@ -79,30 +86,30 @@ const _isBillingEnabled = async projectName => {
7986

8087
/**
8188
* Disable billing for a project by removing its billing account
82-
* @param {string} projectName Name of project disable billing on
89+
* @param {string} projectName The name of the project to disable billing
90+
* @param {boolean} simulateDeactivation
91+
* If true, it won't actually disable billing.
92+
* Useful to validate with test budgets.
93+
* @returns {void}
8394
*/
84-
const _disableBillingForProject = async projectName => {
85-
console.log(`Disabling billing for project '${projectName}'...`);
86-
87-
// To disable billing set the `billingAccountName` field to empty
88-
// LINT: Commented out to pass linter
89-
// const requestBody = {billingAccountName: ''};
95+
const _disableBillingForProject = async (projectName, simulateDeactivation) => {
96+
if (simulateDeactivation) {
97+
console.log('Billing disabled. (Simulated)');
98+
return;
99+
}
90100

91-
// Find more information about `updateBillingInfo` API method here:
101+
// Find more information about `projects/updateBillingInfo` API method here:
92102
// https://cloud.google.com/billing/docs/reference/rest/v1/projects/updateBillingInfo
93-
94103
try {
95-
// DEBUG: Simulate disabling billing
96-
console.log('Billing disabled. (Simulated)');
104+
// To disable billing set the `billingAccountName` field to empty
105+
const requestBody = {billingAccountName: ''};
97106

98-
/*
99107
const [response] = await billing.updateProjectBillingInfo({
100108
name: projectName,
101-
resource: body, // Disable billing
109+
resource: requestBody,
102110
});
103111

104112
console.log(`Billing disabled: ${JSON.stringify(response)}`);
105-
*/
106113
} catch (e) {
107114
console.log('Failed to disable billing, check permissions.', e);
108115
}

0 commit comments

Comments
 (0)