@@ -36,7 +36,8 @@ var overwritesElementType = types.ObjectType{
36
36
}
37
37
38
38
type workspaceSettings struct {
39
- config ConfiguredClient
39
+ config ConfiguredClient
40
+ supportsOverwrites bool
40
41
}
41
42
42
43
type modelWorkspaceSettings struct {
@@ -117,6 +118,11 @@ func (m revertOverwritesIfExecutionModeUnset) PlanModifyList(ctx context.Context
117
118
resp .Diagnostics .Append (req .Config .Get (ctx , & configured )... )
118
119
resp .Diagnostics .Append (req .State .Get (ctx , & state )... )
119
120
121
+ // Check if overwrites are supported by the platform
122
+ if state .Overwrites .IsNull () {
123
+ return
124
+ }
125
+
120
126
overwritesState := make ([]modelOverwrites , 1 )
121
127
state .Overwrites .ElementsAs (ctx , & overwritesState , true )
122
128
@@ -152,20 +158,26 @@ func (m unknownIfExecutionModeUnset) PlanModifyString(ctx context.Context, req p
152
158
resp .Diagnostics .Append (req .Config .Get (ctx , & configured )... )
153
159
resp .Diagnostics .Append (req .State .Get (ctx , & state )... )
154
160
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 )
157
165
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" )
160
172
}
161
173
}
162
174
163
175
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"
165
177
}
166
178
167
179
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"
169
181
}
170
182
171
183
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
237
249
}
238
250
239
251
// 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 {
241
253
result := modelWorkspaceSettings {
242
254
ID : types .StringValue (ws .ID ),
243
255
WorkspaceID : types .StringValue (ws .ID ),
@@ -248,24 +260,20 @@ func workspaceSettingsModelFromTFEWorkspace(ws *tfe.Workspace) *modelWorkspaceSe
248
260
result .AgentPoolID = types .StringValue (ws .AgentPool .ID )
249
261
}
250
262
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 {
258
266
ExecutionMode : types .BoolValue (* ws .SettingOverwrites .ExecutionMode ),
259
267
AgentPool : types .BoolValue (* ws .SettingOverwrites .AgentPool ),
260
268
}
261
- }
262
269
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
+ }
267
274
268
- result .Overwrites = listOverwrites
275
+ result .Overwrites = listOverwrites
276
+ }
269
277
270
278
return & result
271
279
}
@@ -296,7 +304,7 @@ func (r *workspaceSettings) readSettings(ctx context.Context, workspaceID string
296
304
return nil , fmt .Errorf ("couldn't read workspace %s: %s" , workspaceID , err .Error ())
297
305
}
298
306
299
- return workspaceSettingsModelFromTFEWorkspace (ws ), nil
307
+ return r . workspaceSettingsModelFromTFEWorkspace (ws ), nil
300
308
}
301
309
302
310
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
309
317
},
310
318
}
311
319
312
- if executionMode := data .ExecutionMode .ValueString (); executionMode != "" {
320
+ executionMode := data .ExecutionMode .ValueString ()
321
+ if executionMode != "" {
313
322
updateOptions .ExecutionMode = tfe .String (executionMode )
314
323
updateOptions .SettingOverwrites .ExecutionMode = tfe .Bool (true )
315
324
updateOptions .SettingOverwrites .AgentPool = tfe .Bool (true )
316
325
317
326
agentPoolID := data .AgentPoolID .ValueString () // may be empty
318
327
updateOptions .AgentPoolID = tfe .String (agentPoolID )
328
+ } else if executionMode == "" && data .Overwrites .IsNull () {
329
+ // Not supported by TFE
330
+ updateOptions .ExecutionMode = tfe .String ("remote" )
319
331
}
320
332
321
333
ws , err := r .config .Client .Workspaces .UpdateByID (ctx , workspaceID , updateOptions )
0 commit comments