@@ -64,6 +64,7 @@ function restorePolicies(projectId) {
6464 // Note: The policies are restored one at a time because I get 'service
6565 // unavailable' when I try to create multiple alerts simultaneously.
6666 // [START monitoring_alert_restore_policies]
67+ // [START monitoring_alert_create_policy]
6768 const fs = require ( 'fs' ) ;
6869
6970 // Imports the Google Cloud client library
@@ -125,6 +126,7 @@ function restorePolicies(projectId) {
125126 return Promise . reject ( err ) ;
126127 } ) ;
127128 }
129+ // [END monitoring_alert_create_policy]
128130 // [END monitoring_alert_restore_policies]
129131}
130132
@@ -172,8 +174,8 @@ function replaceChannels(projectId, alertPolicyId, channelIds) {
172174 // [END monitoring_alert_replace_channels]
173175}
174176
175- function disablePolicies ( projectId , filter ) {
176- // [START monitoring_alert_disable_policies ]
177+ function enablePolicies ( projectId , enabled , filter ) {
178+ // [START monitoring_alert_enable_policies ]
177179 // Imports the Google Cloud client library
178180 const monitoring = require ( '@google-cloud/monitoring' ) ;
179181
@@ -184,7 +186,8 @@ function disablePolicies(projectId, filter) {
184186 * TODO(developer): Uncomment the following lines before running the sample.
185187 */
186188 // const projectId = 'YOUR_PROJECT_ID';
187- // const filter = 'A filter for selecting policies, e.g. user_labels="active"';
189+ // const enabled = true;
190+ // const filter = 'A filter for selecting policies, e.g. description:"cloud"';
188191
189192 const listAlertPoliciesRequest = {
190193 name : client . projectPath ( projectId ) ,
@@ -203,31 +206,31 @@ function disablePolicies(projectId, filter) {
203206 updateMask : { paths : [ 'disabled' ] } ,
204207 alertPolicy : {
205208 name : policy . name ,
206- disabled : true ,
209+ disabled : enabled ? false : true ,
207210 } ,
208211 } ;
209212 } )
210- . map ( updateAlertPolicyRequest => {
211- return client . updateAlertPolicy ( updateAlertPolicyRequest ) ;
212- } ) ;
213+ . map ( updateAlertPolicyRequest =>
214+ client . updateAlertPolicy ( updateAlertPolicyRequest )
215+ ) ;
213216
214- // Wait for all policies to be disabled
217+ // Wait for all policies to be enabled
215218 return Promise . all ( tasks ) ;
216219 } )
217220 . then ( responses => {
218221 responses . forEach ( response => {
219222 const alertPolicy = response [ 0 ] ;
220- console . log ( `Disabled ${ alertPolicy . name } .` ) ;
223+ console . log ( `${ enabled ? 'Enabled' : ' Disabled' } ${ alertPolicy . name } .` ) ;
221224 } ) ;
222225 } )
223226 . catch ( err => {
224227 console . error ( 'ERROR:' , err ) ;
225228 } ) ;
226- // [END monitoring_alert_disable_policies ]
229+ // [END monitoring_alert_enable_policies ]
227230}
228231
229- function enablePolicies ( projectId , filter ) {
230- // [START monitoring_alert_enable_policies ]
232+ function listPolicies ( projectId ) {
233+ // [START monitoring_alert_list_policies ]
231234 // Imports the Google Cloud client library
232235 const monitoring = require ( '@google-cloud/monitoring' ) ;
233236
@@ -238,46 +241,28 @@ function enablePolicies(projectId, filter) {
238241 * TODO(developer): Uncomment the following lines before running the sample.
239242 */
240243 // const projectId = 'YOUR_PROJECT_ID';
241- // const filter = 'A filter for selecting policies, e.g. description:"cloud"';
242244
243245 const listAlertPoliciesRequest = {
244246 name : client . projectPath ( projectId ) ,
245- // See https://cloud.google.com/monitoring/alerting/docs/sorting-and-filtering
246- filter : filter ,
247247 } ;
248248
249249 client
250250 . listAlertPolicies ( listAlertPoliciesRequest )
251251 . then ( results => {
252252 const policies = results [ 0 ] ;
253253
254- const tasks = policies
255- . map ( policy => {
256- return {
257- updateMask : { paths : [ 'disabled' ] } ,
258- alertPolicy : {
259- name : policy . name ,
260- disabled : false ,
261- } ,
262- } ;
263- } )
264- . map ( updateAlertPolicyRequest =>
265- client . updateAlertPolicy ( updateAlertPolicyRequest )
266- ) ;
267-
268- // Wait for all policies to be enabled
269- return Promise . all ( tasks ) ;
270- } )
271- . then ( responses => {
272- responses . forEach ( response => {
273- const alertPolicy = response [ 0 ] ;
274- console . log ( `Enabled ${ alertPolicy . name } .` ) ;
254+ console . log ( 'Policies:' ) ;
255+ policies . forEach ( policy => {
256+ console . log ( ` Display name: ${ policy . displayName } ` ) ;
257+ if ( policy . documentation && policy . documentation . content ) {
258+ console . log ( ` Documentation: ${ policy . documentation . content } ` ) ;
259+ }
275260 } ) ;
276261 } )
277262 . catch ( err => {
278263 console . error ( 'ERROR:' , err ) ;
279264 } ) ;
280- // [END monitoring_alert_enable_policies ]
265+ // [END monitoring_alert_list_policies ]
281266}
282267
283268require ( `yargs` )
@@ -308,13 +293,19 @@ require(`yargs`)
308293 `disable <projectId> [filter]` ,
309294 `Disables policies that match the given filter.` ,
310295 { } ,
311- opts => disablePolicies ( opts . projectId , opts . filter || `` )
296+ opts => enablePolicies ( opts . projectId , false , opts . filter || `` )
312297 )
313298 . command (
314299 `enable <projectId> [filter]` ,
315300 `Enables policies that match the given filter.` ,
316301 { } ,
317- opts => enablePolicies ( opts . projectId , opts . filter || `` )
302+ opts => enablePolicies ( opts . projectId , true , opts . filter || `` )
303+ )
304+ . command (
305+ `list <projectId>` ,
306+ `Lists alert policies in the specified project.` ,
307+ { } ,
308+ opts => listPolicies ( opts . projectId )
318309 )
319310 . options ( {
320311 alertPolicyName : {
0 commit comments