Repository: GoogleCloudPlatform/terraform-validator
Pull request #493 (2022-01-28 19:36:03)

Adding support for zone and region via env
<!-- Put a description of what this PR is for here, along with any references to issues that this resolves or contributes to -->




<!--
Replace each [ ] with [X] to check it. Switch to the preview view to make it easier to click on links.
These steps will speed up the review process, and we appreciate you spending time on them before sending
your code to be reviewed.
-->
If this PR is for Terraform, I acknowledge that I have:

- [X ] Searched through the [issue tracker](https://github.com/hashicorp/terraform-provider-google/issues) for an open issue that this either resolves or contributes to, commented on it to claim it, and written "fixes {url}" or "part of {url}" in this PR description. If there were no relevant open issues, I opened one and commented that I would like to work on it (not necessary for very small changes).
- [x ] [Generated Terraform](https://github.com/GoogleCloudPlatform/magic-modules#generating-downstream-tools), and ran `make test` and `make lint` to ensure it passes unit and linter tests.
.

<!-- AUTOCHANGELOG for Downstream PRs.

Please select one of the following "release-note:" headings:
    - release-note:bug


    
Unless you choose release-note:none, please add a release note.

See .ci/RELEASE_NOTES_GUIDE.md for writing good release notes.

You can add more release note blocks if you want more than one CHANGELOG
entry for this PR.
-->
**Release Note Template for Downstream PRs (will be copied)**

```release-note:bug

```
This is fix for Issue- https://github.com/GoogleCloudPlatform/terraform-validator/issues/463

Derived from https://github.com/GoogleCloudPlatform/magic-modules/pull/5646


Patch:
diff --git a/converters/google/resources/getconfig.go b/converters/google/resources/getconfig.go
index a4a4755c..88383ba2 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


Test patch:
diff --git a/converters/google/resources/getconfig_test.go b/converters/google/resources/getconfig_test.go
index 911b4abd..3e17c7d6 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",