use of com.google.monitoring.v3.AlertPolicyName in project java-monitoring by googleapis.
the class AlertPolicyServiceClientTest method deleteAlertPolicyExceptionTest.
@Test
public void deleteAlertPolicyExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
mockAlertPolicyService.addException(exception);
try {
AlertPolicyName name = AlertPolicyName.ofProjectAlertPolicyName("[PROJECT]", "[ALERT_POLICY]");
client.deleteAlertPolicy(name);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception.
}
}
use of com.google.monitoring.v3.AlertPolicyName in project java-monitoring by googleapis.
the class AlertPolicyServiceClientTest method getAlertPolicyExceptionTest.
@Test
public void getAlertPolicyExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
mockAlertPolicyService.addException(exception);
try {
AlertPolicyName name = AlertPolicyName.ofProjectAlertPolicyName("[PROJECT]", "[ALERT_POLICY]");
client.getAlertPolicy(name);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception.
}
}
use of com.google.monitoring.v3.AlertPolicyName in project java-monitoring by googleapis.
the class AlertPolicyServiceClient method deleteAlertPolicy.
// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
* Deletes an alerting policy.
*
* <p>Sample code:
*
* <pre>{@code
* try (AlertPolicyServiceClient alertPolicyServiceClient = AlertPolicyServiceClient.create()) {
* AlertPolicyName name =
* AlertPolicyName.ofProjectAlertPolicyName("[PROJECT]", "[ALERT_POLICY]");
* alertPolicyServiceClient.deleteAlertPolicy(name);
* }
* }</pre>
*
* @param name Required. The alerting policy to delete. The format is:
* <p>projects/[PROJECT_ID_OR_NUMBER]/alertPolicies/[ALERT_POLICY_ID]
* <p>For more information, see [AlertPolicy][google.monitoring.v3.AlertPolicy].
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
*/
public final void deleteAlertPolicy(AlertPolicyName name) {
DeleteAlertPolicyRequest request = DeleteAlertPolicyRequest.newBuilder().setName(name == null ? null : name.toString()).build();
deleteAlertPolicy(request);
}
use of com.google.monitoring.v3.AlertPolicyName in project java-monitoring by googleapis.
the class UpdateAlertPolicy method updateAlertPolicy.
public static void updateAlertPolicy(String alertPolicyName, String newPolicyName) throws ApiException, IOException {
// once, and can be reused for multiple requests.
try (AlertPolicyServiceClient alertPolicyServiceClient = AlertPolicyServiceClient.create()) {
String metricType = "compute.googleapis.com/instance/cpu/utilization";
String resourceType = "gce_instance";
// A Filter that identifies which time series should be compared with the threshold
String metricFilter = "metric.type=" + '"' + metricType + '"' + " AND " + "resource.type=" + '"' + resourceType + '"';
// Build Duration
Duration aggregationDuration = Duration.newBuilder().setSeconds(60).build();
// Build Aggregation
Aggregation aggregation = Aggregation.newBuilder().setAlignmentPeriod(aggregationDuration).setCrossSeriesReducer(Aggregation.Reducer.REDUCE_MEAN).setPerSeriesAligner(Aggregation.Aligner.ALIGN_MAX).build();
// Build MetricThreshold
AlertPolicy.Condition.MetricThreshold metricThreshold = AlertPolicy.Condition.MetricThreshold.newBuilder().setComparison(ComparisonType.COMPARISON_GT).addAggregations(aggregation).setFilter(metricFilter).setDuration(aggregationDuration).build();
// build Alert policy condition
AlertPolicy.Condition alertPolicyCondition = AlertPolicy.Condition.newBuilder().setDisplayName(alertPolicyName).setConditionThreshold(metricThreshold).build();
// create alert policy for update
AlertPolicy alertPolicy = AlertPolicy.newBuilder().setName(alertPolicyName).setDisplayName(newPolicyName).addConditions(alertPolicyCondition).setCombiner(AlertPolicy.ConditionCombinerType.AND).build();
// create update alert policy request
UpdateAlertPolicyRequest updateAlertPolicyRequest = UpdateAlertPolicyRequest.newBuilder().setAlertPolicy(alertPolicy).build();
// process update policy request
AlertPolicy updatedAlertPolicy = alertPolicyServiceClient.updateAlertPolicy(updateAlertPolicyRequest);
System.out.print("alert policy updated successfully:" + updatedAlertPolicy.getName());
}
}
use of com.google.monitoring.v3.AlertPolicyName 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()));
}
Aggregations