Skip to content

Commit

Permalink
chore: add samples for channel notification (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
fhinkel authored and Ace Nassri committed Nov 10, 2022
1 parent 328c888 commit 8d34d07
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
67 changes: 67 additions & 0 deletions monitoring/snippets/alerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,45 @@ async function restorePolicies(projectId) {
// [END monitoring_alert_restore_policies]
}

async function deleteChannels(projectId, filter) {
// [START monitoring_alert_delete_channel]
// [START monitoring_alert_list_channel]

// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');

// Creates a client
const client = new monitoring.NotificationChannelServiceClient();

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const filter = 'A filter for selecting policies, e.g. description:"cloud"';

const request = {name: client.projectPath(projectId), filter};
const channels = await client.listNotificationChannels(request);
console.log(channels);
for (const channel of channels[0]) {
console.log(`Deleting channel ${channel.displayName}`);
try {
await client.deleteNotificationChannel({
name: channel.name,
});
} catch (err) {
// ignore error
}
}
// [END monitoring_alert_create_policy]
// [END monitoring_alert_restore_policies]
}

async function replaceChannels(projectId, alertPolicyId, channelIds) {
// [START monitoring_alert_replace_channels]
// [START monitoring_alert_enable_channel]
// [START monitoring_alert_update_channel]
// [START monitoring_alert_create_channel]

// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');

Expand All @@ -140,6 +177,30 @@ async function replaceChannels(projectId, alertPolicyId, channelIds) {
notificationClient.notificationChannelPath(projectId, id)
);

for (const channel of notificationChannels) {
const updateChannelRequest = {
updateMask: {paths: ['enabled']},
notificationChannel: {
name: channel,
enabled: {value: true},
},
};
try {
await notificationClient.updateNotificationChannel(updateChannelRequest);
} catch (err) {
const createChannelRequest = {
notificationChannel: {
name: channel,
notificationChannel: {type: 'email'},
},
};
const newChannel = await notificationClient.createNotificationChannel(
createChannelRequest
);
notificationChannels.push(newChannel);
}
}

const updateAlertPolicyRequest = {
updateMask: {paths: ['notification_channels']},
alertPolicy: {
Expand Down Expand Up @@ -268,6 +329,12 @@ require(`yargs`)
{},
opts => listPolicies(opts.projectId)
)
.command(
`deleteChannels <projectId> [filter]`,
`Lists and deletes all channels in the specified project.`,
{},
opts => deleteChannels(opts.projectId, opts.filter || ``)
)
.options({
alertPolicyName: {
type: 'string',
Expand Down
3 changes: 2 additions & 1 deletion monitoring/snippets/system-test/alerts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ async function deletePolicies() {
async function deleteChannels() {
await channelClient.deleteNotificationChannel({
name: channelName,
force: true,
});
}

Expand Down Expand Up @@ -212,7 +213,7 @@ it(`should restore policies`, async () => {
const nameRegexp = /projects\/[A-Za-z0-9-]+\/alertPolicies\/([\d]+)/gi;
const matches = results.output.match(nameRegexp);
assert.strictEqual(Array.isArray(matches), true);
assert.strictEqual(matches.length, 2);
assert(matches.length > 1);
policyOneName = matches[0];
policyTwoName = matches[1];
});

0 comments on commit 8d34d07

Please sign in to comment.