From 2c3d1790f27b63c6312fb6878ea52553903ffcea Mon Sep 17 00:00:00 2001 From: Syed Ashrafulla Date: Mon, 21 Dec 2020 19:21:33 -0500 Subject: [PATCH] fix: update alert policies one at a time because (concurrent updates are not supported) (#507) --- monitoring/snippets/alerts.js | 33 +++++++++++------------- monitoring/snippets/metrics.js | 2 +- monitoring/snippets/quickstart.js | 5 +++- monitoring/snippets/test/alerts.test.js | 3 ++- monitoring/snippets/test/metrics.test.js | 3 ++- monitoring/snippets/test/uptime.test.js | 3 ++- monitoring/snippets/uptime.js | 2 +- 7 files changed, 27 insertions(+), 24 deletions(-) diff --git a/monitoring/snippets/alerts.js b/monitoring/snippets/alerts.js index 5e40f94c8f..db3db81928 100644 --- a/monitoring/snippets/alerts.js +++ b/monitoring/snippets/alerts.js @@ -254,26 +254,23 @@ async function enablePolicies(projectId, enabled, filter) { }; const [policies] = await client.listAlertPolicies(listAlertPoliciesRequest); - const tasks = await Promise.all( - policies - .map(policy => { - return { - updateMask: { - paths: ['enabled'], - }, - alertPolicy: { - name: policy.name, - enabled: { - value: enabled, - }, + const responses = []; + for (const policy of policies) { + responses.push( + await client.updateAlertPolicy({ + updateMask: { + paths: ['enabled'], + }, + alertPolicy: { + name: policy.name, + enabled: { + value: enabled, }, - }; + }, }) - .map(updateAlertPolicyRequest => - client.updateAlertPolicy(updateAlertPolicyRequest) - ) - ); - tasks.forEach(response => { + ); + } + responses.forEach(response => { const alertPolicy = response[0]; console.log(`${enabled ? 'Enabled' : 'Disabled'} ${alertPolicy.name}.`); }); diff --git a/monitoring/snippets/metrics.js b/monitoring/snippets/metrics.js index 39fedd3360..320b2da685 100644 --- a/monitoring/snippets/metrics.js +++ b/monitoring/snippets/metrics.js @@ -519,7 +519,7 @@ const cli = require('yargs') .options({ projectId: { alias: 'p', - default: process.env.GCLOUD_PROJECT, + default: process.env.GCLOUD_PROJECT || process.env.GOOGLE_CLOUD_PROJECT, global: true, requiresArg: true, type: 'string', diff --git a/monitoring/snippets/quickstart.js b/monitoring/snippets/quickstart.js index 1be7d2e182..b719476869 100644 --- a/monitoring/snippets/quickstart.js +++ b/monitoring/snippets/quickstart.js @@ -20,7 +20,10 @@ const monitoring = require('@google-cloud/monitoring'); async function quickstart() { // Your Google Cloud Platform project ID - const projectId = process.env.GCLOUD_PROJECT || 'YOUR_PROJECT_ID'; + const projectId = + process.env.GCLOUD_PROJECT || + process.env.GOOGLE_CLOUD_PROJECT || + 'YOUR_PROJECT_ID'; // Creates a client const client = new monitoring.MetricServiceClient(); diff --git a/monitoring/snippets/test/alerts.test.js b/monitoring/snippets/test/alerts.test.js index 8662322df4..a3dd3db2a8 100644 --- a/monitoring/snippets/test/alerts.test.js +++ b/monitoring/snippets/test/alerts.test.js @@ -26,7 +26,8 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); const client = new monitoring.AlertPolicyServiceClient(); const channelClient = new monitoring.NotificationChannelServiceClient(); -const projectId = process.env.GCLOUD_PROJECT; +const projectId = + process.env.GCLOUD_PROJECT || process.env.GOOGLE_CLOUD_PROJECT; const cmd = 'node alerts'; let policyOneName, policyTwoName, channelName; diff --git a/monitoring/snippets/test/metrics.test.js b/monitoring/snippets/test/metrics.test.js index 60b09b944d..b9b567cc1f 100644 --- a/monitoring/snippets/test/metrics.test.js +++ b/monitoring/snippets/test/metrics.test.js @@ -26,7 +26,8 @@ const cmd = 'node metrics.js'; const customMetricId = 'custom.googleapis.com/stores/daily_sales'; const computeMetricId = 'compute.googleapis.com/instance/cpu/utilization'; const filter = `metric.type="${computeMetricId}"`; -const projectId = process.env.GCLOUD_PROJECT; +const projectId = + process.env.GCLOUD_PROJECT || process.env.GOOGLE_CLOUD_PROJECT; const resourceId = 'cloudsql_database'; // A helper for delaying integration tests with an exponential backoff. diff --git a/monitoring/snippets/test/uptime.test.js b/monitoring/snippets/test/uptime.test.js index 71b1cd61b7..ffa4961966 100644 --- a/monitoring/snippets/test/uptime.test.js +++ b/monitoring/snippets/test/uptime.test.js @@ -21,7 +21,8 @@ const cp = require('child_process'); const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); const cmd = 'node uptime.js'; -const projectId = process.env.GCLOUD_PROJECT; +const projectId = + process.env.GCLOUD_PROJECT || process.env.GOOGLE_CLOUD_PROJECT; const hostname = 'mydomain.com'; function getResourceObjects(output) { diff --git a/monitoring/snippets/uptime.js b/monitoring/snippets/uptime.js index 442edc85cf..addd5cb483 100644 --- a/monitoring/snippets/uptime.js +++ b/monitoring/snippets/uptime.js @@ -306,7 +306,7 @@ require('yargs') .options({ projectId: { alias: 'p', - default: process.env.GCLOUD_PROJECT, + default: process.env.GCLOUD_PROJECT || process.env.GOOGLE_CLOUD_PROJECT, global: true, requiresArg: true, type: 'string',