From 51287004fb7537604151ba2786d18ba1a3992548 Mon Sep 17 00:00:00 2001 From: prateek2408 Date: Sat, 29 Jan 2022 00:53:00 +0530 Subject: [PATCH] Adding support for zone and region via env (#5646) --- mmv1/third_party/validator/getconfig.go | 12 +++++ mmv1/third_party/validator/getconfig_test.go | 48 ++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/mmv1/third_party/validator/getconfig.go b/mmv1/third_party/validator/getconfig.go index a4a4755c8b78..88383ba2dd14 100644 --- a/mmv1/third_party/validator/getconfig.go +++ b/mmv1/third_party/validator/getconfig.go @@ -36,6 +36,18 @@ func GetConfig(ctx context.Context, project string, offline bool) (*Config, erro "GOOGLE_IMPERSONATE_SERVICE_ACCOUNT", }) + cfg.Zone = multiEnvSearch([]string{ + "GOOGLE_ZONE", + "GCLOUD_ZONE", + "CLOUDSDK_COMPUTE_ZONE", + }) + + cfg.Region = multiEnvSearch([]string{ + "GOOGLE_REGION", + "GCLOUD_REGION", + "CLOUDSDK_COMPUTE_REGION", + }) + // opt in extension for adding to the User-Agent header if ext := os.Getenv("GOOGLE_TERRAFORM_VALIDATOR_USERAGENT_EXTENSION"); ext != "" { ua := cfg.userAgent diff --git a/mmv1/third_party/validator/getconfig_test.go b/mmv1/third_party/validator/getconfig_test.go index 911b4abddb60..3e17c7d607b5 100644 --- a/mmv1/third_party/validator/getconfig_test.go +++ b/mmv1/third_party/validator/getconfig_test.go @@ -22,6 +22,12 @@ func getImpersonateServiceAccount(cfg *Config) string { func getUserAgent(cfg *Config) string { return cfg.UserAgent() } +func getZoneValue(cfg *Config) string { + return cfg.Zone +} +func getRegionValue(cfg *Config) string { + return cfg.Region +} func TestGetConfigExtractsEnvVars(t *testing.T) { ctx := context.Background() @@ -68,6 +74,48 @@ func TestGetConfigExtractsEnvVars(t *testing.T) { expected: "whatever", getConfigValue: getImpersonateServiceAccount, }, + { + name: "GOOGLE_ZONE", + envKey: "GOOGLE_ZONE", + envValue: "whatever", + expected: "whatever", + getConfigValue: getZoneValue, + }, + { + name: "GCLOUD_ZONE", + envKey: "GCLOUD_ZONE", + envValue: "whatever", + expected: "whatever", + getConfigValue: getZoneValue, + }, + { + name: "CLOUDSDK_COMPUTE_ZONE", + envKey: "CLOUDSDK_COMPUTE_ZONE", + envValue: "whatever", + expected: "whatever", + getConfigValue: getZoneValue, + }, + { + name: "GOOGLE_REGION", + envKey: "GOOGLE_REGION", + envValue: "whatever", + expected: "whatever", + getConfigValue: getRegionValue, + }, + { + name: "GCLOUD_REGION", + envKey: "GCLOUD_REGION", + envValue: "whatever", + expected: "whatever", + getConfigValue: getRegionValue, + }, + { + name: "CLOUDSDK_COMPUTE_REGION", + envKey: "CLOUDSDK_COMPUTE_REGION", + envValue: "whatever", + expected: "whatever", + getConfigValue: getRegionValue, + }, { name: "GOOGLE_TERRAFORM_VALIDATOR_USERAGENT_EXTENSION", envKey: "GOOGLE_TERRAFORM_VALIDATOR_USERAGENT_EXTENSION",