Skip to content

Commit 748f0d9

Browse files
authored
Fixes #43. (#44)
Makes alert tests more stable and adds a list alert sample.
1 parent 6991a78 commit 748f0d9

File tree

3 files changed

+73
-65
lines changed

3 files changed

+73
-65
lines changed

monitoring/snippets/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Commands:
4141
alerts.js replace <alertPolicyName> <channelNames..> Replace the notification channels of the specified alert policy.
4242
alerts.js disable <projectId> [filter] Disables policies that match the given filter.
4343
alerts.js enable <projectId> [filter] Enables policies that match the given filter.
44+
alerts.js list <projectId> Lists alert policies in the specified project.
4445
4546
Options:
4647
--version Show version number [boolean]

monitoring/snippets/alerts.js

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -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

283268
require(`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: {

monitoring/snippets/system-test/alerts.test.js

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ test.before(async () => {
3636
alertPolicy: {
3737
displayName: 'first_policy',
3838
combiner: 1,
39+
documentation: {
40+
content: 'Test',
41+
mimeType: 'text/markdown',
42+
},
3943
conditions: [
4044
{
4145
displayName: 'Condition 1',
@@ -131,32 +135,6 @@ test.after.always(async () => {
131135
await deleteChannels();
132136
});
133137

134-
test.serial(`should backup all policies`, async t => {
135-
const results = await tools.spawnAsyncWithIO(
136-
`node`,
137-
[`alerts.js`, `backup`, projectId],
138-
cwd
139-
);
140-
t.regex(results.output, /Saved policies to \.\/policies_backup.json/);
141-
t.true(fs.existsSync(path.join(cwd, `policies_backup.json`)));
142-
await client.deleteAlertPolicy({name: policyOneName});
143-
});
144-
145-
test.serial(`should restore policies`, async t => {
146-
const results = await tools.spawnAsyncWithIO(
147-
`node`,
148-
[`alerts.js`, `restore`, projectId],
149-
cwd
150-
);
151-
t.regex(results.output, /Loading policies from .\/policies_backup.json/);
152-
const nameRegexp = /projects\/[A-Za-z0-9-]+\/alertPolicies\/([\d]+)/gi;
153-
const matches = results.output.match(nameRegexp);
154-
t.true(Array.isArray(matches));
155-
t.is(matches.length, 2);
156-
policyOneName = matches[0];
157-
policyTwoName = matches[1];
158-
});
159-
160138
test.serial(`should replace notification channels`, async t => {
161139
const results = await tools.spawnAsyncWithIO(
162140
`node`,
@@ -188,3 +166,41 @@ test.serial(`should enable policies`, async t => {
188166
t.false(results.output.includes(policyOneName));
189167
t.true(results.output.includes(policyTwoName));
190168
});
169+
170+
test.serial(`should list policies`, async t => {
171+
const results = await tools.spawnAsyncWithIO(
172+
`node`,
173+
[`alerts.js`, `list`, projectId],
174+
cwd
175+
);
176+
t.regex(results.output, /Policies:/);
177+
t.true(results.output.includes('first_policy'));
178+
t.true(results.output.includes('Test'));
179+
t.true(results.output.includes('second'));
180+
});
181+
182+
test.serial(`should backup all policies`, async t => {
183+
const results = await tools.spawnAsyncWithIO(
184+
`node`,
185+
[`alerts.js`, `backup`, projectId],
186+
cwd
187+
);
188+
t.regex(results.output, /Saved policies to \.\/policies_backup.json/);
189+
t.true(fs.existsSync(path.join(cwd, `policies_backup.json`)));
190+
await client.deleteAlertPolicy({name: policyOneName});
191+
});
192+
193+
test.serial(`should restore policies`, async t => {
194+
const results = await tools.spawnAsyncWithIO(
195+
`node`,
196+
[`alerts.js`, `restore`, projectId],
197+
cwd
198+
);
199+
t.regex(results.output, /Loading policies from .\/policies_backup.json/);
200+
const nameRegexp = /projects\/[A-Za-z0-9-]+\/alertPolicies\/([\d]+)/gi;
201+
const matches = results.output.match(nameRegexp);
202+
t.true(Array.isArray(matches));
203+
t.is(matches.length, 2);
204+
policyOneName = matches[0];
205+
policyTwoName = matches[1];
206+
});

0 commit comments

Comments
 (0)