Skip to content

Commit d43ddf4

Browse files
committed
make sensor_update_policy idempotent when schedule is false
1 parent 5abf1f8 commit d43ddf4

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

internal/provider/sensor_update_policy_resource.go

+28-14
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
2121
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
2222
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
23-
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
2423
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
2524
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
2625
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -294,9 +293,7 @@ func (r *sensorUpdatePolicyResource) Schema(
294293
},
295294
"timezone": schema.StringAttribute{
296295
Optional: true,
297-
Computed: true,
298296
Description: "The time zones that will be used for the time blocks. Only set when enabled is true.",
299-
Default: stringdefault.StaticString("Etc/UTC"),
300297
Validators: []validator.String{
301298
stringvalidator.OneOf(timezones...),
302299
},
@@ -383,20 +380,22 @@ func (r *sensorUpdatePolicyResource) Create(
383380
}
384381
policyParams.Body.Resources[0].Settings.UninstallProtection = uninstallProtection
385382

386-
updateSchedular := models.PolicySensorUpdateScheduler{}
387-
updateSchedular.Enabled = plan.Schedule.Enabled.ValueBoolPointer()
388-
updateSchedular.Timezone = plan.Schedule.Timezone.ValueStringPointer()
383+
if plan.Schedule.Enabled.ValueBool() {
384+
updateSchedular := models.PolicySensorUpdateScheduler{}
385+
updateSchedular.Enabled = plan.Schedule.Enabled.ValueBoolPointer()
386+
updateSchedular.Timezone = plan.Schedule.Timezone.ValueStringPointer()
389387

390-
if len(plan.Schedule.TimeBlocks) > 0 {
391-
updateSchedules, diags := createUpdateSchedules(ctx, plan.Schedule.TimeBlocks)
392-
resp.Diagnostics.Append(diags...)
393-
if resp.Diagnostics.HasError() {
394-
return
395-
}
388+
if len(plan.Schedule.TimeBlocks) > 0 {
389+
updateSchedules, diags := createUpdateSchedules(ctx, plan.Schedule.TimeBlocks)
390+
resp.Diagnostics.Append(diags...)
391+
if resp.Diagnostics.HasError() {
392+
return
393+
}
396394

397-
updateSchedular.Schedules = updateSchedules
395+
updateSchedular.Schedules = updateSchedules
396+
}
397+
policyParams.Body.Resources[0].Settings.Scheduler = &updateSchedular
398398
}
399-
policyParams.Body.Resources[0].Settings.Scheduler = &updateSchedular
400399

401400
policy, err := r.client.SensorUpdatePolicies.CreateSensorUpdatePoliciesV2(&policyParams)
402401

@@ -526,6 +525,9 @@ func (r *sensorUpdatePolicyResource) Read(
526525
if policyResource.Settings.Scheduler != nil {
527526
state.Schedule = policySchedule{}
528527
state.Schedule.Enabled = types.BoolValue(*policyResource.Settings.Scheduler.Enabled)
528+
529+
// ignore the timzezone and time_blocks if the schedule is DISABLED
530+
// this allows terraform import to work correctly
529531
if state.Schedule.Enabled.ValueBool() {
530532
state.Schedule.Timezone = types.StringValue(*policyResource.Settings.Scheduler.Timezone)
531533

@@ -676,6 +678,18 @@ func (r *sensorUpdatePolicyResource) Update(
676678
updateSchedular.Timezone = plan.Schedule.Timezone.ValueStringPointer()
677679
updateSchedular.Enabled = plan.Schedule.Enabled.ValueBoolPointer()
678680

681+
// WORKAROUND: The API requires a timezone when we are trying to enable/diable the Scheduler
682+
// due to other limitiations when it comes to imports we need to allow timezone to be null.
683+
// Everything should exist in state etc so knowingly adding drift isn't the best, but
684+
// when the schedule is disabled the timezone does not matter.
685+
// When the schedule is enabled the timezone is required so we will not have issues.
686+
// Permanent fix is to update the API to allow us to provide a null value for timezone when
687+
// the schedule is disabled.
688+
defaultTimezone := "Etc/UTC"
689+
if !*updateSchedular.Enabled && updateSchedular.Timezone == nil {
690+
updateSchedular.Timezone = &defaultTimezone
691+
}
692+
679693
if len(plan.Schedule.TimeBlocks) > 0 {
680694
updateSchedules, diags := createUpdateSchedules(ctx, plan.Schedule.TimeBlocks)
681695
resp.Diagnostics.Append(diags...)

0 commit comments

Comments
 (0)