fix: fix flavor in sql server flex beta

This commit is contained in:
Marcel S. Henselin 2026-05-20 21:08:47 +02:00
parent bf2264120a
commit 4ca2d6a05d
5 changed files with 160 additions and 108 deletions

View file

@ -8,6 +8,7 @@ import (
"strings"
"time"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@ -85,9 +86,9 @@ func (r *instanceResource) Schema(ctx context.Context, req resource.SchemaReques
"description": schema.StringAttribute{
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.String{
UseStateForUnknownIfFlavorUnchanged(req),
},
//PlanModifiers: []planmodifier.String{
// UseStateForUnknownIfFlavorUnchanged(req),
//},
},
"cpu": schema.Int64Attribute{
DeprecationMessage: "Please use flavor_id instead.",
@ -102,8 +103,19 @@ func (r *instanceResource) Schema(ctx context.Context, req resource.SchemaReques
s.Attributes["flavor_id"] = schema.StringAttribute{
Optional: true,
Computed: true,
Description: "The id of the instance flavor.",
MarkdownDescription: "The id of the instance flavor.",
PlanModifiers: []planmodifier.String{
UseStateForUnknownIfFlavorUnchanged(req),
},
}
s.Attributes["replicas"] = schema.Int64Attribute{
Optional: true,
Computed: true,
Description: "How many replicas the instance should have.",
MarkdownDescription: "How many replicas the instance should have.",
}
fields, err := utils.ReadModifiersConfig(modifiersFileByte)
@ -230,29 +242,20 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
}
tflog.Debug(ctx, fmt.Sprintf("loaded flavors: %d", len(flavors)))
tmpNodeType := "Single"
if data.Replicas.ValueInt64() > 1 {
tmpNodeType = "Replica"
}
var foundFlavors []v3beta1api.ListFlavors
for _, flavor := range flavors {
if flModel.CPU.ValueInt64() != int64(flavor.Cpu) {
// tflog.Debug(ctx, fmt.Sprintf("flavor - cpu did not match (%d - %d)", flModel.CPU.ValueInt64(), flavor.Cpu))
continue
}
if flModel.RAM.ValueInt64() != int64(flavor.Memory) {
// tflog.Debug(ctx, fmt.Sprintf("flavor - ram did not match (%d - %d)", flModel.RAM.ValueInt64(), flavor.Memory))
continue
}
tmpNodeType := "Single"
if data.Replicas.ValueInt64() > 1 {
tmpNodeType = "Replica"
}
if strings.ToLower(tmpNodeType) != strings.ToLower(flavor.NodeType) {
//tflog.Debug(
// ctx,
// fmt.Sprintf(
// "flavor - nodeType did not match ('%s' - '%s')",
// strings.ToLower(tmpNodeType),
// strings.ToLower(flavor.NodeType),
// ),
//)
continue
}
tflog.Debug(ctx, fmt.Sprintf("found flavor %s, checking storage classes", flavor.Id))
@ -276,7 +279,33 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
f := foundFlavors[0]
flModel.Description = types.StringValue(f.Description)
flModel.Id = utils.BuildInternalTerraformId(data.ProjectId.ValueString(), region, f.Id)
var flavorValues map[string]attr.Value
flavorValues = map[string]attr.Value{
"id": types.StringValue(f.Id),
"description": types.StringValue(f.Description),
"cpu": types.Int64Value(f.Cpu),
"ram": types.Int64Value(f.Memory),
}
var flavorTypes = map[string]attr.Type{
"id": basetypes.StringType{},
"description": basetypes.StringType{},
"cpu": basetypes.Int64Type{},
"ram": basetypes.Int64Type{},
}
flavorObject, diags := types.ObjectValue(flavorTypes, flavorValues)
if diags.HasError() {
return
}
data.Flavor = flavorObject
data.FlavorId = types.StringValue(f.Id)
//resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("flavor_id"), f.Id)...)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
//flModel. .MaxGb = types.Int32Value(f.MaxGB)
//flModel.MinGb = types.Int32Value(f.MinGB)
}

View file

@ -85,6 +85,10 @@ type resData struct {
FlavorID string
BackupSchedule string
UseEncryption bool
UseFlavorId bool
FlavorCpu int
FlavorRam int
Replicas int
KekKeyID string
KekKeyRingID string
KekKeyVersion uint8
@ -130,6 +134,7 @@ func getExample() resData {
ACLStrings: []string{"0.0.0.0/0"},
AccessScope: "PUBLIC",
Version: "2022",
UseFlavorId: true,
}
}
@ -378,6 +383,77 @@ func TestAccInstanceNoEncryption(t *testing.T) {
})
}
func TestAccInstanceNoEncryptionWithFlavorObject(t *testing.T) {
data := getExample()
data.UseFlavorId = false
data.FlavorID = ""
data.FlavorCpu = 4
data.FlavorRam = 16
data.Replicas = 1
dbName := "testDb"
userName := "testUser"
data.Users = []User{
{
Name: userName,
ProjectID: os.Getenv("TF_ACC_PROJECT_ID"),
Roles: []string{
"##STACKIT_DatabaseManager##",
"##STACKIT_LoginManager##",
"##STACKIT_ProcessManager##",
"##STACKIT_SQLAgentManager##",
"##STACKIT_SQLAgentUser##",
"##STACKIT_ServerManager##",
},
},
}
data.Databases = []Database{
{
Name: dbName,
ProjectID: os.Getenv("TF_ACC_PROJECT_ID"),
Owner: userName,
},
}
testInstanceID := testutils.ResStr(pfx, "instance", data.TfName)
testDatabaseID := testutils.ResStr(pfx, "database", dbName)
testUserID := testutils.ResStr(pfx, "user", userName)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
t.Logf(" ... %s - %s", t.Name(), data.TfName)
},
CheckDestroy: testAccCheckSQLServerFlexDestroy,
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create and verify
{
Config: testutils.StringFromTemplateMust(
"testdata/instance_template.gompl",
data,
),
Check: resource.ComposeAggregateTestCheckFunc(
defaultNoEncInstanceTestChecks(testInstanceID, data),
// check user values are correct
resource.TestCheckResourceAttr(testUserID, "username", userName),
resource.TestCheckResourceAttr(testUserID, "roles.#", strconv.Itoa(len(data.Users[0].Roles))),
// check database values are set
resource.TestCheckResourceAttrSet(testDatabaseID, "id"),
resource.TestCheckResourceAttrSet(testDatabaseID, "name"),
resource.TestCheckResourceAttrSet(testDatabaseID, "owner"),
resource.TestCheckResourceAttrSet(testDatabaseID, "compatibility"),
resource.TestCheckResourceAttrSet(testDatabaseID, "collation"),
// check database values are correct
resource.TestCheckResourceAttr(testDatabaseID, "name", dbName),
resource.TestCheckResourceAttr(testDatabaseID, "owner", userName),
),
},
},
})
}
func TestAccInstanceEncryption(t *testing.T) {
data := getExample()

View file

@ -8,7 +8,15 @@ resource "stackitprivatepreview_sqlserverflexbeta_instance" "{{ .TfName }}" {
name = "{{ .Name }}"
backup_schedule = "{{ .BackupSchedule }}"
retention_days = {{ .RetentionDays }}
{{ if .UseFlavorId }}
flavor_id = "{{ .FlavorID }}"
{{ else }}
flavor = {
cpu = {{ .FlavorCpu }}
ram = {{ .FlavorRam }}
}
replicas = {{ .Replicas }}
{{ end }}
storage = {
class = "{{ .PerformanceClass }}"
size = {{ .Size }}