Kueri izin yang dapat diuji
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Menunjukkan pencantuman izin yang valid untuk resource.
Jelajahi lebih lanjut
Untuk dokumentasi mendetail yang menyertakan contoh kode ini, lihat referensi berikut:
Contoh kode
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page provides code samples demonstrating how to list valid permissions for a given resource using the IAM API across multiple programming languages, including C++, C#, Go, Java, and Python.\u003c/p\u003e\n"],["\u003cp\u003eThe code examples utilize the \u003ccode\u003eQueryTestablePermissions\u003c/code\u003e method to retrieve a list of permissions applicable to a specified resource, which can be used for managing access control.\u003c/p\u003e\n"],["\u003cp\u003eEach code sample directs users to the IAM client libraries and API reference documentation for further details on installation, usage, and specific API functions.\u003c/p\u003e\n"],["\u003cp\u003eThe examples emphasize the need for setting up Application Default Credentials (ADC) to authenticate with the IAM API, with links to instructions on how to achieve this in a local development environment.\u003c/p\u003e\n"]]],[],null,["# Query testable permissions\n\nDemonstrates listing the permissions that are valid for a resource.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Create and manage custom roles](/iam/docs/creating-custom-roles)\n\nCode sample\n-----------\n\n### C++\n\n\nTo learn how to install and use the client library for IAM, see\n[IAM client libraries](/iam/docs/reference/libraries).\n\n\nFor more information, see the\n[IAM C++ API\nreference documentation](/cpp/docs/reference/iam/latest).\n\n\nTo authenticate to IAM, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n namespace iam = ::google::cloud::iam_admin_v1;\n [](std::string const& resource) {\n iam::IAMClient client(iam::MakeIAMConnection());\n google::iam::admin::v1::QueryTestablePermissionsRequest request;\n request.set_full_resource_name(resource);\n int count = 0;\n for (auto& permission : client.QueryTestablePermissions(request)) {\n if (!permission) throw std::move(permission).status();\n std::cout \u003c\u003c \"Permission successfully retrieved: \" \u003c\u003c permission-\u003ename()\n \u003c\u003c \"\\n\";\n ++count;\n }\n if (count == 0) {\n std::cout \u003c\u003c \"No testable permissions found in resource: \" \u003c\u003c resource\n \u003c\u003c \"\\n\";\n }\n }\n\n### C#\n\n\nTo learn how to install and use the client library for IAM, see\n[IAM client libraries](/iam/docs/reference/libraries).\n\n\nFor more information, see the\n[IAM C# API\nreference documentation](https://developers.google.com/api-client-library/dotnet/apis/iam/v1).\n\n\nTo authenticate to IAM, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n using System;\n using System.Collections.Generic;\n using https://cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.html;\n using Google.Apis.Iam.v1;\n using Google.Apis.Iam.v1.Data;\n\n public partial class CustomRoles\n {\n public static IList\u003cPermission\u003e QueryTestablePermissions(\n string fullResourceName)\n {\n var credential = https://cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.GoogleCredential.html.https://cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.GoogleCredential.html#Google_Apis_Auth_OAuth2_GoogleCredential_GetApplicationDefault()\n .https://cloud.google.com/dotnet/docs/reference/Google.Apis/latest/Google.Apis.Auth.OAuth2.GoogleCredential.html#Google_Apis_Auth_OAuth2_GoogleCredential_CreateScoped_System_Collections_Generic_IEnumerable_System_String__(IamService.Scope.CloudPlatform);\n var service = new IamService(new IamService.Initializer\n {\n HttpClientInitializer = credential\n });\n\n var request = new QueryTestablePermissionsRequest\n {\n FullResourceName = fullResourceName\n };\n var response = service.Permissions.QueryTestablePermissions(request)\n .Execute();\n foreach (var p in response.Permissions)\n {\n Console.WriteLine(p.Name);\n }\n return response.Permissions;\n }\n }\n\n### Go\n\n\nTo learn how to install and use the client library for IAM, see\n[IAM client libraries](/iam/docs/reference/libraries).\n\n\nFor more information, see the\n[IAM Go API\nreference documentation](https://godoc.org/google.golang.org/genproto/googleapis/iam/admin/v1).\n\n\nTo authenticate to IAM, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \tiam \"google.golang.org/api/iam/v1\"\n )\n\n // queryTestablePermissions lists testable permissions on a resource.\n func queryTestablePermissions(w io.Writer, fullResourceName string) ([]*iam.Permission, error) {\n \tctx := context.Background()\n \tservice, err := iam.NewService(ctx)\n \tif err != nil {\n \t\treturn nil, fmt.Errorf(\"iam.NewService: %w\", err)\n \t}\n\n \trequest := &iam.QueryTestablePermissionsRequest{\n \t\tFullResourceName: fullResourceName,\n \t}\n \tresponse, err := service.Permissions.QueryTestablePermissions(request).Do()\n \tif err != nil {\n \t\treturn nil, fmt.Errorf(\"Permissions.QueryTestablePermissions: %w\", err)\n \t}\n \tfor _, p := range response.Permissions {\n \t\tfmt.Fprintf(w, \"Found permissions: %v\", p.Name)\n \t}\n \treturn response.Permissions, nil\n }\n\n### Java\n\n\nTo learn how to install and use the client library for IAM, see\n[IAM client libraries](/iam/docs/reference/libraries).\n\n\nFor more information, see the\n[IAM Java API\nreference documentation](https://developers.google.com/api-client-library/java/apis/iam/v1).\n\n\nTo authenticate to IAM, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.cloud.iam.admin.v1.https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.cloud.iam.admin.v1.IAMClient.html;\n import com.google.cloud.iam.admin.v1.https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.cloud.iam.admin.v1.IAMClient.html.https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.cloud.iam.admin.v1.IAMClient.QueryTestablePermissionsPagedResponse.html;\n import com.google.iam.admin.v1.https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.QueryTestablePermissionsRequest.html;\n import java.io.IOException;\n\n /** View available permissions in a project. */\n public class QueryTestablePermissions {\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace the variable before running the sample.\n // Full resource names can take one of the following forms:\n // cloudresourcemanager.googleapis.com/projects/PROJECT_ID\n // cloudresourcemanager.googleapis.com/organizations/NUMERIC_ID\n String fullResourceName = \"your-full-resource-name\";\n\n queryTestablePermissions(fullResourceName);\n }\n\n public static void queryTestablePermissions(String fullResourceName) throws IOException {\n https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.QueryTestablePermissionsRequest.html queryTestablePermissionsRequest =\n https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.QueryTestablePermissionsRequest.html.newBuilder().setFullResourceName(fullResourceName).build();\n\n try (https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.cloud.iam.admin.v1.IAMClient.html iamClient = https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.cloud.iam.admin.v1.IAMClient.html.create()) {\n https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.cloud.iam.admin.v1.IAMClient.QueryTestablePermissionsPagedResponse.html queryTestablePermissionsPagedResponse =\n iamClient.queryTestablePermissions(queryTestablePermissionsRequest);\n queryTestablePermissionsPagedResponse\n .iterateAll()\n .forEach(permission -\u003e System.out.println(permission.getName()));\n }\n }\n }\n\n### Python\n\n\nTo learn how to install and use the client library for IAM, see\n[IAM client libraries](/iam/docs/reference/libraries).\n\n\nFor more information, see the\n[IAM Python API\nreference documentation](https://developers.google.com/api-client-library/python/apis/iam/v1).\n\n\nTo authenticate to IAM, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import os\n from typing import List\n\n from google.cloud import https://cloud.google.com/python/docs/reference/cloudresourcemanager/latest/\n from google.iam.v1 import iam_policy_pb2, policy_pb2\n\n\n def query_testable_permissions(\n project_id: str, permissions: List[str]\n ) -\u003e policy_pb2.Policy:\n \"\"\"Tests IAM permissions of the caller.\n\n project_id: ID or number of the Google Cloud project you want to use.\n permissions: List of permissions to get.\n \"\"\"\n\n client = https://cloud.google.com/python/docs/reference/cloudresourcemanager/latest/.https://cloud.google.com/python/docs/reference/cloudresourcemanager/latest/google.cloud.resourcemanager_v3.services.projects.ProjectsClient.html()\n request = iam_policy_pb2.TestIamPermissionsRequest()\n request.resource = f\"projects/{project_id}\"\n request.permissions.extend(permissions)\n\n permissions_reponse = client.https://cloud.google.com/python/docs/reference/cloudresourcemanager/latest/google.cloud.resourcemanager_v3.services.projects.ProjectsClient.html#google_cloud_resourcemanager_v3_services_projects_ProjectsClient_test_iam_permissions(request)\n print(permissions_reponse)\n return permissions_reponse.permissions\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=iam)."]]