获取政策标记

检索现有政策标记。

代码示例

Go

在试用此示例之前,请按照《Data Catalog 快速入门:使用客户端库》中的 Go 设置说明进行操作。如需了解详情,请参阅 Data Catalog Go API 参考文档

import (
	"context"
	"fmt"
	"io"

	datacatalog "cloud.google.com/go/datacatalog/apiv1beta1"
	datacatalogpb "google.golang.org/genproto/googleapis/cloud/datacatalog/v1beta1"
)

// getPolicyTag prints information about a given policy tag.
func getPolicyTag(w io.Writer, policyTagID string) error {
	// policyTagID := "projects/myproject/locations/us/taxonomies/1234/policyTags/5678"
	ctx := context.Background()
	policyClient, err := datacatalog.NewPolicyTagManagerClient(ctx)
	if err != nil {
		return fmt.Errorf("datacatalog.NewPolicyTagManagerClient: %v", err)
	}
	defer policyClient.Close()

	req := &datacatalogpb.GetPolicyTagRequest{
		Name: policyTagID,
	}
	resp, err := policyClient.GetPolicyTag(ctx, req)
	if err != nil {
		return fmt.Errorf("GetPolicyTag: %v", err)
	}
	fmt.Fprintf(w, "PolicyTag %s has Display Name %s", resp.Name, resp.DisplayName)
	if resp.ParentPolicyTag != "" {
		fmt.Fprintf(w, " and is a child of Policy Tag %s", resp.ParentPolicyTag)
	}
	if len(resp.ChildPolicyTags) > 0 {
		fmt.Fprintf(w, ", with %d child tags", len(resp.ChildPolicyTags))
	}
	fmt.Fprintln(w)
	return nil
}

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器