Search in sources :

Example 31 with AlertPolicy

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

the class AlertPolicyServiceClientTest method getAlertPolicyTest.

@Test
public void getAlertPolicyTest() throws Exception {
    AlertPolicy expectedResponse = AlertPolicy.newBuilder().setName(AlertPolicyName.ofProjectAlertPolicyName("[PROJECT]", "[ALERT_POLICY]").toString()).setDisplayName("displayName1714148973").setDocumentation(AlertPolicy.Documentation.newBuilder().build()).putAllUserLabels(new HashMap<String, String>()).addAllConditions(new ArrayList<AlertPolicy.Condition>()).setEnabled(BoolValue.newBuilder().build()).setValidity(Status.newBuilder().build()).addAllNotificationChannels(new ArrayList<String>()).setCreationRecord(MutationRecord.newBuilder().build()).setMutationRecord(MutationRecord.newBuilder().build()).setAlertStrategy(AlertPolicy.AlertStrategy.newBuilder().build()).build();
    mockAlertPolicyService.addResponse(expectedResponse);
    AlertPolicyName name = AlertPolicyName.ofProjectAlertPolicyName("[PROJECT]", "[ALERT_POLICY]");
    AlertPolicy actualResponse = client.getAlertPolicy(name);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockAlertPolicyService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    GetAlertPolicyRequest actualRequest = ((GetAlertPolicyRequest) actualRequests.get(0));
    Assert.assertEquals(name.toString(), actualRequest.getName());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) AlertPolicy(com.google.monitoring.v3.AlertPolicy) AlertPolicyName(com.google.monitoring.v3.AlertPolicyName) ArrayList(java.util.ArrayList) GetAlertPolicyRequest(com.google.monitoring.v3.GetAlertPolicyRequest) Test(org.junit.Test)

Example 32 with AlertPolicy

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

the class AlertPolicyServiceClientTest method createAlertPolicyTest2.

@Test
public void createAlertPolicyTest2() throws Exception {
    AlertPolicy expectedResponse = AlertPolicy.newBuilder().setName(AlertPolicyName.ofProjectAlertPolicyName("[PROJECT]", "[ALERT_POLICY]").toString()).setDisplayName("displayName1714148973").setDocumentation(AlertPolicy.Documentation.newBuilder().build()).putAllUserLabels(new HashMap<String, String>()).addAllConditions(new ArrayList<AlertPolicy.Condition>()).setEnabled(BoolValue.newBuilder().build()).setValidity(Status.newBuilder().build()).addAllNotificationChannels(new ArrayList<String>()).setCreationRecord(MutationRecord.newBuilder().build()).setMutationRecord(MutationRecord.newBuilder().build()).setAlertStrategy(AlertPolicy.AlertStrategy.newBuilder().build()).build();
    mockAlertPolicyService.addResponse(expectedResponse);
    OrganizationName name = OrganizationName.of("[ORGANIZATION]");
    AlertPolicy alertPolicy = AlertPolicy.newBuilder().build();
    AlertPolicy actualResponse = client.createAlertPolicy(name, alertPolicy);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockAlertPolicyService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    CreateAlertPolicyRequest actualRequest = ((CreateAlertPolicyRequest) actualRequests.get(0));
    Assert.assertEquals(name.toString(), actualRequest.getName());
    Assert.assertEquals(alertPolicy, actualRequest.getAlertPolicy());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : OrganizationName(com.google.monitoring.v3.OrganizationName) AbstractMessage(com.google.protobuf.AbstractMessage) CreateAlertPolicyRequest(com.google.monitoring.v3.CreateAlertPolicyRequest) AlertPolicy(com.google.monitoring.v3.AlertPolicy) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 33 with AlertPolicy

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

the class AlertPolicyServiceClientTest method createAlertPolicyExceptionTest2.

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

Example 34 with AlertPolicy

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

the class GetAlertPolicy method getAlertPolicy.

public static void getAlertPolicy(String alertPolicyName) throws ApiException, IOException {
    // once, and can be reused for multiple requests.
    try (AlertPolicyServiceClient alertPolicyServiceClient = AlertPolicyServiceClient.create()) {
        // Gets a single alerting policy
        AlertPolicy alertPolicy = alertPolicyServiceClient.getAlertPolicy(alertPolicyName);
        System.out.format("alert policy retrieved successfully:%s", alertPolicy.getName());
    }
}
Also used : AlertPolicy(com.google.monitoring.v3.AlertPolicy) AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient)

Example 35 with AlertPolicy

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

the class ListAlertPolicy method listAlertPolicy.

public static void listAlertPolicy(String projectId) throws ApiException, IOException {
    // once, and can be reused for multiple requests.
    try (AlertPolicyServiceClient alertPolicyServiceClient = AlertPolicyServiceClient.create()) {
        // Lists the existing alerting policies for the project.
        ListAlertPoliciesRequest listAlertPoliciesRequest = ListAlertPoliciesRequest.newBuilder().setName(ProjectName.of(projectId).toString()).build();
        // process response
        AlertPolicyServiceClient.ListAlertPoliciesPagedResponse response = alertPolicyServiceClient.listAlertPolicies(listAlertPoliciesRequest);
        // List all the policy.
        response.iterateAll().forEach(alertPolicy -> System.out.format("success! alert policy %s is policyId %s%n", alertPolicy.getDisplayName(), alertPolicy.getName()));
    }
}
Also used : ListAlertPoliciesRequest(com.google.monitoring.v3.ListAlertPoliciesRequest) AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient)

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