Create policy tag

Create a new policy tag.

Code sample

Go

Before trying this sample, follow the Go setup instructions in the Data Catalog quickstart using client libraries. For more information, see the Data Catalog Go API reference documentation.

import (
	"context"
	"fmt"
	"io"

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

// createPolicyTag creates a policy tag resource under a given parent taxonomy.
//
// It optionally accepts a parent ID, which can be used to create a hierarchical
// relationship between tags.
func createPolicyTag(w io.Writer, parent, displayName, parentPolicyTag string) (string, error) {
	// parent := "projects/myproject/locations/us/taxonomies/1234"
	// displayName := "Example Policy Tag"
	// parentPolicyTag := "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.CreatePolicyTagRequest{
		Parent: parent,
		PolicyTag: &datacatalogpb.PolicyTag{
			DisplayName: displayName,
			Description: "Example description for the policy tag",
		},
	}
	if parentPolicyTag != "" {
		req.PolicyTag.ParentPolicyTag = parentPolicyTag
	}
	resp, err := policyClient.CreatePolicyTag(ctx, req)
	if err != nil {
		return "", fmt.Errorf("CreatePolicyTag: %v", err)
	}

	fmt.Fprintf(w, "PolicyTag %s was created.\n", resp.Name)
	return resp.Name, nil
}

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.