chore(terraform): refactor region overrides in mapping funcs (#872)

This commit is contained in:
Ruben Hönle 2025-06-06 16:17:02 +02:00 committed by GitHub
parent 0272a86292
commit 5e5404b459
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
31 changed files with 185 additions and 194 deletions

View file

@ -6,6 +6,8 @@ import (
"net/http"
"strings"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
@ -61,6 +63,13 @@ func (pd *ProviderData) GetRegion() string {
return "eu01"
}
func (pd *ProviderData) GetRegionWithOverride(overrideRegion types.String) string {
if overrideRegion.IsUnknown() || overrideRegion.IsNull() {
return pd.GetRegion()
}
return overrideRegion.ValueString()
}
// DiagsToError Converts TF diagnostics' errors into an error with a human-readable description.
// If there are no errors, the output is nil
func DiagsToError(diags diag.Diagnostics) error {

View file

@ -0,0 +1,100 @@
package core
import (
"testing"
"github.com/hashicorp/terraform-plugin-framework/types"
)
func TestProviderData_GetRegionWithOverride(t *testing.T) {
type args struct {
overrideRegion types.String
}
tests := []struct {
name string
providerData *ProviderData
args args
want string
}{
{
name: "override region is null string",
providerData: &ProviderData{
DefaultRegion: "eu02",
},
args: args{
types.StringNull(),
},
want: "eu02",
},
{
name: "override region is unknown string",
providerData: &ProviderData{
DefaultRegion: "eu02",
},
args: args{
types.StringUnknown(),
},
want: "eu02",
},
{
name: "override region is set",
providerData: &ProviderData{
DefaultRegion: "eu02",
},
args: args{
types.StringValue("eu01"),
},
want: "eu01",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.providerData.GetRegionWithOverride(tt.args.overrideRegion); got != tt.want {
t.Errorf("GetRegionWithOverride() = %v, want %v", got, tt.want)
}
})
}
}
func TestProviderData_GetRegion(t *testing.T) {
tests := []struct {
name string
providerData *ProviderData
want string
}{
{
name: "default region is set",
providerData: &ProviderData{
DefaultRegion: "eu02",
},
want: "eu02",
},
{
name: "(legacy) region is set",
providerData: &ProviderData{
Region: "eu02",
},
want: "eu02",
},
{
name: "default region wins over (legacy) region",
providerData: &ProviderData{
DefaultRegion: "eu02",
Region: "eu01",
},
want: "eu02",
},
{
name: "final fallback - neither region (legacy) nor default region is set",
providerData: &ProviderData{},
want: "eu01",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.providerData.GetRegion(); got != tt.want {
t.Errorf("GetRegion() = %v, want %v", got, tt.want)
}
})
}
}