Search in sources :

Example 1 with AlertPolicyName

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.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) AlertPolicyName(com.google.monitoring.v3.AlertPolicyName) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Example 2 with AlertPolicyName

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.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) AlertPolicyName(com.google.monitoring.v3.AlertPolicyName) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Example 3 with AlertPolicyName

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);
}
Also used : DeleteAlertPolicyRequest(com.google.monitoring.v3.DeleteAlertPolicyRequest)

Example 4 with AlertPolicyName

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());
    }
}
Also used : Aggregation(com.google.monitoring.v3.Aggregation) AlertPolicy(com.google.monitoring.v3.AlertPolicy) UpdateAlertPolicyRequest(com.google.monitoring.v3.UpdateAlertPolicyRequest) AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient) Duration(com.google.protobuf.Duration)

Example 5 with AlertPolicyName

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

Aggregations

AlertPolicy (com.google.monitoring.v3.AlertPolicy)4 AlertPolicyName (com.google.monitoring.v3.AlertPolicyName)4 Test (org.junit.Test)4 AlertPolicyServiceClient (com.google.cloud.monitoring.v3.AlertPolicyServiceClient)3 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)2 Aggregation (com.google.monitoring.v3.Aggregation)2 DeleteAlertPolicyRequest (com.google.monitoring.v3.DeleteAlertPolicyRequest)2 AbstractMessage (com.google.protobuf.AbstractMessage)2 Duration (com.google.protobuf.Duration)2 StatusRuntimeException (io.grpc.StatusRuntimeException)2 GetAlertPolicyRequest (com.google.monitoring.v3.GetAlertPolicyRequest)1 ProjectName (com.google.monitoring.v3.ProjectName)1 UpdateAlertPolicyRequest (com.google.monitoring.v3.UpdateAlertPolicyRequest)1 Empty (com.google.protobuf.Empty)1 ArrayList (java.util.ArrayList)1