@@ -19,18 +19,17 @@ import (
19
19
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
20
20
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
21
21
"github.com/hashicorp/terraform-plugin-framework/types"
22
+ "github.com/hashicorp/terraform-plugin-framework/types/basetypes"
22
23
)
23
24
24
25
// tfe_project_settings resource
25
26
var _ resource.Resource = & projectSettings {}
26
27
27
28
// projectOverwritesElementType is the object type definition for the
28
29
// overwrites field schema.
29
- var projectOverwritesElementType = types.ObjectType {
30
- AttrTypes : map [string ]attr.Type {
31
- "default_execution_mode" : types .BoolType ,
32
- "default_agent_pool_id" : types .BoolType ,
33
- },
30
+ var projectOverwritesElementType = map [string ]attr.Type {
31
+ "default_execution_mode" : types .BoolType ,
32
+ "default_agent_pool_id" : types .BoolType ,
34
33
}
35
34
36
35
type projectSettings struct {
@@ -42,7 +41,7 @@ type modelProjectSettings struct {
42
41
ProjectID types.String `tfsdk:"project_id"`
43
42
DefaultExecutionMode types.String `tfsdk:"default_execution_mode"`
44
43
DefaultAgentPoolID types.String `tfsdk:"default_agent_pool_id"`
45
- Overwrites types.List `tfsdk:"overwrites"`
44
+ Overwrites types.Object `tfsdk:"overwrites"`
46
45
}
47
46
48
47
type projectOverwrites struct {
@@ -79,9 +78,9 @@ type unknownIfDefaultExecutionModeUnset struct{}
79
78
type overwriteExecutionModeIfSpecified struct {}
80
79
81
80
var _ planmodifier.String = (* validateProjectDefaultAgentExecutionMode )(nil )
82
- var _ planmodifier.List = (* revertOverwritesIfDefaultExecutionModeUnset )(nil )
81
+ var _ planmodifier.Object = (* revertOverwritesIfDefaultExecutionModeUnset )(nil )
83
82
var _ planmodifier.String = (* unknownIfDefaultExecutionModeUnset )(nil )
84
- var _ planmodifier.List = (* overwriteExecutionModeIfSpecified )(nil )
83
+ var _ planmodifier.Object = (* overwriteExecutionModeIfSpecified )(nil )
85
84
86
85
func (m validateProjectDefaultAgentExecutionMode ) PlanModifyString (ctx context.Context , req planmodifier.StringRequest , resp * planmodifier.StringResponse ) {
87
86
configured := modelProjectSettings {}
@@ -104,7 +103,7 @@ func (m validateProjectDefaultAgentExecutionMode) MarkdownDescription(_ context.
104
103
return "Validates that configuration values for \" default_agent_pool_id\" and \" default_execution_mode\" are compatible"
105
104
}
106
105
107
- func (m revertOverwritesIfDefaultExecutionModeUnset ) PlanModifyList (ctx context.Context , req planmodifier.ListRequest , resp * planmodifier.ListResponse ) {
106
+ func (m revertOverwritesIfDefaultExecutionModeUnset ) PlanModifyObject (ctx context.Context , req planmodifier.ObjectRequest , resp * planmodifier.ObjectResponse ) {
108
107
// Check if the resource is being created.
109
108
if req .State .Raw .IsNull () {
110
109
return
@@ -122,18 +121,19 @@ func (m revertOverwritesIfDefaultExecutionModeUnset) PlanModifyList(ctx context.
122
121
return
123
122
}
124
123
125
- overwritesState := make ([]projectOverwrites , 1 )
126
- state .Overwrites .ElementsAs (ctx , & overwritesState , true )
124
+ overwritesState := projectOverwrites {}
125
+
126
+ state .Overwrites .As (ctx , & overwritesState , basetypes.ObjectAsOptions {})
127
127
128
128
// if there is a default execution mode set in state, but not one configured, then set the overwrites to false
129
- if configured .DefaultExecutionMode .IsNull () && overwritesState [ 0 ] .DefaultExecutionMode .ValueBool () {
130
- overwritesState [ 0 ] .DefaultAgentPoolID = types .BoolValue (false )
131
- overwritesState [ 0 ] .DefaultExecutionMode = types .BoolValue (false )
129
+ if configured .DefaultExecutionMode .IsNull () && overwritesState .DefaultExecutionMode .ValueBool () {
130
+ overwritesState .DefaultAgentPoolID = types .BoolValue (false )
131
+ overwritesState .DefaultExecutionMode = types .BoolValue (false )
132
132
133
- newList , diags := types .ListValueFrom (ctx , projectOverwritesElementType , overwritesState )
133
+ newProjOverwrites , diags := types .ObjectValueFrom (ctx , projectOverwritesElementType , overwritesState )
134
134
resp .Diagnostics .Append (diags ... )
135
135
136
- resp .PlanValue = newList
136
+ resp .PlanValue = newProjOverwrites
137
137
}
138
138
}
139
139
@@ -159,11 +159,11 @@ func (m unknownIfDefaultExecutionModeUnset) PlanModifyString(ctx context.Context
159
159
resp .Diagnostics .Append (req .State .Get (ctx , & state )... )
160
160
161
161
if ! state .Overwrites .IsNull () {
162
- overwritesState := make ([] projectOverwrites , 1 )
163
- state .Overwrites .ElementsAs (ctx , & overwritesState , true )
162
+ overwritesState := projectOverwrites {}
163
+ state .Overwrites .As (ctx , & overwritesState , basetypes. ObjectAsOptions {} )
164
164
165
165
// if there is a default execution mode set in state, but not one configured, then set the planned value for the default execution mode and agent pool to unknown
166
- if configured .DefaultExecutionMode .IsNull () && overwritesState [ 0 ] .DefaultExecutionMode .ValueBool () {
166
+ if configured .DefaultExecutionMode .IsNull () && overwritesState .DefaultExecutionMode .ValueBool () {
167
167
resp .PlanValue = types .StringUnknown ()
168
168
}
169
169
}
@@ -177,7 +177,7 @@ func (m unknownIfDefaultExecutionModeUnset) MarkdownDescription(_ context.Contex
177
177
return "Resets default_execution_mode to an unknown value if it is unset"
178
178
}
179
179
180
- func (m overwriteExecutionModeIfSpecified ) PlanModifyList (ctx context.Context , req planmodifier.ListRequest , resp * planmodifier.ListResponse ) {
180
+ func (m overwriteExecutionModeIfSpecified ) PlanModifyObject (ctx context.Context , req planmodifier.ObjectRequest , resp * planmodifier.ObjectResponse ) {
181
181
// Check if the resource is being created.
182
182
if req .State .Raw .IsNull () {
183
183
return
@@ -189,16 +189,16 @@ func (m overwriteExecutionModeIfSpecified) PlanModifyList(ctx context.Context, r
189
189
resp .Diagnostics .Append (req .Config .Get (ctx , & configured )... )
190
190
resp .Diagnostics .Append (req .State .Get (ctx , & state )... )
191
191
192
- overwritesState := make ([] projectOverwrites , 1 )
193
- state .Overwrites .ElementsAs (ctx , & overwritesState , true )
192
+ overwritesState := projectOverwrites {}
193
+ state .Overwrites .As (ctx , & overwritesState , basetypes. ObjectAsOptions {} )
194
194
195
195
if ! state .Overwrites .IsNull () {
196
196
// if an execution mode is configured, ensure that the overwrites are set to true
197
197
if ! configured .DefaultExecutionMode .IsNull () {
198
- overwritesState [ 0 ] .DefaultAgentPoolID = types .BoolValue (true )
199
- overwritesState [ 0 ] .DefaultExecutionMode = types .BoolValue (true )
198
+ overwritesState .DefaultAgentPoolID = types .BoolValue (true )
199
+ overwritesState .DefaultExecutionMode = types .BoolValue (true )
200
200
201
- newList , diags := types .ListValueFrom (ctx , projectOverwritesElementType , overwritesState )
201
+ newList , diags := types .ObjectValueFrom (ctx , projectOverwritesElementType , overwritesState )
202
202
resp .Diagnostics .Append (diags ... )
203
203
204
204
resp .PlanValue = newList
@@ -255,13 +255,20 @@ func (r *projectSettings) Schema(ctx context.Context, req resource.SchemaRequest
255
255
validateProjectDefaultAgentExecutionMode {},
256
256
},
257
257
},
258
- // ListAttribute was required here because we are still using plugin protocol v5.
259
- // Once compatibility is broken for v1, and we convert all
260
- // providers to protocol v6, this can become a single nested object.
261
- "overwrites" : schema.ListAttribute {
258
+ "overwrites" : schema.SingleNestedAttribute {
262
259
Computed : true ,
263
- ElementType : projectOverwritesElementType ,
264
- PlanModifiers : []planmodifier.List {
260
+ Description : "Describes which settings are being overwritten from the organization defaults" ,
261
+ Attributes : map [string ]schema.Attribute {
262
+ "default_execution_mode" : schema.BoolAttribute {
263
+ Computed : true ,
264
+ Description : "Whether the default_execution_mode is being overwritten from the organization default" ,
265
+ },
266
+ "default_agent_pool_id" : schema.BoolAttribute {
267
+ Computed : true ,
268
+ Description : "Whether the default_agent_pool_id is being overwritten from the organization default" ,
269
+ },
270
+ },
271
+ PlanModifiers : []planmodifier.Object {
265
272
revertOverwritesIfDefaultExecutionModeUnset {},
266
273
overwriteExecutionModeIfSpecified {},
267
274
},
@@ -282,19 +289,19 @@ func (r *projectSettings) projectSettingsModelFromTFEProject(proj *tfe.Project)
282
289
result .DefaultAgentPoolID = types .StringValue (proj .DefaultAgentPool .ID )
283
290
}
284
291
285
- result .Overwrites = types .ListNull (projectOverwritesElementType )
292
+ result .Overwrites = types .ObjectNull (projectOverwritesElementType )
286
293
if proj .SettingOverwrites != nil {
287
294
settingsModel := projectOverwrites {
288
295
DefaultExecutionMode : types .BoolValue (* proj .SettingOverwrites .ExecutionMode ),
289
296
DefaultAgentPoolID : types .BoolValue (* proj .SettingOverwrites .AgentPool ),
290
297
}
291
298
292
- listOverwrites , diags := types .ListValueFrom (ctx , projectOverwritesElementType , [] projectOverwrites { settingsModel } )
299
+ objectOverwrites , diags := types .ObjectValueFrom (ctx , projectOverwritesElementType , settingsModel )
293
300
if diags .HasError () {
294
- panic ("Could not build list value from slice of models . This should not be possible unless the model breaks reflection rules." )
301
+ panic ("Could not build object value from model . This should not be possible unless the model breaks reflection rules." )
295
302
}
296
303
297
- result .Overwrites = listOverwrites
304
+ result .Overwrites = objectOverwrites
298
305
}
299
306
300
307
return & result
0 commit comments