Skip to content

Commit 2e69fbd

Browse files
committed
refactor
1 parent a07f685 commit 2e69fbd

File tree

2 files changed

+35
-98
lines changed

2 files changed

+35
-98
lines changed

internal/provider/resource_tfe_opa_version.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (r *OPAVersionResource) Schema(ctx context.Context, req resource.SchemaRequ
9999
"url": schema.StringAttribute{
100100
Required: true,
101101
},
102-
"sha": schema.StringAttribute{ // Ensure lowercase
102+
"sha": schema.StringAttribute{
103103
Required: true,
104104
},
105105
"os": schema.StringAttribute{
@@ -113,8 +113,8 @@ func (r *OPAVersionResource) Schema(ctx context.Context, req resource.SchemaRequ
113113
Computed: true,
114114
Optional: true,
115115
PlanModifiers: []planmodifier.Set{
116-
PreserveAMD64ArchsOnURLChange(),
117116
setplanmodifier.UseStateForUnknown(), // This ensures that we don't show a warning for invisible changes in updates when using refresh-only mode
117+
PreserveAMD64ArchsOnChange(), // This ensures that we don't remove AMD64 archs when the URL/SHA changes
118118
},
119119
},
120120
},

internal/provider/tool_helpers.go

Lines changed: 33 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ func convertToToolVersionArchitectures(ctx context.Context, archs types.Set) ([]
226226
return result, nil
227227
}
228228

229-
// PreserveAMD64ArchsOnURLChange creates a plan modifier that preserves AMD64 architecture entries
229+
// PreserveAMD64ArchsOnChange creates a plan modifier that preserves AMD64 architecture entries
230230
// when top-level URL or SHA changes, to be used across all tool version resources
231-
func PreserveAMD64ArchsOnURLChange() planmodifier.Set {
231+
func PreserveAMD64ArchsOnChange() planmodifier.Set {
232232
return &preserveAMD64ArchsModifier{}
233233
}
234234

@@ -248,7 +248,7 @@ func (m *preserveAMD64ArchsModifier) MarkdownDescription(ctx context.Context) st
248248
// PlanModifySet modifies the plan to ensure AMD64 architecture entries are preserved
249249
func (m *preserveAMD64ArchsModifier) PlanModifySet(ctx context.Context, req planmodifier.SetRequest, resp *planmodifier.SetResponse) {
250250
// Skip if we're destroying the resource or no state
251-
if req.Plan.Raw.IsNull() || req.State.Raw.IsNull() {
251+
if req.Plan.Raw.IsNull() || req.State.Raw.IsNull() || req.StateValue.IsNull() {
252252
return
253253
}
254254

@@ -272,76 +272,14 @@ func (m *preserveAMD64ArchsModifier) PlanModifySet(ctx context.Context, req plan
272272
stateArchs := req.StateValue
273273
planArchs := req.PlanValue
274274

275-
// If no state archs, nothing to preserve
276-
if stateArchs.IsNull() {
277-
return
278-
}
279-
280-
var configArchsList []ToolArchitecture
281-
configArchs := req.ConfigValue
282-
configHasArchs := !configArchs.IsNull() && !configArchs.IsUnknown()
283-
if configHasArchs {
284-
diags := configArchs.ElementsAs(ctx, &configArchsList, false)
285-
if diags.HasError() {
286-
tflog.Debug(ctx, "Error extracting config architectures", map[string]interface{}{
287-
"diagnostics": diags,
288-
})
289-
return
290-
}
291-
}
292-
293-
// IMPORTANT: Check if AMD64 was intentionally removed in config
294-
if configHasArchs {
295-
var configArchsList []ToolArchitecture
296-
diags := configArchs.ElementsAs(ctx, &configArchsList, false)
297-
if diags.HasError() {
298-
tflog.Debug(ctx, "Error extracting config architectures", map[string]interface{}{
299-
"diagnostics": diags,
300-
})
301-
return
302-
}
303-
304-
// Check each arch in config to see if AMD64 is there
305-
foundAMD64 := false
306-
for _, arch := range configArchsList {
307-
if arch.Arch.ValueString() == "amd64" {
308-
foundAMD64 = true
309-
break
310-
}
311-
}
312-
313-
// If AMD64 is explicitly NOT in config, don't add it back
314-
if !foundAMD64 {
315-
tflog.Debug(ctx, "AMD64 arch explicitly removed in config, not preserving")
316-
return
317-
}
318-
}
319-
320-
321275
// Extract archs from state
322276
var stateArchsList []ToolArchitecture
323277
diags := stateArchs.ElementsAs(ctx, &stateArchsList, false)
324278
if diags.HasError() {
325279
return
326280
}
327281

328-
// Extract AMD64 arch from state
329-
var amd64Arch *ToolArchitecture
330-
for _, arch := range stateArchsList {
331-
if arch.Arch.ValueString() == "amd64" {
332-
tmpArch := arch
333-
amd64Arch = &tmpArch
334-
break
335-
}
336-
}
337-
338-
// If no AMD64 in state, nothing to preserve
339-
if amd64Arch == nil {
340-
return
341-
}
342-
343-
// If we got here, we need to preserve AMD64
344-
// Add the AMD64 architecture from the state to the plan if it's not already present
282+
// we need to update the plan amd url and sha to match the top level values
345283
var planArchsList []ToolArchitecture
346284
diags = planArchs.ElementsAs(ctx, &planArchsList, false)
347285
if diags.HasError() {
@@ -352,39 +290,38 @@ func (m *preserveAMD64ArchsModifier) PlanModifySet(ctx context.Context, req plan
352290
}
353291

354292
// Check if AMD64 is already in the plan
355-
foundAMD64 := false
356-
for _, arch := range planArchsList {
293+
for i, arch := range planArchsList {
357294
if arch.Arch.ValueString() == "amd64" {
358-
foundAMD64 = true
359-
break
360-
}
361-
}
362-
363-
// If AMD64 is not in the plan, add it
364-
if !foundAMD64 {
365-
planArchsList = append(planArchsList, *amd64Arch)
366-
newPlanArchs := ToolArchitecturesToSet(planArchsList)
367-
resp.PlanValue = newPlanArchs
368-
}
369-
}
370-
371-
func ToolArchitecturesToSet(archs []ToolArchitecture) types.Set {
372-
archObjectType := ObjectTypeForArchitectures()
373-
attrValues := make([]attr.Value, len(archs))
295+
// If URL or SHA is changing, update the AMD64 arch to match
296+
if urlChanged {
297+
arch.URL = planURL
298+
}
299+
if shaChanged {
300+
arch.Sha = planSHA
301+
}
302+
// Update the plan architecture list with the modified AMD64 arch
303+
planArchsList[i] = arch
304+
305+
// Update the plan with the modified AMD64 arch
306+
archObjectType := ObjectTypeForArchitectures()
307+
attrValues := make([]attr.Value, len(planArchsList))
308+
309+
for i, arch := range planArchsList {
310+
attrValues[i] = types.ObjectValueMust(
311+
archObjectType.AttrTypes,
312+
map[string]attr.Value{
313+
"url": arch.URL,
314+
"sha": arch.Sha,
315+
"os": arch.OS,
316+
"arch": arch.Arch,
317+
},
318+
)
319+
}
374320

375-
for i, arch := range archs {
376-
attrValues[i] = types.ObjectValueMust(
377-
archObjectType.AttrTypes,
378-
map[string]attr.Value{
379-
"url": arch.URL,
380-
"sha": arch.Sha,
381-
"os": arch.OS,
382-
"arch": arch.Arch,
383-
},
384-
)
321+
resp.PlanValue = types.SetValueMust(archObjectType, attrValues)
322+
return
323+
}
385324
}
386-
387-
return types.SetValueMust(archObjectType, attrValues)
388325
}
389326

390327
// ValidateToolVersion provides common validation for tool version resources

0 commit comments

Comments
 (0)