Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Adding support for zone and region via env (#5646) (GoogleCloudPlatfo…
Browse files Browse the repository at this point in the history
…rm#493)

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jan 28, 2022
1 parent 153962e commit e5dcb0e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
12 changes: 12 additions & 0 deletions converters/google/resources/getconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions converters/google/resources/getconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit e5dcb0e

Please sign in to comment.