@@ -41,16 +41,6 @@ func resourceTFEWorkspace() *schema.Resource {
41
41
},
42
42
43
43
CustomizeDiff : func (c context.Context , d * schema.ResourceDiff , meta interface {}) error {
44
- // NOTE: execution mode and agent_pool_id must be set to default first before calling
45
- // the validation functions
46
- if err := setComputedDefaults (c , d ); err != nil {
47
- return err
48
- }
49
-
50
- if err := overwriteDefaultExecutionMode (c , d ); err != nil {
51
- return err
52
- }
53
-
54
44
if err := validateAgentExecution (c , d ); err != nil {
55
45
return err
56
46
}
@@ -128,24 +118,6 @@ func resourceTFEWorkspace() *schema.Resource {
128
118
),
129
119
},
130
120
131
- "setting_overwrites" : {
132
- Type : schema .TypeList ,
133
- Computed : true ,
134
- Elem : & schema.Resource {
135
- Schema : map [string ]* schema.Schema {
136
- "execution_mode" : {
137
- Type : schema .TypeBool ,
138
- Required : true ,
139
- },
140
-
141
- "agent_pool" : {
142
- Type : schema .TypeBool ,
143
- Required : true ,
144
- },
145
- },
146
- },
147
- },
148
-
149
121
"file_triggers_enabled" : {
150
122
Type : schema .TypeBool ,
151
123
Optional : true ,
@@ -519,18 +491,6 @@ func resourceTFEWorkspaceRead(d *schema.ResourceData, meta interface{}) error {
519
491
d .Set ("organization" , workspace .Organization .Name )
520
492
d .Set ("resource_count" , workspace .ResourceCount )
521
493
522
- var settingOverwrites []interface {}
523
- if workspace .SettingOverwrites != nil {
524
- settingOverwrites = append (settingOverwrites , map [string ]interface {}{
525
- "execution_mode" : workspace .SettingOverwrites .ExecutionMode ,
526
- "agent_pool" : workspace .SettingOverwrites .AgentPool ,
527
- })
528
- }
529
- err = d .Set ("setting_overwrites" , settingOverwrites )
530
- if err != nil {
531
- return err
532
- }
533
-
534
494
if workspace .Links ["self-html" ] != nil {
535
495
baseAPI := config .Client .BaseURL ()
536
496
htmlURL := url.URL {
@@ -600,18 +560,15 @@ func resourceTFEWorkspaceUpdate(d *schema.ResourceData, meta interface{}) error
600
560
config := meta .(ConfiguredClient )
601
561
id := d .Id ()
602
562
603
- workspaceControlsAgentPool := isSettingOverwritten ("agent_pool" , d )
604
- workspaceControlsExecutionMode := isSettingOverwritten ("execution_mode" , d )
605
-
606
563
if d .HasChange ("name" ) || d .HasChange ("auto_apply" ) || d .HasChange ("auto_apply_run_trigger" ) || d .HasChange ("queue_all_runs" ) ||
607
564
d .HasChange ("terraform_version" ) || d .HasChange ("working_directory" ) ||
608
565
d .HasChange ("vcs_repo" ) || d .HasChange ("file_triggers_enabled" ) ||
609
566
d .HasChange ("trigger_prefixes" ) || d .HasChange ("trigger_patterns" ) ||
610
567
d .HasChange ("allow_destroy_plan" ) || d .HasChange ("speculative_enabled" ) ||
611
- d .HasChange ("operations" ) || ( d .HasChange ("execution_mode" ) && workspaceControlsExecutionMode ) ||
612
- d .HasChange ("description" ) || ( d .HasChange ("agent_pool_id" ) && workspaceControlsAgentPool ) ||
568
+ d .HasChange ("operations" ) || d .HasChange ("execution_mode" ) ||
569
+ d .HasChange ("description" ) || d .HasChange ("agent_pool_id" ) ||
613
570
d .HasChange ("global_remote_state" ) || d .HasChange ("structured_run_output_enabled" ) ||
614
- d .HasChange ("assessments_enabled" ) || d .HasChange ("project_id" ) || d . HasChange ( "setting_overwrites" ) {
571
+ d .HasChange ("assessments_enabled" ) || d .HasChange ("project_id" ) {
615
572
// Create a new options struct.
616
573
options := tfe.WorkspaceUpdateOptions {
617
574
Name : tfe .String (d .Get ("name" ).(string )),
@@ -639,7 +596,7 @@ func resourceTFEWorkspaceUpdate(d *schema.ResourceData, meta interface{}) error
639
596
}
640
597
}
641
598
642
- if ( d .HasChange ("agent_pool_id" ) && workspaceControlsAgentPool ) || d . HasChange ( "setting_overwrites " ) {
599
+ if d .HasChange ("agent_pool_id" ) {
643
600
// Need the raw configuration value of the agent_pool_id because when the workspace's execution mode is set
644
601
// to default, we can't know for certain what the default value of the agent pool will be. This means we can
645
602
// only set the agent_pool_id as "NewComputed", meaning that the value returned by the ResourceData will be
@@ -650,40 +607,21 @@ func resourceTFEWorkspaceUpdate(d *schema.ResourceData, meta interface{}) error
650
607
// be sufficient
651
608
if ! agentPoolID .IsNull () {
652
609
options .AgentPoolID = tfe .String (agentPoolID .AsString ())
610
+
653
611
// set setting overwrites
654
612
options .SettingOverwrites = & tfe.WorkspaceSettingOverwritesOptions {
655
613
AgentPool : tfe .Bool (true ),
656
614
}
657
615
}
658
616
}
659
617
660
- if (d .HasChange ("execution_mode" ) && workspaceControlsExecutionMode ) || d .HasChange ("setting_overwrites" ) {
661
- executionMode := d .GetRawConfig ().GetAttr ("execution_mode" )
662
-
663
- // if the TFE instance knows about setting-overwrites
664
- if _ , ok := d .GetOk ("setting_overwrites" ); ok {
665
- if options .SettingOverwrites == nil {
666
- // initialize setting-overwrites if it has not been initialized already
667
- options .SettingOverwrites = & tfe.WorkspaceSettingOverwritesOptions {}
668
- }
669
-
670
- // if execution mode is currently unset...
671
- operations := d .GetRawConfig ().GetAttr ("operations" )
672
- if executionMode .IsNull () && operations .IsNull () {
673
- // set execution mode to default (inherit from the parent organization/project)
674
- options .SettingOverwrites .ExecutionMode = tfe .Bool (false )
675
- }
618
+ if d .HasChange ("execution_mode" ) {
619
+ if v , ok := d .GetOk ("execution_mode" ); ok {
620
+ options .ExecutionMode = tfe .String (v .(string ))
676
621
677
- // if execution has been set...
678
- if ! executionMode .IsNull () {
679
- // set the execution mode to be "overwritten"
680
- options .SettingOverwrites .ExecutionMode = tfe .Bool (true )
681
- options .ExecutionMode = tfe .String (executionMode .AsString ())
682
- }
683
- } else {
684
- // since the TFE instance doesn't know about setting-overwrites, set the execution mode as normal
685
- if v , ok := d .GetOk ("execution_mode" ); ok {
686
- options .ExecutionMode = tfe .String (v .(string ))
622
+ // set setting overwrites
623
+ options .SettingOverwrites = & tfe.WorkspaceSettingOverwritesOptions {
624
+ ExecutionMode : tfe .Bool (true ),
687
625
}
688
626
}
689
627
}
@@ -942,105 +880,6 @@ func resourceTFEWorkspaceDelete(d *schema.ResourceData, meta interface{}) error
942
880
return nil
943
881
}
944
882
945
- // since execution_mode and agent_pool_id are marked as Optional: true, and
946
- // Computed: true, unsetting the execution_mode/agent_pool_id in the config
947
- // after it's been set to a valid value is not detected by ResourceDiff so
948
- // we need to read the value from RawConfig instead
949
- func setComputedDefaults (_ context.Context , d * schema.ResourceDiff ) error {
950
- configMap := d .GetRawConfig ().AsValueMap ()
951
- operations , operationsReadOk := configMap ["operations" ]
952
- executionMode , executionModeReadOk := configMap ["execution_mode" ]
953
- executionModeState := d .Get ("execution_mode" )
954
- agentPoolID , agentPoolIDReadOk := configMap ["agent_pool_id" ]
955
- agentPoolIDState := d .Get ("agent_pool_id" )
956
-
957
- // forcefully setting the defaults is only necessary when an existing workspace is being updated
958
- isRecordPersisted := d .Id () != ""
959
- if isRecordPersisted != true {
960
- return nil
961
- }
962
-
963
- if ! operationsReadOk || ! executionModeReadOk || ! agentPoolIDReadOk {
964
- return nil
965
- }
966
-
967
- // find out if the current TFE version supports setting-overwrites
968
- currentTfeSupportsSettingOverwrites := false
969
- if v , ok := d .GetOkExists ("setting_overwrites" ); ok {
970
- settingOverwrites := v .([]interface {})
971
- currentTfeSupportsSettingOverwrites = len (settingOverwrites ) != 0
972
- }
973
- executionModeWasUnset := executionModeState != "remote" && operations .IsNull () && executionMode .IsNull ()
974
- agentPoolWasUnset := (agentPoolID .IsNull () || ! agentPoolIDReadOk ) && agentPoolIDState != ""
975
-
976
- // if current version of TFE does not support setting-overwrites, update the computed values if either of
977
- // them have been set to a nil value
978
- if ! currentTfeSupportsSettingOverwrites {
979
- if executionModeWasUnset {
980
- err := d .SetNew ("execution_mode" , "remote" )
981
- if err != nil {
982
- return fmt .Errorf ("failed to set execution_mode: %w" , err )
983
- }
984
- }
985
-
986
- if agentPoolWasUnset {
987
- err := d .SetNew ("agent_pool_id" , nil )
988
- if err != nil {
989
- return fmt .Errorf ("failed to clear agent_pool_id: %w" , err )
990
- }
991
- }
992
- return nil
993
- }
994
-
995
- return nil
996
- }
997
-
998
- func overwriteDefaultExecutionMode (_ context.Context , d * schema.ResourceDiff ) error {
999
- configMap := d .GetRawConfig ().AsValueMap ()
1000
- executionMode , executionModeReadOk := configMap ["execution_mode" ]
1001
- operations , operationsReadOk := configMap ["operations" ]
1002
-
1003
- // if the execution mode was previously overwritten, but being set to default in the current config, make sure that
1004
- // the setting overwrites will be set to false and the execution_mode and agent_pool_id are set to computed as we
1005
- // are not able to tell what the default execution mode is until after we update the workspace
1006
- if executionMode .IsNull () && operations .IsNull () {
1007
- if v , ok := d .GetOk ("setting_overwrites" ); ok {
1008
- settingOverwrites := v .([]interface {})[0 ].(map [string ]interface {})
1009
- if settingOverwrites ["execution_mode" ] == true {
1010
- newSettingOverwrites := map [string ]interface {}{
1011
- "execution_mode" : false ,
1012
- "agent_pool" : false ,
1013
- }
1014
- d .SetNew ("setting_overwrites" , []interface {}{newSettingOverwrites })
1015
- d .SetNewComputed ("execution_mode" )
1016
- d .SetNewComputed ("agent_pool_id" )
1017
- }
1018
- return nil
1019
- }
1020
- }
1021
-
1022
- if (executionMode .IsNull () || ! executionModeReadOk ) && (operations .IsNull () || ! operationsReadOk ) {
1023
- return nil
1024
- }
1025
-
1026
- // if the default execution mode and the execution_mode in the config matches, nothing will happen
1027
- // unless we inform TFE that the new execution_mode is meant to overwrite the current execution mode
1028
- if v , ok := d .GetOk ("setting_overwrites" ); ok {
1029
- settingOverwrites := v .([]interface {})[0 ].(map [string ]interface {})
1030
- if settingOverwrites ["execution_mode" ] == false {
1031
- agentPoolID , agentPoolReadOk := configMap ["agent_pool_id" ]
1032
-
1033
- newSettingOverwrites := map [string ]interface {}{
1034
- "execution_mode" : true ,
1035
- "agent_pool" : agentPoolID .IsKnown () && agentPoolReadOk ,
1036
- }
1037
- d .SetNew ("setting_overwrites" , []interface {}{newSettingOverwrites })
1038
- }
1039
- }
1040
-
1041
- return nil
1042
- }
1043
-
1044
883
// An agent pool can only be specified when execution_mode is set to "agent". You currently cannot specify a
1045
884
// schema validation based on a different argument's value, so we do so here at plan time instead.
1046
885
func validateAgentExecution (_ context.Context , d * schema.ResourceDiff ) error {
@@ -1157,23 +996,3 @@ func errWorkspaceResourceCountCheck(workspaceID string, resourceCount int) error
1157
996
}
1158
997
return nil
1159
998
}
1160
-
1161
- // isSettingOverwritten checks if the value of a setting is being overwritten by the workspace or not. in other words,
1162
- // if the value of the setting is determined by the workspace, this function will return true for that setting
1163
- func isSettingOverwritten (setting string , d * schema.ResourceData ) bool {
1164
- if v , ok := d .GetOk ("setting_overwrites" ); ok {
1165
- settingOverwrites := v .([]interface {})
1166
- if len (settingOverwrites ) != 1 {
1167
- // current TFE version does not support setting-overwrites, so all settings are set at workspace-level
1168
- return true
1169
- }
1170
-
1171
- // check the value of the setting
1172
- settingOverwritesValue := settingOverwrites [0 ].(map [string ]interface {})
1173
- executionModeOverwritten := settingOverwritesValue [setting ]
1174
-
1175
- return executionModeOverwritten .(bool )
1176
- }
1177
-
1178
- return true
1179
- }
0 commit comments