List taxonomies

List existing taxonomies.

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.

To authenticate to Data Catalog, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import (
	"context"
	"fmt"
	"io"

	datacatalog "cloud.google.com/go/datacatalog/apiv1beta1"
	"cloud.google.com/go/datacatalog/apiv1beta1/datacatalogpb"
	"google.golang.org/api/iterator"
)

// listTaxonomies prints information about the taxonomies contained within a specific
// project and location.
func listTaxonomies(w io.Writer, projectID, location string) error {
	// projectID := "my-project-id"
	// location := "us"
	ctx := context.Background()
	policyClient, err := datacatalog.NewPolicyTagManagerClient(ctx)
	if err != nil {
		return fmt.Errorf("datacatalog.NewPolicyTagManagerClient: %w", err)
	}
	defer policyClient.Close()

	req := &datacatalogpb.ListTaxonomiesRequest{
		Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
	}
	it := policyClient.ListTaxonomies(ctx, req)
	fmt.Fprintf(w, "listing taxonomies in project %s and location %s\n", projectID, location)
	for {
		resp, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("ListTaxonomies iteration error: %w", err)
		}

		fmt.Fprintf(w, "\t- %s: %s\n", resp.Name, resp.DisplayName)
	}
	return nil
}

Node.js

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

To authenticate to Data Catalog, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

// Import the Google Cloud client library.
const {DataCatalogClient, PolicyTagManagerClient} =
  require('@google-cloud/datacatalog').v1;
const dataCatalog = new DataCatalogClient();
const policyTagManager = new PolicyTagManagerClient();

async function listTaxonomies() {
  /**
   * TODO(developer): Uncomment the following lines before running the sample.
   */
  // const projectId = 'my_project'; // Google Cloud Platform project
  // const location = 'us';

  // Parent project location format is `projects/${projectId}/locations/${location}`
  const parent = dataCatalog.locationPath(projectId, location);

  const request = {
    parent: parent,
  };

  try {
    const [taxonomies] = await policyTagManager.listTaxonomies(request);
    console.log('Taxonomies:');
    taxonomies.forEach(taxonomy => {
      console.log(taxonomy.name);
    });
  } catch (e) {
    console.error(e);
    process.exitCode = 1;
  }
}

What's next

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