gcloud
gcp

gcloudコマンドでconfigurationを切り替える

GCP上にプロジェクトが増えてきてプロジェクトのconfigurationを切り替えながらコマンド操作したくなったときのメモです。

$ gcloud version                                                                                                                     
Google Cloud SDK 219.0.1
bq 2.0.34
core 2018.09.28
gsutil 4.34
kubectl 2018.09.17

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(config)を確認する

$ gcloud config list
[core]
account = [your account]
disable_usage_reporting = True
project = [your project]

Your active configuration is: [newly configured name]

configurationを切り替える

$ gcloud config configurations activate [configuration name like default]
Activated [configuration name like default].