Search in sources :

Example 6 with NotificationChannel

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

the class NotificationChannelServiceClientTest method createNotificationChannelExceptionTest2.

@Test
public void createNotificationChannelExceptionTest2() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockNotificationChannelService.addException(exception);
    try {
        OrganizationName name = OrganizationName.of("[ORGANIZATION]");
        NotificationChannel notificationChannel = NotificationChannel.newBuilder().build();
        client.createNotificationChannel(name, notificationChannel);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : OrganizationName(com.google.monitoring.v3.OrganizationName) NotificationChannel(com.google.monitoring.v3.NotificationChannel) InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Example 7 with NotificationChannel

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

the class NotificationChannelServiceClientTest method createNotificationChannelTest3.

@Test
public void createNotificationChannelTest3() throws Exception {
    NotificationChannel expectedResponse = NotificationChannel.newBuilder().setType("type3575610").setName(NotificationChannelName.ofProjectNotificationChannelName("[PROJECT]", "[NOTIFICATION_CHANNEL]").toString()).setDisplayName("displayName1714148973").setDescription("description-1724546052").putAllLabels(new HashMap<String, String>()).putAllUserLabels(new HashMap<String, String>()).setEnabled(BoolValue.newBuilder().build()).setCreationRecord(MutationRecord.newBuilder().build()).addAllMutationRecords(new ArrayList<MutationRecord>()).build();
    mockNotificationChannelService.addResponse(expectedResponse);
    ProjectName name = ProjectName.of("[PROJECT]");
    NotificationChannel notificationChannel = NotificationChannel.newBuilder().build();
    NotificationChannel actualResponse = client.createNotificationChannel(name, notificationChannel);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockNotificationChannelService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    CreateNotificationChannelRequest actualRequest = ((CreateNotificationChannelRequest) actualRequests.get(0));
    Assert.assertEquals(name.toString(), actualRequest.getName());
    Assert.assertEquals(notificationChannel, actualRequest.getNotificationChannel());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : NotificationChannel(com.google.monitoring.v3.NotificationChannel) AbstractMessage(com.google.protobuf.AbstractMessage) HashMap(java.util.HashMap) ProjectName(com.google.monitoring.v3.ProjectName) ArrayList(java.util.ArrayList) CreateNotificationChannelRequest(com.google.monitoring.v3.CreateNotificationChannelRequest) Test(org.junit.Test)

Example 8 with NotificationChannel

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

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 9 with NotificationChannel

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

the class AlertSample method restoreNotificationChannels.

private Map<String, String> restoreNotificationChannels(String projectId, List<NotificationChannel> channels, boolean isSameProject) {
    Map<String, String> newChannelNames = Maps.newHashMap();
    for (NotificationChannel channel : channels) {
        // Update channel name if project ID is different.
        boolean channelUpdated = false;
        if (isSameProject) {
            try {
                NotificationChannel updatedChannel = notificationChannelClient.updateNotificationChannel(NOTIFICATION_CHANNEL_UPDATE_MASK, channel);
                newChannelNames.put(channel.getName(), updatedChannel.getName());
                channelUpdated = true;
            } catch (Exception e) {
                channelUpdated = false;
            }
        }
        if (!channelUpdated) {
            NotificationChannel newChannel = notificationChannelClient.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) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException)

Example 10 with NotificationChannel

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

the class AlertSample method writePoliciesBackupFile.

private 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

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