From adde1c66e80b9e2cb923f07f63b2ac69e3e8c3f7 Mon Sep 17 00:00:00 2001 From: Prateek Khushalani Date: Tue, 25 Jan 2022 05:02:36 +0000 Subject: [PATCH 1/3] Adding support for zone and region via env --- 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..ed7691ae7c8e 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 getZone(cfg *Config) string { + return cfg.Zone +} +func getRegion(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: getZone, + }, + { + name: "GCLOUD_ZONE", + envKey: "GCLOUD_ZONE", + envValue: "whatever", + expected: "whatever", + getConfigValue: getZone, + }, + { + name: "CLOUDSDK_COMPUTE_ZONE", + envKey: "CLOUDSDK_COMPUTE_ZONE", + envValue: "whatever", + expected: "whatever", + getConfigValue: getZone, + }, + { + name: "GOOGLE_REGION", + envKey: "GOOGLE_REGION", + envValue: "whatever", + expected: "whatever", + getConfigValue: getRegion, + }, + { + name: "GCLOUD_REGION", + envKey: "GCLOUD_REGION", + envValue: "whatever", + expected: "whatever", + getConfigValue: getRegion, + }, + { + name: "CLOUDSDK_COMPUTE_REGION" + envKey: "CLOUDSDK_COMPUTE_REGION" + envValue: "whatever", + expected: "whatever", + getConfigValue: getRegion, + }, { name: "GOOGLE_TERRAFORM_VALIDATOR_USERAGENT_EXTENSION", envKey: "GOOGLE_TERRAFORM_VALIDATOR_USERAGENT_EXTENSION", From 7ebe97cf5f10c026e0ce088ceaf82d8470d9c47b Mon Sep 17 00:00:00 2001 From: Prateek Khushalani Date: Thu, 27 Jan 2022 04:52:14 +0000 Subject: [PATCH 2/3] Adding missing comma --- mmv1/third_party/validator/getconfig_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mmv1/third_party/validator/getconfig_test.go b/mmv1/third_party/validator/getconfig_test.go index ed7691ae7c8e..1710837f7735 100644 --- a/mmv1/third_party/validator/getconfig_test.go +++ b/mmv1/third_party/validator/getconfig_test.go @@ -110,8 +110,8 @@ func TestGetConfigExtractsEnvVars(t *testing.T) { getConfigValue: getRegion, }, { - name: "CLOUDSDK_COMPUTE_REGION" - envKey: "CLOUDSDK_COMPUTE_REGION" + name: "CLOUDSDK_COMPUTE_REGION", + envKey: "CLOUDSDK_COMPUTE_REGION", envValue: "whatever", expected: "whatever", getConfigValue: getRegion, From d6c502956179a38976754ff01b36c730da151fb3 Mon Sep 17 00:00:00 2001 From: Prateek Khushalani Date: Fri, 28 Jan 2022 17:05:41 +0000 Subject: [PATCH 3/3] Renaming test function names as there are already functions with that name --- mmv1/third_party/validator/getconfig_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/mmv1/third_party/validator/getconfig_test.go b/mmv1/third_party/validator/getconfig_test.go index 1710837f7735..3e17c7d607b5 100644 --- a/mmv1/third_party/validator/getconfig_test.go +++ b/mmv1/third_party/validator/getconfig_test.go @@ -22,10 +22,10 @@ func getImpersonateServiceAccount(cfg *Config) string { func getUserAgent(cfg *Config) string { return cfg.UserAgent() } -func getZone(cfg *Config) string { +func getZoneValue(cfg *Config) string { return cfg.Zone } -func getRegion(cfg *Config) string { +func getRegionValue(cfg *Config) string { return cfg.Region } @@ -79,42 +79,42 @@ func TestGetConfigExtractsEnvVars(t *testing.T) { envKey: "GOOGLE_ZONE", envValue: "whatever", expected: "whatever", - getConfigValue: getZone, + getConfigValue: getZoneValue, }, { name: "GCLOUD_ZONE", envKey: "GCLOUD_ZONE", envValue: "whatever", expected: "whatever", - getConfigValue: getZone, + getConfigValue: getZoneValue, }, { name: "CLOUDSDK_COMPUTE_ZONE", envKey: "CLOUDSDK_COMPUTE_ZONE", envValue: "whatever", expected: "whatever", - getConfigValue: getZone, + getConfigValue: getZoneValue, }, { name: "GOOGLE_REGION", envKey: "GOOGLE_REGION", envValue: "whatever", expected: "whatever", - getConfigValue: getRegion, + getConfigValue: getRegionValue, }, { name: "GCLOUD_REGION", envKey: "GCLOUD_REGION", envValue: "whatever", expected: "whatever", - getConfigValue: getRegion, + getConfigValue: getRegionValue, }, { name: "CLOUDSDK_COMPUTE_REGION", envKey: "CLOUDSDK_COMPUTE_REGION", envValue: "whatever", expected: "whatever", - getConfigValue: getRegion, + getConfigValue: getRegionValue, }, { name: "GOOGLE_TERRAFORM_VALIDATOR_USERAGENT_EXTENSION",