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()));
}
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()));
}
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;
}
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"));
}
}
}
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"));
}
}
Aggregations