Repository: GoogleCloudPlatform/terraform-validator
Pull request #698 (2022-05-12 20:18:54)

Add an no-op ancestry manager
Add an no op ancestry manager, so internal code can use this for testing, and no need to implement the ancestry manager interface. The interface will need to be modified. 


Patch:
diff --git a/ancestrymanager/ancestrymanager.go b/ancestrymanager/ancestrymanager.go
index 0b0a4369..5d8dc8f8 100644
--- a/ancestrymanager/ancestrymanager.go
+++ b/ancestrymanager/ancestrymanager.go
@@ -178,3 +178,12 @@ func ancestryPath(as []*cloudresourcemanager.Ancestor) string {
 	}
 	return strings.Join(path, "/")
 }
+
+type NoOpAncestryManager struct{}
+
+func (m *NoOpAncestryManager) GetAncestry(project string) (string, error) {
+	return "", nil
+}
+func (m *NoOpAncestryManager) GetAncestryWithResource(project string, tfData resources.TerraformResourceData, cai resources.Asset) (string, error) {
+	return "", nil
+}
diff --git a/converters/google/resources/resource_converters.go b/converters/google/resources/resource_converters.go
index 7308e53a..1c50e59e 100644
--- a/converters/google/resources/resource_converters.go
+++ b/converters/google/resources/resource_converters.go
@@ -73,6 +73,7 @@ func ResourceConverters() map[string][]ResourceConverter {
 		"google_kms_key_ring":                                     {resourceConverterKMSKeyRing()},
 		"google_filestore_instance":                               {resourceConverterFilestoreInstance()},
 		"google_access_context_manager_service_perimeter":         {resourceConverterAccessContextManagerServicePerimeter()},
+		"google_access_context_manager_access_policy":             {resourceConverterAccessContextManagerAccessPolicy()},
 		"google_cloud_run_service":                                {resourceConverterCloudRunService()},
 		"google_cloud_run_domain_mapping":                         {resourceConverterCloudRunDomainMapping()},
 		"google_cloudfunctions_function":                          {resourceConverterCloudFunctionsCloudFunction()},
diff --git a/go.mod b/go.mod
index 5d7f1653..f6fe6eb5 100644
--- a/go.mod
+++ b/go.mod
@@ -12,7 +12,7 @@ require (
 	github.com/hashicorp/go-cleanhttp v0.5.2
 	github.com/hashicorp/terraform-json v0.13.0
 	github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
-	github.com/hashicorp/terraform-provider-google v1.20.1-0.20220512165904-44248ab85cec
+	github.com/hashicorp/terraform-provider-google v1.20.1-0.20220512170723-9f3e9c8765a1
 	github.com/kr/pretty v0.3.0 // indirect
 	github.com/mitchellh/go-homedir v1.1.0
 	github.com/onsi/gomega v1.17.0 // indirect
diff --git a/go.sum b/go.sum
index d6a17193..54577195 100644
--- a/go.sum
+++ b/go.sum
@@ -696,8 +696,8 @@ github.com/hashicorp/terraform-plugin-log v0.2.0 h1:rjflRuBqCnSk3UHOR25MP1G5BDLK
 github.com/hashicorp/terraform-plugin-log v0.2.0/go.mod h1:E1kJmapEHzqu1x6M++gjvhzM2yMQNXPVWZRCB8sgYjg=
 github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1 h1:B9AocC+dxrCqcf4vVhztIkSkt3gpRjUkEka8AmZWGlQ=
 github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1/go.mod h1:FjM9DXWfP0w/AeOtJoSKHBZ01LqmaO6uP4bXhv3fekw=
-github.com/hashicorp/terraform-provider-google v1.20.1-0.20220512165904-44248ab85cec h1:PDwUTCYggzg2OUTqJ+4KuKxHY2CLW1TfS1svkMbq1ZU=
-github.com/hashicorp/terraform-provider-google v1.20.1-0.20220512165904-44248ab85cec/go.mod h1:Rt/7kFHGCqyKTWt5NKpz4X8HrX8hApKVqbY8bKonN1s=
+github.com/hashicorp/terraform-provider-google v1.20.1-0.20220512170723-9f3e9c8765a1 h1:isS7PnMSuQ3IU52b4h91hhhNnF2sn3ZvJ1SwgnTvn7c=
+github.com/hashicorp/terraform-provider-google v1.20.1-0.20220512170723-9f3e9c8765a1/go.mod h1:Rt/7kFHGCqyKTWt5NKpz4X8HrX8hApKVqbY8bKonN1s=
 github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896 h1:1FGtlkJw87UsTMg5s8jrekrHmUPUJaMcu6ELiVhQrNw=
 github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896/go.mod h1:bzBPnUIkI0RxauU8Dqo+2KrZZ28Cf48s8V6IHt3p4co=
 github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=


Test patch:
diff --git a/converters/google/convert_test.go b/converters/google/convert_test.go
index 0d0b2d0e..a8f1dd54 100644
--- a/converters/google/convert_test.go
+++ b/converters/google/convert_test.go
@@ -32,7 +32,6 @@ const testProject = "test-project"
 
 func newTestConverter(convertUnchanged bool) (*Converter, error) {
 	ctx := context.Background()
-	ancestry := "default-ancestry"
 	project := testProject
 	offline := true
 	cfg, err := resources.GetConfig(ctx, project, offline)
@@ -40,14 +39,8 @@ func newTestConverter(convertUnchanged bool) (*Converter, error) {
 		return nil, errors.Wrap(err, "constructing configuration")
 	}
 	errorLogger := zap.NewExample()
-	entries := map[string]string{
-		project: ancestry,
-	}
-	ancestryManager, err := ancestrymanager.New(cfg.NewResourceManagerClient(cfg.UserAgent()), entries, errorLogger)
-	if err != nil {
-		return nil, errors.Wrap(err, "constructing resource ancestryManager")
-	}
-	c := NewConverter(cfg, ancestryManager, offline, convertUnchanged, errorLogger)
+	c := NewConverter(cfg, &ancestrymanager.NoOpAncestryManager{}, offline, convertUnchanged, errorLogger)
+
 	return c, nil
 }
 
diff --git a/test/cli_test.go b/test/cli_test.go
index 5bd0f957..d4aebf39 100644
--- a/test/cli_test.go
+++ b/test/cli_test.go
@@ -65,6 +65,7 @@ func TestCLI(t *testing.T) {
 		{name: "example_storage_bucket_iam_binding", compareConvertOutput: compareMergedIamBindingOutput},
 		{name: "example_storage_bucket_iam_member", compareConvertOutput: compareMergedIamMemberOutput},
 		// auto inserted tests that are not in list above or manually inserted in read_test.go
+		{name: "example_access_context_manager_access_policy"},
 		{name: "example_access_context_manager_service_perimeter"},
 		{name: "example_bigquery_dataset"},
 		{name: "example_bigquery_dataset_iam_binding"},
diff --git a/test/environment_test.go b/test/environment_test.go
index bbb011aa..94b94a31 100644
--- a/test/environment_test.go
+++ b/test/environment_test.go
@@ -21,7 +21,7 @@ const (
 	defaultOrganizationDomain = "meep.test.com"
 	defaultOrganizationTarget = "13579"
 	defaultProject            = "foobar"
-	defaultProviderVersion    = "4.4.0"
+	defaultProviderVersion    = "4.20.0"
 	defaultRegion             = "us-central1"
 	defaultServiceAccount     = "meep@foobar.iam.gserviceaccount.com"
 )
diff --git a/test/read_test.go b/test/read_test.go
index 763305c2..865b0679 100644
--- a/test/read_test.go
+++ b/test/read_test.go
@@ -29,6 +29,7 @@ func TestReadPlannedAssetsCoverage(t *testing.T) {
 		{name: "example_storage_bucket_iam_binding"},
 		{name: "example_storage_bucket_iam_member"},
 		// auto inserted tests that are not in list above or manually inserted in cli_test.go
+		{name: "example_access_context_manager_access_policy"},
 		{name: "example_access_context_manager_service_perimeter"},
 		{name: "example_bigquery_dataset"},
 		{name: "example_bigquery_dataset_iam_binding"},
diff --git a/testdata/templates/example_access_context_manager_access_policy.json b/testdata/templates/example_access_context_manager_access_policy.json
new file mode 100644
index 00000000..fe3b4aae
--- /dev/null
+++ b/testdata/templates/example_access_context_manager_access_policy.json
@@ -0,0 +1,20 @@
+[
+    {
+        "name": "//accesscontextmanager.googleapis.com/accessPolicies/placeholder-BpLnfgDs",
+        "asset_type": "accesscontextmanager.googleapis.com/AccessPolicy",
+        "ancestry_path": "{{.Ancestry}}/project/{{.Provider.project}}",
+        "resource": {
+            "version": "v1",
+            "discovery_document_uri": "https://www.googleapis.com/discovery/v1/apis/accesscontextmanager/v1/rest",
+            "discovery_name": "AccessPolicy",
+            "parent": "//cloudresourcemanager.googleapis.com/projects/{{.Provider.project}}",
+            "data": {
+                "parent": "organizations/{{.OrgID}}",
+                "scopes": [
+                    "projects/{{.Provider.project}}"
+                ],
+                "title": "Scoped Access Policy"
+            }
+        }
+    }
+]
\ No newline at end of file
diff --git a/testdata/templates/example_access_context_manager_access_policy.tf b/testdata/templates/example_access_context_manager_access_policy.tf
new file mode 100644
index 00000000..9c4f0226
--- /dev/null
+++ b/testdata/templates/example_access_context_manager_access_policy.tf
@@ -0,0 +1,34 @@
+/**
+ * Copyright 2019 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+terraform {
+  required_providers {
+    google = {
+      source = "hashicorp/google"
+      version = "~> {{.Provider.version}}"
+    }
+  }
+}
+
+provider "google" {
+  {{if .Provider.credentials }}credentials = "{{.Provider.credentials}}"{{end}}
+}
+
+resource "google_access_context_manager_access_policy" "access-policy" {
+  parent = "organizations/{{.OrgID}}"
+  title  = "Scoped Access Policy"
+  scopes = ["projects/{{.Provider.project}}"]
+}
\ No newline at end of file
diff --git a/testdata/templates/example_access_context_manager_access_policy.tfplan.json b/testdata/templates/example_access_context_manager_access_policy.tfplan.json
new file mode 100644
index 00000000..849ee349
--- /dev/null
+++ b/testdata/templates/example_access_context_manager_access_policy.tfplan.json
@@ -0,0 +1,90 @@
+{
+    "format_version": "0.1",
+    "terraform_version": "0.12.31",
+    "planned_values": {
+        "root_module": {
+            "resources": [
+                {
+                    "address": "google_access_context_manager_access_policy.access-policy",
+                    "mode": "managed",
+                    "type": "google_access_context_manager_access_policy",
+                    "name": "access-policy",
+                    "provider_name": "google",
+                    "schema_version": 0,
+                    "values": {
+                        "parent": "organizations/{{.OrgID}}",
+                        "scopes": [
+                            "projects/{{.Provider.project}}"
+                        ],
+                        "timeouts": null,
+                        "title": "Scoped Access Policy"
+                    }
+                }
+            ]
+        }
+    },
+    "resource_changes": [
+        {
+            "address": "google_access_context_manager_access_policy.access-policy",
+            "mode": "managed",
+            "type": "google_access_context_manager_access_policy",
+            "name": "access-policy",
+            "provider_name": "google",
+            "change": {
+                "actions": [
+                    "create"
+                ],
+                "before": null,
+                "after": {
+                    "parent": "organizations/{{.OrgID}}",
+                    "scopes": [
+                        "projects/{{.Provider.project}}"
+                    ],
+                    "timeouts": null,
+                    "title": "Scoped Access Policy"
+                },
+                "after_unknown": {
+                    "create_time": true,
+                    "id": true,
+                    "name": true,
+                    "scopes": [
+                        false
+                    ],
+                    "update_time": true
+                }
+            }
+        }
+    ],
+    "configuration": {
+        "provider_config": {
+            "google": {
+                "name": "google"
+            }
+        },
+        "root_module": {
+            "resources": [
+                {
+                    "address": "google_access_context_manager_access_policy.access-policy",
+                    "mode": "managed",
+                    "type": "google_access_context_manager_access_policy",
+                    "name": "access-policy",
+                    "provider_config_key": "google",
+                    "expressions": {
+                        "parent": {
+                            "constant_value": "organizations/{{.OrgID}}"
+                        },
+                        "scopes": {
+                            "constant_value": [
+                                "projects/{{.Provider.project}}"
+                            ]
+                        },
+                        "title": {
+                            "constant_value": "Scoped Access Policy"
+                        }
+                    },
+                    "schema_version": 0
+                }
+            ]
+        }
+    }
+}
\ No newline at end of file
diff --git a/testdata/templates/example_filestore_instance.json b/testdata/templates/example_filestore_instance.json
index 2dfe453b..2d38ec5f 100644
--- a/testdata/templates/example_filestore_instance.json
+++ b/testdata/templates/example_filestore_instance.json
@@ -17,6 +17,7 @@
         ],
         "networks": [
           {
+            "connectMode": "DIRECT_PEERING",
             "modes": [
               "MODE_IPV4"
             ],
diff --git a/testdata/templates/example_filestore_instance.tfplan.json b/testdata/templates/example_filestore_instance.tfplan.json
index 505d5b80..e1ab2894 100644
--- a/testdata/templates/example_filestore_instance.tfplan.json
+++ b/testdata/templates/example_filestore_instance.tfplan.json
@@ -1,6 +1,6 @@
 {
   "format_version": "0.1",
-  "terraform_version": "0.13.5",
+  "terraform_version": "0.12.31",
   "planned_values": {
     "root_module": {
       "resources": [
@@ -9,20 +9,24 @@
           "mode": "managed",
           "type": "google_filestore_instance",
           "name": "test",
-          "provider_name": "registry.terraform.io/hashicorp/google",
-          "schema_version": 0,
+          "provider_name": "google",
+          "schema_version": 1,
           "values": {
             "description": null,
             "file_shares": [
               {
                 "capacity_gb": 2660,
-                "name": "share1"
+                "name": "share1",
+                "nfs_export_options": []
               }
             ],
+            "kms_key_name": null,
             "labels": null,
+            "location": "us-central1-b",
             "name": "test-instance",
             "networks": [
               {
+                "connect_mode": "DIRECT_PEERING",
                 "modes": [
                   "MODE_IPV4"
                 ],
@@ -30,8 +34,7 @@
               }
             ],
             "tier": "BASIC_SSD",
-            "timeouts": null,
-            "location": "us-central1-b"
+            "timeouts": null
           }
         }
       ]
@@ -43,7 +46,7 @@
       "mode": "managed",
       "type": "google_filestore_instance",
       "name": "test",
-      "provider_name": "registry.terraform.io/hashicorp/google",
+      "provider_name": "google",
       "change": {
         "actions": [
           "create"
@@ -54,13 +57,17 @@
           "file_shares": [
             {
               "capacity_gb": 2660,
-              "name": "share1"
+              "name": "share1",
+              "nfs_export_options": []
             }
           ],
+          "kms_key_name": null,
           "labels": null,
+          "location": "us-central1-b",
           "name": "test-instance",
           "networks": [
             {
+              "connect_mode": "DIRECT_PEERING",
               "modes": [
                 "MODE_IPV4"
               ],
@@ -68,12 +75,16 @@
             }
           ],
           "tier": "BASIC_SSD",
-          "timeouts": null,
-          "location": "us-central1-b"
+          "timeouts": null
         },
         "after_unknown": {
           "create_time": true,
           "etag": true,
+          "file_shares": [
+            {
+                "nfs_export_options": []
+            }
+        ],
           "id": true,
           "networks": [
             {
@@ -84,7 +95,8 @@
               "reserved_ip_range": true
             }
           ],
-          "project": true
+          "project": true,
+          "zone": true
         }
       }
     }
@@ -92,20 +104,7 @@
   "configuration": {
     "provider_config": {
       "google": {
-        "name": "google",
-        "expressions": {
-          "project": {
-            "constant_value": "{{.Provider.project}}"
-          }
-        }
-      },
-      "google": {
-        "name": "google",
-        "expressions": {
-          "project": {
-            "constant_value": "{{.Provider.project}}"
-          }
-        }
+        "name": "google"
       }
     },
     "root_module": {
@@ -127,6 +126,9 @@
                 }
               }
             ],
+            "location": {
+              "constant_value": "us-central1-b"
+            },
             "name": {
               "constant_value": "test-instance"
             },
@@ -144,12 +146,9 @@
             ],
             "tier": {
               "constant_value": "BASIC_SSD"
-            },
-            "location": {
-              "constant_value": "us-central1-b"
             }
           },
-          "schema_version": 0
+          "schema_version": 1
         }
       ]
     }