BatchGetEffectiveIamPolicies API のサンプルコードを作成する

コレクションでコンテンツを整理 必要に応じて、コンテンツの保存と分類を行います。

一般提供開始に伴い、BatchGetEffectiveIamPolicies API のサンプルコードを作成します。

コードサンプル

Go


// Sample asset-quickstart batch get effective iam policies.
package main

import (
	"context"
	"flag"
	"fmt"
	"log"

	asset "cloud.google.com/go/asset/apiv1"
	"cloud.google.com/go/asset/apiv1/assetpb"
)

func main() {
	projectId := flag.String("projectId", "", "Project Id to construct the scope under which the effective IAM policies to get.")
	fullResourceName := flag.String("fullResourceName", "", "Resource on which the IAM policies are effective.")

	flag.Parse()
	ctx := context.Background()
	client, err := asset.NewClient(ctx)
	if err != nil {
		log.Fatalf("asset.NewClient: %v", err)
	}
	defer client.Close()

	req := &assetpb.BatchGetEffectiveIamPoliciesRequest{
		Scope: fmt.Sprintf("projects/%s", *projectId),
		Names: []string{*fullResourceName},
	}

	op, err := client.BatchGetEffectiveIamPolicies(ctx, req)
	if err != nil {
		log.Fatal(err)
	}
	for index, result := range op.PolicyResults {
		fmt.Println(index, result)
	}
}

Node.js

const {AssetServiceClient} = require('@google-cloud/asset');

const client = new AssetServiceClient();

async function batchGetEffectiveIamPolicies() {
  const projectId = await client.getProjectId();
  const request = {
    scope: `projects/${projectId}`,
    names: assetNames.split(','),
  };

  // Handle the operation using the promise pattern.
  const result = await client.batchGetEffectiveIamPolicies(request);
  // Handle the response.
  console.dir(result, {depth: null});
}

Python

from google.cloud import asset_v1

# TODO scope = 'project ID/number, folder number or org number'
# TODO resource_names = 'List of resource names'

client = asset_v1.AssetServiceClient()

response = client.batch_get_effective_iam_policies(
    request={"scope": scope, "names": resource_names}
)
print(response)

次のステップ

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