Skip to content

Commit 9d12523

Browse files
authored
fix: pg config bug (#457)
* fix: pg config bug * fix: pgd pg config fix * fix: pg config pg cluster refactor * fix: lint fix * fix: comments
1 parent 360c5b2 commit 9d12523

File tree

4 files changed

+141
-97
lines changed

4 files changed

+141
-97
lines changed

pkg/plan_modifier/data_group_custom_diff.go

+11
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,17 @@ func (m CustomDataGroupDiffModifier) PlanModifySet(ctx context.Context, req plan
367367
planMW,
368368
stateDgRegion))
369369
}
370+
371+
// pg config
372+
planPgConfig := planDg.(basetypes.ObjectValue).Attributes()["pg_config"]
373+
statePgConfig := stateDgs[*stateDgKey].(basetypes.ObjectValue).Attributes()["pg_config"]
374+
375+
if !planPgConfig.Equal(statePgConfig) {
376+
resp.Diagnostics.AddWarning("Pg config changed", fmt.Sprintf("Pg config changed from %v to %v for data group with region %v",
377+
statePgConfig,
378+
planPgConfig,
379+
stateDgRegion))
380+
}
370381
}
371382

372383
}

pkg/provider/data_source_pgd.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,16 @@ func (p pgdDataSource) Read(ctx context.Context, req datasource.ReadRequest, res
455455
data.ID = cluster.ClusterId
456456
data.ClusterID = cluster.ClusterId
457457

458-
buildTFGroupsAs(ctx, &resp.Diagnostics, resp.State, *cluster, &data.DataGroups, &data.WitnessGroups)
458+
buildGroups := PGD{}
459+
460+
buildTFGroupsAs(ctx, &resp.Diagnostics, resp.State, *cluster, &buildGroups)
459461
if resp.Diagnostics.HasError() {
460462
return
461463
}
462464

465+
data.DataGroups = buildGroups.DataGroups
466+
data.WitnessGroups = buildGroups.WitnessGroups
467+
463468
diags = resp.State.Set(ctx, &data)
464469
resp.Diagnostics.Append(diags...)
465470
}

pkg/provider/resource_cluster.go

