@@ -19,24 +19,23 @@ const gcpMetadata = require('gcp-metadata');
1919
2020const 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 }
0 commit comments