Create sample code for BatchGetEffectiveIamPolicies API.

Stay organized with collections Save and categorize content based on your preferences.

Create sample code for BatchGetEffectiveIamPolicies API as part of its launch to GA.

Code sample

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)

What's next

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