From 582ba98a66cee8a0358afff6937876b3f21e53ba Mon Sep 17 00:00:00 2001 From: Or Sela Date: Thu, 8 May 2025 12:12:59 +0000 Subject: [PATCH 1/9] Add PSC fields to Filestore instance in beta --- mmv1/products/filestore/Instance.yaml | 22 ++++ .../resource_filestore_instance_test.go.tmpl | 114 +++++++++++++++++- 2 files changed, 135 insertions(+), 1 deletion(-) diff --git a/mmv1/products/filestore/Instance.yaml b/mmv1/products/filestore/Instance.yaml index 058fec6eac85..8c471e0d35b5 100644 --- a/mmv1/products/filestore/Instance.yaml +++ b/mmv1/products/filestore/Instance.yaml @@ -209,6 +209,12 @@ properties: An integer representing the anonymous group id with a default value of 65534. Anon_gid may only be set with squashMode of ROOT_SQUASH. An error will be returned if this field is specified for other squashMode settings. + - name: 'network' + type: String + min_version: beta + description: | + The source VPC network for `ip_ranges`. + Required for instances using Private Service Connect, optional otherwise. max_size: 10 max_size: 1 - name: 'networks' @@ -273,6 +279,22 @@ properties: enum_values: - 'DIRECT_PEERING' - 'PRIVATE_SERVICE_ACCESS' + - 'PRIVATE_SERVICE_CONNECT' + - name: 'pscConfig' + type: NestedObject + min_version: beta + description: | + Private Service Connect configuration. + Should only be set when connect_mode is PRIVATE_SERVICE_CONNECT. + properties: + - name: endpointProject + type: String + description: | + Consumer service project in which the Private Service Connect endpoint + would be set up. This is optional, and only relevant in case the network + is a shared VPC. If this is not specified, the endpoint would be set up + in the VPC host project. + immutable: true min_size: 1 - name: 'etag' type: String diff --git a/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl b/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl index 4c915b76595e..c4a993007511 100644 --- a/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl +++ b/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl @@ -1,11 +1,13 @@ package filestore_test import ( + "bytes" "context" "fmt" "reflect" "regexp" "testing" + "text/template" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-provider-google/google/acctest" @@ -592,4 +594,114 @@ resource "google_filestore_instance" "instance" { } `, name, location, tier) } -{{- end }} \ No newline at end of file + +{{- end }} +{{- if ne $.TargetVersionName "ga" }} + +func TestAccFilestoreInstance_psc(t *testing.T) { + t.Parallel() + + data := map[string]string{ + "Name": fmt.Sprintf("tf-test-%d", acctest.RandInt(t)), + "Location": "us-central1", + "Tier": "REGIONAL", + } + config, err := executeTemplate(pscInstanceConfigTemplate, data) + if err != nil { + t.Fatalf("Failed to execute config template: %v", err) + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t), + CheckDestroy: testAccCheckFilestoreInstanceDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("google_filestore_instance.instance", "networks.0.connect_mode", "PRIVATE_SERVICE_CONNECT"), + ), + }, + { + ResourceName: "google_filestore_instance.instance", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"zone"}, + }, + }, + }) +} + +func executeTemplate(tmplString string, data any) (string, error) { + tmpl, err := template.New("tmpl").Delims("[[", "]]").Parse(tmplString) + if err != nil { + return "", err + } + var b bytes.Buffer + if err := tmpl.Execute(&b, data); err != nil { + return "", err + } + return b.String(), nil +} + +const pscInstanceConfigTemplate = ` +data "google_client_config" "current" {} + +resource "google_compute_network" "psc_network" { + provider = google-beta + name = "[[.Name]]" + auto_create_subnetworks = false +} + +resource "google_compute_subnetwork" "psc_subnet" { + provider = google-beta + name = "[[.Name]]" + ip_cidr_range = "10.2.0.0/16" + region = "[[.Location]]" + network = google_compute_network.psc_network.id +} + +resource "google_network_connectivity_service_connection_policy" "default" { + provider = google-beta + name = "[[.Name]]" + location = "[[.Location]]" + service_class = "google-cloud-filestore" + network = google_compute_network.psc_network.id + psc_config { + subnetworks = [google_compute_subnetwork.psc_subnet.id] + } +} + +resource "google_filestore_instance" "instance" { + provider = google-beta + depends_on = [ + google_network_connectivity_service_connection_policy.default + ] + name = "[[.Name]]" + location = "[[.Location]]" + tier = "[[.Tier]]" + description = "An instance created during testing." + protocol = "NFS_V4_1" + + file_shares { + capacity_gb = 1024 + name = "share" + + nfs_export_options { + ip_ranges = ["70.0.0.1/24"] + network = google_compute_network.psc_network.name + } + } + + networks { + network = google_compute_network.psc_network.name + modes = ["MODE_IPV4"] + connect_mode = "PRIVATE_SERVICE_CONNECT" + psc_config { + endpoint_project = data.google_client_config.current.project + } + } +} +` + +{{- end }} From 9c97aef02b5d2021702dc1c78dcd3bcc7ceda86d Mon Sep 17 00:00:00 2001 From: Or Sela Date: Fri, 9 May 2025 12:51:20 +0300 Subject: [PATCH 2/9] Add provider to google_client_config data source --- .../filestore/resource_filestore_instance_test.go.tmpl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl b/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl index c4a993007511..5477ba64a097 100644 --- a/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl +++ b/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl @@ -645,7 +645,9 @@ func executeTemplate(tmplString string, data any) (string, error) { } const pscInstanceConfigTemplate = ` -data "google_client_config" "current" {} +data "google_client_config" "current" { + provider = "google-beta" +} resource "google_compute_network" "psc_network" { provider = google-beta From 67e8b5fe3b32586e24f0fda2245027e2e9b80485 Mon Sep 17 00:00:00 2001 From: Or Sela Date: Fri, 9 May 2025 12:52:18 +0300 Subject: [PATCH 3/9] Remove quotes --- .../services/filestore/resource_filestore_instance_test.go.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl b/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl index 5477ba64a097..1337c94c5505 100644 --- a/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl +++ b/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl @@ -646,7 +646,7 @@ func executeTemplate(tmplString string, data any) (string, error) { const pscInstanceConfigTemplate = ` data "google_client_config" "current" { - provider = "google-beta" + provider = google-beta } resource "google_compute_network" "psc_network" { From ae13402e7b6de53caad25f7e342a4149a494a1d5 Mon Sep 17 00:00:00 2001 From: Or Sela Date: Fri, 9 May 2025 14:01:46 +0300 Subject: [PATCH 4/9] Fix unused import in GA tests --- .../resource_filestore_instance_test.go.tmpl | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl b/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl index 1337c94c5505..9ae61e8ca09e 100644 --- a/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl +++ b/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl @@ -632,18 +632,6 @@ func TestAccFilestoreInstance_psc(t *testing.T) { }) } -func executeTemplate(tmplString string, data any) (string, error) { - tmpl, err := template.New("tmpl").Delims("[[", "]]").Parse(tmplString) - if err != nil { - return "", err - } - var b bytes.Buffer - if err := tmpl.Execute(&b, data); err != nil { - return "", err - } - return b.String(), nil -} - const pscInstanceConfigTemplate = ` data "google_client_config" "current" { provider = google-beta @@ -707,3 +695,15 @@ resource "google_filestore_instance" "instance" { ` {{- end }} + +func executeTemplate(tmplString string, data any) (string, error) { + tmpl, err := template.New("tmpl").Delims("[[", "]]").Parse(tmplString) + if err != nil { + return "", err + } + var b bytes.Buffer + if err := tmpl.Execute(&b, data); err != nil { + return "", err + } + return b.String(), nil +} \ No newline at end of file From 722b137af7be9fcfee39dc976d70bb0687279100 Mon Sep 17 00:00:00 2001 From: Or Sela Date: Wed, 14 May 2025 09:29:40 +0300 Subject: [PATCH 5/9] Replace go template with acctest.Nprintf --- .../resource_filestore_instance_test.go.tmpl | 51 +++++++------------ 1 file changed, 17 insertions(+), 34 deletions(-) diff --git a/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl b/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl index 9ae61e8ca09e..d7f58b440ff2 100644 --- a/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl +++ b/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl @@ -1,13 +1,11 @@ package filestore_test import ( - "bytes" "context" "fmt" "reflect" "regexp" "testing" - "text/template" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-provider-google/google/acctest" @@ -601,14 +599,10 @@ resource "google_filestore_instance" "instance" { func TestAccFilestoreInstance_psc(t *testing.T) { t.Parallel() - data := map[string]string{ - "Name": fmt.Sprintf("tf-test-%d", acctest.RandInt(t)), - "Location": "us-central1", - "Tier": "REGIONAL", - } - config, err := executeTemplate(pscInstanceConfigTemplate, data) - if err != nil { - t.Fatalf("Failed to execute config template: %v", err) + context := map[string]interface{}{ + "name": fmt.Sprintf("tf-test-%d", acctest.RandInt(t)), + "location": "us-central1", + "tier": "REGIONAL", } acctest.VcrTest(t, resource.TestCase{ @@ -617,8 +611,8 @@ func TestAccFilestoreInstance_psc(t *testing.T) { CheckDestroy: testAccCheckFilestoreInstanceDestroyProducer(t), Steps: []resource.TestStep{ { - Config: config, - Check: resource.ComposeTestCheckFunc( + Config: testAccFilestoreInstance_psc(context), + Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("google_filestore_instance.instance", "networks.0.connect_mode", "PRIVATE_SERVICE_CONNECT"), ), }, @@ -632,29 +626,30 @@ func TestAccFilestoreInstance_psc(t *testing.T) { }) } -const pscInstanceConfigTemplate = ` +func testAccFilestoreInstance_psc(context map[string]interface{}) string { + return acctest.Nprintf(` data "google_client_config" "current" { provider = google-beta } resource "google_compute_network" "psc_network" { provider = google-beta - name = "[[.Name]]" + name = "%{name}" auto_create_subnetworks = false } resource "google_compute_subnetwork" "psc_subnet" { provider = google-beta - name = "[[.Name]]" + name = "%{name}" ip_cidr_range = "10.2.0.0/16" - region = "[[.Location]]" + region = "%{location}" network = google_compute_network.psc_network.id } resource "google_network_connectivity_service_connection_policy" "default" { provider = google-beta - name = "[[.Name]]" - location = "[[.Location]]" + name = "%{name}" + location = "%{location}" service_class = "google-cloud-filestore" network = google_compute_network.psc_network.id psc_config { @@ -667,9 +662,9 @@ resource "google_filestore_instance" "instance" { depends_on = [ google_network_connectivity_service_connection_policy.default ] - name = "[[.Name]]" - location = "[[.Location]]" - tier = "[[.Tier]]" + name = "%{name}" + location = "%{location}" + tier = "%{tier}" description = "An instance created during testing." protocol = "NFS_V4_1" @@ -694,16 +689,4 @@ resource "google_filestore_instance" "instance" { } ` -{{- end }} - -func executeTemplate(tmplString string, data any) (string, error) { - tmpl, err := template.New("tmpl").Delims("[[", "]]").Parse(tmplString) - if err != nil { - return "", err - } - var b bytes.Buffer - if err := tmpl.Execute(&b, data); err != nil { - return "", err - } - return b.String(), nil -} \ No newline at end of file +{{- end }} \ No newline at end of file From bef21a23f35839d1f79b7a9f766cd0ee85bb2cc9 Mon Sep 17 00:00:00 2001 From: Or Sela Date: Wed, 14 May 2025 09:49:10 +0300 Subject: [PATCH 6/9] Fix build --- .../filestore/resource_filestore_instance_test.go.tmpl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl b/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl index d7f58b440ff2..e335960dfedb 100644 --- a/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl +++ b/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl @@ -687,6 +687,7 @@ resource "google_filestore_instance" "instance" { } } } -` +`, context) +} {{- end }} \ No newline at end of file From 421ae46339cff41a042fe657b75133b5ee22ca7e Mon Sep 17 00:00:00 2001 From: Or Sela Date: Thu, 15 May 2025 11:31:53 +0300 Subject: [PATCH 7/9] Test update of NFSExportOptions network field --- .../resource_filestore_instance_test.go.tmpl | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl b/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl index e335960dfedb..16b9dc2dfbc2 100644 --- a/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl +++ b/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl @@ -690,4 +690,65 @@ resource "google_filestore_instance" "instance" { `, context) } +func TestAccFilestoreInstance_nfsExportOptionsNetwork_update(t *testing.T) { + t.Parallel() + + name := fmt.Sprintf("tf-test-%d", acctest.RandInt(t)) + location := "us-central1-a" + tier := "ZONAL" + + // Currently, we can only alternate between an empty network and the instance network of non-PSC instances. + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckFilestoreInstanceDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccFilestoreInstance_nfsExportOptionsNetwork_update(name, location, tier, ""), + }, + { + ResourceName: "google_filestore_instance.instance", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"zone"}, + }, + { + Config: testAccFilestoreInstance_nfsExportOptionsNetwork_update(name, location, tier, "default"), + }, + { + ResourceName: "google_filestore_instance.instance", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"zone"}, + }, + }, + }) +} + +func testAccFilestoreInstance_nfsExportOptionsNetwork_update(name, location, tier, network string) string { + return fmt.Sprintf(` +resource "google_filestore_instance" "instance" { + name = "%s" + zone = "%s" + tier = "%s" + description = "An instance created during testing." + + file_shares { + capacity_gb = 1024 + name = "share" + + nfs_export_options { + ip_ranges = ["70.0.0.1/24"] + network = "%s" + } + } + + networks { + network = "default" + modes = ["MODE_IPV4"] + } +} +`, name, location, tier, network) +} + {{- end }} \ No newline at end of file From 10ab846146b7c838bb994c57401e9e5f604b2b54 Mon Sep 17 00:00:00 2001 From: Or Sela Date: Thu, 15 May 2025 11:35:32 +0300 Subject: [PATCH 8/9] Fix network field test --- .../filestore/resource_filestore_instance_test.go.tmpl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl b/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl index 16b9dc2dfbc2..0ddb1d056d3d 100644 --- a/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl +++ b/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl @@ -700,7 +700,7 @@ func TestAccFilestoreInstance_nfsExportOptionsNetwork_update(t *testing.T) { // Currently, we can only alternate between an empty network and the instance network of non-PSC instances. acctest.VcrTest(t, resource.TestCase{ PreCheck: func() { acctest.AccTestPreCheck(t) }, - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t), CheckDestroy: testAccCheckFilestoreInstanceDestroyProducer(t), Steps: []resource.TestStep{ { @@ -728,6 +728,7 @@ func TestAccFilestoreInstance_nfsExportOptionsNetwork_update(t *testing.T) { func testAccFilestoreInstance_nfsExportOptionsNetwork_update(name, location, tier, network string) string { return fmt.Sprintf(` resource "google_filestore_instance" "instance" { + provider = google-beta name = "%s" zone = "%s" tier = "%s" From d61b9cdc217c9d0f42635e3ee6806a8dc2d8a4fb Mon Sep 17 00:00:00 2001 From: Or Sela Date: Thu, 15 May 2025 12:46:41 +0300 Subject: [PATCH 9/9] Check the network field value --- .../services/filestore/resource_filestore_instance_test.go.tmpl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl b/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl index 0ddb1d056d3d..146fa0df13c6 100644 --- a/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl +++ b/mmv1/third_party/terraform/services/filestore/resource_filestore_instance_test.go.tmpl @@ -705,6 +705,7 @@ func TestAccFilestoreInstance_nfsExportOptionsNetwork_update(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccFilestoreInstance_nfsExportOptionsNetwork_update(name, location, tier, ""), + Check: resource.TestCheckResourceAttr("google_filestore_instance.instance", "file_shares.0.nfs_export_options.0.network", ""), }, { ResourceName: "google_filestore_instance.instance", @@ -714,6 +715,7 @@ func TestAccFilestoreInstance_nfsExportOptionsNetwork_update(t *testing.T) { }, { Config: testAccFilestoreInstance_nfsExportOptionsNetwork_update(name, location, tier, "default"), + Check: resource.TestCheckResourceAttr("google_filestore_instance.instance", "file_shares.0.nfs_export_options.0.network", "default"), }, { ResourceName: "google_filestore_instance.instance",