Search in sources :

Example 11 with NotificationChannel

use of com.google.monitoring.v3.NotificationChannel in project java-docs-samples by GoogleCloudPlatform.

the class AlertSample method restorePolicies.

// [END monitoring_alert_backup_policies]
// [START monitoring_alert_restore_policies]
void restorePolicies(String projectId, String filePath) throws IOException {
    FileReader reader = new FileReader(filePath);
    BufferedReader bufferedReader = new BufferedReader(reader);
    JsonObject backupContent = getPolicyJsonContents(filePath, bufferedReader, gson);
    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 12 with NotificationChannel

use of com.google.monitoring.v3.NotificationChannel in project java-docs-samples by GoogleCloudPlatform.

the class AlertSample method restoreNotificationChannels.

// [START monitoring_alert_create_channel]
// [START monitoring_alert_update_channel]
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;
}
Also used : NotificationChannel(com.google.monitoring.v3.NotificationChannel) NotificationChannelServiceClient(com.google.cloud.monitoring.v3.NotificationChannelServiceClient) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException)

Example 13 with NotificationChannel

use of com.google.monitoring.v3.NotificationChannel in project java-docs-samples by GoogleCloudPlatform.

the class DeleteNotificationChannelIT method setupClass.

@BeforeClass
public static void setupClass() throws IOException {
    try (NotificationChannelServiceClient client = NotificationChannelServiceClient.create()) {
        String projectId = getProjectId();
        NOTIFICATION_CHANNEL = NotificationChannel.newBuilder().setType("email").putLabels("email_address", "java-docs-samples-testing@google.com").build();
        NotificationChannel channel = client.createNotificationChannel(ProjectName.of(projectId), NOTIFICATION_CHANNEL);
        NOTIFICATION_CHANNEL_NAME = channel.getName();
    }
}
Also used : NotificationChannel(com.google.monitoring.v3.NotificationChannel) NotificationChannelServiceClient(com.google.cloud.monitoring.v3.NotificationChannelServiceClient) BeforeClass(org.junit.BeforeClass)

Example 14 with NotificationChannel

use of com.google.monitoring.v3.NotificationChannel in project java-security-private-ca by googleapis.

the class MonitorCertificateAuthority method createCaMonitoringPolicy.

// Creates a monitoring policy that notifies you 30 days before a managed CA expires.
public static void createCaMonitoringPolicy(String project) throws IOException {
    /* Initialize client that will be used to send requests. This client only needs to be created
    once, and can be reused for multiple requests. After completing all of your requests, call
    the `client.close()` method on the client to safely
    clean up any remaining background resources. */
    try (AlertPolicyServiceClient client = AlertPolicyServiceClient.create();
        NotificationChannelServiceClient notificationClient = NotificationChannelServiceClient.create()) {
        String policyName = "policy-name";
        /* Query which indicates the resource to monitor and the constraints.
      Here, the alert policy notifies you 30 days before a managed CA expires.
      For more info on creating queries, see: https://cloud.google.com/monitoring/mql/alerts */
        String query = "fetch privateca.googleapis.com/CertificateAuthority" + "| metric 'privateca.googleapis.com/ca/cert_chain_expiration'" + "| group_by 5m," + "[value_cert_chain_expiration_mean: mean(value.cert_chain_expiration)]" + "| every 5m" + "| condition val() < 2.592e+06 's'";
        // Create a notification channel.
        NotificationChannel notificationChannel = NotificationChannel.newBuilder().setType("email").putLabels("email_address", "java-docs-samples-testing@google.com").build();
        NotificationChannel channel = notificationClient.createNotificationChannel(ProjectName.of(project), notificationChannel);
        // Set the query and notification channel.
        AlertPolicy alertPolicy = AlertPolicy.newBuilder().setDisplayName(policyName).addConditions(Condition.newBuilder().setDisplayName("ca-cert-chain-expiration").setConditionMonitoringQueryLanguage(MonitoringQueryLanguageCondition.newBuilder().setQuery(query).build()).build()).setCombiner(ConditionCombinerType.AND).addNotificationChannels(channel.getName()).build();
        AlertPolicy policy = client.createAlertPolicy(ProjectName.of(project), alertPolicy);
        System.out.println("Monitoring policy successfully created !" + policy.getName());
    }
}
Also used : NotificationChannel(com.google.monitoring.v3.NotificationChannel) AlertPolicy(com.google.monitoring.v3.AlertPolicy) AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient) NotificationChannelServiceClient(com.google.cloud.monitoring.v3.NotificationChannelServiceClient)

Example 15 with NotificationChannel

use of com.google.monitoring.v3.NotificationChannel in project java-docs-samples by GoogleCloudPlatform.

the class AlertSample method getNotificationChannels.

private List<NotificationChannel> getNotificationChannels(String projectId) {
    List<NotificationChannel> notificationChannels = Lists.newArrayList();
    ListNotificationChannelsPagedResponse listNotificationChannelsResponse = notificationChannelClient.listNotificationChannels(ProjectName.of(projectId));
    for (NotificationChannel channel : listNotificationChannelsResponse.iterateAll()) {
        notificationChannels.add(channel);
    }
    return notificationChannels;
}
Also used : NotificationChannel(com.google.monitoring.v3.NotificationChannel) ListNotificationChannelsPagedResponse(com.google.cloud.monitoring.v3.NotificationChannelServiceClient.ListNotificationChannelsPagedResponse)

Aggregations

NotificationChannel (com.google.monitoring.v3.NotificationChannel)28 Test (org.junit.Test)18 AbstractMessage (com.google.protobuf.AbstractMessage)13 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)5 ListNotificationChannelsPagedResponse (com.google.cloud.monitoring.v3.NotificationChannelServiceClient.ListNotificationChannelsPagedResponse)5 AlertPolicy (com.google.monitoring.v3.AlertPolicy)5 StatusRuntimeException (io.grpc.StatusRuntimeException)5 NotificationChannelServiceClient (com.google.cloud.monitoring.v3.NotificationChannelServiceClient)4 JsonObject (com.google.gson.JsonObject)4 CreateNotificationChannelRequest (com.google.monitoring.v3.CreateNotificationChannelRequest)4 ListNotificationChannelsRequest (com.google.monitoring.v3.ListNotificationChannelsRequest)4 ListNotificationChannelsResponse (com.google.monitoring.v3.ListNotificationChannelsResponse)4 FolderName (com.google.monitoring.v3.FolderName)3 OrganizationName (com.google.monitoring.v3.OrganizationName)3 ProjectName (com.google.monitoring.v3.ProjectName)3 JsonArray (com.google.gson.JsonArray)2 JsonPrimitive (com.google.gson.JsonPrimitive)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)2