演示如何列出提醒政策。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
C#
static void ListAlertPolicies(string projectId)
{
var client = AlertPolicyServiceClient.Create();
var response = client.ListAlertPolicies(new ProjectName(projectId));
foreach (AlertPolicy policy in response)
{
Console.WriteLine(policy.Name);
if (policy.DisplayName != null)
{
Console.WriteLine(policy.DisplayName);
}
if (policy.Documentation?.Content != null)
{
Console.WriteLine(policy.Documentation.Content);
}
Console.WriteLine();
}
}
Go
// listAlertPolicies lists the alert policies in the project.
func listAlertPolicies(w io.Writer, projectID string) error {
ctx := context.Background()
client, err := monitoring.NewAlertPolicyClient(ctx)
if err != nil {
return err
}
defer client.Close()
req := &monitoringpb.ListAlertPoliciesRequest{
Name: "projects/" + projectID,
// Filter: "", // See https://cloud.google.com/monitoring/api/v3/sorting-and-filtering.
// OrderBy: "", // See https://cloud.google.com/monitoring/api/v3/sorting-and-filtering.
}
it := client.ListAlertPolicies(ctx, req)
for {
resp, err := it.Next()
if err == iterator.Done {
fmt.Fprintln(w, "Done")
break
}
if err != nil {
return err
}
fmt.Fprintf(w, " Name: %q\n", resp.GetName())
fmt.Fprintf(w, " Display Name: %q\n", resp.GetDisplayName())
fmt.Fprintf(w, " Documentation Content: %q\n\n", resp.GetDocumentation().GetContent())
}
return nil
}
Java
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());
}
}
}
}
Node.js
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.AlertPolicyServiceClient();
async function listPolicies() {
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
const listAlertPoliciesRequest = {
name: client.projectPath(projectId),
};
const [policies] = await client.listAlertPolicies(listAlertPoliciesRequest);
console.log('Policies:');
policies.forEach(policy => {
console.log(` Display name: ${policy.displayName}`);
if (policy.documentation && policy.documentation.content) {
console.log(` Documentation: ${policy.documentation.content}`);
}
});
}
listPolicies();
PHP
use Google\Cloud\Monitoring\V3\AlertPolicyServiceClient;
/**
* Adds a new column to the Albums table in the example database.
* Example:
* ```
* alert_list_policies($projectId);
* ```
*
* @param string $projectId Your project ID
*/
function alert_list_policies($projectId)
{
$alertClient = new AlertPolicyServiceClient([
'projectId' => $projectId,
]);
$policies = $alertClient->listAlertPolicies(
$alertClient->projectName($projectId)
);
foreach ($policies->iterateAllElements() as $policy) {
printf('Name: %s (%s)' . PHP_EOL, $policy->getDisplayName(), $policy->getName());
}
}
Python
def list_alert_policies(project_name):
"""List alert policies in a project.
Arguments:
project_name (str): The Google Cloud Project to use. The project name
must be in the format - 'projects/<PROJECT_NAME>'.
"""
client = monitoring_v3.AlertPolicyServiceClient()
policies = client.list_alert_policies(name=project_name)
print(
str(
tabulate.tabulate(
[(policy.name, policy.display_name) for policy in policies],
("name", "display_name"),
)
)
)
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。