fix(dns): store IDs immediately after provisioning (#1022)

relates to STACKITTPR-373
This commit is contained in:
Ruben Hönle 2025-10-15 11:16:30 +02:00 committed by GitHub
parent f0433984f4
commit 4103c33fd2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 133 additions and 18 deletions

View file

@ -7,6 +7,8 @@ import (
"regexp"
"strings"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
@ -186,3 +188,11 @@ func CheckListRemoval(ctx context.Context, configModelList, planModelList types.
}
}
}
// SetAndLogStateFields writes the given map of key-value pairs to the state
func SetAndLogStateFields(ctx context.Context, diags *diag.Diagnostics, state *tfsdk.State, values map[string]any) {
for key, val := range values {
ctx = tflog.SetField(ctx, key, val)
diags.Append(state.SetAttribute(ctx, path.Root(key), val)...)
}
}

View file

@ -6,6 +6,9 @@ import (
"reflect"
"testing"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
@ -551,3 +554,92 @@ func TestCheckListRemoval(t *testing.T) {
})
}
}
func TestSetAndLogStateFields(t *testing.T) {
testSchema := schema.Schema{
Attributes: map[string]schema.Attribute{
"project_id": schema.StringAttribute{},
"instance_id": schema.StringAttribute{},
},
}
type args struct {
diags *diag.Diagnostics
state *tfsdk.State
values map[string]interface{}
}
type want struct {
hasError bool
state *tfsdk.State
}
tests := []struct {
name string
args args
want want
}{
{
name: "empty map",
args: args{
diags: &diag.Diagnostics{},
state: &tfsdk.State{},
values: map[string]interface{}{},
},
want: want{
hasError: false,
state: &tfsdk.State{},
},
},
{
name: "base",
args: args{
diags: &diag.Diagnostics{},
state: func() *tfsdk.State {
ctx := context.Background()
state := tfsdk.State{
Raw: tftypes.NewValue(testSchema.Type().TerraformType(ctx), map[string]tftypes.Value{
"project_id": tftypes.NewValue(tftypes.String, "9b15d120-86f8-45f5-81d8-a554f09c7582"),
"instance_id": tftypes.NewValue(tftypes.String, nil),
}),
Schema: testSchema,
}
return &state
}(),
values: map[string]interface{}{
"project_id": "a414f971-3f7a-4e9a-8671-51a8acb7bcc8",
"instance_id": "97073250-8cad-46c3-8424-6258ac0b3731",
},
},
want: want{
hasError: false,
state: func() *tfsdk.State {
ctx := context.Background()
state := tfsdk.State{
Raw: tftypes.NewValue(testSchema.Type().TerraformType(ctx), map[string]tftypes.Value{
"project_id": tftypes.NewValue(tftypes.String, nil),
"instance_id": tftypes.NewValue(tftypes.String, nil),
}),
Schema: testSchema,
}
state.SetAttribute(ctx, path.Root("project_id"), "a414f971-3f7a-4e9a-8671-51a8acb7bcc8")
state.SetAttribute(ctx, path.Root("instance_id"), "97073250-8cad-46c3-8424-6258ac0b3731")
return &state
}(),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
SetAndLogStateFields(ctx, tt.args.diags, tt.args.state, tt.args.values)
if tt.args.diags.HasError() != tt.want.hasError {
t.Errorf("TestSetAndLogStateFields() error count = %v, hasErr %v", tt.args.diags.ErrorsCount(), tt.want.hasError)
}
diff := cmp.Diff(tt.args.state, tt.want.state)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
})
}
}