in monitoring/api/AlertSample/Program.cs [175:284]
static void RestorePolicies(string projectId, string filePath)
{
var policyClient = AlertPolicyServiceClient.Create();
var channelClient = NotificationChannelServiceClient.Create();
List<Exception> exceptions = new List<Exception>();
var backup = JsonConvert.DeserializeObject<BackupRecord>(
File.ReadAllText(filePath), new ProtoMessageConverter());
var projectName = new ProjectName(projectId);
bool isSameProject = projectId == backup.ProjectId;
// When a channel is recreated, rather than updated, it will get
// a new name. We have to update the AlertPolicy with the new
// name. Track the names in this map.
var channelNameMap = new Dictionary<string, string>();
foreach (NotificationChannel channel in backup.Channels)
{
// [END monitoring_alert_create_policy]
try
{
bool updated = false;
Console.WriteLine("Updating channel.\n{0}",
channel.DisplayName);
// This field is immutable and it is illegal to specify a
// non-default value (UNVERIFIED or VERIFIED) in the
// Create() or Update() operations.
channel.VerificationStatus = NotificationChannel.Types
.VerificationStatus.Unspecified;
if (isSameProject)
try
{
channelClient.UpdateNotificationChannel(
null, channel);
updated = true;
}
catch (Grpc.Core.RpcException e)
when (e.Status.StatusCode == StatusCode.NotFound)
{ }
if (!updated)
{
// The channel no longer exists. Recreate it.
string oldName = channel.Name;
channel.Name = null;
var response = channelClient.CreateNotificationChannel(
projectName, channel);
channelNameMap.Add(oldName, response.Name);
}
}
catch (Exception e)
{
// If one failed, continue trying to update the others.
exceptions.Add(e);
}
// [START monitoring_alert_create_policy]
}
foreach (AlertPolicy policy in backup.Policies)
{
// [END monitoring_alert_create_channel]
// [END monitoring_alert_update_channel]
string policyName = policy.Name;
// These two fields cannot be set directly, so clear them.
policy.CreationRecord = null;
policy.MutationRecord = null;
// Update channel names if the channel was recreated with
// another name.
for (int i = 0; i < policy.NotificationChannels.Count; ++i)
{
if (channelNameMap.ContainsKey(policy.NotificationChannels[i]))
{
policy.NotificationChannels[i] =
channelNameMap[policy.NotificationChannels[i]];
}
}
try
{
Console.WriteLine("Updating policy.\n{0}",
policy.DisplayName);
bool updated = false;
if (isSameProject)
try
{
policyClient.UpdateAlertPolicy(null, policy);
updated = true;
}
catch (Grpc.Core.RpcException e)
when (e.Status.StatusCode == StatusCode.NotFound)
{ }
if (!updated)
{
// The policy no longer exists. Recreate it.
policy.Name = null;
foreach (var condition in policy.Conditions)
{
condition.Name = null;
}
policyClient.CreateAlertPolicy(projectName, policy);
}
Console.WriteLine("Restored {0}.", policyName);
}
catch (Exception e)
{
// If one failed, continue trying to update the others.
exceptions.Add(e);
}
// [START monitoring_alert_create_channel]
// [START monitoring_alert_update_channel]
}
if (exceptions.Count > 0)
{
throw new AggregateException(exceptions);
}
}