Create taxonomy

Create a new taxonomy.

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"
)

// createTaxonomy creates a taxonomy resource, which holds the collection of policy tags.
//
// This example marks the taxonomy as valid for defining fine grained access control, also
// known as column-level access control when used in conjunction with BigQuery.
func createTaxonomy(w io.Writer, projectID, location, displayName string) (string, error) {
	// projectID := "my-project-id"
	// location := "us"
	// displayName := "example-taxonomy"
	ctx := context.Background()
	policyClient, err := datacatalog.NewPolicyTagManagerClient(ctx)
	if err != nil {
		return "", fmt.Errorf("datacatalog.NewPolicyTagManagerClient: %v", err)
	}
	defer policyClient.Close()

	req := &datacatalogpb.CreateTaxonomyRequest{
		Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
		Taxonomy: &datacatalogpb.Taxonomy{
			DisplayName: displayName,
			Description: "Taxonomy created via basic snippet testing",
			ActivatedPolicyTypes: []datacatalogpb.Taxonomy_PolicyType{
				datacatalogpb.Taxonomy_FINE_GRAINED_ACCESS_CONTROL,
			},
		},
	}
	resp, err := policyClient.CreateTaxonomy(ctx, req)
	if err != nil {
		return "", fmt.Errorf("CreateTaxonomy: %v", err)
	}

	fmt.Fprintf(w, "Taxonomy %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.