Search in sources :

Example 1 with ListAlertPoliciesPagedResponse

use of com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse in project java-monitoring by googleapis.

the class AlertPolicyServiceClientTest method listAlertPoliciesTest2.

@Test
public void listAlertPoliciesTest2() throws Exception {
    AlertPolicy responsesElement = AlertPolicy.newBuilder().build();
    ListAlertPoliciesResponse expectedResponse = ListAlertPoliciesResponse.newBuilder().setNextPageToken("").addAllAlertPolicies(Arrays.asList(responsesElement)).build();
    mockAlertPolicyService.addResponse(expectedResponse);
    OrganizationName name = OrganizationName.of("[ORGANIZATION]");
    ListAlertPoliciesPagedResponse pagedListResponse = client.listAlertPolicies(name);
    List<AlertPolicy> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getAlertPoliciesList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockAlertPolicyService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListAlertPoliciesRequest actualRequest = ((ListAlertPoliciesRequest) actualRequests.get(0));
    Assert.assertEquals(name.toString(), actualRequest.getName());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : ListAlertPoliciesResponse(com.google.monitoring.v3.ListAlertPoliciesResponse) OrganizationName(com.google.monitoring.v3.OrganizationName) AbstractMessage(com.google.protobuf.AbstractMessage) ListAlertPoliciesRequest(com.google.monitoring.v3.ListAlertPoliciesRequest) AlertPolicy(com.google.monitoring.v3.AlertPolicy) ListAlertPoliciesPagedResponse(com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse) Test(org.junit.Test)

Example 2 with ListAlertPoliciesPagedResponse

use of com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse in project java-monitoring by googleapis.

the class AlertPolicyServiceClientTest method listAlertPoliciesTest3.

@Test
public void listAlertPoliciesTest3() throws Exception {
    AlertPolicy responsesElement = AlertPolicy.newBuilder().build();
    ListAlertPoliciesResponse expectedResponse = ListAlertPoliciesResponse.newBuilder().setNextPageToken("").addAllAlertPolicies(Arrays.asList(responsesElement)).build();
    mockAlertPolicyService.addResponse(expectedResponse);
    ProjectName name = ProjectName.of("[PROJECT]");
    ListAlertPoliciesPagedResponse pagedListResponse = client.listAlertPolicies(name);
    List<AlertPolicy> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getAlertPoliciesList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockAlertPolicyService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListAlertPoliciesRequest actualRequest = ((ListAlertPoliciesRequest) actualRequests.get(0));
    Assert.assertEquals(name.toString(), actualRequest.getName());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : ListAlertPoliciesResponse(com.google.monitoring.v3.ListAlertPoliciesResponse) AbstractMessage(com.google.protobuf.AbstractMessage) ListAlertPoliciesRequest(com.google.monitoring.v3.ListAlertPoliciesRequest) ProjectName(com.google.monitoring.v3.ProjectName) AlertPolicy(com.google.monitoring.v3.AlertPolicy) ListAlertPoliciesPagedResponse(com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse) Test(org.junit.Test)

Example 3 with ListAlertPoliciesPagedResponse

use of com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse in project java-docs-samples by GoogleCloudPlatform.

the class AlertSample method getAlertPolicies.

private List<AlertPolicy> getAlertPolicies(String projectId) {
    List<AlertPolicy> alertPolicies = Lists.newArrayList();
    ListAlertPoliciesPagedResponse response = alertPolicyClient.listAlertPolicies(ProjectName.of(projectId));
    for (AlertPolicy policy : response.iterateAll()) {
        alertPolicies.add(policy);
    }
    return alertPolicies;
}
Also used : AlertPolicy(com.google.monitoring.v3.AlertPolicy) ListAlertPoliciesPagedResponse(com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse)

Example 4 with ListAlertPoliciesPagedResponse

use of com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse in project java-docs-samples by GoogleCloudPlatform.

the class AlertSample method enablePolicies.

// [END monitoring_alert_replace_channels]
// [START monitoring_alert_enable_policies]
// [START monitoring_alert_disable_policies]
private static void enablePolicies(String projectId, String filter, boolean enable) throws IOException {
    try (AlertPolicyServiceClient client = AlertPolicyServiceClient.create()) {
        ListAlertPoliciesPagedResponse response = client.listAlertPolicies(ListAlertPoliciesRequest.newBuilder().setName(ProjectName.of(projectId).toString()).setFilter(filter).build());
        for (AlertPolicy policy : response.iterateAll()) {
            if (policy.getEnabled().getValue() == enable) {
                System.out.println(String.format("Policy %s is already %b.", policy.getName(), enable ? "enabled" : "disabled"));
                continue;
            }
            AlertPolicy updatedPolicy = AlertPolicy.newBuilder().setName(policy.getName()).setEnabled(BoolValue.newBuilder().setValue(enable)).build();
            AlertPolicy result = client.updateAlertPolicy(FieldMask.newBuilder().addPaths("enabled").build(), updatedPolicy);
            System.out.println(String.format("%s %s", result.getDisplayName(), result.getEnabled().getValue() ? "enabled" : "disabled"));
        }
    }
}
Also used : AlertPolicy(com.google.monitoring.v3.AlertPolicy) AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient) ListAlertPoliciesPagedResponse(com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse)

Example 5 with ListAlertPoliciesPagedResponse

use of com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse in project java-docs-samples by GoogleCloudPlatform.

the class AlertSample method enablePolicies.

// [END monitoring_alert_replace_channels]
// [START monitoring_alert_enable_policies]
// [START monitoring_alert_disable_policies]
void enablePolicies(String projectId, String filter, boolean enable) {
    ListAlertPoliciesPagedResponse response = alertPolicyClient.listAlertPolicies(ListAlertPoliciesRequest.newBuilder().setName(projectId).setFilter(filter).build());
    for (AlertPolicy policy : response.iterateAll()) {
        if (policy.getEnabled().getValue() == enable) {
            outputStream.println(String.format("Policy %s is already %b.", policy.getName(), enable));
            continue;
        }
        AlertPolicy updatedPolicy = AlertPolicy.newBuilder().setName(AlertPolicyName.of(projectId, policy.getName()).toString()).setEnabled(BoolValue.newBuilder().setValue(enable)).build();
        AlertPolicy result = alertPolicyClient.updateAlertPolicy(FieldMask.newBuilder().addPaths("enabled").build(), updatedPolicy);
        outputStream.println(String.format("%s %s", result.getName(), result.getEnabled().getValue() ? "Enabled" : "Disabled"));
    }
}
Also used : AlertPolicy(com.google.monitoring.v3.AlertPolicy) ListAlertPoliciesPagedResponse(com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse)

Aggregations

ListAlertPoliciesPagedResponse (com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse)8 AlertPolicy (com.google.monitoring.v3.AlertPolicy)8 ListAlertPoliciesRequest (com.google.monitoring.v3.ListAlertPoliciesRequest)4 ListAlertPoliciesResponse (com.google.monitoring.v3.ListAlertPoliciesResponse)4 AbstractMessage (com.google.protobuf.AbstractMessage)4 Test (org.junit.Test)4 AlertPolicyServiceClient (com.google.cloud.monitoring.v3.AlertPolicyServiceClient)2 FolderName (com.google.monitoring.v3.FolderName)1 OrganizationName (com.google.monitoring.v3.OrganizationName)1 ProjectName (com.google.monitoring.v3.ProjectName)1