Skip to content

Commit

Permalink
Allow scaling spec to be set dynamically (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
vandyliu authored Nov 15, 2024
1 parent 40a4737 commit 5396de3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
11 changes: 2 additions & 9 deletions internal/provider/resources/resource_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
Expand Down
61 changes: 54 additions & 7 deletions internal/provider/resources/resource_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
`
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 5396de3

Please sign in to comment.