From c6506897d29e91ca34cb7f11d228c0c3e34b9d9c Mon Sep 17 00:00:00 2001 From: BBBmau Date: Tue, 27 Feb 2024 21:36:49 -0800 Subject: [PATCH 01/17] add regionFromZone function logic --- .../terraform/functions/region_from_zone.go | 51 +++++++++++++++++++ .../fwprovider/framework_provider.go.erb | 1 + 2 files changed, 52 insertions(+) create mode 100644 mmv1/third_party/terraform/functions/region_from_zone.go diff --git a/mmv1/third_party/terraform/functions/region_from_zone.go b/mmv1/third_party/terraform/functions/region_from_zone.go new file mode 100644 index 000000000000..fb363a6f5b84 --- /dev/null +++ b/mmv1/third_party/terraform/functions/region_from_zone.go @@ -0,0 +1,51 @@ +package functions + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/function" +) + +var _ function.Function = RegionFromZoneFunction{} + +func NewRegionFromZoneFunction() function.Function { + return &RegionFromZoneFunction{} +} + +type RegionFromZoneFunction struct{} + +func (f RegionFromZoneFunction) Metadata(ctx context.Context, req function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "region_from_zone" +} + +func (f RegionFromZoneFunction) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Returns the region within a provided resource's zone", + Description: "Takes a single string argument, which should be a resource's zone.", + Parameters: []function.Parameter{ + function.StringParameter{ + Name: "zone", + Description: "A string of a resource's zone.", + }, + }, + Return: function.StringReturn{}, + } +} + +func (f RegionFromZoneFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + // Load arguments from function call + var arg0 string + resp.Diagnostics.Append(req.Arguments.GetArgument(ctx, 0, &arg0)...) + + if resp.Diagnostics.HasError() { + return + } + + // Validate input - can't be an empty string, every zone will have 2nd to last element in string be a `-` + if len(arg0) != 0 && arg0[len(arg0)-2] == '-' { + resp.Diagnostics.Append(resp.Result.Set(ctx, arg0[:len(arg0)-2])...) + } else { + return + } + +} diff --git a/mmv1/third_party/terraform/fwprovider/framework_provider.go.erb b/mmv1/third_party/terraform/fwprovider/framework_provider.go.erb index f3389f2ddb3e..b435abbca471 100644 --- a/mmv1/third_party/terraform/fwprovider/framework_provider.go.erb +++ b/mmv1/third_party/terraform/fwprovider/framework_provider.go.erb @@ -304,5 +304,6 @@ func (p *FrameworkProvider) Resources(_ context.Context) []func() resource.Resou func (p *FrameworkProvider) Functions(_ context.Context) []func() function.Function { return []func() function.Function{ functions.NewProjectFromIdFunction, + functions.NewRegionFromZoneFunction, } } \ No newline at end of file From 0297be275ab09a6996f59006a075c731802d57aa Mon Sep 17 00:00:00 2001 From: BBBmau Date: Tue, 27 Feb 2024 22:06:25 -0800 Subject: [PATCH 02/17] add region_from_zone_test.go --- .../functions/region_from_zone_test.go | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 mmv1/third_party/terraform/functions/region_from_zone_test.go diff --git a/mmv1/third_party/terraform/functions/region_from_zone_test.go b/mmv1/third_party/terraform/functions/region_from_zone_test.go new file mode 100644 index 000000000000..a6fae7b80d92 --- /dev/null +++ b/mmv1/third_party/terraform/functions/region_from_zone_test.go @@ -0,0 +1,66 @@ +package functions_test + +import ( + "fmt" + "regexp" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-provider-google/google/acctest" + "github.com/hashicorp/terraform-provider-google/google/envvar" +) + +func TestAccProviderFunction_region_from_zone(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "function_name": "region_from_zone", + "output_name": "zone", + "resource_name": fmt.Sprintf("tf-test-region-from-zone-func-%s", acctest.RandString(t, 10)), + } + + acctest.VcrTest(t, resource.TestCase{ + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + Steps: []resource.TestStep{ + { + Config: testProviderFunction_get_region_from_zone(context), + Check: resource.ComposeTestCheckFunc( + resource.TestMatchOutput(context["output_name"].(string), "us-central1"), + ), + }, + }, + }) +} + +func testProviderFunction_get_region_from_zone(context map[string]interface{}) string { + return acctest.Nprintf(` +# terraform block required for provider function to be found +terraform { + required_providers { + google = { + source = "hashicorp/google" + } + } +} + +resource "google_filestore_instance" "instance" { + name = "%{resource_name}" + location = "us-central1-b" + tier = "BASIC_HDD" + + file_shares { + capacity_gb = 1024 + name = "share1" + } + + networks { + network = "default" + modes = ["MODE_IPV4"] + } + } + +output "%{output_name}" { + value = provider::google::%{function_name}(google_filestore_instance.default.location) +} +`, context) +} From 3b475ef3d55d65f11c40b5ab894ab395efe83e85 Mon Sep 17 00:00:00 2001 From: BBBmau Date: Tue, 27 Feb 2024 22:17:23 -0800 Subject: [PATCH 03/17] add region_from_zone_internal_test.go --- .../region_from_zone_internal_test.go | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 mmv1/third_party/terraform/functions/region_from_zone_internal_test.go diff --git a/mmv1/third_party/terraform/functions/region_from_zone_internal_test.go b/mmv1/third_party/terraform/functions/region_from_zone_internal_test.go new file mode 100644 index 000000000000..eaa20016d112 --- /dev/null +++ b/mmv1/third_party/terraform/functions/region_from_zone_internal_test.go @@ -0,0 +1,88 @@ +package functions + +import ( + "context" + "fmt" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/hashicorp/terraform-plugin-framework/attr" + "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-framework/function" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" +) + +func TestFunctionRun_region_from_zone(t *testing.T) { + t.Parallel() + + region := "us-central1" + + testCases := map[string]struct { + request function.RunRequest + expected function.RunResponse + }{ + "it returns the expected output value when given a valid zone input": { + request: function.RunRequest{ + Arguments: function.NewArgumentsData([]attr.Value{types.StringValue("us-central1-b")}), + }, + expected: function.RunResponse{ + Result: function.NewResultData(types.StringValue(region)), + }, + }, + "it returns an error when given input that is empty": { + request: function.RunRequest{ + Arguments: function.NewArgumentsData([]attr.Value{types.StringNull()}), + }, + expected: function.RunResponse{ + Result: function.NewResultData(types.StringNull()), + Diagnostics: diag.Diagnostics{ + diag.NewArgumentErrorDiagnostic( + 0, + noMatchesErrorSummary, + fmt.Sprintf("The input string is empty."), + ), + }, + }, + }, + "it returns an error when given input that is not a zone": { + request: function.RunRequest{ + Arguments: function.NewArgumentsData([]attr.Value{types.StringValue("foobar")}), + }, + expected: function.RunResponse{ + Result: function.NewResultData(types.StringNull()), + Diagnostics: diag.Diagnostics{ + diag.NewArgumentErrorDiagnostic( + 0, + noMatchesErrorSummary, + fmt.Sprintf("The input string foobar is not a valid zone."), + ), + }, + }, + }, + } + + for name, testCase := range testCases { + tn, tc := name, testCase + + t.Run(tn, func(t *testing.T) { + t.Parallel() + + // Arrange + got := function.RunResponse{ + Result: function.NewResultData(basetypes.StringValue{}), + } + + // Act + NewRegionFromZoneFunction().Run(context.Background(), tc.request, &got) + + // Assert + if diff := cmp.Diff(got.Result, tc.expected.Result); diff != "" { + t.Errorf("unexpected diff between expected and received result: %s", diff) + } + if diff := cmp.Diff(got.Diagnostics, tc.expected.Diagnostics); diff != "" { + t.Errorf("unexpected diff between expected and received diagnostics: %s", diff) + } + }) + } +} From 9a898d2bb54ac2127eb62b219e536b9edf473329 Mon Sep 17 00:00:00 2001 From: BBBmau Date: Wed, 28 Feb 2024 12:37:58 -0800 Subject: [PATCH 04/17] add region_from_zone.html.markdown --- .../functions/region_from_zone.html.markdown | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 mmv1/third_party/terraform/website/docs/functions/region_from_zone.html.markdown diff --git a/mmv1/third_party/terraform/website/docs/functions/region_from_zone.html.markdown b/mmv1/third_party/terraform/website/docs/functions/region_from_zone.html.markdown new file mode 100644 index 000000000000..7a115521a50b --- /dev/null +++ b/mmv1/third_party/terraform/website/docs/functions/region_from_zone.html.markdown @@ -0,0 +1,57 @@ +--- +page_title: region_from_zone Function - terraform-provider-google +description: |- + Returns the region within a provided zone. +--- + +# Function: region_from_zone + +Returns the region within a provided resource's zone. + +For more information about using provider-defined functions with Terraform [see the official documentation](https://developer.hashicorp.com/terraform/plugin/framework/functions/concepts). + +## Example Usage + +### Use with the `google` provider + +```terraform +terraform { + required_providers { + google = { + source = "hashicorp/google" + } + } +} + +# Value is "us-central1" +output "function_output" { + value = provider::google::region_from_zone("us-central1-b") +} +``` + +### Use with the `google-beta` provider + +```terraform +terraform { + required_providers { + google-beta = { + source = "hashicorp/google-beta" + } + } +} + +# Value is "us-central1" +output "function_output" { + value = provider::google-beta::region_from_zone("us-central1-b") +} +``` + +## Signature + +```text +region_from_zone(zone string) string +``` + +## Arguments + +1. `zone` (String) A string of a resource's zone From 6acc98eb172e07572a6a0d15647b9f9f03ce2d5a Mon Sep 17 00:00:00 2001 From: BBBmau Date: Thu, 29 Feb 2024 11:49:54 -0800 Subject: [PATCH 05/17] get region_from_zone_test.go passing --- .../terraform/functions/region_from_zone_test.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mmv1/third_party/terraform/functions/region_from_zone_test.go b/mmv1/third_party/terraform/functions/region_from_zone_test.go index a6fae7b80d92..6d73753a04e2 100644 --- a/mmv1/third_party/terraform/functions/region_from_zone_test.go +++ b/mmv1/third_party/terraform/functions/region_from_zone_test.go @@ -1,3 +1,5 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 package functions_test import ( @@ -13,6 +15,9 @@ import ( func TestAccProviderFunction_region_from_zone(t *testing.T) { t.Parallel() + projectZone := envvar.GetTestZoneFromEnv() + projectZoneRegex := regexp.MustCompile(fmt.Sprintf("^%s$", projectZone[:len(projectZone)-2])) + context := map[string]interface{}{ "function_name": "region_from_zone", "output_name": "zone", @@ -25,7 +30,7 @@ func TestAccProviderFunction_region_from_zone(t *testing.T) { { Config: testProviderFunction_get_region_from_zone(context), Check: resource.ComposeTestCheckFunc( - resource.TestMatchOutput(context["output_name"].(string), "us-central1"), + resource.TestMatchOutput(context["output_name"].(string), projectZoneRegex), ), }, }, @@ -60,7 +65,7 @@ resource "google_filestore_instance" "instance" { } output "%{output_name}" { - value = provider::google::%{function_name}(google_filestore_instance.default.location) + value = provider::google::%{function_name}(google_filestore_instance.instance.location) } `, context) } From e7207ef5dfda15508f6707e44daa823d9e47df94 Mon Sep 17 00:00:00 2001 From: BBBmau Date: Fri, 1 Mar 2024 00:17:45 -0800 Subject: [PATCH 06/17] fix region_from_zone_internal_test.go tests --- .../terraform/functions/region_from_zone.go | 22 +++++++++++++++---- .../region_from_zone_internal_test.go | 13 ++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/mmv1/third_party/terraform/functions/region_from_zone.go b/mmv1/third_party/terraform/functions/region_from_zone.go index fb363a6f5b84..19a5a0fd5898 100644 --- a/mmv1/third_party/terraform/functions/region_from_zone.go +++ b/mmv1/third_party/terraform/functions/region_from_zone.go @@ -1,3 +1,5 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 package functions import ( @@ -41,11 +43,23 @@ func (f RegionFromZoneFunction) Run(ctx context.Context, req function.RunRequest return } - // Validate input - can't be an empty string, every zone will have 2nd to last element in string be a `-` - if len(arg0) != 0 && arg0[len(arg0)-2] == '-' { - resp.Diagnostics.Append(resp.Result.Set(ctx, arg0[:len(arg0)-2])...) - } else { + if arg0 == "" { + resp.Diagnostics.AddArgumentError( + 0, + noMatchesErrorSummary, + "The input string is empty.", + ) return } + if arg0[len(arg0)-2] != '-' { + resp.Diagnostics.AddArgumentError( + 0, + noMatchesErrorSummary, + "The input string is invalid.", + ) + return + } + + resp.Diagnostics.Append(resp.Result.Set(ctx, arg0[:len(arg0)-2])...) } diff --git a/mmv1/third_party/terraform/functions/region_from_zone_internal_test.go b/mmv1/third_party/terraform/functions/region_from_zone_internal_test.go index eaa20016d112..884bf4857211 100644 --- a/mmv1/third_party/terraform/functions/region_from_zone_internal_test.go +++ b/mmv1/third_party/terraform/functions/region_from_zone_internal_test.go @@ -1,8 +1,9 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 package functions import ( "context" - "fmt" "testing" "github.com/google/go-cmp/cmp" @@ -30,9 +31,9 @@ func TestFunctionRun_region_from_zone(t *testing.T) { Result: function.NewResultData(types.StringValue(region)), }, }, - "it returns an error when given input that is empty": { + "it returns an error when given input is empty": { request: function.RunRequest{ - Arguments: function.NewArgumentsData([]attr.Value{types.StringNull()}), + Arguments: function.NewArgumentsData([]attr.Value{types.StringValue("")}), }, expected: function.RunResponse{ Result: function.NewResultData(types.StringNull()), @@ -40,12 +41,12 @@ func TestFunctionRun_region_from_zone(t *testing.T) { diag.NewArgumentErrorDiagnostic( 0, noMatchesErrorSummary, - fmt.Sprintf("The input string is empty."), + "The input string is empty.", ), }, }, }, - "it returns an error when given input that is not a zone": { + "it returns an error when given input is not a zone": { request: function.RunRequest{ Arguments: function.NewArgumentsData([]attr.Value{types.StringValue("foobar")}), }, @@ -55,7 +56,7 @@ func TestFunctionRun_region_from_zone(t *testing.T) { diag.NewArgumentErrorDiagnostic( 0, noMatchesErrorSummary, - fmt.Sprintf("The input string foobar is not a valid zone."), + "The input string is invalid.", ), }, }, From f56f7ecf07465d0126a3b4cac4d7ebc7837556a3 Mon Sep 17 00:00:00 2001 From: BBBmau Date: Fri, 1 Mar 2024 01:08:00 -0800 Subject: [PATCH 07/17] shorten test config setup time --- .../functions/region_from_zone_test.go | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/mmv1/third_party/terraform/functions/region_from_zone_test.go b/mmv1/third_party/terraform/functions/region_from_zone_test.go index 6d73753a04e2..b871ad76a664 100644 --- a/mmv1/third_party/terraform/functions/region_from_zone_test.go +++ b/mmv1/third_party/terraform/functions/region_from_zone_test.go @@ -19,9 +19,10 @@ func TestAccProviderFunction_region_from_zone(t *testing.T) { projectZoneRegex := regexp.MustCompile(fmt.Sprintf("^%s$", projectZone[:len(projectZone)-2])) context := map[string]interface{}{ - "function_name": "region_from_zone", - "output_name": "zone", - "resource_name": fmt.Sprintf("tf-test-region-from-zone-func-%s", acctest.RandString(t, 10)), + "function_name": "region_from_zone", + "output_name": "zone", + "resource_name": fmt.Sprintf("tf-test-region-from-zone-func-%s", acctest.RandString(t, 10)), + "resource_location": projectZone, } acctest.VcrTest(t, resource.TestCase{ @@ -48,24 +49,26 @@ terraform { } } -resource "google_filestore_instance" "instance" { - name = "%{resource_name}" - location = "us-central1-b" - tier = "BASIC_HDD" +resource "google_cloud_run_service" "default" { + name = "%{resource_name}" + location = "%{resource_location}" - file_shares { - capacity_gb = 1024 - name = "share1" + template { + spec { + containers { + image = "us-docker.pkg.dev/cloudrun/container/hello" + } + } } - networks { - network = "default" - modes = ["MODE_IPV4"] + traffic { + percent = 100 + latest_revision = true } } output "%{output_name}" { - value = provider::google::%{function_name}(google_filestore_instance.instance.location) + value = provider::google::%{function_name}(google_cloud_run_service.default.location) } `, context) } From 4e36837fb9166e120ff296da1910acbdd5cdda05 Mon Sep 17 00:00:00 2001 From: BBBmau Date: Fri, 1 Mar 2024 09:00:01 -0800 Subject: [PATCH 08/17] have more explicit error for invalid input --- mmv1/third_party/terraform/functions/region_from_zone.go | 3 ++- .../terraform/functions/region_from_zone_internal_test.go | 4 +--- mmv1/third_party/terraform/functions/region_from_zone_test.go | 2 -- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/mmv1/third_party/terraform/functions/region_from_zone.go b/mmv1/third_party/terraform/functions/region_from_zone.go index 19a5a0fd5898..b3fd0bacc401 100644 --- a/mmv1/third_party/terraform/functions/region_from_zone.go +++ b/mmv1/third_party/terraform/functions/region_from_zone.go @@ -4,6 +4,7 @@ package functions import ( "context" + "fmt" "github.com/hashicorp/terraform-plugin-framework/function" ) @@ -56,7 +57,7 @@ func (f RegionFromZoneFunction) Run(ctx context.Context, req function.RunRequest resp.Diagnostics.AddArgumentError( 0, noMatchesErrorSummary, - "The input string is invalid.", + fmt.Sprintf("The input string \"%s\" is not a valid zone name.", arg0), ) return } diff --git a/mmv1/third_party/terraform/functions/region_from_zone_internal_test.go b/mmv1/third_party/terraform/functions/region_from_zone_internal_test.go index 884bf4857211..c71398e2dd15 100644 --- a/mmv1/third_party/terraform/functions/region_from_zone_internal_test.go +++ b/mmv1/third_party/terraform/functions/region_from_zone_internal_test.go @@ -1,5 +1,3 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 package functions import ( @@ -56,7 +54,7 @@ func TestFunctionRun_region_from_zone(t *testing.T) { diag.NewArgumentErrorDiagnostic( 0, noMatchesErrorSummary, - "The input string is invalid.", + "The input string \"foobar\" is not a valid zone name.", ), }, }, diff --git a/mmv1/third_party/terraform/functions/region_from_zone_test.go b/mmv1/third_party/terraform/functions/region_from_zone_test.go index b871ad76a664..5944d24f2488 100644 --- a/mmv1/third_party/terraform/functions/region_from_zone_test.go +++ b/mmv1/third_party/terraform/functions/region_from_zone_test.go @@ -1,5 +1,3 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 package functions_test import ( From 828853112af115eb7d99bd5d0fdd8a6ab8b6a7db Mon Sep 17 00:00:00 2001 From: Mauricio Alvarez Leon <65101411+BBBmau@users.noreply.github.com> Date: Fri, 1 Mar 2024 09:00:34 -0800 Subject: [PATCH 09/17] Update mmv1/third_party/terraform/functions/region_from_zone.go Co-authored-by: Sarah French <15078782+SarahFrench@users.noreply.github.com> --- mmv1/third_party/terraform/functions/region_from_zone.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmv1/third_party/terraform/functions/region_from_zone.go b/mmv1/third_party/terraform/functions/region_from_zone.go index b3fd0bacc401..3cce5a9ce4ef 100644 --- a/mmv1/third_party/terraform/functions/region_from_zone.go +++ b/mmv1/third_party/terraform/functions/region_from_zone.go @@ -48,7 +48,7 @@ func (f RegionFromZoneFunction) Run(ctx context.Context, req function.RunRequest resp.Diagnostics.AddArgumentError( 0, noMatchesErrorSummary, - "The input string is empty.", + "The input string cannot be empty.", ) return } From 85d8e38180ffc2cd68a5d52969909a021470913b Mon Sep 17 00:00:00 2001 From: BBBmau Date: Fri, 1 Mar 2024 09:09:02 -0800 Subject: [PATCH 10/17] use compute_disk for better test time --- .../functions/region_from_zone_test.go | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/mmv1/third_party/terraform/functions/region_from_zone_test.go b/mmv1/third_party/terraform/functions/region_from_zone_test.go index 5944d24f2488..9f5bb74e0e0a 100644 --- a/mmv1/third_party/terraform/functions/region_from_zone_test.go +++ b/mmv1/third_party/terraform/functions/region_from_zone_test.go @@ -42,31 +42,24 @@ func testProviderFunction_get_region_from_zone(context map[string]interface{}) s terraform { required_providers { google = { - source = "hashicorp/google" + source = "hashicorp/google" } } } -resource "google_cloud_run_service" "default" { - name = "%{resource_name}" - location = "%{resource_location}" - - template { - spec { - containers { - image = "us-docker.pkg.dev/cloudrun/container/hello" - } - } - } - - traffic { - percent = 100 - latest_revision = true +resource "google_compute_disk" "default" { + name = "%{resource_name}" + type = "pd-ssd" + zone = "%{resource_location}" + image = "debian-11-bullseye-v20220719" + labels = { + environment = "dev" } + physical_block_size_bytes = 4096 } output "%{output_name}" { - value = provider::google::%{function_name}(google_cloud_run_service.default.location) + value = provider::google::%{function_name}(google_compute_disk.default.zone) } `, context) } From 48d7f3f609bec9483f8fb928698d2769f1e38732 Mon Sep 17 00:00:00 2001 From: Mauricio Alvarez Leon <65101411+BBBmau@users.noreply.github.com> Date: Thu, 7 Mar 2024 08:48:28 -0800 Subject: [PATCH 11/17] Update mmv1/third_party/terraform/website/docs/functions/region_from_zone.html.markdown Co-authored-by: Sarah French <15078782+SarahFrench@users.noreply.github.com> --- .../website/docs/functions/region_from_zone.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmv1/third_party/terraform/website/docs/functions/region_from_zone.html.markdown b/mmv1/third_party/terraform/website/docs/functions/region_from_zone.html.markdown index 7a115521a50b..2fc328f7580c 100644 --- a/mmv1/third_party/terraform/website/docs/functions/region_from_zone.html.markdown +++ b/mmv1/third_party/terraform/website/docs/functions/region_from_zone.html.markdown @@ -6,7 +6,7 @@ description: |- # Function: region_from_zone -Returns the region within a provided resource's zone. +Returns a region name derived from a provided zone. For more information about using provider-defined functions with Terraform [see the official documentation](https://developer.hashicorp.com/terraform/plugin/framework/functions/concepts). From 017af5b7a058f4541dd9c9540f06ebf5c4c0b096 Mon Sep 17 00:00:00 2001 From: Sarah French <15078782+SarahFrench@users.noreply.github.com> Date: Fri, 8 Mar 2024 14:14:57 +0000 Subject: [PATCH 12/17] Update mmv1/third_party/terraform/functions/region_from_zone_test.go --- mmv1/third_party/terraform/functions/region_from_zone_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mmv1/third_party/terraform/functions/region_from_zone_test.go b/mmv1/third_party/terraform/functions/region_from_zone_test.go index 9f5bb74e0e0a..e4d88dd9d581 100644 --- a/mmv1/third_party/terraform/functions/region_from_zone_test.go +++ b/mmv1/third_party/terraform/functions/region_from_zone_test.go @@ -12,7 +12,8 @@ import ( func TestAccProviderFunction_region_from_zone(t *testing.T) { t.Parallel() - + // Skipping due to requiring TF 1.8.0 in VCR systems : https://github.com/hashicorp/terraform-provider-google/issues/17451 + acctest.SkipIfVcr(t) projectZone := envvar.GetTestZoneFromEnv() projectZoneRegex := regexp.MustCompile(fmt.Sprintf("^%s$", projectZone[:len(projectZone)-2])) From 23764b5e6d5c1a6e0a9bdb2ee42ec6b5c223916e Mon Sep 17 00:00:00 2001 From: Sarah French <15078782+SarahFrench@users.noreply.github.com> Date: Fri, 1 Mar 2024 12:56:45 +0000 Subject: [PATCH 13/17] Add `location_from_id` provider-defined function (#10061) * Add `location_from_id` function, tests, docs * Fix whitespace in acc test HCL config --- mmv1/third_party/terraform/fwprovider/framework_provider.go.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/mmv1/third_party/terraform/fwprovider/framework_provider.go.erb b/mmv1/third_party/terraform/fwprovider/framework_provider.go.erb index b435abbca471..88c54dbc29e4 100644 --- a/mmv1/third_party/terraform/fwprovider/framework_provider.go.erb +++ b/mmv1/third_party/terraform/fwprovider/framework_provider.go.erb @@ -305,5 +305,6 @@ func (p *FrameworkProvider) Functions(_ context.Context) []func() function.Funct return []func() function.Function{ functions.NewProjectFromIdFunction, functions.NewRegionFromZoneFunction, + functions.NewLocationFromIdFunction, } } \ No newline at end of file From 0a793c9fcad0e57add1c504c73fc467a15609b3c Mon Sep 17 00:00:00 2001 From: BBBmau Date: Fri, 8 Mar 2024 12:00:17 -0800 Subject: [PATCH 14/17] fix runtime error and update internal test output --- .../terraform/functions/region_from_zone_internal_test.go | 2 +- .../third_party/terraform/functions/region_from_zone_test.go | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/mmv1/third_party/terraform/functions/region_from_zone_internal_test.go b/mmv1/third_party/terraform/functions/region_from_zone_internal_test.go index c71398e2dd15..bd506ae679d6 100644 --- a/mmv1/third_party/terraform/functions/region_from_zone_internal_test.go +++ b/mmv1/third_party/terraform/functions/region_from_zone_internal_test.go @@ -39,7 +39,7 @@ func TestFunctionRun_region_from_zone(t *testing.T) { diag.NewArgumentErrorDiagnostic( 0, noMatchesErrorSummary, - "The input string is empty.", + "The input string cannot be empty.", ), }, }, diff --git a/mmv1/third_party/terraform/functions/region_from_zone_test.go b/mmv1/third_party/terraform/functions/region_from_zone_test.go index e4d88dd9d581..6d516b51f4fb 100644 --- a/mmv1/third_party/terraform/functions/region_from_zone_test.go +++ b/mmv1/third_party/terraform/functions/region_from_zone_test.go @@ -15,7 +15,10 @@ func TestAccProviderFunction_region_from_zone(t *testing.T) { // Skipping due to requiring TF 1.8.0 in VCR systems : https://github.com/hashicorp/terraform-provider-google/issues/17451 acctest.SkipIfVcr(t) projectZone := envvar.GetTestZoneFromEnv() - projectZoneRegex := regexp.MustCompile(fmt.Sprintf("^%s$", projectZone[:len(projectZone)-2])) + if projectZone != "" { + projectZone = projectZone[:len(projectZone)-2] + } + projectZoneRegex := regexp.MustCompile(fmt.Sprintf("^%s$", projectZone)) context := map[string]interface{}{ "function_name": "region_from_zone", From 34b513585827ea70aece247dba0414ff13364dc0 Mon Sep 17 00:00:00 2001 From: Mauricio Alvarez Leon <65101411+BBBmau@users.noreply.github.com> Date: Mon, 11 Mar 2024 08:53:47 -0700 Subject: [PATCH 15/17] Update mmv1/third_party/terraform/functions/region_from_zone_test.go Co-authored-by: Sarah French <15078782+SarahFrench@users.noreply.github.com> --- .../terraform/functions/region_from_zone_test.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/mmv1/third_party/terraform/functions/region_from_zone_test.go b/mmv1/third_party/terraform/functions/region_from_zone_test.go index 6d516b51f4fb..fb2d9c3a29ca 100644 --- a/mmv1/third_party/terraform/functions/region_from_zone_test.go +++ b/mmv1/third_party/terraform/functions/region_from_zone_test.go @@ -14,11 +14,9 @@ func TestAccProviderFunction_region_from_zone(t *testing.T) { t.Parallel() // Skipping due to requiring TF 1.8.0 in VCR systems : https://github.com/hashicorp/terraform-provider-google/issues/17451 acctest.SkipIfVcr(t) - projectZone := envvar.GetTestZoneFromEnv() - if projectZone != "" { - projectZone = projectZone[:len(projectZone)-2] - } - projectZoneRegex := regexp.MustCompile(fmt.Sprintf("^%s$", projectZone)) + projectZone := "us-central1-a" + projectRegion := "us-central1" + projectRegionRegex := regexp.MustCompile(fmt.Sprintf("^%s$", projectRegion)) context := map[string]interface{}{ "function_name": "region_from_zone", From 607f0627ab8a8e569c8a5c672aafaecb3c76401a Mon Sep 17 00:00:00 2001 From: BBBmau Date: Mon, 11 Mar 2024 08:59:38 -0700 Subject: [PATCH 16/17] update to use projectRegionRegex --- mmv1/third_party/terraform/functions/region_from_zone_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mmv1/third_party/terraform/functions/region_from_zone_test.go b/mmv1/third_party/terraform/functions/region_from_zone_test.go index fb2d9c3a29ca..68c001ada13a 100644 --- a/mmv1/third_party/terraform/functions/region_from_zone_test.go +++ b/mmv1/third_party/terraform/functions/region_from_zone_test.go @@ -7,7 +7,6 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-provider-google/google/acctest" - "github.com/hashicorp/terraform-provider-google/google/envvar" ) func TestAccProviderFunction_region_from_zone(t *testing.T) { @@ -31,7 +30,7 @@ func TestAccProviderFunction_region_from_zone(t *testing.T) { { Config: testProviderFunction_get_region_from_zone(context), Check: resource.ComposeTestCheckFunc( - resource.TestMatchOutput(context["output_name"].(string), projectZoneRegex), + resource.TestMatchOutput(context["output_name"].(string), projectRegionRegex), ), }, }, From 032fe7e804845c109c52af7740a8f88406c8cc33 Mon Sep 17 00:00:00 2001 From: Sarah French <15078782+SarahFrench@users.noreply.github.com> Date: Mon, 11 Mar 2024 19:28:33 +0000 Subject: [PATCH 17/17] Update mmv1/third_party/terraform/functions/region_from_zone.go --- mmv1/third_party/terraform/functions/region_from_zone.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/mmv1/third_party/terraform/functions/region_from_zone.go b/mmv1/third_party/terraform/functions/region_from_zone.go index 3cce5a9ce4ef..61a1addbdd3a 100644 --- a/mmv1/third_party/terraform/functions/region_from_zone.go +++ b/mmv1/third_party/terraform/functions/region_from_zone.go @@ -1,5 +1,3 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 package functions import (