Feat/stackittpr 20 region adjustments | tfp (migrate first service to new regions concept) (#664)
* feat: completed bucket and credential group * feat: fix linter warnings * feat: updated documentation * feat: updated to current version of the regional api * feat: implement review findings * feat: implement further review findings * fix: make sure region is stored for the data-source in the state
This commit is contained in:
parent
c4e25f560b
commit
2923621ab0
22 changed files with 503 additions and 104 deletions
39
stackit/internal/utils/regions.go
Normal file
39
stackit/internal/utils/regions.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/path"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||
)
|
||||
|
||||
// AdaptRegion rewrites the region of a terraform plan
|
||||
func AdaptRegion(ctx context.Context, configRegion types.String, planRegion *types.String, defaultRegion string, resp *resource.ModifyPlanResponse) {
|
||||
// Get the intended region. This is either set directly set in the individual
|
||||
// config or the provider region has to be used
|
||||
var intendedRegion types.String
|
||||
if configRegion.IsNull() {
|
||||
if defaultRegion == "" {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "set region", "no region defined in config or provider")
|
||||
return
|
||||
}
|
||||
intendedRegion = types.StringValue(defaultRegion)
|
||||
} else {
|
||||
intendedRegion = configRegion
|
||||
}
|
||||
|
||||
// check if the currently configured region corresponds to the planned region
|
||||
// on mismatch override the planned region with the intended region
|
||||
// and force a replace of the resource
|
||||
p := path.Root("region")
|
||||
if !intendedRegion.Equal(*planRegion) {
|
||||
resp.RequiresReplace.Append(p)
|
||||
*planRegion = intendedRegion
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Plan.SetAttribute(ctx, p, *planRegion)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
}
|
||||
87
stackit/internal/utils/regions_test.go
Normal file
87
stackit/internal/utils/regions_test.go
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
func TestAdaptRegion(t *testing.T) {
|
||||
type model struct {
|
||||
Region types.String `tfsdk:"region"`
|
||||
}
|
||||
type args struct {
|
||||
configRegion types.String
|
||||
defaultRegion string
|
||||
}
|
||||
testcases := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
wantRegion types.String
|
||||
}{
|
||||
{
|
||||
"no configured region, use provider region",
|
||||
args{
|
||||
types.StringNull(),
|
||||
"eu01",
|
||||
},
|
||||
false,
|
||||
types.StringValue("eu01"),
|
||||
},
|
||||
{
|
||||
"no configured region, no provider region => want error",
|
||||
args{
|
||||
types.StringNull(),
|
||||
"",
|
||||
},
|
||||
true,
|
||||
types.StringNull(),
|
||||
},
|
||||
{
|
||||
"configuration region overrides provider region",
|
||||
args{
|
||||
types.StringValue("eu01-m"),
|
||||
"eu01",
|
||||
},
|
||||
false,
|
||||
types.StringValue("eu01-m"),
|
||||
},
|
||||
}
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
plan := tfsdk.Plan{
|
||||
Schema: schema.Schema{
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"region": schema.StringAttribute{
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if diags := plan.Set(context.Background(), model{types.StringValue("unknown")}); diags.HasError() {
|
||||
t.Fatalf("cannot create test model: %v", diags)
|
||||
}
|
||||
resp := resource.ModifyPlanResponse{
|
||||
Plan: plan,
|
||||
}
|
||||
|
||||
configModel := model{
|
||||
Region: tc.args.configRegion,
|
||||
}
|
||||
planModel := model{}
|
||||
AdaptRegion(context.Background(), configModel.Region, &planModel.Region, tc.args.defaultRegion, &resp)
|
||||
if diags := resp.Diagnostics; tc.wantErr != diags.HasError() {
|
||||
t.Errorf("unexpected diagnostics: want err: %v, actual %v", tc.wantErr, diags.Errors())
|
||||
}
|
||||
if expected, actual := tc.wantRegion, planModel.Region; !expected.Equal(actual) {
|
||||
t.Errorf("wrong result region. expect %s but got %s", expected, actual)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -104,3 +104,13 @@ func QuoteValues(values []string) []string {
|
|||
func IsLegacyProjectRole(role string) bool {
|
||||
return utils.Contains(LegacyProjectRoles, role)
|
||||
}
|
||||
|
||||
type value interface {
|
||||
IsUnknown() bool
|
||||
IsNull() bool
|
||||
}
|
||||
|
||||
// IsUndefined checks if a passed value is unknown or null
|
||||
func IsUndefined(val value) bool {
|
||||
return val.IsUnknown() || val.IsNull()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue