1616// [START functions_stop_instance_pubsub]
1717const functions = require ( '@google-cloud/functions-framework' ) ;
1818const { SqlInstancesServiceClient} = require ( '@google-cloud/sql' ) ;
19- const sqlInstancesClient = new SqlInstancesServiceClient ( { fallback : true } ) ;
19+ const sqlInstancesClient = new SqlInstancesServiceClient ( { fallback : true } ) ;
2020// [END functions_stop_instance_pubsub]
2121
2222/**
@@ -27,50 +27,58 @@ const sqlInstancesClient = new SqlInstancesServiceClient({ fallback: true });
2727 *
2828 */
2929functions . cloudEvent ( 'startInstanceEvent' , async cloudEvent => {
30- try {
31- const payload = _validatePayload ( cloudEvent ) ;
32- const project = await sqlInstancesClient . getProjectId ( ) ;
33- const [ labelKey , labelValue ] = payload . label . split ( '=' ) ;
34- const filter = `settings.userLabels.${ labelKey } :${ labelValue } ` ;
35-
36- console . log ( `Attempting to start instances. Project ID resolved to: '${ project } '. Filter applied: '${ filter } '` ) ;
37-
38- // Fetch the response object
39- const [ response ] = await sqlInstancesClient . list ( {
40- project,
41- filter,
42- } ) ;
43-
44- // Extract the array from the 'items' property (default to empty array if undefined)
45- const instances = response . items || [ ] ;
46-
47- console . log ( `Raw instances array retrieved. Found ${ instances . length } matching instances.` ) ;
48-
49- if ( instances . length === 0 ) {
50- console . log ( `No SQL instances found in project '${ project } ' matching filter '${ filter } '.` ) ;
51- return ;
52- }
53-
54- await Promise . all (
55- instances . map ( async instance => {
56- console . log ( `Starting Cloud SQL instance: ${ instance . name } ` ) ;
57- const request = {
58- project,
59- instance : instance . name ,
60- body : {
61- settings : {
62- activationPolicy : 'ALWAYS' ,
30+ try {
31+ const payload = _validatePayload ( cloudEvent ) ;
32+ const project = await sqlInstancesClient . getProjectId ( ) ;
33+ const [ labelKey , labelValue ] = payload . label . split ( '=' ) ;
34+ const filter = `settings.userLabels.${ labelKey } :${ labelValue } ` ;
35+
36+ console . log (
37+ `Attempting to start instances. Project ID resolved to: '${ project } '. Filter applied: '${ filter } '`
38+ ) ;
39+
40+ // Fetch the response object
41+ const [ response ] = await sqlInstancesClient . list ( {
42+ project,
43+ filter,
44+ } ) ;
45+
46+ // Extract the array from the 'items' property (default to empty array if undefined)
47+ const instances = response . items || [ ] ;
48+
49+ console . log (
50+ `Raw instances array retrieved. Found ${ instances . length } matching instances.`
51+ ) ;
52+
53+ if ( instances . length === 0 ) {
54+ console . log (
55+ `No SQL instances found in project '${ project } ' matching filter '${ filter } '.`
56+ ) ;
57+ return ;
58+ }
59+
60+ await Promise . all (
61+ instances . map ( async instance => {
62+ console . log ( `Starting Cloud SQL instance: ${ instance . name } ` ) ;
63+ const request = {
64+ project,
65+ instance : instance . name ,
66+ body : {
67+ settings : {
68+ activationPolicy : 'ALWAYS' ,
69+ } ,
6370 } ,
64- } ,
65- } ;
66- const [ operation ] = await sqlInstancesClient . patch ( request ) ;
67- console . log ( `Patch operation started for ${ instance . name } : ${ operation . name } ` ) ;
68- } )
69- ) ;
70- } catch ( err ) {
71- console . error ( err ) ;
72- throw err ;
73- }
71+ } ;
72+ const [ operation ] = await sqlInstancesClient . patch ( request ) ;
73+ console . log (
74+ `Patch operation started for ${ instance . name } : ${ operation . name } `
75+ ) ;
76+ } )
77+ ) ;
78+ } catch ( err ) {
79+ console . error ( err ) ;
80+ throw err ;
81+ }
7482} ) ;
7583// [END functions_start_instance_pubsub]
7684// [START functions_stop_instance_pubsub]
@@ -83,68 +91,75 @@ try {
8391 *
8492 */
8593functions . cloudEvent ( 'stopInstanceEvent' , async cloudEvent => {
86- try {
87- const payload = _validatePayload ( cloudEvent ) ;
88- const project = await sqlInstancesClient . getProjectId ( ) ;
89- const [ labelKey , labelValue ] = payload . label . split ( '=' ) ;
90- const filter = `settings.userLabels.${ labelKey } :${ labelValue } ` ;
91-
92- console . log ( `Attempting to stop instances. Project ID resolved to: '${ project } '. Filter applied: '${ filter } '` ) ;
93-
94- // Fetch the response object
95- const [ response ] = await sqlInstancesClient . list ( {
96- project,
97- filter,
98- } ) ;
99-
100- // Extract the array from the 'items' property (default to empty array if undefined)
101- const instances = response . items || [ ] ;
102-
103- console . log ( `Raw instances array retrieved. Found ${ instances . length } matching instances.` ) ;
104-
105- if ( instances . length === 0 ) {
106- console . log ( `No SQL instances found in project '${ project } ' matching filter '${ filter } '.` ) ;
107- return ;
108- }
109-
110- await Promise . all (
111- instances . map ( async instance => {
112- console . log ( `Stopping Cloud SQL instance: ${ instance . name } ` ) ;
113- const request = {
114- project,
115- instance : instance . name ,
116- body : {
117- settings : {
118- activationPolicy : 'NEVER' ,
94+ try {
95+ const payload = _validatePayload ( cloudEvent ) ;
96+ const project = await sqlInstancesClient . getProjectId ( ) ;
97+ const [ labelKey , labelValue ] = payload . label . split ( '=' ) ;
98+ const filter = `settings.userLabels.${ labelKey } :${ labelValue } ` ;
99+
100+ console . log (
101+ `Attempting to stop instances. Project ID resolved to: '${ project } '. Filter applied: '${ filter } '`
102+ ) ;
103+
104+ // Fetch the response object
105+ const [ response ] = await sqlInstancesClient . list ( {
106+ project,
107+ filter,
108+ } ) ;
109+
110+ // Extract the array from the 'items' property (default to empty array if undefined)
111+ const instances = response . items || [ ] ;
112+
113+ console . log (
114+ `Raw instances array retrieved. Found ${ instances . length } matching instances.`
115+ ) ;
116+
117+ if ( instances . length === 0 ) {
118+ console . log ( `
119+ No SQL instances found in project '${ project } ' matching filter '${ filter } '.` ) ;
120+ return ;
121+ }
122+
123+ await Promise . all (
124+ instances . map ( async instance => {
125+ console . log ( `Stopping Cloud SQL instance: ${ instance . name } ` ) ;
126+ const request = {
127+ project,
128+ instance : instance . name ,
129+ body : {
130+ settings : {
131+ activationPolicy : 'NEVER' ,
132+ } ,
119133 } ,
120- } ,
121- } ;
122- const [ operation ] = await sqlInstancesClient . patch ( request ) ;
123- console . log ( `Patch operation started for ${ instance . name } : ${ operation . name } ` ) ;
124- } )
125- ) ;
126- } catch ( err ) {
127- console . error ( err ) ;
128- throw err ;
129- }
134+ } ;
135+ const [ operation ] = await sqlInstancesClient . patch ( request ) ;
136+ console . log (
137+ `Patch operation started for ${ instance . name } : ${ operation . name } `
138+ ) ;
139+ } )
140+ ) ;
141+ } catch ( err ) {
142+ console . error ( err ) ;
143+ throw err ;
144+ }
130145} ) ;
131146// [START functions_start_instance_pubsub]
132147
133148/**
134149 * Validates that a request payload contains the expected fields.
135150 */
136151const _validatePayload = cloudEvent => {
137- let payload ;
138- try {
139- const base64Data = cloudEvent . data . message . data ;
140- payload = JSON . parse ( Buffer . from ( base64Data , 'base64' ) . toString ( ) ) ;
141- } catch ( err ) {
142- throw new Error ( 'Invalid CloudEvent / Pub/Sub message: ' + err ) ;
143- }
144- if ( ! payload . label ) {
145- throw new Error ( "Attribute 'label' missing from payload" ) ;
146- }
147- return payload ;
152+ let payload ;
153+ try {
154+ const base64Data = cloudEvent . data . message . data ;
155+ payload = JSON . parse ( Buffer . from ( base64Data , 'base64' ) . toString ( ) ) ;
156+ } catch ( err ) {
157+ throw new Error ( 'Invalid CloudEvent / Pub/Sub message: ' + err ) ;
158+ }
159+ if ( ! payload . label ) {
160+ throw new Error ( "Attribute 'label' missing from payload" ) ;
161+ }
162+ return payload ;
148163} ;
149164// [END functions_start_instance_pubsub]
150165// [END functions_stop_instance_pubsub]
0 commit comments