Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
with tags
Browse files Browse the repository at this point in the history
  • Loading branch information
fhinkel committed Dec 13, 2018
1 parent 99b8d4a commit 40463ad
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
61 changes: 61 additions & 0 deletions samples/alerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,40 @@ 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};
let channels = await client.listNotificationChannels(request);

for(let channel of channels) {
console.log(`Deleting channel ${channel.displayName}`);
await client.deleteNotificationChannel({name: client.notificationChannelPath(projectId, channel.name)});
}
// [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 +172,29 @@ async function replaceChannels(projectId, alertPolicyId, channelIds) {
notificationClient.notificationChannelPath(projectId, id)
);


for(let 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'},
},
};
let newChannel = await notificationClient.createNotificationChannel(createChannelRequest);
notificationChannels.push(newChannel);
}
}

const updateAlertPolicyRequest = {
updateMask: {paths: ['notification_channels']},
alertPolicy: {
Expand Down Expand Up @@ -268,6 +323,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
2 changes: 1 addition & 1 deletion samples/system-test/alerts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,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 40463ad

Please sign in to comment.