Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function restorePolicies(projectId) {
// Note: The policies are restored one at a time because I get 'service
// unavailable' when I try to create multiple alerts simultaneously.
// [START monitoring_alert_restore_policies]
// [START monitoring_alert_create_policy]
const fs = require('fs');
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.AlertPolicyServiceClient();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
console.log('Loading policies from ./policies_backup.json');
const fileContent = fs.readFileSync('./policies_backup.json', 'utf-8');
const policies = JSON.parse(fileContent);
for (const index in policies) {
// Restore each policy one at a time
let policy = policies[index];
if (await doesAlertPolicyExist(policy.name)) {
policy = await client.updateAlertPolicy({alertPolicy: policy});
} else {
async function listPolicies(projectId) {
// [START monitoring_alert_list_policies]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.AlertPolicyServiceClient();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
const listAlertPoliciesRequest = {
name: client.projectPath(projectId),
};
const [policies] = await client.listAlertPolicies(listAlertPoliciesRequest);
console.log('Policies:');
policies.forEach(policy => {
console.log(` Display name: ${policy.displayName}`);
if (policy.documentation && policy.documentation.content) {
console.log(` Documentation: ${policy.documentation.content}`);
}
async function backupPolicies(projectId) {
// [START monitoring_alert_backup_policies]
const fs = require('fs');
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.AlertPolicyServiceClient();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
const listAlertPoliciesRequest = {
name: client.projectPath(projectId),
};
let [policies] = await client.listAlertPolicies(listAlertPoliciesRequest);
// filter out any policies created by tests for this sample
policies = policies.filter(policy => {
return !policy.displayName.startsWith('gcloud-tests-');
});
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');
// Creates clients
const alertClient = new monitoring.AlertPolicyServiceClient();
const notificationClient = new monitoring.NotificationChannelServiceClient();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const alertPolicyId = '123456789012314';
// const channelIds = [
// 'channel-1',
// 'channel-2',
// 'channel-3',
// ];
const notificationChannels = channelIds.map(id =>
notificationClient.notificationChannelPath(projectId, id)
);
async function enablePolicies(projectId, enabled, filter) {
// [START monitoring_alert_enable_policies]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.AlertPolicyServiceClient();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const enabled = true;
// const filter = 'A filter for selecting policies, e.g. description:"cloud"';
const listAlertPoliciesRequest = {
name: client.projectPath(projectId),
// See https://cloud.google.com/monitoring/alerting/docs/sorting-and-filtering
filter: filter,
};
const [policies] = await client.listAlertPolicies(listAlertPoliciesRequest);
const tasks = await Promise.all(