停用自訂角色
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
示範如何停用自訂角色。
深入探索
如需包含這個程式碼範例的詳細說明文件,請參閱下列內容:
程式碼範例
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[],[],null,["# Disable a custom role\n\nDemonstrates disabling a custom role.\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### 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.iam.admin.v1.https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.Role.html;\n import com.google.iam.admin.v1.https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.UpdateRoleRequest.html;\n import com.google.protobuf.https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask.html;\n import java.io.IOException;\n\n public class DisableRole {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace the variables before running the sample.\n // Role ID must point to an existing role.\n String projectId = \"your-project-id\";\n String roleId = \"testRole\";\n\n https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.Role.html role = disableRole(projectId, roleId);\n System.out.println(\"Role name: \" + role.https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.Role.html#com_google_iam_admin_v1_Role_getName__());\n System.out.println(\"Role stage: \" + role.https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.Role.html#com_google_iam_admin_v1_Role_getStage__());\n }\n\n public static https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.Role.html disableRole(String projectId, String roleId)\n throws IOException {\n String roleName = \"projects/\" + projectId + \"/roles/\" + roleId;\n https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.Role.html role = https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.Role.html.newBuilder()\n .setName(roleName)\n .setStage(https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.Role.html.RoleLaunchStage.DISABLED)\n .build();\n\n https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask.html fieldMask = https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask.html.newBuilder().https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask.Builder.html#com_google_protobuf_FieldMask_Builder_addPaths_java_lang_String_(\"stage\").build();\n https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.UpdateRoleRequest.html updateRoleRequest =\n https://cloud.google.com/java/docs/reference/google-iam-admin/latest/com.google.iam.admin.v1.UpdateRoleRequest.html.newBuilder()\n .setName(roleName)\n .setRole(role)\n .setUpdateMask(fieldMask)\n .build();\n\n // Initialize client for sending requests. This client only needs to be created\n // once, and can be reused for multiple requests.\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 return iamClient.updateRole(updateRoleRequest);\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 from google.api_core.exceptions import NotFound\n from google.cloud.iam_admin_v1 import https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.types.GetRoleRequest.html, https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.services.iam.IAMClient.html, https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.types.Role.html, https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.types.UpdateRoleRequest.html\n\n\n def disable_role(project_id: str, role_id: str) -\u003e Role:\n \"\"\"Disables an IAM role in a GCP project.\n\n Args:\n project_id: GCP project ID\n role_id: ID of GCP IAM role\n\n Returns: Updated google.cloud.iam_admin_v1.Role object with disabled stage\n \"\"\"\n client = IAMClient()\n name = f\"projects/{project_id}/roles/{role_id}\"\n get_request = GetRoleRequest(name=name)\n try:\n role = client.https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.services.iam.IAMClient.html#google_cloud_iam_admin_v1_services_iam_IAMClient_get_role(get_request)\n role.stage = https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.types.Role.html.https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.types.Role.RoleLaunchStage.html.DISABLED\n update_request = UpdateRoleRequest(name=role.name, role=role)\n client.https://cloud.google.com/python/docs/reference/iam/latest/google.cloud.iam_admin_v1.services.iam.IAMClient.html#google_cloud_iam_admin_v1_services_iam_IAMClient_update_role(update_request)\n print(f\"Disabled role: {role_id}: {role}\")\n return role\n except NotFound as exc:\n raise NotFound(f'Role with id [{role_id}] not found, take some actions') from exc\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)."]]