feat: refactor data source models and update mapping functions for improved consistency
This commit is contained in:
parent
f0e7c19cdf
commit
184e133a2a
34 changed files with 980 additions and 1017 deletions
|
|
@ -14,26 +14,21 @@ import (
|
|||
sqlserverflexResGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/instance/resources_gen"
|
||||
)
|
||||
|
||||
func mapResponseToModel(
|
||||
// instanceModel is a type constraint for models that can be mapped from a GetInstanceResponse.
|
||||
type instanceModel interface {
|
||||
*dataSourceModel | *resourceModel
|
||||
}
|
||||
|
||||
func mapFields[T instanceModel](
|
||||
ctx context.Context,
|
||||
resp *sqlserverflex.GetInstanceResponse,
|
||||
m *sqlserverflexResGen.InstanceModel,
|
||||
m T,
|
||||
tfDiags diag.Diagnostics,
|
||||
) error {
|
||||
m.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
|
||||
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())
|
||||
tfDiags.Append(diags...)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf(
|
||||
"error converting network acl response value",
|
||||
)
|
||||
return fmt.Errorf("error converting network acl response value")
|
||||
}
|
||||
net, diags := sqlserverflexResGen.NewNetworkValue(
|
||||
sqlserverflexResGen.NetworkValue{}.AttributeTypes(ctx),
|
||||
|
|
@ -46,22 +41,8 @@ func mapResponseToModel(
|
|||
)
|
||||
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()),
|
||||
)
|
||||
return fmt.Errorf("error converting network response value")
|
||||
}
|
||||
m.Network = net
|
||||
m.Replicas = types.Int64Value(int64(resp.GetReplicas()))
|
||||
m.RetentionDays = types.Int64Value(resp.GetRetentionDays())
|
||||
m.Status = types.StringValue(string(resp.GetStatus()))
|
||||
|
||||
stor, diags := sqlserverflexResGen.NewStorageValue(
|
||||
sqlserverflexResGen.StorageValue{}.AttributeTypes(ctx),
|
||||
|
|
@ -74,14 +55,47 @@ func mapResponseToModel(
|
|||
if diags.HasError() {
|
||||
return fmt.Errorf("error converting storage response value")
|
||||
}
|
||||
m.Storage = stor
|
||||
|
||||
m.Version = types.StringValue(string(resp.GetVersion()))
|
||||
// The interface conversion is safe due to the type constraint.
|
||||
model := any(m)
|
||||
|
||||
if rm, ok := model.(*resourceModel); ok {
|
||||
rm.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
|
||||
rm.Edition = types.StringValue(string(resp.GetEdition()))
|
||||
rm.Encryption = handleEncryption(rm.Encryption, resp)
|
||||
rm.FlavorId = types.StringValue(resp.GetFlavorId())
|
||||
rm.Id = types.StringValue(resp.GetId())
|
||||
rm.InstanceId = types.StringValue(resp.GetId())
|
||||
rm.IsDeletable = types.BoolValue(resp.GetIsDeletable())
|
||||
rm.Name = types.StringValue(resp.GetName())
|
||||
rm.Network = net
|
||||
rm.Replicas = types.Int64Value(int64(resp.GetReplicas()))
|
||||
rm.RetentionDays = types.Int64Value(resp.GetRetentionDays())
|
||||
rm.Status = types.StringValue(string(resp.GetStatus()))
|
||||
rm.Storage = stor
|
||||
rm.Version = types.StringValue(string(resp.GetVersion()))
|
||||
} else if dm, ok := model.(*dataSourceModel); ok {
|
||||
dm.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
|
||||
dm.Edition = types.StringValue(string(resp.GetEdition()))
|
||||
dm.Encryption = handleEncryption(dm.Encryption, resp)
|
||||
dm.FlavorId = types.StringValue(resp.GetFlavorId())
|
||||
dm.Id = types.StringValue(resp.GetId())
|
||||
dm.InstanceId = types.StringValue(resp.GetId())
|
||||
dm.IsDeletable = types.BoolValue(resp.GetIsDeletable())
|
||||
dm.Name = types.StringValue(resp.GetName())
|
||||
dm.Network = net
|
||||
dm.Replicas = types.Int64Value(int64(resp.GetReplicas()))
|
||||
dm.RetentionDays = types.Int64Value(resp.GetRetentionDays())
|
||||
dm.Status = types.StringValue(string(resp.GetStatus()))
|
||||
dm.Storage = stor
|
||||
dm.Version = types.StringValue(string(resp.GetVersion()))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleEncryption(
|
||||
m *sqlserverflexResGen.InstanceModel,
|
||||
encryptionValue sqlserverflexResGen.EncryptionValue,
|
||||
resp *sqlserverflex.GetInstanceResponse,
|
||||
) sqlserverflexResGen.EncryptionValue {
|
||||
if !resp.HasEncryption() ||
|
||||
|
|
@ -91,10 +105,10 @@ func handleEncryption(
|
|||
resp.Encryption.KekKeyVersion == nil ||
|
||||
resp.Encryption.ServiceAccount == nil {
|
||||
|
||||
if m.Encryption.IsNull() || m.Encryption.IsUnknown() {
|
||||
if encryptionValue.IsNull() || encryptionValue.IsUnknown() {
|
||||
return sqlserverflexResGen.NewEncryptionValueNull()
|
||||
}
|
||||
return m.Encryption
|
||||
return encryptionValue
|
||||
}
|
||||
|
||||
enc := sqlserverflexResGen.NewEncryptionValueNull()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue