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)
}