Some checks failed
CI Workflow / Check GoReleaser config (pull_request) Successful in 13s
CI Workflow / CI (pull_request) Failing after 29s
Publish / Check GoReleaser config (pull_request) Has been skipped
CI Workflow / Code coverage report (pull_request) Has been skipped
Publish / Publish provider (pull_request) Has been skipped
feat: add connection info fix: prevent postgresql from failing when encryption is empty
164 lines
6.9 KiB
Go
164 lines
6.9 KiB
Go
package postgresflexalpha
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/attr"
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
|
|
"github.com/hashicorp/terraform-plugin-log/tflog"
|
|
postgresflex "github.com/mhenselin/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
|
postgresflexalphadatasource "github.com/mhenselin/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/instance/datasources_gen"
|
|
postgresflexalpharesource "github.com/mhenselin/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/instance/resources_gen"
|
|
"github.com/mhenselin/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
|
)
|
|
|
|
func mapGetInstanceResponseToModel(ctx context.Context, m *postgresflexalpharesource.InstanceModel, resp *postgresflex.GetInstanceResponse) error {
|
|
tflog.Debug(ctx, ">>>> MSH DEBUG <<<<", map[string]interface{}{
|
|
"id": m.Id.ValueString(),
|
|
"instance_id": m.InstanceId.ValueString(),
|
|
"backup_schedule": m.BackupSchedule.ValueString(),
|
|
"flavor_id": m.FlavorId.ValueString(),
|
|
"encryption.kek_key_id": m.Encryption.KekKeyId.ValueString(),
|
|
"encryption.kek_key_ring_id": m.Encryption.KekKeyRingId.ValueString(),
|
|
"encryption.kek_key_version": m.Encryption.KekKeyVersion.ValueString(),
|
|
"encryption.service_account": m.Encryption.ServiceAccount.ValueString(),
|
|
"is_deletable": m.IsDeletable.ValueBool(),
|
|
"name": m.Name.ValueString(),
|
|
"status": m.Status.ValueString(),
|
|
"retention_days": m.RetentionDays.ValueInt64(),
|
|
"replicas": m.Replicas.ValueInt64(),
|
|
"network.instance_address": m.Network.InstanceAddress.ValueString(),
|
|
"network.router_address": m.Network.RouterAddress.ValueString(),
|
|
"version": m.Version.ValueString(),
|
|
"network.acl": m.Network.Acl.String(),
|
|
})
|
|
|
|
m.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
|
|
if resp.HasEncryption() {
|
|
m.Encryption = postgresflexalpharesource.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.ConnectionInfo.Host = types.StringValue(resp.ConnectionInfo.GetHost())
|
|
m.ConnectionInfo.Port = types.Int64Value(resp.ConnectionInfo.GetPort())
|
|
|
|
m.FlavorId = types.StringValue(resp.GetFlavorId())
|
|
if m.Id.IsNull() || m.Id.IsUnknown() {
|
|
m.Id = utils.BuildInternalTerraformId(m.ProjectId.ValueString(), m.Region.ValueString(), m.InstanceId.ValueString())
|
|
}
|
|
m.InstanceId = types.StringPointerValue(resp.Id)
|
|
|
|
m.IsDeletable = types.BoolValue(resp.GetIsDeletable())
|
|
|
|
netAcl, diags := types.ListValueFrom(ctx, types.StringType, resp.Network.GetAcl())
|
|
if diags.HasError() {
|
|
return fmt.Errorf("failed converting network acl from response")
|
|
}
|
|
|
|
netInstAdd := types.StringValue("")
|
|
if instAdd, ok := resp.Network.GetInstanceAddressOk(); ok {
|
|
netInstAdd = types.StringValue(instAdd)
|
|
}
|
|
|
|
netRtrAdd := types.StringValue("")
|
|
if rtrAdd, ok := resp.Network.GetRouterAddressOk(); ok {
|
|
netRtrAdd = types.StringValue(rtrAdd)
|
|
}
|
|
|
|
net, diags := postgresflexalpharesource.NewNetworkValue(
|
|
postgresflexalpharesource.NetworkValue{}.AttributeTypes(ctx),
|
|
map[string]attr.Value{
|
|
"access_scope": basetypes.NewStringValue(string(resp.Network.GetAccessScope())),
|
|
"acl": netAcl,
|
|
"instance_address": netInstAdd,
|
|
"router_address": netRtrAdd,
|
|
},
|
|
)
|
|
if diags.HasError() {
|
|
return fmt.Errorf("failed converting network from response")
|
|
}
|
|
|
|
m.Network = net
|
|
m.Replicas = types.Int64Value(int64(resp.GetReplicas()))
|
|
m.RetentionDays = types.Int64Value(resp.GetRetentionDays())
|
|
|
|
m.Name = types.StringValue(resp.GetName())
|
|
|
|
m.Status = types.StringValue(string(resp.GetStatus()))
|
|
|
|
storage, diags := postgresflexalpharesource.NewStorageValue(
|
|
postgresflexalpharesource.StorageValue{}.AttributeTypes(ctx),
|
|
map[string]attr.Value{
|
|
"performance_class": types.StringValue(resp.Storage.GetPerformanceClass()),
|
|
"size": types.Int64Value(resp.Storage.GetSize()),
|
|
},
|
|
)
|
|
if diags.HasError() {
|
|
return fmt.Errorf("failed converting storage from response")
|
|
}
|
|
m.Storage = storage
|
|
|
|
m.Version = types.StringValue(resp.GetVersion())
|
|
return nil
|
|
}
|
|
|
|
func mapGetDataInstanceResponseToModel(ctx context.Context, m *postgresflexalphadatasource.InstanceModel, resp *postgresflex.GetInstanceResponse) error {
|
|
m.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
|
|
m.Encryption = postgresflexalphadatasource.EncryptionValue{
|
|
KekKeyId: types.StringValue(resp.Encryption.GetKekKeyId()),
|
|
KekKeyRingId: types.StringValue(resp.Encryption.GetKekKeyRingId()),
|
|
KekKeyVersion: types.StringValue(resp.Encryption.GetKekKeyVersion()),
|
|
ServiceAccount: types.StringValue(resp.Encryption.GetServiceAccount()),
|
|
}
|
|
m.ConnectionInfo.Host = types.StringValue(resp.ConnectionInfo.GetHost())
|
|
m.ConnectionInfo.Port = types.Int64Value(resp.ConnectionInfo.GetPort())
|
|
m.FlavorId = types.StringValue(resp.GetFlavorId())
|
|
m.Id = utils.BuildInternalTerraformId(m.ProjectId.ValueString(), m.Region.ValueString(), m.InstanceId.ValueString())
|
|
m.InstanceId = types.StringPointerValue(resp.Id)
|
|
m.IsDeletable = types.BoolValue(resp.GetIsDeletable())
|
|
m.Name = types.StringValue(resp.GetName())
|
|
netAcl, diags := types.ListValueFrom(ctx, types.StringType, resp.Network.GetAcl())
|
|
if diags.HasError() {
|
|
return fmt.Errorf("failed converting network acl from response")
|
|
}
|
|
|
|
net, diags := postgresflexalphadatasource.NewNetworkValue(
|
|
postgresflexalphadatasource.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()),
|
|
},
|
|
)
|
|
if diags.HasError() {
|
|
return fmt.Errorf("failed converting network from response")
|
|
}
|
|
|
|
m.Network = net
|
|
m.Replicas = types.Int64Value(int64(resp.GetReplicas()))
|
|
m.RetentionDays = types.Int64Value(resp.GetRetentionDays())
|
|
m.Status = types.StringValue(string(resp.GetStatus()))
|
|
storage, diags := postgresflexalphadatasource.NewStorageValue(
|
|
postgresflexalphadatasource.StorageValue{}.AttributeTypes(ctx),
|
|
map[string]attr.Value{
|
|
"performance_class": types.StringValue(resp.Storage.GetPerformanceClass()),
|
|
"size": types.Int64Value(resp.Storage.GetSize()),
|
|
},
|
|
)
|
|
if diags.HasError() {
|
|
return fmt.Errorf("failed converting storage from response")
|
|
}
|
|
m.Storage = storage
|
|
m.Version = types.StringValue(resp.GetVersion())
|
|
return nil
|
|
}
|