クライアント ライブラリを使用してロールを付与する
使い慣れたプログラミング言語で Resource Manager API から IAM メソッドを使用する方法について説明します。
このタスクを Google Cloud コンソールで直接行う際の順を追ったガイダンスについては、ガイドを表示をクリックしてください。
始める前に
Google Cloud プロジェクトを作成する
このクイックスタートでは、新しい Google Cloud プロジェクトが必要になります。
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Enable the Resource Manager API:
gcloud services enable cloudresourcemanager.googleapis.com
-
Create local authentication credentials for your user account:
gcloud auth application-default login
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/resourcemanager.projectIamAdmin
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
- Replace
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Enable the Resource Manager API:
gcloud services enable cloudresourcemanager.googleapis.com
-
Create local authentication credentials for your user account:
gcloud auth application-default login
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/resourcemanager.projectIamAdmin
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
- Replace
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
クライアント ライブラリをインストールする
C#
C# 開発環境の設定の詳細については、C# 開発環境設定ガイドをご覧ください。
install-package Google.Apis.Iam.v1 install-package Google.Apis.CloudResourceManager.v1
Go
go get golang.org/x/oauth2/google go get google.golang.org/api/cloudresourcemanager/v1
Java
Java 開発環境の設定の詳細については、Java 開発環境設定ガイドをご覧ください。
Maven を使用している場合は、pom.xml
ファイルに以下の内容を追加します。
Python
Python 開発環境の設定の詳細については、Python 開発環境設定ガイドをご覧ください。
pip install --upgrade google-api-python-client google-auth google-auth-httplib2
許可ポリシーの読み取り、変更、書き込み
このクイックスタートのコード スニペットでは、次の処理を行います。
- Google Cloudプロジェクトを管理する Resource Manager サービスを初期化します。
- プロジェクトの許可ポリシーを読み取ります。
- Google アカウントにログ書き込みロール(
roles/logging.logWriter
)を付与して、許可ポリシーを変更します。 - 更新後の許可ポリシーを書き込みます。
- プロジェクト レベルでログ書き込みロール(
roles/logging.logWriter
)を持つすべてのプリンシパルを出力します。 - ログ書き込みロールを取り消します。
コード スニペットが実行される前に、次の値を置き換えます。
your-project
: プロジェクトの ID。your-member
: ユーザー アカウントのメールアドレス。例:user:my-user@example.com
C#
Resource Manager 用のクライアント ライブラリをインストールして使用する方法については、Resource Manager クライアント ライブラリをご覧ください。詳細については、Resource Manager C# API のリファレンス ドキュメントをご覧ください。
Resource Manager に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
Go
Resource Manager 用のクライアント ライブラリをインストールして使用する方法については、Resource Manager クライアント ライブラリをご覧ください。詳細については、Resource Manager Go API のリファレンス ドキュメントをご覧ください。
Resource Manager に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
Java
Resource Manager 用のクライアント ライブラリをインストールして使用する方法については、Resource Manager クライアント ライブラリをご覧ください。詳細については、Resource Manager Java API のリファレンス ドキュメントをご覧ください。
Resource Manager に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
Python
Resource Manager 用のクライアント ライブラリをインストールして使用する方法については、Resource Manager クライアント ライブラリをご覧ください。詳細については、Resource Manager Python API のリファレンス ドキュメントをご覧ください。
Resource Manager に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
これで完了です。Resource Manager API の IAM メソッドを使用して、プロジェクトのアクセス権を変更しました。
いかがでしたか
クリーンアップ
-
Optional: Revoke the authentication credentials that you created, and delete the local credential file.
gcloud auth application-default revoke
-
Optional: Revoke credentials from the gcloud CLI.
gcloud auth revoke
次のステップ
- IAM の仕組みについて確認する。
- アクセス権の付与、変更、取り消しについて学習する。
- ポリシーのトラブルシューティングでアクセスに関する問題のトラブルシューティングを行う。