fix: try fix errors #71

Merged
marcel.henselin merged 1 commit from fix/sort_fixes_and_test_fixes into alpha 2026-02-16 13:40:06 +00:00
14 changed files with 140 additions and 40 deletions

View file

@ -593,7 +593,7 @@ func (r *instanceResource) ImportState(
ctx, &resp.Diagnostics, ctx, &resp.Diagnostics,
"Error importing instance", "Error importing instance",
fmt.Sprintf( fmt.Sprintf(
"Expected import identifier with format: [project_id],[region],[instance_id] Got: %q", "Expected import identifier with format [project_id],[region],[instance_id] Got: %q",
req.ID, req.ID,
), ),
) )

View file

@ -48,7 +48,7 @@ resource "stackitprivatepreview_postgresflexalpha_database" "{{ $db.Name }}" {
project_id = "{{ $db.ProjectId }}" project_id = "{{ $db.ProjectId }}"
instance_id = stackitprivatepreview_postgresflexalpha_instance.{{ $tfName }}.instance_id instance_id = stackitprivatepreview_postgresflexalpha_instance.{{ $tfName }}.instance_id
name = "{{ $db.Name }}" name = "{{ $db.Name }}"
owner = "{{ $db.Owner }}" owner = stackitprivatepreview_postgresflexalpha_user.{{ $db.Owner }}.name
} }
{{ end }} {{ end }}
{{ end }} {{ end }}

View file

@ -548,7 +548,7 @@ func (r *databaseResource) ImportState(
ctx, &resp.Diagnostics, ctx, &resp.Diagnostics,
"Error importing database", "Error importing database",
fmt.Sprintf( fmt.Sprintf(
"Expected import identifier with format: [project_id],[region],[instance_id],[database_name] Got: %q", "Expected import identifier with format [project_id],[region],[instance_id],[database_name] Got: %q",
req.ID, req.ID,
), ),
) )

View file

@ -523,7 +523,7 @@ func (r *instanceResource) ImportState(
ctx, &resp.Diagnostics, ctx, &resp.Diagnostics,
"Error importing instance", "Error importing instance",
fmt.Sprintf( fmt.Sprintf(
"Expected import identifier with format: [project_id],[region],[instance_id] Got: %q", "Expected import identifier with format [project_id],[region],[instance_id] Got: %q",
req.ID, req.ID,
), ),
) )

View file

