Criar tag de política

Crie uma nova tag de política.

Amostra de código

Go

Antes de testar essa amostra, siga as instruções de configuração para Go no Guia de início rápido do Data Catalog usando as bibliotecas de cliente. Para mais informações, consulte a documentação de referência da API Data Catalog Go.

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
}

Node.js

Antes de testar essa amostra, siga as instruções de configuração para Node.js no Guia de início rápido do Data Catalog usando as bibliotecas de cliente. Para mais informações, consulte a documentação de referência da API do Data Catalog para Node.js.

// Create a policy tag resource under a given parent taxonomy.

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

async function createPolicyTag() {
  /**
   * TODO(developer): Uncomment the following lines before running the sample.
   */
  // const projectId = 'my_project'; // Google Cloud Platform project
  // const location = 'us';
  // const taxonomy = 'my_existing_taxonomy';
  // const parent = `projects/${projectId}/locations/${location}/taxonomies/${taxonomy}`;

  const request = {
    parent,
    policyTag: {
      displayName: 'nodejs_samples_tag',
      //   // It optionally accepts a parent ID, which can be used to create a hierarchical
      //   // relationship between tags.
      //   parentPolicyTag: `projects/${projectId}/locations/${location}/taxonomies/${taxonomy}/policyTags/my_existing_policy_tag`
    },
  };

  try {
    const [metadata] = await policyClient.createPolicyTag(request);
    console.log(`Created policy tag: ${metadata.name}`);
  } catch (e) {
    console.error(e);
    process.exitCode = 1;
  }
}

Próximas etapas

Para pesquisar e filtrar amostras de código de outros produtos do Google Cloud, consulte a amostra de navegador do Google Cloud.