fix: refactor to use identity and different model mapping
Some checks failed
CI Workflow / Check GoReleaser config (pull_request) Successful in 5s
CI Workflow / Test readiness for publishing provider (pull_request) Successful in 22m50s
CI Workflow / CI (pull_request) Failing after 27m13s
CI Workflow / Code coverage report (pull_request) Has been skipped

This commit is contained in:
Marcel_Henselin 2026-02-03 19:00:44 +01:00
parent 762c39fbbd
commit 8d7323bbc1
9 changed files with 191 additions and 212 deletions

View file

@ -6,7 +6,7 @@ import (
"math"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
sqlserverflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
@ -14,200 +14,103 @@ import (
sqlserverflexResGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/instance/resources_gen"
)
func mapCreateResponseToModel(
func mapResponseToModel(
ctx context.Context,
resp *sqlserverflex.GetInstanceResponse,
m *sqlserverflexResGen.InstanceModel,
tfResp *resource.CreateResponse,
tfDiags diag.Diagnostics,
) error {
m.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
if resp.HasEncryption() {
m.Encryption = sqlserverflexResGen.NewEncryptionValueMust(
m.Encryption.AttributeTypes(ctx),
map[string]attr.Value{
"kek_key_id": types.StringValue(resp.Encryption.GetKekKeyId()),
"kek_key_ring_id": types.StringValue(resp.Encryption.GetKekKeyRingId()),
"kek_key_version": types.StringValue(resp.Encryption.GetKekKeyVersion()),
"service_account": types.StringValue(resp.Encryption.GetServiceAccount()),
},
)
}
m.Edition = types.StringValue(string(resp.GetEdition()))
m.Encryption = handleEncryption(m, resp)
m.FlavorId = types.StringValue(resp.GetFlavorId())
m.Id = types.StringValue(resp.GetId())
m.InstanceId = types.StringValue(resp.GetId())
m.IsDeletable = types.BoolValue(resp.GetIsDeletable())
m.Name = types.StringValue(resp.GetName())
netAcl, diags := types.ListValueFrom(ctx, types.StringType, resp.Network.GetAcl())
tfResp.Diagnostics.Append(diags...)
tfDiags.Append(diags...)
if diags.HasError() {
return fmt.Errorf("error converting api response value")
return fmt.Errorf(
"error converting network acl response value",
)
}
m.Network = sqlserverflexResGen.NetworkValue{
AccessScope: types.StringValue(string(resp.Network.GetAccessScope())),
Acl: netAcl,
InstanceAddress: types.StringValue(resp.Network.GetInstanceAddress()),
RouterAddress: types.StringValue(resp.Network.GetRouterAddress()),
net, diags := sqlserverflexResGen.NewNetworkValue(
sqlserverflexResGen.NetworkValue{}.AttributeTypes(ctx),
map[string]attr.Value{
"access_scope": types.StringValue(string(resp.Network.GetAccessScope())),
"acl": netAcl,
"instance_address": types.StringValue(resp.Network.GetInstanceAddress()),
"router_address": types.StringValue(resp.Network.GetRouterAddress()),
},
)
tfDiags.Append(diags...)
if diags.HasError() {
return fmt.Errorf(
"error converting network response value",
"access_scope",
types.StringValue(string(resp.Network.GetAccessScope())),
"acl",
netAcl,
"instance_address",
types.StringValue(resp.Network.GetInstanceAddress()),
"router_address",
types.StringValue(resp.Network.GetRouterAddress()),
)
}
m.Network = net
m.Replicas = types.Int64Value(int64(resp.GetReplicas()))
m.RetentionDays = types.Int64Value(resp.GetRetentionDays())
m.Storage = sqlserverflexResGen.StorageValue{
Class: types.StringValue(resp.Storage.GetClass()),
Size: types.Int64Value(resp.Storage.GetSize()),
}
m.Status = types.StringValue(string(resp.GetStatus()))
m.Version = types.StringValue(string(resp.GetVersion()))
stor, diags := sqlserverflexResGen.NewStorageValue(
sqlserverflexResGen.StorageValue{}.AttributeTypes(ctx),
map[string]attr.Value{
"class": types.StringValue(resp.Storage.GetClass()),
"size": types.Int64Value(resp.Storage.GetSize()),
},
)
tfDiags.Append(diags...)
if diags.HasError() {
return fmt.Errorf("error converting storage response value")
}
m.Storage = stor
m.Version = types.StringValue(string(resp.GetVersion()))
return nil
}
func mapReadResponseToModel(
ctx context.Context,
resp *sqlserverflex.GetInstanceResponse,
func handleEncryption(
m *sqlserverflexResGen.InstanceModel,
tfResp *resource.ReadResponse,
) error {
m.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
if resp.HasEncryption() {
m.Encryption = sqlserverflexResGen.NewEncryptionValueMust(
m.Encryption.AttributeTypes(ctx),
map[string]attr.Value{
"kek_key_id": types.StringValue(resp.Encryption.GetKekKeyId()),
"kek_key_ring_id": types.StringValue(resp.Encryption.GetKekKeyRingId()),
"kek_key_version": types.StringValue(resp.Encryption.GetKekKeyVersion()),
"service_account": types.StringValue(resp.Encryption.GetServiceAccount()),
},
)
}
m.FlavorId = types.StringValue(resp.GetFlavorId())
m.Id = types.StringValue(resp.GetId())
m.InstanceId = types.StringValue(resp.GetId())
m.Name = types.StringValue(resp.GetName())
netAcl, diags := types.ListValueFrom(ctx, types.StringType, resp.Network.GetAcl())
tfResp.Diagnostics.Append(diags...)
if diags.HasError() {
return fmt.Errorf("error converting api response value")
}
m.Network = sqlserverflexResGen.NetworkValue{
AccessScope: types.StringValue(string(resp.Network.GetAccessScope())),
Acl: netAcl,
InstanceAddress: types.StringValue(resp.Network.GetInstanceAddress()),
RouterAddress: types.StringValue(resp.Network.GetRouterAddress()),
}
m.RetentionDays = types.Int64Value(resp.GetRetentionDays())
m.Storage = sqlserverflexResGen.StorageValue{
Class: types.StringValue(resp.Storage.GetClass()),
Size: types.Int64Value(resp.Storage.GetSize()),
}
m.Status = types.StringValue(string(resp.GetStatus()))
m.Version = types.StringValue(string(resp.GetVersion()))
return nil
}
func mapUpdateResponseToModel(
ctx context.Context,
resp *sqlserverflex.GetInstanceResponse,
m *sqlserverflexResGen.InstanceModel,
tfResp *resource.UpdateResponse,
) error {
m.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
) sqlserverflexResGen.EncryptionValue {
if !resp.HasEncryption() ||
resp.Encryption == nil ||
resp.Encryption.KekKeyId == nil ||
resp.Encryption.KekKeyRingId == nil ||
resp.Encryption.KekKeyVersion == nil ||
resp.Encryption.ServiceAccount == nil {
if resp.HasEncryption() {
m.Encryption = sqlserverflexResGen.NewEncryptionValueMust(
m.Encryption.AttributeTypes(ctx),
map[string]attr.Value{
"kek_key_id": types.StringValue(resp.Encryption.GetKekKeyId()),
"kek_key_ring_id": types.StringValue(resp.Encryption.GetKekKeyRingId()),
"kek_key_version": types.StringValue(resp.Encryption.GetKekKeyVersion()),
"service_account": types.StringValue(resp.Encryption.GetServiceAccount()),
},
)
if m.Encryption.IsNull() || m.Encryption.IsUnknown() {
return sqlserverflexResGen.NewEncryptionValueNull()
}
return m.Encryption
}
m.FlavorId = types.StringValue(resp.GetFlavorId())
m.Id = types.StringValue(resp.GetId())
m.InstanceId = types.StringValue(resp.GetId())
m.Name = types.StringValue(resp.GetName())
netAcl, diags := types.ListValueFrom(ctx, types.StringType, resp.Network.GetAcl())
tfResp.Diagnostics.Append(diags...)
if diags.HasError() {
return fmt.Errorf("error converting api response value")
enc := sqlserverflexResGen.NewEncryptionValueNull()
if kVal, ok := resp.Encryption.GetKekKeyIdOk(); ok {
enc.KekKeyId = types.StringValue(kVal)
}
m.Network = sqlserverflexResGen.NetworkValue{
AccessScope: types.StringValue(string(resp.Network.GetAccessScope())),
Acl: netAcl,
InstanceAddress: types.StringValue(resp.Network.GetInstanceAddress()),
RouterAddress: types.StringValue(resp.Network.GetRouterAddress()),
if kkVal, ok := resp.Encryption.GetKekKeyRingIdOk(); ok {
enc.KekKeyRingId = types.StringValue(kkVal)
}
m.RetentionDays = types.Int64Value(resp.GetRetentionDays())
m.Storage = sqlserverflexResGen.StorageValue{
Class: types.StringValue(resp.Storage.GetClass()),
Size: types.Int64Value(resp.Storage.GetSize()),
if kkvVal, ok := resp.Encryption.GetKekKeyVersionOk(); ok {
enc.KekKeyVersion = types.StringValue(kkvVal)
}
m.Status = types.StringValue(string(resp.GetStatus()))
m.Version = types.StringValue(string(resp.GetVersion()))
return nil
}
func mapDataSourceResponseToModel(
ctx context.Context,
resp *sqlserverflex.GetInstanceResponse,
m *sqlserverflexResGen.InstanceModel,
tfResp *datasource.ReadResponse,
) error {
m.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
if resp.HasEncryption() {
m.Encryption = sqlserverflexResGen.NewEncryptionValueMust(
m.Encryption.AttributeTypes(ctx),
map[string]attr.Value{
"kek_key_id": types.StringValue(resp.Encryption.GetKekKeyId()),
"kek_key_ring_id": types.StringValue(resp.Encryption.GetKekKeyRingId()),
"kek_key_version": types.StringValue(resp.Encryption.GetKekKeyVersion()),
"service_account": types.StringValue(resp.Encryption.GetServiceAccount()),
},
)
if sa, ok := resp.Encryption.GetServiceAccountOk(); ok {
enc.ServiceAccount = types.StringValue(sa)
}
m.FlavorId = types.StringValue(resp.GetFlavorId())
m.Id = types.StringValue(resp.GetId())
m.InstanceId = types.StringValue(resp.GetId())
m.Name = types.StringValue(resp.GetName())
netAcl, diags := types.ListValueFrom(ctx, types.StringType, resp.Network.GetAcl())
tfResp.Diagnostics.Append(diags...)
if diags.HasError() {
return fmt.Errorf("error converting api response value")
}
m.Network = sqlserverflexResGen.NetworkValue{
AccessScope: types.StringValue(string(resp.Network.GetAccessScope())),
Acl: netAcl,
InstanceAddress: types.StringValue(resp.Network.GetInstanceAddress()),
RouterAddress: types.StringValue(resp.Network.GetRouterAddress()),
}
m.RetentionDays = types.Int64Value(resp.GetRetentionDays())
m.Storage = sqlserverflexResGen.StorageValue{
Class: types.StringValue(resp.Storage.GetClass()),
Size: types.Int64Value(resp.Storage.GetSize()),
}
m.Status = types.StringValue(string(resp.GetStatus()))
m.Version = types.StringValue(string(resp.GetVersion()))
return nil
return enc
}
//func mapFields(
@ -357,6 +260,7 @@ func mapDataSourceResponseToModel(
//}
func toCreatePayload(
ctx context.Context,
model *sqlserverflexResGen.InstanceModel,
) (*sqlserverflex.CreateInstanceRequestPayload, error) {
if model == nil {
@ -390,11 +294,9 @@ func toCreatePayload(
)
var resList []string
aclList := model.Network.Acl.Elements()
for _, aclItem := range aclList {
if !aclItem.IsNull() && !aclItem.IsUnknown() {
resList = append(resList, aclItem.String())
}
diags := model.Network.Acl.ElementsAs(ctx, &resList, false)
if diags.HasError() {
return nil, fmt.Errorf("error converting network acl list")
}
networkPayload.Acl = &resList
}