Skip to content

Commit dd50c7c

Browse files
committed
Remove setting_overwrites from tfe_workspace
1 parent 9543098 commit dd50c7c

File tree

3 files changed

+11
-316
lines changed

3 files changed

+11
-316
lines changed

internal/provider/resource_tfe_workspace.go

Lines changed: 11 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,6 @@ func resourceTFEWorkspace() *schema.Resource {
4141
},
4242

4343
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-
5444
if err := validateAgentExecution(c, d); err != nil {
5545
return err
5646
}
@@ -128,24 +118,6 @@ func resourceTFEWorkspace() *schema.Resource {
128118
),
129119
},
130120

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-
149121
"file_triggers_enabled": {
150122
Type: schema.TypeBool,
151123
Optional: true,
@@ -519,18 +491,6 @@ func resourceTFEWorkspaceRead(d *schema.ResourceData, meta interface{}) error {
519491
d.Set("organization", workspace.Organization.Name)
520492
d.Set("resource_count", workspace.ResourceCount)
521493

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-
534494
if workspace.Links["self-html"] != nil {
535495
baseAPI := config.Client.BaseURL()
536496
htmlURL := url.URL{
@@ -600,18 +560,15 @@ func resourceTFEWorkspaceUpdate(d *schema.ResourceData, meta interface{}) error
600560
config := meta.(ConfiguredClient)
601561
id := d.Id()
602562

603-
workspaceControlsAgentPool := isSettingOverwritten("agent_pool", d)
604-
workspaceControlsExecutionMode := isSettingOverwritten("execution_mode", d)
605-
606563
if d.HasChange("name") || d.HasChange("auto_apply") || d.HasChange("auto_apply_run_trigger") || d.HasChange("queue_all_runs") ||
607564
d.HasChange("terraform_version") || d.HasChange("working_directory") ||
608565
d.HasChange("vcs_repo") || d.HasChange("file_triggers_enabled") ||
609566
d.HasChange("trigger_prefixes") || d.HasChange("trigger_patterns") ||
610567
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") ||
613570
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") {
615572
// Create a new options struct.
616573
options := tfe.WorkspaceUpdateOptions{
617574
Name: tfe.String(d.Get("name").(string)),
@@ -639,7 +596,7 @@ func resourceTFEWorkspaceUpdate(d *schema.ResourceData, meta interface{}) error
639596
}
640597
}
641598

642-
if (d.HasChange("agent_pool_id") && workspaceControlsAgentPool) || d.HasChange("setting_overwrites") {
599+
if d.HasChange("agent_pool_id") {
643600
// Need the raw configuration value of the agent_pool_id because when the workspace's execution mode is set
644601
// to default, we can't know for certain what the default value of the agent pool will be. This means we can
645602
// 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
650607
// be sufficient
651608
if !agentPoolID.IsNull() {
652609
options.AgentPoolID = tfe.String(agentPoolID.AsString())
610+
653611
// set setting overwrites
654612
options.SettingOverwrites = &tfe.WorkspaceSettingOverwritesOptions{
655613
AgentPool: tfe.Bool(true),
656614
}
657615
}
658616
}
659617

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))
676621

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),
687625
}
688626
}
689627
}
@@ -942,105 +880,6 @@ func resourceTFEWorkspaceDelete(d *schema.ResourceData, meta interface{}) error
942880
return nil
943881
}
944882

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-
1044883
// An agent pool can only be specified when execution_mode is set to "agent". You currently cannot specify a
1045884
// schema validation based on a different argument's value, so we do so here at plan time instead.
1046885
func validateAgentExecution(_ context.Context, d *schema.ResourceDiff) error {
@@ -1157,23 +996,3 @@ func errWorkspaceResourceCountCheck(workspaceID string, resourceCount int) error
1157996
}
1158997
return nil
1159998
}
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-
}

internal/provider/resource_tfe_workspace_test.go

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,111 +1927,6 @@ func TestAccTFEWorkspace_operationsAndExecutionModeInteroperability(t *testing.T
19271927
})
19281928
}
19291929

1930-
func TestAccTFEWorkspace_unsetExecutionMode(t *testing.T) {
1931-
skipIfEnterprise(t)
1932-
1933-
tfeClient, err := getClientUsingEnv()
1934-
if err != nil {
1935-
t.Fatal(err)
1936-
}
1937-
1938-
org, orgCleanup := createBusinessOrganization(t, tfeClient)
1939-
t.Cleanup(orgCleanup)
1940-
1941-
workspace := &tfe.Workspace{}
1942-
1943-
resource.Test(t, resource.TestCase{
1944-
PreCheck: func() { testAccPreCheck(t) },
1945-
Providers: testAccProviders,
1946-
CheckDestroy: testAccCheckTFEWorkspaceDestroy,
1947-
Steps: []resource.TestStep{
1948-
{
1949-
Config: testAccTFEWorkspace_executionModeAgent(org.Name),
1950-
Check: resource.ComposeTestCheckFunc(
1951-
testAccCheckTFEWorkspaceExists(
1952-
"tfe_workspace.foobar", workspace, testAccProvider),
1953-
resource.TestCheckResourceAttr(
1954-
"tfe_workspace.foobar", "operations", "true"),
1955-
resource.TestCheckResourceAttr(
1956-
"tfe_workspace.foobar", "execution_mode", "agent"),
1957-
resource.TestCheckResourceAttrSet(
1958-
"tfe_workspace.foobar", "agent_pool_id"),
1959-
),
1960-
},
1961-
{
1962-
Config: testAccTFEWorkspace_executionModeNull(org.Name),
1963-
Check: resource.ComposeTestCheckFunc(
1964-
testAccCheckTFEWorkspaceExists(
1965-
"tfe_workspace.foobar", workspace, testAccProvider),
1966-
resource.TestCheckResourceAttr(
1967-
"tfe_workspace.foobar", "operations", "true"),
1968-
resource.TestCheckResourceAttr(
1969-
"tfe_workspace.foobar", "execution_mode", "remote"),
1970-
resource.TestCheckResourceAttr(
1971-
"tfe_workspace.foobar", "agent_pool_id", ""),
1972-
),
1973-
},
1974-
},
1975-
})
1976-
}
1977-
1978-
func TestAccTFEWorkspace_unsetExecutionModeWithOrgLevelDefault(t *testing.T) {
1979-
skipIfEnterprise(t)
1980-
1981-
tfeClient, err := getClientUsingEnv()
1982-
if err != nil {
1983-
t.Fatal(err)
1984-
}
1985-
1986-
org, agentPool, orgCleanup := createBusinessOrganizationWithAgentDefaultExecutionMode(t, tfeClient)
1987-
t.Cleanup(orgCleanup)
1988-
1989-
workspace := &tfe.Workspace{}
1990-
1991-
resource.Test(t, resource.TestCase{
1992-
PreCheck: func() { testAccPreCheck(t) },
1993-
Providers: testAccProviders,
1994-
CheckDestroy: testAccCheckTFEWorkspaceDestroy,
1995-
Steps: []resource.TestStep{
1996-
{
1997-
Config: testAccTFEWorkspace_executionModeAgent(org.Name),
1998-
Check: resource.ComposeTestCheckFunc(
1999-
testAccCheckTFEWorkspaceExists(
2000-
"tfe_workspace.foobar", workspace, testAccProvider),
2001-
resource.TestCheckResourceAttr(
2002-
"tfe_workspace.foobar", "operations", "true"),
2003-
resource.TestCheckResourceAttr(
2004-
"tfe_workspace.foobar", "execution_mode", "agent"),
2005-
resource.TestCheckResourceAttrSet(
2006-
"tfe_workspace.foobar", "agent_pool_id"),
2007-
resource.TestCheckResourceAttr(
2008-
"tfe_workspace.foobar", "setting_overwrites.0.execution_mode", "true"),
2009-
resource.TestCheckResourceAttr(
2010-
"tfe_workspace.foobar", "setting_overwrites.0.agent_pool", "true"),
2011-
),
2012-
},
2013-
{
2014-
Config: testAccTFEWorkspace_executionModeNull(org.Name),
2015-
Check: resource.ComposeTestCheckFunc(
2016-
testAccCheckTFEWorkspaceExists(
2017-
"tfe_workspace.foobar", workspace, testAccProvider),
2018-
resource.TestCheckResourceAttr(
2019-
"tfe_workspace.foobar", "operations", "true"),
2020-
resource.TestCheckResourceAttr(
2021-
"tfe_workspace.foobar", "execution_mode", "agent"),
2022-
// workspace should now be using the organization default agent pool
2023-
resource.TestCheckResourceAttr(
2024-
"tfe_workspace.foobar", "agent_pool_id", agentPool.ID),
2025-
resource.TestCheckResourceAttr(
2026-
"tfe_workspace.foobar", "setting_overwrites.0.execution_mode", "false"),
2027-
resource.TestCheckResourceAttr(
2028-
"tfe_workspace.foobar", "setting_overwrites.0.agent_pool", "false"),
2029-
),
2030-
},
2031-
},
2032-
})
2033-
}
2034-
20351930
func TestAccTFEWorkspace_globalRemoteState(t *testing.T) {
20361931
workspace := &tfe.Workspace{}
20371932
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

0 commit comments

Comments
 (0)