@@ -20,7 +20,6 @@ import (
20
20
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
21
21
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
22
22
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
23
- "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
24
23
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
25
24
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
26
25
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -294,9 +293,7 @@ func (r *sensorUpdatePolicyResource) Schema(
294
293
},
295
294
"timezone" : schema.StringAttribute {
296
295
Optional : true ,
297
- Computed : true ,
298
296
Description : "The time zones that will be used for the time blocks. Only set when enabled is true." ,
299
- Default : stringdefault .StaticString ("Etc/UTC" ),
300
297
Validators : []validator.String {
301
298
stringvalidator .OneOf (timezones ... ),
302
299
},
@@ -383,20 +380,22 @@ func (r *sensorUpdatePolicyResource) Create(
383
380
}
384
381
policyParams .Body .Resources [0 ].Settings .UninstallProtection = uninstallProtection
385
382
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 ()
389
387
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
+ }
396
394
397
- updateSchedular .Schedules = updateSchedules
395
+ updateSchedular .Schedules = updateSchedules
396
+ }
397
+ policyParams .Body .Resources [0 ].Settings .Scheduler = & updateSchedular
398
398
}
399
- policyParams .Body .Resources [0 ].Settings .Scheduler = & updateSchedular
400
399
401
400
policy , err := r .client .SensorUpdatePolicies .CreateSensorUpdatePoliciesV2 (& policyParams )
402
401
@@ -526,6 +525,9 @@ func (r *sensorUpdatePolicyResource) Read(
526
525
if policyResource .Settings .Scheduler != nil {
527
526
state .Schedule = policySchedule {}
528
527
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
529
531
if state .Schedule .Enabled .ValueBool () {
530
532
state .Schedule .Timezone = types .StringValue (* policyResource .Settings .Scheduler .Timezone )
531
533
@@ -676,6 +678,18 @@ func (r *sensorUpdatePolicyResource) Update(
676
678
updateSchedular .Timezone = plan .Schedule .Timezone .ValueStringPointer ()
677
679
updateSchedular .Enabled = plan .Schedule .Enabled .ValueBoolPointer ()
678
680
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
+
679
693
if len (plan .Schedule .TimeBlocks ) > 0 {
680
694
updateSchedules , diags := createUpdateSchedules (ctx , plan .Schedule .TimeBlocks )
681
695
resp .Diagnostics .Append (diags ... )
0 commit comments