Skip to content

Commit

Permalink
Make region from zone functions support more zone names (#10714)
Browse files Browse the repository at this point in the history
  • Loading branch information
trodge committed Jun 24, 2024
1 parent 4b0dfcc commit cf92c50
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
18 changes: 10 additions & 8 deletions mmv1/third_party/terraform/functions/region_from_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package functions
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/function"
)
Expand Down Expand Up @@ -35,23 +36,24 @@ func (f RegionFromZoneFunction) Definition(ctx context.Context, req function.Def

func (f RegionFromZoneFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
// Load arguments from function call
var arg0 string
resp.Error = function.ConcatFuncErrors(req.Arguments.GetArgument(ctx, 0, &arg0))
var zone string
resp.Error = function.ConcatFuncErrors(req.Arguments.GetArgument(ctx, 0, &zone))
if resp.Error != nil {
return
}

if arg0 == "" {
if zone == "" {
err := function.NewArgumentFuncError(0, "The input string cannot be empty.")
resp.Error = function.ConcatFuncErrors(err)
return
}

if arg0[len(arg0)-2] != '-' {
err := function.NewArgumentFuncError(0, fmt.Sprintf("The input string \"%s\" is not a valid zone name.", arg0))
zoneParts := strings.Split(zone, "-")

if len(zoneParts) < 3 {
err := function.NewArgumentFuncError(0, fmt.Sprintf("The input string \"%s\" is not a valid zone name.", zone))
resp.Error = function.ConcatFuncErrors(err)
return
} else {
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, strings.Join(zoneParts[:len(zoneParts)-1], "-")))
}

resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, arg0[:len(arg0)-2]))
}
11 changes: 5 additions & 6 deletions mmv1/third_party/terraform/tpgresource/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,14 @@ type TerraformResourceDiff interface {
// Contains functions that don't really belong anywhere else.

// GetRegionFromZone returns the region from a zone for Google cloud.
// This is by removing the last two chars from the zone name to leave the region
// If there aren't enough characters in the input string, an empty string is returned
// This is by removing the characters after the last '-'.
// e.g. southamerica-west1-a => southamerica-west1
func GetRegionFromZone(zone string) string {
if zone != "" && len(zone) > 2 {
region := zone[:len(zone)-2]
return region
zoneParts := strings.Split(zone, "-")
if len(zoneParts) < 3 {
return ""
}
return ""
return strings.Join(zoneParts[:len(zoneParts)-1], "-")
}

// Infers the region based on the following (in order of priority):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ provider "google" {

resource "google_pubsub_lite_topic" "example" {
name = "example-topic"
zone = "us-central1a"
zone = "us-central1-a"

partition_config {
count = 1
Expand Down

0 comments on commit cf92c50

Please sign in to comment.