diff --git a/internal/schemautil/custom_diff.go b/internal/schemautil/custom_diff.go index 5f149295b..9603f2406 100644 --- a/internal/schemautil/custom_diff.go +++ b/internal/schemautil/custom_diff.go @@ -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 { @@ -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, @@ -254,3 +261,31 @@ func checkForMultipleValues(v cty.Value) error { return nil } + +// CustomizeDiffAdditionalDiskSpace checks that additional_disk_space is not set if autoscaler is enabled +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 + } + + for _, i := range s.ServiceIntegrations { + if i.IntegrationType == service.IntegrationTypeAutoscaler { + // Autoscaler is enabled, so we cannot set additional_disk_space + return ErrAutoscalerConflict + } + } + + // Autoscaler is not enabled, so additional_disk_space will be 0 + return diff.SetNew("additional_disk_space", "0") +} diff --git a/internal/sdkprovider/service/pg/pg_test.go b/internal/sdkprovider/service/pg/pg_test.go index 22eb37a56..13290a95b 100644 --- a/internal/sdkprovider/service/pg/pg_test.go +++ b/internal/sdkprovider/service/pg/pg_test.go @@ -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"), ), },