From e5dcb0e825d4e432c2a974b125e3062c7123f936 Mon Sep 17 00:00:00 2001 From: The Magician Date: Fri, 28 Jan 2022 11:36:10 -0800 Subject: [PATCH] Adding support for zone and region via env (#5646) (#493) Signed-off-by: Modular Magician --- converters/google/resources/getconfig.go | 12 +++++ converters/google/resources/getconfig_test.go | 48 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/converters/google/resources/getconfig.go b/converters/google/resources/getconfig.go index a4a4755c8..88383ba2d 100644 --- a/converters/google/resources/getconfig.go +++ b/converters/google/resources/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/converters/google/resources/getconfig_test.go b/converters/google/resources/getconfig_test.go index 911b4abdd..3e17c7d60 100644 --- a/converters/google/resources/getconfig_test.go +++ b/converters/google/resources/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",