Repository: GoogleCloudPlatform/terraform-validator
Pull request #1008 (2022-09-28 19:14:44)

Return unknown org when folder is empty
Part of the fix for https://github.com/GoogleCloudPlatform/terraform-validator/issues/999
Subsequent fix to check folder exist or not check need to get this fixed first.  


Patch:
diff --git a/ancestrymanager/ancestrymanager.go b/ancestrymanager/ancestrymanager.go
index b0e2f70a..7961cbd6 100644
--- a/ancestrymanager/ancestrymanager.go
+++ b/ancestrymanager/ancestrymanager.go
@@ -143,7 +143,7 @@ func (m *manager) fetchAncestors(config *resources.Config, tfData resources.Terr
 	switch cai.Type {
 	case "cloudresourcemanager.googleapis.com/Folder":
 		if !folderOK {
-			return nil, fmt.Errorf("folder id not found in terraform data")
+			return []string{"organizations/unknown"}, nil
 		}
 		key = folderKey
 	case "cloudresourcemanager.googleapis.com/Organization":
diff --git a/ancestrymanager/ancestryutil.go b/ancestrymanager/ancestryutil.go
index 088b69d3..c5a28a67 100644
--- a/ancestrymanager/ancestryutil.go
+++ b/ancestrymanager/ancestryutil.go
@@ -18,12 +18,15 @@ func assetParent(cai *resources.Asset, ancestors []string) (string, error) {
 	}
 	switch cai.Type {
 	case "cloudresourcemanager.googleapis.com/Folder":
-		if len(ancestors) < 2 {
-			return "", fmt.Errorf("unexpected value for ancestors: %s", ancestors)
+		if len(ancestors) > 1 {
+			parent := ancestors[1]
+			if strings.HasPrefix(parent, "folders/") || strings.HasPrefix(parent, "organizations/") {
+				return fmt.Sprintf("//cloudresourcemanager.googleapis.com/%s", ancestors[1]), nil
+			}
 		}
-		parent := ancestors[1]
-		if strings.HasPrefix(parent, "folders/") || strings.HasPrefix(parent, "organizations/") {
-			return fmt.Sprintf("//cloudresourcemanager.googleapis.com/%s", ancestors[1]), nil
+		if len(ancestors) == 1 && strings.HasPrefix(ancestors[0], "organizations/") {
+			// organizations/unknown
+			return fmt.Sprintf("//cloudresourcemanager.googleapis.com/%s", ancestors[0]), nil
 		}
 	case "cloudresourcemanager.googleapis.com/Organization":
 		return "", nil
diff --git a/converters/google/resources/compute_backend_bucket.go b/converters/google/resources/compute_backend_bucket.go
index 59aafb50..56cf4da4 100644
--- a/converters/google/resources/compute_backend_bucket.go
+++ b/converters/google/resources/compute_backend_bucket.go
@@ -60,6 +60,12 @@ func GetComputeBackendBucketApiObject(d TerraformResourceData, config *Config) (
 	} else if v, ok := d.GetOkExists("cdn_policy"); !isEmptyValue(reflect.ValueOf(cdnPolicyProp)) && (ok || !reflect.DeepEqual(v, cdnPolicyProp)) {
 		obj["cdnPolicy"] = cdnPolicyProp
 	}
+	compressionModeProp, err := expandComputeBackendBucketCompressionMode(d.Get("compression_mode"), d, config)
+	if err != nil {
+		return nil, err
+	} else if v, ok := d.GetOkExists("compression_mode"); !isEmptyValue(reflect.ValueOf(compressionModeProp)) && (ok || !reflect.DeepEqual(v, compressionModeProp)) {
+		obj["compressionMode"] = compressionModeProp
+	}
 	edgeSecurityPolicyProp, err := expandComputeBackendBucketEdgeSecurityPolicy(d.Get("edge_security_policy"), d, config)
 	if err != nil {
 		return nil, err
@@ -316,6 +322,10 @@ func expandComputeBackendBucketCdnPolicyBypassCacheOnRequestHeadersHeaderName(v
 	return v, nil
 }
 
+func expandComputeBackendBucketCompressionMode(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
+	return v, nil
+}
+
 func expandComputeBackendBucketEdgeSecurityPolicy(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
 	return v, nil
 }
diff --git a/converters/google/resources/compute_backend_service.go b/converters/google/resources/compute_backend_service.go
index 4d72bd20..ef1b009a 100644
--- a/converters/google/resources/compute_backend_service.go
+++ b/converters/google/resources/compute_backend_service.go
@@ -221,6 +221,12 @@ func GetComputeBackendServiceApiObject(d TerraformResourceData, config *Config)
 	} else if v, ok := d.GetOkExists("circuit_breakers"); !isEmptyValue(reflect.ValueOf(circuitBreakersProp)) && (ok || !reflect.DeepEqual(v, circuitBreakersProp)) {
 		obj["circuitBreakers"] = circuitBreakersProp
 	}
+	compressionModeProp, err := expandComputeBackendServiceCompressionMode(d.Get("compression_mode"), d, config)
+	if err != nil {
+		return nil, err
+	} else if v, ok := d.GetOkExists("compression_mode"); !isEmptyValue(reflect.ValueOf(compressionModeProp)) && (ok || !reflect.DeepEqual(v, compressionModeProp)) {
+		obj["compressionMode"] = compressionModeProp
+	}
 	consistentHashProp, err := expandComputeBackendServiceConsistentHash(d.Get("consistent_hash"), d, config)
 	if err != nil {
 		return nil, err
@@ -598,6 +604,10 @@ func expandComputeBackendServiceCircuitBreakersMaxRetries(v interface{}, d Terra
 	return v, nil
 }
 
+func expandComputeBackendServiceCompressionMode(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
+	return v, nil
+}
+
 func expandComputeBackendServiceConsistentHash(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
 	l := v.([]interface{})
 	if len(l) == 0 || l[0] == nil {
diff --git a/converters/google/resources/dns_managed_zone.go b/converters/google/resources/dns_managed_zone.go
index 786f2756..081143f6 100644
--- a/converters/google/resources/dns_managed_zone.go
+++ b/converters/google/resources/dns_managed_zone.go
@@ -107,6 +107,12 @@ func GetDNSManagedZoneApiObject(d TerraformResourceData, config *Config) (map[st
 	} else if v, ok := d.GetOkExists("peering_config"); !isEmptyValue(reflect.ValueOf(peeringConfigProp)) && (ok || !reflect.DeepEqual(v, peeringConfigProp)) {
 		obj["peeringConfig"] = peeringConfigProp
 	}
+	cloudLoggingConfigProp, err := expandDNSManagedZoneCloudLoggingConfig(d.Get("cloud_logging_config"), d, config)
+	if err != nil {
+		return nil, err
+	} else if v, ok := d.GetOkExists("cloud_logging_config"); !isEmptyValue(reflect.ValueOf(cloudLoggingConfigProp)) && (ok || !reflect.DeepEqual(v, cloudLoggingConfigProp)) {
+		obj["cloudLoggingConfig"] = cloudLoggingConfigProp
+	}
 
 	return obj, nil
 }
@@ -415,3 +421,26 @@ func expandDNSManagedZonePeeringConfigTargetNetworkNetworkUrl(v interface{}, d T
 	}
 	return ConvertSelfLinkToV1(url), nil
 }
+
+func expandDNSManagedZoneCloudLoggingConfig(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
+	l := v.([]interface{})
+	if len(l) == 0 || l[0] == nil {
+		return nil, nil
+	}
+	raw := l[0]
+	original := raw.(map[string]interface{})
+	transformed := make(map[string]interface{})
+
+	transformedEnableLogging, err := expandDNSManagedZoneCloudLoggingConfigEnableLogging(original["enable_logging"], d, config)
+	if err != nil {
+		return nil, err
+	} else if val := reflect.ValueOf(transformedEnableLogging); val.IsValid() && !isEmptyValue(val) {
+		transformed["enableLogging"] = transformedEnableLogging
+	}
+
+	return transformed, nil
+}
+
+func expandDNSManagedZoneCloudLoggingConfigEnableLogging(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
+	return v, nil
+}
diff --git a/go.mod b/go.mod
index 53e8a3c7..4c875f12 100644
--- a/go.mod
+++ b/go.mod
@@ -26,7 +26,7 @@ require (
 	github.com/hashicorp/go-cleanhttp v0.5.2
 	github.com/hashicorp/terraform-json v0.14.0
 	github.com/hashicorp/terraform-plugin-sdk/v2 v2.18.0
-	github.com/hashicorp/terraform-provider-google v1.20.1-0.20220928182821-1e3c865b12ec
+	github.com/hashicorp/terraform-provider-google v1.20.1-0.20220928193603-21c447bf4b72
 	github.com/mitchellh/go-homedir v1.1.0
 	github.com/pkg/errors v0.9.1
 	github.com/sirupsen/logrus v1.8.1
diff --git a/go.sum b/go.sum
index e3fb50de..9d9d2789 100644
--- a/go.sum
+++ b/go.sum
@@ -650,8 +650,8 @@ github.com/hashicorp/terraform-plugin-log v0.4.1 h1:xpbmVhvuU3mgHzLetOmx9pkOL2rm
 github.com/hashicorp/terraform-plugin-log v0.4.1/go.mod h1:p4R1jWBXRTvL4odmEkFfDdhUjHf9zcs/BCoNHAc7IK4=
 github.com/hashicorp/terraform-plugin-sdk/v2 v2.18.0 h1:/cdI5di5XA+N80gXzXF4YcHq36DprBskubk6Z8i26ZQ=
 github.com/hashicorp/terraform-plugin-sdk/v2 v2.18.0/go.mod h1:L3SHkD/Q8zPVgXviQmpVwy9nKwpXXZscVIpVEnQ/T50=
-github.com/hashicorp/terraform-provider-google v1.20.1-0.20220928182821-1e3c865b12ec h1:t30exQxsNRa0Xuut7UYpkAh3BKnau1dKsWfgdn9YdLM=
-github.com/hashicorp/terraform-provider-google v1.20.1-0.20220928182821-1e3c865b12ec/go.mod h1:XkWEq9m0whYwjW0vmHmzjskXkneDxOIKcAYQNaAyo7s=
+github.com/hashicorp/terraform-provider-google v1.20.1-0.20220928193603-21c447bf4b72 h1:aDd47FH/nI3ebW6FwngkgAUsuycwp7F5cqVou72Kvso=
+github.com/hashicorp/terraform-provider-google v1.20.1-0.20220928193603-21c447bf4b72/go.mod h1:XkWEq9m0whYwjW0vmHmzjskXkneDxOIKcAYQNaAyo7s=
 github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c h1:D8aRO6+mTqHfLsK/BC3j5OAoogv1WLRWzY1AaTo3rBg=
 github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c/go.mod h1:Wn3Na71knbXc1G8Lh+yu/dQWWJeFQEpDeJMtWMtlmNI=
 github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=


Test patch:
diff --git a/ancestrymanager/ancestrymanager_test.go b/ancestrymanager/ancestrymanager_test.go
index 5bcefe41..e971ad9d 100644
--- a/ancestrymanager/ancestrymanager_test.go
+++ b/ancestrymanager/ancestrymanager_test.go
@@ -629,6 +629,23 @@ func TestGetAncestors_Folder(t *testing.T) {
 			v1Count: 1,
 			v3Count: 1,
 		},
+		{
+			name: "folder ID is empty string and no ancestor",
+			data: tfdata.NewFakeResourceData(
+				"google_folder_iam_member",
+				p.ResourcesMap["google_folder_iam_member"].Schema,
+				map[string]interface{}{
+					"folder": "",
+				},
+			),
+			asset: &resources.Asset{
+				Type: "cloudresourcemanager.googleapis.com/Folder",
+			},
+			v3Responses: map[string]*crmv3.Project{},
+			v1Responses: map[string][]*crmv1.Ancestor{},
+			want:        []string{"organizations/unknown"},
+			parent:      "//cloudresourcemanager.googleapis.com/organizations/unknown",
+		},
 	}
 	for _, c := range cases {
 		t.Run(c.name, func(t *testing.T) {
diff --git a/ancestrymanager/ancestryutil_test.go b/ancestrymanager/ancestryutil_test.go
index 3a09af30..1cd46365 100644
--- a/ancestrymanager/ancestryutil_test.go
+++ b/ancestrymanager/ancestryutil_test.go
@@ -40,6 +40,12 @@ func TestAssetParent(t *testing.T) {
 			cai:       &resources.Asset{Type: "cloudresourcemanager.googleapis.com/Folder"},
 			want:      "//cloudresourcemanager.googleapis.com/organizations/789",
 		},
+		{
+			name:      "new folder with unknown org",
+			ancestors: []string{"organizations/unknown"},
+			cai:       &resources.Asset{Type: "cloudresourcemanager.googleapis.com/Folder"},
+			want:      "//cloudresourcemanager.googleapis.com/organizations/unknown",
+		},
 		{
 			name:      "organization",
 			ancestors: []string{"organizations/789"},