GCP上にプロジェクトが増えてきてプロジェクトのconfigurationを切り替えながらコマンド操作したくなったときのメモです。
$ gcloud version
gcloud version
Google Cloud SDK 301.0.0
app-engine-go
app-engine-python 1.9.91
beta 2020.07.10
bq 2.0.58
cloud-build-local
cloud-datastore-emulator 2.1.0
core 2020.07.10
docker-credential-gcr
gsutil 4.51
kpt
kubectl 2020.05.01
pubsub-emulator 2019.09.27
configurationを増やす
$ gcloud init
つぎのようにいい感じに対話形式で設定できます。
Welcome! This command will take you through the configuration of gcloud.
Settings from your current configuration [default] are:
compute:
region: asia-northeast1
zone: asia-northeast1-b
core:
account: [your account]
disable_usage_reporting: 'True'
project: [your project]
Pick configuration to use:
[1] Re-initialize this configuration [default] with new settings
[2] Create a new configuration
Please enter your numeric choice: [your choice]
Enter configuration name. Names start with a lower case letter and
contain only lower case letters a-z, digits 0-9, and hyphens '-': [newly configured name]
Your current configuration has been set to: [newly configured name]
You can skip diagnostics next time by using the following flag:
gcloud init --skip-diagnostics
Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.
Reachability Check passed.
Network diagnostic (1/1 checks) passed.
Choose the account you would like to use to perform operations for
this configuration:
[1] [your account]
[2] Log in with a new account
Please enter your numeric choice: 1
You are logged in as: [your account].
Pick cloud project to use:
[1] [your existing project]
[2] Create a new project
Please enter numeric choice or text value (must exactly match list
item): [your choice]
Your current project has been set to: [your choice].
Not setting default zone/region (this feature makes it easier to use
[gcloud compute] by setting an appropriate default value for the
--zone and --region flag).
See https://cloud.google.com/compute/docs/gcloud-compute section on how to set
default compute region and zone manually. If you would like [gcloud init] to be
able to do this for you the next time you run it, make sure the
Compute Engine API is enabled for your project on the
https://console.developers.google.com/apis page.
Your Google Cloud SDK is configured and ready to use!
* Commands that require authentication will use [your account] by default
* Commands will reference project `[your project ID]` by default
Run `gcloud help config` to learn how to change individual settings
This gcloud configuration is called [your choice]. You can create additional configurations if you work with multiple accounts and/or projects.
Run `gcloud topic configurations` to learn more.
Some things to try next:
* Run `gcloud --help` to see the Cloud Platform services you can interact with. And run `gcloud help COMMAND` to get help on any gcloud command.
* Run `gcloud topic -h` to learn about advanced features of the SDK like arg files and output formatting
configurationを一覧する
$ gcloud config configurations list
NAME IS_ACTIVE ACCOUNT PROJECT DEFAULT_ZONE DEFAULT_REGION
default False [your account] xxxxxxxx asia-northeast1-b asia-northeast1
[newly configured name] True [your account] [your project]
現在のconfigurationを確認する
$ gcloud config list
[core]
account = [your account]
disable_usage_reporting = True
project = [your project]
Your active configuration is: [newly configured name]
現在のconfigurationを更新する
# 別アカウントにする
$ gcloud config set core/account [another account]
# 別プロジェクトにする
$ gcloud config set project [another project]
# デフォルトのcompute regionを設定する
$ gcloud config set compute/region asia-northeast1
# デフォルトのcompute zoneを設定する
$ gcloud config set compute/zone asia-northeast1-a
# 更新後のconfigurationを確認する
$ gcloud config list
[compute]
region = asia-northeast1
zone = asia-northeast1-a
[core]
account = [another account]
disable_usage_reporting = False
project = [another project]
※利用可能なプロパティ(computeのregionやcoreのaccountなど)はgcloud config --help
で確認できる。
configurationを切り替える
$ gcloud config configurations activate [configuration name like default]
Activated [configuration name like default].
listしてactivateして…は、pecoを利用したつぎのようなエイリアスを作っておくと便利です。
.zshrc
alias chp='change_project'
function change_project() {
gcloud config configurations activate $(gcloud config configurations list | awk '{print $1}' | grep -v NAME | peco)
}