IAM-Richtlinie abrufen

IAM-Richtlinie abrufen.

Codebeispiel

Go

Lesen Sie die Anleitung zum Einrichten von Go in der Data Catalog-Kurzanleitung mit Clientbibliotheken, bevor Sie dieses Beispiel ausprobieren. Weitere Informationen finden Sie in der Referenzdokumentation zur Data Catalog Go API.

Richten Sie zum Authentifizieren bei Data Catalog die Standardanmeldedaten für Anwendungen ein. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

import (
	"context"
	"fmt"
	"io"
	"strings"

	datacatalog "cloud.google.com/go/datacatalog/apiv1beta1"
	iampb "google.golang.org/genproto/googleapis/iam/v1"
)

// getIAMPolicy prints information about the IAM policy associated with a
// given taxonomy or policy tag resource.
func getIAMPolicy(w io.Writer, resourceID string) error {
	// resourceID := "projects/myproject/locations/us/taxonomies/1234"
	ctx := context.Background()
	policyClient, err := datacatalog.NewPolicyTagManagerClient(ctx)
	if err != nil {
		return fmt.Errorf("datacatalog.NewPolicyTagManagerClient: %w", err)
	}
	defer policyClient.Close()

	req := &iampb.GetIamPolicyRequest{
		Resource: resourceID,
		Options: &iampb.GetPolicyOptions{
			RequestedPolicyVersion: 3,
		},
	}
	policy, err := policyClient.GetIamPolicy(ctx, req)
	if err != nil {
		return fmt.Errorf("GetIamPolicy: %w", err)
	}
	fmt.Fprintf(w, "Policy has version %d with Etag %x and %d bindings\n", policy.Version, policy.Etag, len(policy.Bindings))
	if len(policy.Bindings) > 0 {
		for _, binding := range policy.Bindings {
			fmt.Fprintf(w, "\trole %s with %d members: (%s)\n", binding.Role, len(binding.Members), strings.Join(binding.Members, ", "))
		}
	}
	return nil
}

Nächste Schritte

Informationen zum Suchen und Filtern von Codebeispielen für andere Google Cloud-Produkte finden Sie unter Google Cloud-Beispielbrowser.