+88-77
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ func (c *clusterResource) Schema(ctx context.Context, req resource.SchemaRequest
189189

190190
"pg_config": schema.SetNestedBlock{
191191
MarkdownDescription: "Database configuration parameters. See [Modifying database configuration parameters](https://www.enterprisedb.com/docs/biganimal/latest/using_cluster/03_modifying_your_cluster/05_db_configuration_parameters/) for details.",
192-
PlanModifiers: []planmodifier.Set{plan_modifier.CustomPGConfig()},
192+
PlanModifiers: []planmodifier.Set{
193+
setplanmodifier.UseStateForUnknown(),
194+
},
193195
NestedObject: schema.NestedBlockObject{
194196
Attributes: map[string]schema.Attribute{
195197
"name": schema.StringAttribute{
@@ -595,108 +597,117 @@ func (c *clusterResource) ImportState(ctx context.Context, req resource.ImportSt
595597
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("cluster_id"), idParts[1])...)
596598
}
597599

598-
func (c *clusterResource) read(ctx context.Context, clusterResource *ClusterResourceModel) error {
599-
cluster, err := c.client.Read(ctx, clusterResource.ProjectId, *clusterResource.ClusterId)
600+
func (c *clusterResource) read(ctx context.Context, tfClusterResource *ClusterResourceModel) error {
601+
apiCluster, err := c.client.Read(ctx, tfClusterResource.ProjectId, *tfClusterResource.ClusterId)
600602
if err != nil {
601603
return err
602604
}
603605

604-
connection, err := c.client.ConnectionString(ctx, clusterResource.ProjectId, *clusterResource.ClusterId)
606+
connection, err := c.client.ConnectionString(ctx, tfClusterResource.ProjectId, *tfClusterResource.ClusterId)
605607
if err != nil {
606608
return err
607609
}
608610

609-
clusterResource.ID = types.StringValue(fmt.Sprintf("%s/%s", clusterResource.ProjectId, *clusterResource.ClusterId))
610-
clusterResource.ClusterId = cluster.ClusterId
611-
clusterResource.ClusterName = types.StringPointerValue(cluster.ClusterName)
612-
clusterResource.ClusterType = cluster.ClusterType
613-
clusterResource.Phase = cluster.Phase
614-
clusterResource.CloudProvider = types.StringValue(cluster.Provider.CloudProviderId)
615-
clusterResource.ClusterArchitecture = &ClusterArchitectureResourceModel{
616-
Id: cluster.ClusterArchitecture.ClusterArchitectureId,
617-
Nodes: cluster.ClusterArchitecture.Nodes,
618-
Name: types.StringValue(cluster.ClusterArchitecture.ClusterArchitectureName),
619-
}
620-
clusterResource.Region = types.StringValue(cluster.Region.Id)
621-
clusterResource.InstanceType = types.StringValue(cluster.InstanceType.InstanceTypeId)
622-
clusterResource.Storage = &StorageResourceModel{
623-
VolumeType: types.StringPointerValue(cluster.Storage.VolumeTypeId),
624-
VolumeProperties: types.StringPointerValue(cluster.Storage.VolumePropertiesId),
625-
Size: types.StringPointerValue(cluster.Storage.Size),
626-
Iops: types.StringPointerValue(cluster.Storage.Iops),
627-
Throughput: types.StringPointerValue(cluster.Storage.Throughput),
628-
}
629-
clusterResource.ResizingPvc = StringSliceToList(cluster.ResizingPvc)
630-
clusterResource.ReadOnlyConnections = types.BoolPointerValue(cluster.ReadOnlyConnections)
631-
clusterResource.ConnectionUri = &connection.PgUri
632-
clusterResource.RoConnectionUri = &connection.ReadOnlyPgUri
633-
clusterResource.CspAuth = types.BoolPointerValue(cluster.CSPAuth)
634-
clusterResource.LogsUrl = cluster.LogsUrl
635-
clusterResource.MetricsUrl = cluster.MetricsUrl
636-
clusterResource.BackupRetentionPeriod = types.StringPointerValue(cluster.BackupRetentionPeriod)
637-
clusterResource.PgVersion = types.StringValue(cluster.PgVersion.PgVersionId)
638-
clusterResource.PgType = types.StringValue(cluster.PgType.PgTypeId)
639-
clusterResource.FarawayReplicaIds = StringSliceToSet(cluster.FarawayReplicaIds)
640-
clusterResource.PrivateNetworking = types.BoolPointerValue(cluster.PrivateNetworking)
641-
clusterResource.SuperuserAccess = types.BoolPointerValue(cluster.SuperuserAccess)
642-
if cluster.Extensions != nil {
643-
for _, v := range *cluster.Extensions {
611+
tfClusterResource.ID = types.StringValue(fmt.Sprintf("%s/%s", tfClusterResource.ProjectId, *tfClusterResource.ClusterId))
612+
tfClusterResource.ClusterId = apiCluster.ClusterId
613+
tfClusterResource.ClusterName = types.StringPointerValue(apiCluster.ClusterName)
614+
tfClusterResource.ClusterType = apiCluster.ClusterType
615+
tfClusterResource.Phase = apiCluster.Phase
616+
tfClusterResource.CloudProvider = types.StringValue(apiCluster.Provider.CloudProviderId)
617+
tfClusterResource.ClusterArchitecture = &ClusterArchitectureResourceModel{
618+
Id: apiCluster.ClusterArchitecture.ClusterArchitectureId,
619+
Nodes: apiCluster.ClusterArchitecture.Nodes,
620+
Name: types.StringValue(apiCluster.ClusterArchitecture.ClusterArchitectureName),
621+
}
622+
tfClusterResource.Region = types.StringValue(apiCluster.Region.Id)
623+
tfClusterResource.InstanceType = types.StringValue(apiCluster.InstanceType.InstanceTypeId)
624+
tfClusterResource.Storage = &StorageResourceModel{
625+
VolumeType: types.StringPointerValue(apiCluster.Storage.VolumeTypeId),
626+
VolumeProperties: types.StringPointerValue(apiCluster.Storage.VolumePropertiesId),
627+
Size: types.StringPointerValue(apiCluster.Storage.Size),
628+
Iops: types.StringPointerValue(apiCluster.Storage.Iops),
629+
Throughput: types.StringPointerValue(apiCluster.Storage.Throughput),
630+
}
631+
tfClusterResource.ResizingPvc = StringSliceToList(apiCluster.ResizingPvc)
632+
tfClusterResource.ReadOnlyConnections = types.BoolPointerValue(apiCluster.ReadOnlyConnections)
633+
tfClusterResource.ConnectionUri = &connection.PgUri
634+
tfClusterResource.RoConnectionUri = &connection.ReadOnlyPgUri
635+
tfClusterResource.CspAuth = types.BoolPointerValue(apiCluster.CSPAuth)
636+
tfClusterResource.LogsUrl = apiCluster.LogsUrl
637+
tfClusterResource.MetricsUrl = apiCluster.MetricsUrl
638+
tfClusterResource.BackupRetentionPeriod = types.StringPointerValue(apiCluster.BackupRetentionPeriod)
639+
tfClusterResource.PgVersion = types.StringValue(apiCluster.PgVersion.PgVersionId)
640+
tfClusterResource.PgType = types.StringValue(apiCluster.PgType.PgTypeId)
641+
tfClusterResource.FarawayReplicaIds = StringSliceToSet(apiCluster.FarawayReplicaIds)
642+
tfClusterResource.PrivateNetworking = types.BoolPointerValue(apiCluster.PrivateNetworking)
643+
tfClusterResource.SuperuserAccess = types.BoolPointerValue(apiCluster.SuperuserAccess)
644+
if apiCluster.Extensions != nil {
645+
for _, v := range *apiCluster.Extensions {
644646
if v.Enabled && v.ExtensionId == "pgvector" {
645-
clusterResource.Pgvector = types.BoolValue(true)
647+
tfClusterResource.Pgvector = types.BoolValue(true)
646648
break
647649
}
648650
}
649651
}
650652

651-
if cluster.FirstRecoverabilityPointAt != nil {
652-
firstPointAt := cluster.FirstRecoverabilityPointAt.String()
653-
clusterResource.FirstRecoverabilityPointAt = &firstPointAt
653+
if apiCluster.FirstRecoverabilityPointAt != nil {
654+
firstPointAt := apiCluster.FirstRecoverabilityPointAt.String()
655+
tfClusterResource.FirstRecoverabilityPointAt = &firstPointAt
654656
}
655657

656-
clusterResource.PgConfig = []PgConfigResourceModel{}
657-
if configs := cluster.PgConfig; configs != nil {
658-
for _, kv := range *configs {
659-
clusterResource.PgConfig = append(clusterResource.PgConfig, PgConfigResourceModel{
660-
Name: kv.Name,
661-
Value: kv.Value,
662-
})
658+
// pgConfig. If tf resource pg config elem matches with api response pg config elem then add the elem to tf resource pg config
659+
newPgConfig := []PgConfigResourceModel{}
660+
if configs := apiCluster.PgConfig; configs != nil {
661+
for _, tfCRPgConfig := range tfClusterResource.PgConfig {
662+
for _, apiConfig := range *configs {
663+
if tfCRPgConfig.Name == apiConfig.Name {
664+
newPgConfig = append(newPgConfig, PgConfigResourceModel{
665+
Name: apiConfig.Name,
666+
Value: apiConfig.Value,
667+
})
668+
}
669+
}
663670
}
664671
}
665672

666-
clusterResource.AllowedIpRanges = []AllowedIpRangesResourceModel{}
667-
if allowedIpRanges := cluster.AllowedIpRanges; allowedIpRanges != nil {
673+
if len(newPgConfig) > 0 {
674+
tfClusterResource.PgConfig = newPgConfig
675+
}
676+
677+
tfClusterResource.AllowedIpRanges = []AllowedIpRangesResourceModel{}
678+
if allowedIpRanges := apiCluster.AllowedIpRanges; allowedIpRanges != nil {
668679
for _, ipRange := range *allowedIpRanges {
669-
clusterResource.AllowedIpRanges = append(clusterResource.AllowedIpRanges, AllowedIpRangesResourceModel{
680+
tfClusterResource.AllowedIpRanges = append(tfClusterResource.AllowedIpRanges, AllowedIpRangesResourceModel{
670681
CidrBlock: ipRange.CidrBlock,
671682
Description: types.StringValue(ipRange.Description),
672683
})
673684
}
674685
}
675686

676-
if pt := cluster.CreatedAt; pt != nil {
677-
clusterResource.CreatedAt = types.StringValue(pt.String())
687+
if pt := apiCluster.CreatedAt; pt != nil {
688+
tfClusterResource.CreatedAt = types.StringValue(pt.String())
678689
}
679690

680-
if cluster.MaintenanceWindow != nil {
681-
clusterResource.MaintenanceWindow = &commonTerraform.MaintenanceWindow{
682-
IsEnabled: cluster.MaintenanceWindow.IsEnabled,
683-
StartDay: types.Int64PointerValue(utils.ToPointer(int64(*cluster.MaintenanceWindow.StartDay))),
684-
StartTime: types.StringPointerValue(cluster.MaintenanceWindow.StartTime),
691+
if apiCluster.MaintenanceWindow != nil {
692+
tfClusterResource.MaintenanceWindow = &commonTerraform.MaintenanceWindow{
693+
IsEnabled: apiCluster.MaintenanceWindow.IsEnabled,
694+
StartDay: types.Int64PointerValue(utils.ToPointer(int64(*apiCluster.MaintenanceWindow.StartDay))),
695+
StartTime: types.StringPointerValue(apiCluster.MaintenanceWindow.StartTime),
685696
}
686697
}
687698

688-
if cluster.PeAllowedPrincipalIds != nil {
689-
clusterResource.PeAllowedPrincipalIds = StringSliceToSet(utils.ToValue(&cluster.PeAllowedPrincipalIds))
699+
if apiCluster.PeAllowedPrincipalIds != nil {
700+
tfClusterResource.PeAllowedPrincipalIds = StringSliceToSet(utils.ToValue(&apiCluster.PeAllowedPrincipalIds))
690701
}
691702

692-
if cluster.ServiceAccountIds != nil {
693-
clusterResource.ServiceAccountIds = StringSliceToSet(utils.ToValue(&cluster.ServiceAccountIds))
703+
if apiCluster.ServiceAccountIds != nil {
704+
tfClusterResource.ServiceAccountIds = StringSliceToSet(utils.ToValue(&apiCluster.ServiceAccountIds))
694705
}
695706

696-
if cluster.PgBouncer != nil {
697-
clusterResource.PgBouncer = &PgBouncerModel{}
698-
*clusterResource.PgBouncer = PgBouncerModel{
699-
IsEnabled: cluster.PgBouncer.IsEnabled,
707+
if apiCluster.PgBouncer != nil {
708+
tfClusterResource.PgBouncer = &PgBouncerModel{}
709+
*tfClusterResource.PgBouncer = PgBouncerModel{
710+
IsEnabled: apiCluster.PgBouncer.IsEnabled,
700711
}
701712

702713
settingsElemType := map[string]attr.Type{"name": types.StringType, "operation": types.StringType, "value": types.StringType}
@@ -706,24 +717,24 @@ func (c *clusterResource) read(ctx context.Context, clusterResource *ClusterReso
706717
"value": basetypes.NewStringValue(""),
707718
})
708719

709-
if !cluster.PgBouncer.IsEnabled {
710-
clusterResource.PgBouncer.Settings = basetypes.NewSetNull(elem.Type(ctx))
711-
} else if cluster.PgBouncer.IsEnabled &&
712-
cluster.PgBouncer.Settings != nil &&
713-
len(*cluster.PgBouncer.Settings) == 0 {
714-
clusterResource.PgBouncer.Settings = basetypes.NewSetNull(elem.Type(ctx))
715-
} else if cluster.PgBouncer.Settings != nil && len(*cluster.PgBouncer.Settings) > 0 {
720+
if !apiCluster.PgBouncer.IsEnabled {
721+
tfClusterResource.PgBouncer.Settings = basetypes.NewSetNull(elem.Type(ctx))
722+
} else if apiCluster.PgBouncer.IsEnabled &&
723+
apiCluster.PgBouncer.Settings != nil &&
724+
len(*apiCluster.PgBouncer.Settings) == 0 {
725+
tfClusterResource.PgBouncer.Settings = basetypes.NewSetNull(elem.Type(ctx))
726+
} else if apiCluster.PgBouncer.Settings != nil && len(*apiCluster.PgBouncer.Settings) > 0 {
716727
settings := []attr.Value{}
717728

718-
for _, v := range *cluster.PgBouncer.Settings {
729+
for _, v := range *apiCluster.PgBouncer.Settings {
719730
object := basetypes.NewObjectValueMust(settingsElemType, map[string]attr.Value{
720731
"name": basetypes.NewStringValue(*v.Name),
721732
"operation": basetypes.NewStringValue(*v.Operation),
722733
"value": basetypes.NewStringValue(*v.Value),
723734
})
724735
settings = append(settings, object)
725736
}
726-
clusterResource.PgBouncer.Settings = basetypes.NewSetValueMust(elem.Type(ctx), settings)
737+
tfClusterResource.PgBouncer.Settings = basetypes.NewSetValueMust(elem.Type(ctx), settings)
727738
}
728739
}
729740

0 commit comments

Comments
 (0)