Skip to content

Commit

Permalink
feat: allow to purge additional_disk_space
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov committed Oct 30, 2024
1 parent 04b1dc9 commit ad77d80
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 20 deletions.
59 changes: 58 additions & 1 deletion internal/schemautil/custom_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import (
"strings"

"github.com/aiven/aiven-go-client/v2"
avngen "github.com/aiven/go-client-codegen"
"github.com/aiven/go-client-codegen/handler/service"
"github.com/docker/go-units"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"golang.org/x/exp/slices"

"github.com/aiven/terraform-provider-aiven/internal/common"
)

func CustomizeDiffGenericService(serviceType string) schema.CustomizeDiffFunc {
Expand All @@ -27,7 +31,10 @@ func CustomizeDiffGenericService(serviceType string) schema.CustomizeDiffFunc {
),
customdiff.IfValueChange("additional_disk_space",
ShouldNotBeEmpty,
CustomizeDiffCheckDiskSpace,
customdiff.Sequence(
CustomizeDiffCheckDiskSpace,
CustomizeDiffAdditionalDiskSpace,
),
),
customdiff.IfValueChange("service_integrations",
ShouldNotBeEmpty,
Expand Down Expand Up @@ -254,3 +261,53 @@ func checkForMultipleValues(v cty.Value) error {

return nil
}

// CustomizeDiffAdditionalDiskSpace
// 1. checks that additional_disk_space is not set if autoscaler is enabled
// 2. outputs a diff for a computed field, which otherwise would be suppressed when removed
func CustomizeDiffAdditionalDiskSpace(ctx context.Context, diff *schema.ResourceDiff, _ interface{}) error {
client, err := common.GenClient()
if err != nil {
return err
}

s, err := client.ServiceGet(ctx, diff.Get("project").(string), diff.Get("service_name").(string))
if avngen.IsNotFound(err) {
// The service does not exist, so we cannot check if autoscaler is enabled
return nil
}

if err != nil {
return err
}

isAutoscalerEnabled := false
for _, i := range s.ServiceIntegrations {
if i.IntegrationType == service.IntegrationTypeAutoscaler {
isAutoscalerEnabled = true
break
}
}

k := "additional_disk_space"
c := diff.GetRawConfig()
isSet := !(c.IsNull() || c.AsValueMap()[k].IsNull())
if isAutoscalerEnabled && isSet {
// Autoscaler is enabled, so we cannot set additional_disk_space
return ErrAutoscalerConflict
}

_, wasSet := diff.GetOk(k)
if !isAutoscalerEnabled && wasSet {
// We must output a diff for the computed field,
// which otherwise would be suppressed when removed
err = diff.SetNew(k, "0B")
if err != nil {
return err
}
// The disk_space_used will be recalculated
return diff.SetNewComputed("disk_space_used")
}

return nil
}
22 changes: 4 additions & 18 deletions internal/schemautil/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,31 +691,17 @@ func copyServicePropertiesFromAPIResponseToTerraform(
if s.DiskSpaceMb != nil {
diskSpace = *s.DiskSpaceMb
}
additionalDiskSpace := diskSpace - servicePlanParams.DiskSizeMBDefault

_, isAdditionalDiskSpaceSet := d.GetOk("additional_disk_space")
_, isDiskSpaceSet := d.GetOk("disk_space")

// Handles two different cases:
//
// 1. During import when neither `additional_disk_space` nor `disk_space` are set
// 2. During create / update when `additional_disk_space` is set
if additionalDiskSpace > 0 && (!isDiskSpaceSet || isAdditionalDiskSpaceSet) {
if err := d.Set("additional_disk_space", HumanReadableByteSize(additionalDiskSpace*units.MiB)); err != nil {
return err
}
if err := d.Set("disk_space", nil); err != nil {
return err
}
additionalDiskSpace := diskSpace - servicePlanParams.DiskSizeMBDefault
if err := d.Set("additional_disk_space", HumanReadableByteSize(additionalDiskSpace*units.MiB)); err != nil {
return err
}

_, isDiskSpaceSet := d.GetOk("disk_space")
if isDiskSpaceSet && diskSpace > 0 {
if err := d.Set("disk_space", HumanReadableByteSize(diskSpace*units.MiB)); err != nil {
return err
}
if err := d.Set("additional_disk_space", nil); err != nil {
return err
}
}

if err := d.Set("disk_space_used", HumanReadableByteSize(diskSpace*units.MiB)); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/sdkprovider/service/pg/pg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func TestAccAivenPG_deleting_additional_disk_size(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "maintenance_window_dow", "monday"),
resource.TestCheckResourceAttr(resourceName, "maintenance_window_time", "10:00:00"),
resource.TestCheckResourceAttr(resourceName, "disk_space_default", "80GiB"),
resource.TestCheckResourceAttr(resourceName, "disk_space_used", "100GiB"),
resource.TestCheckResourceAttr(resourceName, "disk_space_used", "80GiB"),
resource.TestCheckResourceAttr(resourceName, "termination_protection", "false"),
),
},
Expand Down

0 comments on commit ad77d80

Please sign in to comment.