/** * * * <pre> * Required if the policy exists. The resource name for this policy. The * syntax is: * projects/[PROJECT_ID]/alertPolicies/[ALERT_POLICY_ID] * `[ALERT_POLICY_ID]` is assigned by Stackdriver Monitoring when the policy * is created. When calling the * [alertPolicies.create][google.monitoring.v3.AlertPolicyService.CreateAlertPolicy] * method, do not include the `name` field in the alerting policy passed as * part of the request. * </pre> * * <code>string name = 1;</code> */ public Builder clearName() { name_ = getDefaultInstance().getName(); onChanged(); return this; } /**
public Builder mergeFrom(com.google.monitoring.v3.AlertPolicy other) { if (other == com.google.monitoring.v3.AlertPolicy.getDefaultInstance()) return this; if (!other.getName().isEmpty()) { name_ = other.name_; onChanged();
hash = (19 * hash) + getDescriptor().hashCode(); hash = (37 * hash) + NAME_FIELD_NUMBER; hash = (53 * hash) + getName().hashCode(); hash = (37 * hash) + DISPLAY_NAME_FIELD_NUMBER; hash = (53 * hash) + getDisplayName().hashCode();
private static void listAlertPolicies(String projectId) throws IOException { try (AlertPolicyServiceClient client = AlertPolicyServiceClient.create()) { ListAlertPoliciesPagedResponse response = client.listAlertPolicies(ProjectName.of(projectId)); System.out.println("Alert Policies:"); for (AlertPolicy policy : response.iterateAll()) { System.out.println( String.format("\nPolicy %s\nalert-id: %s", policy.getDisplayName(), policy.getName())); int channels = policy.getNotificationChannelsCount(); if (channels > 0) { System.out.println("notification-channels:"); for (int i = 0; i < channels; i++) { System.out.println("\t" + policy.getNotificationChannels(i)); } } if (policy.hasDocumentation() && policy.getDocumentation().getContent() != null) { System.out.println(policy.getDocumentation().getContent()); } } } } // [END monitoring_alert_list_policies]
private static void restoreRevisedPolicies( String projectId, boolean isSameProject, List<AlertPolicy> policies) throws IOException { try (AlertPolicyServiceClient client = AlertPolicyServiceClient.create()) { for (AlertPolicy policy : policies) { if (!isSameProject) { policy = client.createAlertPolicy(ProjectName.of(projectId), policy); } else { try { client.updateAlertPolicy(null, policy); } catch (Exception e) { policy = client.createAlertPolicy( ProjectName.of(projectId), policy.toBuilder().clearName().build()); } } System.out.println(String.format("Restored %s", policy.getName())); } } } // [END monitoring_alert_create_policy]
private static void replaceChannels(String projectId, String alertPolicyId, String[] channelIds) throws IOException { AlertPolicy.Builder policyBuilder = AlertPolicy .newBuilder() .setName(AlertPolicyName.of(projectId, alertPolicyId).toString()); for (String channelId : channelIds) { policyBuilder.addNotificationChannels( NotificationChannelName.of(projectId, channelId).toString()); } try (AlertPolicyServiceClient client = AlertPolicyServiceClient.create()) { AlertPolicy result = client.updateAlertPolicy( FieldMask.newBuilder().addPaths("notification_channels").build(), policyBuilder.build()); System.out.println(String.format("Updated %s", result.getName())); } } // [END monitoring_alert_replace_channels]
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")); } } } // [END monitoring_alert_enable_policies]