Skip to content

Commit bab158b

Browse files
committed
Ensure support for TFE when unsetting execution_mode
Because organization defaults do not apply to older versions of TFE, the client needs to set this value to the previous default, "remote"
1 parent 8363b46 commit bab158b

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

internal/provider/resource_tfe_workspace_settings.go

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ var overwritesElementType = types.ObjectType{
3636
}
3737

3838
type workspaceSettings struct {
39-
config ConfiguredClient
39+
config ConfiguredClient
40+
supportsOverwrites bool
4041
}
4142

4243
type modelWorkspaceSettings struct {
@@ -117,6 +118,11 @@ func (m revertOverwritesIfExecutionModeUnset) PlanModifyList(ctx context.Context
117118
resp.Diagnostics.Append(req.Config.Get(ctx, &configured)...)
118119
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
119120

121+
// Check if overwrites are supported by the platform
122+
if state.Overwrites.IsNull() {
123+
return
124+
}
125+
120126
overwritesState := make([]modelOverwrites, 1)
121127
state.Overwrites.ElementsAs(ctx, &overwritesState, true)
122128

@@ -152,20 +158,26 @@ func (m unknownIfExecutionModeUnset) PlanModifyString(ctx context.Context, req p
152158
resp.Diagnostics.Append(req.Config.Get(ctx, &configured)...)
153159
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
154160

155-
overwritesState := make([]modelOverwrites, 1)
156-
state.Overwrites.ElementsAs(ctx, &overwritesState, true)
161+
if !state.Overwrites.IsNull() {
162+
// Normal operation
163+
overwritesState := make([]modelOverwrites, 1)
164+
state.Overwrites.ElementsAs(ctx, &overwritesState, true)
157165

158-
if configured.ExecutionMode.IsNull() && overwritesState[0].ExecutionMode.ValueBool() {
159-
resp.PlanValue = types.StringUnknown()
166+
if configured.ExecutionMode.IsNull() && overwritesState[0].ExecutionMode.ValueBool() {
167+
resp.PlanValue = types.StringUnknown()
168+
}
169+
} else if req.Path.Equal(path.Root("execution_mode")) {
170+
// TFE does not support overwrites so default the execution mode to "remote"
171+
resp.PlanValue = types.StringValue("remote")
160172
}
161173
}
162174

163175
func (m unknownIfExecutionModeUnset) Description(_ context.Context) string {
164-
return "Resets execution_mode to \"remote\" if it is unset"
176+
return "Resets execution_mode to an unknown value if it is unset"
165177
}
166178

167179
func (m unknownIfExecutionModeUnset) MarkdownDescription(_ context.Context) string {
168-
return "Resets execution_mode to \"remote\" if it is unset"
180+
return "Resets execution_mode to an unknown value if it is unset"
169181
}
170182

171183
func (r *workspaceSettings) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
@@ -237,7 +249,7 @@ func (r *workspaceSettings) Schema(ctx context.Context, req resource.SchemaReque
237249
}
238250

239251
// workspaceSettingsModelFromTFEWorkspace builds a resource model from the TFE model
240-
func workspaceSettingsModelFromTFEWorkspace(ws *tfe.Workspace) *modelWorkspaceSettings {
252+
func (r *workspaceSettings) workspaceSettingsModelFromTFEWorkspace(ws *tfe.Workspace) *modelWorkspaceSettings {
241253
result := modelWorkspaceSettings{
242254
ID: types.StringValue(ws.ID),
243255
WorkspaceID: types.StringValue(ws.ID),
@@ -248,24 +260,20 @@ func workspaceSettingsModelFromTFEWorkspace(ws *tfe.Workspace) *modelWorkspaceSe
248260
result.AgentPoolID = types.StringValue(ws.AgentPool.ID)
249261
}
250262

251-
settingsModel := modelOverwrites{
252-
ExecutionMode: types.BoolValue(false),
253-
AgentPool: types.BoolValue(false),
254-
}
255-
256-
if ws.SettingOverwrites != nil {
257-
settingsModel = modelOverwrites{
263+
result.Overwrites = types.ListNull(overwritesElementType)
264+
if r.supportsOverwrites = ws.SettingOverwrites != nil; r.supportsOverwrites {
265+
settingsModel := modelOverwrites{
258266
ExecutionMode: types.BoolValue(*ws.SettingOverwrites.ExecutionMode),
259267
AgentPool: types.BoolValue(*ws.SettingOverwrites.AgentPool),
260268
}
261-
}
262269

263-
listOverwrites, diags := types.ListValueFrom(ctx, overwritesElementType, []modelOverwrites{settingsModel})
264-
if diags.HasError() {
265-
panic("Could not build list value from slice of models. This should not be possible unless the model breaks reflection rules.")
266-
}
270+
listOverwrites, diags := types.ListValueFrom(ctx, overwritesElementType, []modelOverwrites{settingsModel})
271+
if diags.HasError() {
272+
panic("Could not build list value from slice of models. This should not be possible unless the model breaks reflection rules.")
273+
}
267274

268-
result.Overwrites = listOverwrites
275+
result.Overwrites = listOverwrites
276+
}
269277

270278
return &result
271279
}
@@ -296,7 +304,7 @@ func (r *workspaceSettings) readSettings(ctx context.Context, workspaceID string
296304
return nil, fmt.Errorf("couldn't read workspace %s: %s", workspaceID, err.Error())
297305
}
298306

299-
return workspaceSettingsModelFromTFEWorkspace(ws), nil
307+
return r.workspaceSettingsModelFromTFEWorkspace(ws), nil
300308
}
301309

302310
func (r *workspaceSettings) updateSettings(ctx context.Context, data *modelWorkspaceSettings, state *tfsdk.State) error {
@@ -309,13 +317,17 @@ func (r *workspaceSettings) updateSettings(ctx context.Context, data *modelWorks
309317
},
310318
}
311319

312-
if executionMode := data.ExecutionMode.ValueString(); executionMode != "" {
320+
executionMode := data.ExecutionMode.ValueString()
321+
if executionMode != "" {
313322
updateOptions.ExecutionMode = tfe.String(executionMode)
314323
updateOptions.SettingOverwrites.ExecutionMode = tfe.Bool(true)
315324
updateOptions.SettingOverwrites.AgentPool = tfe.Bool(true)
316325

317326
agentPoolID := data.AgentPoolID.ValueString() // may be empty
318327
updateOptions.AgentPoolID = tfe.String(agentPoolID)
328+
} else if executionMode == "" && data.Overwrites.IsNull() {
329+
// Not supported by TFE
330+
updateOptions.ExecutionMode = tfe.String("remote")
319331
}
320332

321333
ws, err := r.config.Client.Workspaces.UpdateByID(ctx, workspaceID, updateOptions)

0 commit comments

Comments
 (0)