IAM ポリシーの取得

IAM ポリシーを取得します。

コードサンプル

Go

このサンプルを試す前に、クライアント ライブラリを使用した Data Catalog のクイックスタートにある Go の設定手順を行ってください。 詳細については、Data Catalog Go API のリファレンス ドキュメントをご覧ください。

Data Catalog への認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。

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

	datacatalog "cloud.google.com/go/datacatalog/apiv1beta1"
	"cloud.google.com/go/iam/apiv1/iampb"
)

// 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
}

次のステップ

他の Google Cloud プロダクトに関連するコードサンプルを検索およびフィルタするには、Google Cloud サンプル ブラウザをご覧ください。