Search in sources :

Example 21 with AlertPolicy

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;
}
Also used : AlertPolicy(com.google.monitoring.v3.AlertPolicy)

Example 22 with AlertPolicy

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);
}
Also used : NotificationChannel(com.google.monitoring.v3.NotificationChannel) AlertPolicy(com.google.monitoring.v3.AlertPolicy) BufferedReader(java.io.BufferedReader) JsonObject(com.google.gson.JsonObject) FileReader(java.io.FileReader)

Example 23 with AlertPolicy

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());
            }
        }
    }
}
Also used : AlertPolicy(com.google.monitoring.v3.AlertPolicy) AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient) ListAlertPoliciesPagedResponse(com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse)

Example 24 with AlertPolicy

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()));
    }
}
Also used : AlertPolicy(com.google.monitoring.v3.AlertPolicy) AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient)

Example 25 with AlertPolicy

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();
}
Also used : JsonArray(com.google.gson.JsonArray) NotificationChannel(com.google.monitoring.v3.NotificationChannel) JsonPrimitive(com.google.gson.JsonPrimitive) AlertPolicy(com.google.monitoring.v3.AlertPolicy) FileWriter(java.io.FileWriter) JsonObject(com.google.gson.JsonObject)

Aggregations

AlertPolicy (com.google.monitoring.v3.AlertPolicy)33 Test (org.junit.Test)16 AbstractMessage (com.google.protobuf.AbstractMessage)11 AlertPolicyServiceClient (com.google.cloud.monitoring.v3.AlertPolicyServiceClient)9 ListAlertPoliciesPagedResponse (com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse)8 ArrayList (java.util.ArrayList)7 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)5 ListAlertPoliciesRequest (com.google.monitoring.v3.ListAlertPoliciesRequest)5 NotificationChannel (com.google.monitoring.v3.NotificationChannel)5 StatusRuntimeException (io.grpc.StatusRuntimeException)5 JsonObject (com.google.gson.JsonObject)4 CreateAlertPolicyRequest (com.google.monitoring.v3.CreateAlertPolicyRequest)4 ListAlertPoliciesResponse (com.google.monitoring.v3.ListAlertPoliciesResponse)4 ProjectName (com.google.monitoring.v3.ProjectName)4 Aggregation (com.google.monitoring.v3.Aggregation)3 FolderName (com.google.monitoring.v3.FolderName)3 OrganizationName (com.google.monitoring.v3.OrganizationName)3 UpdateAlertPolicyRequest (com.google.monitoring.v3.UpdateAlertPolicyRequest)3 Duration (com.google.protobuf.Duration)3 JsonArray (com.google.gson.JsonArray)2