private static void restorePolicies(String projectId, String filePath) throws IOException {
FileReader reader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(reader);
JsonObject backupContent = getPolicyJsonContents(filePath, bufferedReader);
String backupProjectId = backupContent.get("project_id").getAsString();
boolean isSameProject = projectId.equals(backupProjectId);
AlertPolicy[] policies = gson.fromJson(backupContent.get("policies"), AlertPolicy[].class);
List<NotificationChannel> notificationChannels = readNotificationChannelsJson(backupContent);
Map<String, String> restoredChannelIds =
restoreNotificationChannels(projectId, notificationChannels, isSameProject);
List<AlertPolicy> policiesToRestore =
reviseRestoredPolicies(policies, isSameProject, restoredChannelIds);
restoreRevisedPolicies(projectId, isSameProject, policiesToRestore);
}
private static List<AlertPolicy> reviseRestoredPolicies(
AlertPolicy[] policies, boolean isSameProject, Map<String, String> restoredChannelIds) {
List<AlertPolicy> newPolicies = Lists.newArrayListWithCapacity(policies.length);
for (AlertPolicy policy : policies) {
AlertPolicy.Builder policyBuilder =
policy
.toBuilder()
.clearNotificationChannels()
.clearMutationRecord()
.clearCreationRecord();
// Update restored notification channel names.
for (String channelName : policy.getNotificationChannelsList()) {
String newChannelName = restoredChannelIds.get(channelName);
if (!Strings.isNullOrEmpty(newChannelName)) {
policyBuilder.addNotificationChannels(newChannelName);
}
}
if (!isSameProject) {
policyBuilder.clearName();
policyBuilder.clearConditions();
for (AlertPolicy.Condition condition : policy.getConditionsList()) {
policyBuilder.addConditions(condition.toBuilder().clearName());
}
}
newPolicies.add(policyBuilder.build());
}
return newPolicies;
}
private static void restoreRevisedPolicies(
String projectId, boolean isSameProject, List<AlertPolicy> policies) throws IOException {
try (AlertPolicyServiceClient client = AlertPolicyServiceClient.create()) {
for (AlertPolicy policy : policies) {
if (!isSameProject) {
policy = client.createAlertPolicy(ProjectName.of(projectId), policy);
} else {
try {
client.updateAlertPolicy(null, policy);
} catch (Exception e) {
policy =
client.createAlertPolicy(
ProjectName.of(projectId), policy.toBuilder().clearName().build());
}
}
System.out.println(String.format("Restored %s", policy.getName()));
}
}
}
private static List<NotificationChannel> readNotificationChannelsJson(JsonObject backupContent) {
if (backupContent.has("notification_channels")) {
NotificationChannel[] channels =
gson.fromJson(backupContent.get("notification_channels"), NotificationChannel[].class);
return Lists.newArrayList(channels);
}
return Lists.newArrayList();
}
private static Map<String, String> restoreNotificationChannels(
String projectId, List<NotificationChannel> channels, boolean isSameProject)
throws IOException {
Map<String, String> newChannelNames = Maps.newHashMap();
try (NotificationChannelServiceClient client = NotificationChannelServiceClient.create()) {
for (NotificationChannel channel : channels) {
// Update channel name if project ID is different.
boolean channelUpdated = false;
if (isSameProject) {
try {
NotificationChannel updatedChannel =
client.updateNotificationChannel(NOTIFICATION_CHANNEL_UPDATE_MASK, channel);
newChannelNames.put(channel.getName(), updatedChannel.getName());
channelUpdated = true;
} catch (Exception e) {
channelUpdated = false;
}
}
if (!channelUpdated) {
NotificationChannel newChannel =
client.createNotificationChannel(
ProjectName.of(projectId),
channel.toBuilder().clearName().clearVerificationStatus().build());
newChannelNames.put(channel.getName(), newChannel.getName());
}
}
}
return newChannelNames;
}
private static JsonObject getPolicyJsonContents(String filePath, BufferedReader content) {
try {
return gson.fromJson(content, JsonObject.class);
} catch (JsonSyntaxException jse) {
throw new RuntimeException(String.format("Could not parse policies file %s", filePath), jse);
}
}