@ -63,9 +63,9 @@ func TestMapDataSourceFields(t *testing.T) {
Roles: types.List( Roles: types.List(
types.SetValueMust( types.SetValueMust(
types.StringType, []attr.Value{ types.StringType, []attr.Value{
types.StringValue(""),
types.StringValue("role_1"), types.StringValue("role_1"),
types.StringValue("role_2"), types.StringValue("role_2"),
types.StringValue(""),
}, },
), ),
), ),
@ -138,7 +138,7 @@ func TestMapDataSourceFields(t *testing.T) {
t.Fatalf("Should not have failed: %v", err) t.Fatalf("Should not have failed: %v", err)
} }
if tt.isValid { if tt.isValid {
diff := cmp.Diff(state, &tt.expected) diff := cmp.Diff(&tt.expected, state)
if diff != "" { if diff != "" {
t.Fatalf("Data does not match: %s", diff) t.Fatalf("Data does not match: %s", diff)
} }
@ -183,8 +183,8 @@ func TestMapFieldsCreate(t *testing.T) {
&sqlserverflexalpha.CreateUserResponse{ &sqlserverflexalpha.CreateUserResponse{
Id: utils.Ptr(int64(2)), Id: utils.Ptr(int64(2)),
Roles: &[]string{ Roles: &[]string{
"role_1",
"role_2", "role_2",
"role_1",
"", "",
}, },
Username: utils.Ptr("username"), Username: utils.Ptr("username"),
@ -204,9 +204,9 @@ func TestMapFieldsCreate(t *testing.T) {
Roles: types.List( Roles: types.List(
types.SetValueMust( types.SetValueMust(
types.StringType, []attr.Value{ types.StringType, []attr.Value{
types.StringValue(""),
types.StringValue("role_1"), types.StringValue("role_1"),
types.StringValue("role_2"), types.StringValue("role_2"),
types.StringValue(""),
}, },
), ),
), ),
@ -292,7 +292,7 @@ func TestMapFieldsCreate(t *testing.T) {
t.Fatalf("Should not have failed: %v", err) t.Fatalf("Should not have failed: %v", err)
} }
if tt.isValid { if tt.isValid {
diff := cmp.Diff(state, &tt.expected) diff := cmp.Diff(&tt.expected, state)
if diff != "" { if diff != "" {
t.Fatalf("Data does not match: %s", diff) t.Fatalf("Data does not match: %s", diff)
} }
@ -332,8 +332,8 @@ func TestMapFields(t *testing.T) {
"simple_values", "simple_values",
&sqlserverflexalpha.GetUserResponse{ &sqlserverflexalpha.GetUserResponse{
Roles: &[]string{ Roles: &[]string{
"role_1",
"role_2", "role_2",
"role_1",
"", "",
}, },
Username: utils.Ptr("username"), Username: utils.Ptr("username"),
@ -350,9 +350,9 @@ func TestMapFields(t *testing.T) {
Roles: types.List( Roles: types.List(
types.SetValueMust( types.SetValueMust(
types.StringType, []attr.Value{ types.StringType, []attr.Value{
types.StringValue(""),
types.StringValue("role_1"), types.StringValue("role_1"),
types.StringValue("role_2"), types.StringValue("role_2"),
types.StringValue(""),
}, },
), ),
), ),
@ -423,7 +423,7 @@ func TestMapFields(t *testing.T) {
t.Fatalf("Should not have failed: %v", err) t.Fatalf("Should not have failed: %v", err)
} }
if tt.isValid { if tt.isValid {
diff := cmp.Diff(state, &tt.expected) diff := cmp.Diff(&tt.expected, state)
if diff != "" { if diff != "" {
t.Fatalf("Data does not match: %s", diff) t.Fatalf("Data does not match: %s", diff)
} }
@ -516,7 +516,7 @@ func TestToCreatePayload(t *testing.T) {
t.Fatalf("Should not have failed: %v", err) t.Fatalf("Should not have failed: %v", err)
} }
if tt.isValid { if tt.isValid {
diff := cmp.Diff(output, tt.expected) diff := cmp.Diff(tt.expected, output)
if diff != "" { if diff != "" {
t.Fatalf("Data does not match: %s", diff) t.Fatalf("Data does not match: %s", diff)
} }

View file

@ -548,7 +548,7 @@ func (r *databaseResource) ImportState(
ctx, &resp.Diagnostics, ctx, &resp.Diagnostics,
"Error importing database", "Error importing database",
fmt.Sprintf( fmt.Sprintf(
"Expected import identifier with format: [project_id],[region],[instance_id],[database_name] Got: %q", "Expected import identifier with format [project_id],[region],[instance_id],[database_name] Got: %q",
req.ID, req.ID,
), ),
) )

View file

@ -293,13 +293,6 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
return return
} }
// Read identity data
var identityData InstanceResourceIdentityModel
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
if resp.Diagnostics.HasError() {
return
}
ctx = core.InitProviderContext(ctx) ctx = core.InitProviderContext(ctx)
projectId := data.ProjectId.ValueString() projectId := data.ProjectId.ValueString()
@ -523,7 +516,7 @@ func (r *instanceResource) ImportState(
ctx, &resp.Diagnostics, ctx, &resp.Diagnostics,
"Error importing instance", "Error importing instance",
fmt.Sprintf( fmt.Sprintf(
"Expected import identifier with format: [project_id],[region],[instance_id] Got: %q", "Expected import identifier with format [project_id],[region],[instance_id] Got: %q",
req.ID, req.ID,
), ),
) )

View file

@ -15,7 +15,7 @@ import (
"github.com/stackitcloud/stackit-sdk-go/core/config" "github.com/stackitcloud/stackit-sdk-go/core/config"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/internal/testutils" "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/internal/testutils"
sqlserverflexbeta2 "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta" sqlserverflexbetaResGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
sqlserverflexbeta "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/instance" sqlserverflexbeta "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/instance"
// The fwresource import alias is so there is no collision // The fwresource import alias is so there is no collision
@ -36,7 +36,7 @@ func init() {
F: func(region string) error { F: func(region string) error {
ctx := context.Background() ctx := context.Background()
apiClientConfigOptions := []config.ConfigurationOption{} apiClientConfigOptions := []config.ConfigurationOption{}
apiClient, err := sqlserverflexbeta2.NewAPIClient(apiClientConfigOptions...) apiClient, err := sqlserverflexbetaResGen.NewAPIClient(apiClientConfigOptions...)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@ -228,6 +228,67 @@ func TestAccInstance(t *testing.T) {
}) })
} }
func TestAccInstanceReApply(t *testing.T) {
exData := getExample()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
t.Logf(" ... working on instance %s", exData.TfName)
testInstances = append(testInstances, exData.TfName)
},
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create and verify
{
Config: testutils.StringFromTemplateMust(
"testdata/instance_template.gompl",
exData,
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resName("instance", exData.TfName), "name", exData.Name),
resource.TestCheckResourceAttrSet(resName("instance", exData.TfName), "id"),
// TODO: check all fields
),
},
// Create and verify
{
Config: testutils.StringFromTemplateMust(
"testdata/instance_template.gompl",
exData,
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resName("instance", exData.TfName), "name", exData.Name),
resource.TestCheckResourceAttrSet(resName("instance", exData.TfName), "id"),
// TODO: check all fields
),
},
{
RefreshState: true,
},
// Create and verify
{
Config: testutils.StringFromTemplateMust(
"testdata/instance_template.gompl",
exData,
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resName("instance", exData.TfName), "name", exData.Name),
resource.TestCheckResourceAttrSet(resName("instance", exData.TfName), "id"),
// TODO: check all fields
),
},
// Import test
{
ResourceName: resName("instance", exData.TfName),
ImportStateKind: resource.ImportBlockWithResourceIdentity,
ImportState: true,
// ImportStateVerify is not supported with plannable import blocks
// ImportStateVerify: true,
},
},
})
}
func TestAccInstanceNoEncryption(t *testing.T) { func TestAccInstanceNoEncryption(t *testing.T) {
data := getExample() data := getExample()

View file

@ -46,7 +46,9 @@ func mapDataSourceFields(userResp *sqlserverflexbeta.GetUserResponse, model *dat
model.Roles = types.List(types.SetNull(types.StringType)) model.Roles = types.List(types.SetNull(types.StringType))
} else { } else {
var roles []attr.Value var roles []attr.Value
for _, role := range *user.Roles { resRoles := *user.Roles
slices.Sort(resRoles)
for _, role := range resRoles {
roles = append(roles, types.StringValue(string(role))) roles = append(roles, types.StringValue(string(role)))
} }
rolesSet, diags := types.SetValue(types.StringType, roles) rolesSet, diags := types.SetValue(types.StringType, roles)
@ -183,9 +185,14 @@ func toCreatePayload(
return nil, fmt.Errorf("nil model") return nil, fmt.Errorf("nil model")
} }
return &sqlserverflexbeta.CreateUserRequestPayload{ pl := sqlserverflexbeta.CreateUserRequestPayload{
Username: conversion.StringValueToPointer(model.Username), Username: conversion.StringValueToPointer(model.Username),
DefaultDatabase: conversion.StringValueToPointer(model.DefaultDatabase), Roles: &roles,
Roles: &roles, }
}, nil slices.Sort(roles)
if !model.DefaultDatabase.IsNull() || !model.DefaultDatabase.IsUnknown() {
pl.DefaultDatabase = conversion.StringValueToPointer(model.DefaultDatabase)
}
return &pl, nil
} }

View file

@ -63,9 +63,9 @@ func TestMapDataSourceFields(t *testing.T) {
Roles: types.List( Roles: types.List(
types.SetValueMust( types.SetValueMust(
types.StringType, []attr.Value{ types.StringType, []attr.Value{
types.StringValue(""),
types.StringValue("role_1"), types.StringValue("role_1"),
types.StringValue("role_2"), types.StringValue("role_2"),
types.StringValue(""),
}, },
), ),
), ),
@ -138,7 +138,7 @@ func TestMapDataSourceFields(t *testing.T) {
t.Fatalf("Should not have failed: %v", err) t.Fatalf("Should not have failed: %v", err)
} }
if tt.isValid { if tt.isValid {
diff := cmp.Diff(state, &tt.expected) diff := cmp.Diff(&tt.expected, state)
if diff != "" { if diff != "" {
t.Fatalf("Data does not match: %s", diff) t.Fatalf("Data does not match: %s", diff)
} }
@ -204,9 +204,9 @@ func TestMapFieldsCreate(t *testing.T) {
Roles: types.List( Roles: types.List(
types.SetValueMust( types.SetValueMust(
types.StringType, []attr.Value{ types.StringType, []attr.Value{
types.StringValue(""),
types.StringValue("role_1"), types.StringValue("role_1"),
types.StringValue("role_2"), types.StringValue("role_2"),
types.StringValue(""),
}, },
), ),
), ),
@ -292,7 +292,7 @@ func TestMapFieldsCreate(t *testing.T) {
t.Fatalf("Should not have failed: %v", err) t.Fatalf("Should not have failed: %v", err)
} }
if tt.isValid { if tt.isValid {
diff := cmp.Diff(state, &tt.expected) diff := cmp.Diff(&tt.expected, state)
if diff != "" { if diff != "" {
t.Fatalf("Data does not match: %s", diff) t.Fatalf("Data does not match: %s", diff)
} }
@ -332,8 +332,8 @@ func TestMapFields(t *testing.T) {
"simple_values", "simple_values",
&sqlserverflexbeta.GetUserResponse{ &sqlserverflexbeta.GetUserResponse{
Roles: &[]string{ Roles: &[]string{
"role_1",
"role_2", "role_2",
"role_1",
"", "",
}, },
Username: utils.Ptr("username"), Username: utils.Ptr("username"),
@ -350,9 +350,9 @@ func TestMapFields(t *testing.T) {
Roles: types.List( Roles: types.List(
types.SetValueMust( types.SetValueMust(
types.StringType, []attr.Value{ types.StringType, []attr.Value{
types.StringValue(""),
types.StringValue("role_1"), types.StringValue("role_1"),
types.StringValue("role_2"), types.StringValue("role_2"),
types.StringValue(""),
}, },
), ),
), ),
@ -423,7 +423,7 @@ func TestMapFields(t *testing.T) {
t.Fatalf("Should not have failed: %v", err) t.Fatalf("Should not have failed: %v", err)
} }
if tt.isValid { if tt.isValid {
diff := cmp.Diff(state, &tt.expected) diff := cmp.Diff(&tt.expected, state)
if diff != "" { if diff != "" {
t.Fatalf("Data does not match: %s", diff) t.Fatalf("Data does not match: %s", diff)
} }

View file

@ -2,6 +2,7 @@ fields:
- name: 'id' - name: 'id'
modifiers: modifiers:
- 'UseStateForUnknown' - 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'instance_id' - name: 'instance_id'
validators: validators:
@ -22,6 +23,7 @@ fields:
- name: 'region' - name: 'region'
modifiers: modifiers:
- 'RequiresReplace' - 'RequiresReplace'
- 'RequiresReplace'
- name: 'user_id' - name: 'user_id'
modifiers: modifiers:
@ -31,10 +33,12 @@ fields:
- name: 'username' - name: 'username'
modifiers: modifiers:
- 'UseStateForUnknown' - 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'roles' - name: 'roles'
modifiers: modifiers:
- 'UseStateForUnknown' - 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'password' - name: 'password'
modifiers: modifiers:

View file

@ -11,6 +11,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/identityschema" "github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
@ -107,6 +108,38 @@ func (r *userResource) ModifyPlan(
return return
} }
// TODO: verify if this is needed - START
var configRoles []string
diags := configModel.Roles.ElementsAs(ctx, &configRoles, false)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}
var planRoles []string
diags = planModel.Roles.ElementsAs(ctx, &planRoles, false)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}
slices.Sort(configRoles)
slices.Sort(planRoles)
if !slices.Equal(configRoles, planRoles) {
var roles []attr.Value
for _, role := range configRoles {
roles = append(roles, types.StringValue(string(role)))
}
rolesSet, diags := types.SetValue(types.StringType, roles)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}
planModel.Roles = types.List(rolesSet)
}
// TODO: verify if this is needed - END
resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...) resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...)
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return

View file

@ -208,7 +208,7 @@ func PartialUpdateInstanceWaitHandler(
case InstanceStateUnknown: case InstanceStateUnknown:
return false, nil, nil return false, nil, nil
case InstanceStateFailed: case InstanceStateFailed:
return true, s, fmt.Errorf("update failed for instance with id %s", instanceId) return true, s, fmt.Errorf("update got status FAILURE for instance with id %s", instanceId)
} }
}, },
) )

View file

@ -161,8 +161,10 @@ func CreateInstanceWaitHandler(
return false, nil, nil return false, nil, nil
} }
return true, s, nil return true, s, nil
case strings.ToLower(InstanceStateUnknown), strings.ToLower(InstanceStateFailed): case strings.ToLower(InstanceStateUnknown):
return true, nil, fmt.Errorf("create failed for instance with id %s", instanceId) return true, nil, fmt.Errorf("create failed for instance %s with status %s", instanceId, InstanceStateUnknown)
case strings.ToLower(InstanceStateFailed):
return true, nil, fmt.Errorf("create failed for instance %s with status %s", instanceId, InstanceStateFailed)
case strings.ToLower(InstanceStatePending), strings.ToLower(InstanceStateProcessing): case strings.ToLower(InstanceStatePending), strings.ToLower(InstanceStateProcessing):
tflog.Info( tflog.Info(
ctx, "request is being handled", map[string]interface{}{ ctx, "request is being handled", map[string]interface{}{