use of com.google.monitoring.v3.AlertPolicy in project java-docs-samples by GoogleCloudPlatform.
the class AlertSample method reviseRestoredPolicies.
private 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;
}
use of com.google.monitoring.v3.AlertPolicy in project java-docs-samples by GoogleCloudPlatform.
the class AlertSample method restorePolicies.
// [END monitoring_alert_backup_policies]
// [START monitoring_alert_restore_policies]
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);
}
use of com.google.monitoring.v3.AlertPolicy in project java-docs-samples by GoogleCloudPlatform.
the class AlertSample method listAlertPolicies.
// [START monitoring_alert_list_policies]
private static void listAlertPolicies(String projectId) throws IOException {
try (AlertPolicyServiceClient client = AlertPolicyServiceClient.create()) {
ListAlertPoliciesPagedResponse response = client.listAlertPolicies(ProjectName.of(projectId));
System.out.println("Alert Policies:");
for (AlertPolicy policy : response.iterateAll()) {
System.out.println(String.format("\nPolicy %s\nalert-id: %s", policy.getDisplayName(), policy.getName()));
int channels = policy.getNotificationChannelsCount();
if (channels > 0) {
System.out.println("notification-channels:");
for (int i = 0; i < channels; i++) {
System.out.println("\t" + policy.getNotificationChannels(i));
}
}
if (policy.hasDocumentation() && policy.getDocumentation().getContent() != null) {
System.out.println(policy.getDocumentation().getContent());
}
}
}
}
use of com.google.monitoring.v3.AlertPolicy in project java-docs-samples by GoogleCloudPlatform.
the class AlertSample method replaceChannels.
// [END monitoring_alert_restore_policies]
// [START monitoring_alert_replace_channels]
private static void replaceChannels(String projectId, String alertPolicyId, String[] channelIds) throws IOException {
AlertPolicy.Builder policyBuilder = AlertPolicy.newBuilder().setName(AlertPolicyName.of(projectId, alertPolicyId).toString());
for (String channelId : channelIds) {
policyBuilder.addNotificationChannels(NotificationChannelName.of(projectId, channelId).toString());
}
try (AlertPolicyServiceClient client = AlertPolicyServiceClient.create()) {
AlertPolicy result = client.updateAlertPolicy(FieldMask.newBuilder().addPaths("notification_channels").build(), policyBuilder.build());
System.out.println(String.format("Updated %s", result.getName()));
}
}
use of com.google.monitoring.v3.AlertPolicy in project java-docs-samples by GoogleCloudPlatform.
the class AlertSample method writePoliciesBackupFile.
// [END monitoring_alert_list_channels]
private static void writePoliciesBackupFile(String projectId, String filePath, List<AlertPolicy> alertPolicies, List<NotificationChannel> notificationChannels) throws IOException {
JsonObject backupContents = new JsonObject();
backupContents.add("project_id", new JsonPrimitive(projectId));
JsonArray policiesJson = new JsonArray();
for (AlertPolicy policy : alertPolicies) {
policiesJson.add(gson.toJsonTree(policy));
}
backupContents.add("policies", policiesJson);
JsonArray notificationsJson = new JsonArray();
for (NotificationChannel channel : notificationChannels) {
notificationsJson.add(gson.toJsonTree(channel));
}
backupContents.add("notification_channels", notificationsJson);
FileWriter writer = new FileWriter(filePath);
writer.write(backupContents.toString());
writer.close();
}
Aggregations