From 5396de3ce0de320ffe7476e8521a89450d7ba7b1 Mon Sep 17 00:00:00 2001 From: Vandy Liu <33995460+vandyliu@users.noreply.github.com> Date: Fri, 15 Nov 2024 09:50:38 -0800 Subject: [PATCH 1/2] Allow scaling spec to be set dynamically (#171) --- .../provider/resources/resource_deployment.go | 11 +--- .../resources/resource_deployment_test.go | 61 ++++++++++++++++--- 2 files changed, 56 insertions(+), 16 deletions(-) diff --git a/internal/provider/resources/resource_deployment.go b/internal/provider/resources/resource_deployment.go index bddbb064..dc72e0e6 100644 --- a/internal/provider/resources/resource_deployment.go +++ b/internal/provider/resources/resource_deployment.go @@ -902,13 +902,6 @@ func validateHostedConfig(ctx context.Context, data *models.DeploymentResource) tflog.Error(ctx, "failed to convert hibernation spec", map[string]interface{}{"error": diags}) return diags } - if hibernationSpec.Override.IsNull() && hibernationSpec.Schedules.IsNull() { - diags.AddError( - "scaling_spec (hibernation) must have either override or schedules", - "Please provide either override or schedules in 'scaling_spec.hibernation_spec'", - ) - return diags - } } // Need to check worker_queues for hosted deployments have `astro_machine` and do not have `node_pool_id` @@ -1006,8 +999,8 @@ func RequestScalingSpec(ctx context.Context, scalingSpecObj types.Object) (*plat platformScalingSpec.HibernationSpec = &platform.DeploymentHibernationSpecRequest{} if hibernationSpec.Override.IsNull() && hibernationSpec.Schedules.IsNull() { - // If the hibernation spec is set but both override and schedules are not set, return an empty hibernation spec for the request - return platformScalingSpec, nil + // If the hibernation spec is set but both override and schedules are not set, return an error + return platformScalingSpec, diag.Diagnostics{diag.NewErrorDiagnostic("scaling_spec.hibernation_spec must have either override or schedules", "Please provide either override or schedules in 'scaling_spec.hibernation_spec")} } if !hibernationSpec.Override.IsNull() { var override models.HibernationSpecOverride diff --git a/internal/provider/resources/resource_deployment_test.go b/internal/provider/resources/resource_deployment_test.go index 2d09329e..f824c172 100644 --- a/internal/provider/resources/resource_deployment_test.go +++ b/internal/provider/resources/resource_deployment_test.go @@ -374,13 +374,6 @@ func TestAcc_ResourceDeploymentStandardScalingSpec(t *testing.T) { ), ExpectError: regexp.MustCompile(`Inappropriate value for attribute "scaling_spec"`), }, - { - Config: astronomerprovider.ProviderConfig(t, astronomerprovider.HOSTED) + developmentDeployment(scalingSpecDeploymentName, - `scaling_spec = { - hibernation_spec = {} - }`), - ExpectError: regexp.MustCompile(`scaling_spec \(hibernation\) must have either override or schedules`), - }, { Config: astronomerprovider.ProviderConfig(t, astronomerprovider.HOSTED) + developmentDeployment(scalingSpecDeploymentName, ` @@ -496,6 +489,60 @@ func TestAcc_ResourceDeploymentStandardScalingSpec(t *testing.T) { resource.TestCheckResourceAttr(scalingSpecResourceVar, "scaling_spec.hibernation_spec.schedules.0.wake_at_cron", "59 * * * *"), ), }, + // Dynamically creating scaling spec depending on variable: setting it to null + { + Config: astronomerprovider.ProviderConfig(t, astronomerprovider.HOSTED) + developmentDeployment(scalingSpecDeploymentName, + ` `), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(scalingSpecResourceVar, "scaling_spec.%", "0"), + ), + }, + { + Config: `variable "environment_name" { + type = string + default = "dev" + }` + + astronomerprovider.ProviderConfig(t, astronomerprovider.HOSTED) + developmentDeployment(scalingSpecDeploymentName, + `scaling_spec = var.environment_name != "prd" ? { + hibernation_spec = { + schedules = [{ + is_enabled = true + hibernate_at_cron = "0 22 * * *" + wake_at_cron = "0 14 * * *" + }] + } + } : null`), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(scalingSpecResourceVar, "scaling_spec.hibernation_spec.schedules.0.is_enabled", "true"), + ), + }, + // Dynamically creating scaling spec depending on variable: setting it to null + { + Config: astronomerprovider.ProviderConfig(t, astronomerprovider.HOSTED) + developmentDeployment(scalingSpecDeploymentName, + ` `), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(scalingSpecResourceVar, "scaling_spec.%", "0"), + ), + }, + { + Config: `variable "environment_name" { + type = string + default = "prd" + }` + + astronomerprovider.ProviderConfig(t, astronomerprovider.HOSTED) + developmentDeployment(scalingSpecDeploymentName, + `scaling_spec = var.environment_name != "prd" ? { + hibernation_spec = { + schedules = [{ + is_enabled = true + hibernate_at_cron = "0 22 * * *" + wake_at_cron = "0 14 * * *" + }] + } + } : null`), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(scalingSpecResourceVar, "scaling_spec.%", "0"), // scaling spec should be null + ), + }, // Import existing deployment and check it is correctly imported - https://stackoverflow.com/questions/68824711/how-can-i-test-terraform-import-in-acceptance-tests { ResourceName: scalingSpecResourceVar, From 36f4aaf5da8debeac6d9cf7abf8013a41c5cfb47 Mon Sep 17 00:00:00 2001 From: Vandy Liu <33995460+vandyliu@users.noreply.github.com> Date: Wed, 20 Nov 2024 11:03:54 -0800 Subject: [PATCH 2/2] Allow dynamic isDevelopmentMode for deployment resource (#173) --- internal/provider/resources/resource_deployment.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/internal/provider/resources/resource_deployment.go b/internal/provider/resources/resource_deployment.go index dc72e0e6..6b15e272 100644 --- a/internal/provider/resources/resource_deployment.go +++ b/internal/provider/resources/resource_deployment.go @@ -872,14 +872,6 @@ func validateHostedConfig(ctx context.Context, data *models.DeploymentResource) ) } - // Need to check that scaling_spec is only for is_development_mode set to true - if !data.IsDevelopmentMode.ValueBool() && !data.ScalingSpec.IsNull() { - diags.AddError( - "scaling_spec (hibernation) is only supported for is_development_mode set to true", - "Either set is_development_mode to true or remove scaling_spec", - ) - } - // Need to check that scaling_spec has either override or schedules if !data.ScalingSpec.IsNull() { var scalingSpec models.DeploymentScalingSpec