Skip to content

Commit 2b11d94

Browse files
committed
skip updating plan archs if archs are in the config
1 parent ed51354 commit 2b11d94

File tree

2 files changed

+115
-94
lines changed

2 files changed

+115
-94
lines changed

internal/provider/resource_tfe_opa_version.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ import (
2121
)
2222

2323
var (
24-
_ resource.Resource = &OPAVersionResource{}
25-
_ resource.ResourceWithConfigure = &OPAVersionResource{}
26-
_ resource.ResourceWithImportState = &OPAVersionResource{}
27-
_ resource.ResourceWithValidateConfig = &OPAVersionResource{} // Add this line
24+
_ resource.Resource = &OPAVersionResource{}
25+
_ resource.ResourceWithConfigure = &OPAVersionResource{}
26+
_ resource.ResourceWithImportState = &OPAVersionResource{}
27+
// _ resource.ResourceWithValidateConfig = &OPAVersionResource{}
2828
)
2929

3030
type OPAVersionResource struct {
@@ -391,14 +391,14 @@ func (r *OPAVersionResource) ImportState(ctx context.Context, req resource.Impor
391391
}
392392
}
393393

394-
// Make OPAVersionResource implement the ToolVersionValidator interface
395-
func (r *OPAVersionResource) ValidateConfig(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) {
396-
var config modelAdminOPAVersion
397-
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
398-
if resp.Diagnostics.HasError() {
399-
return
400-
}
401-
402-
// Use the simplified validation function
403-
resp.Diagnostics.Append(ValidateToolVersion(ctx, config.URL, config.SHA, config.Archs, "OPA Version")...)
404-
}
394+
// // Make OPAVersionResource implement the ToolVersionValidator interface
395+
// func (r *OPAVersionResource) ValidateConfig(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) {
396+
// var config modelAdminOPAVersion
397+
// resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
398+
// if resp.Diagnostics.HasError() {
399+
// return
400+
// }
401+
402+
// // Use the simplified validation function
403+
// resp.Diagnostics.Append(ValidateToolVersion(ctx, config.URL, config.SHA, config.Archs, "OPA Version")...)
404+
// }

internal/provider/tool_helpers.go

Lines changed: 100 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,13 @@ func (m *preserveAMD64ArchsModifier) PlanModifySet(ctx context.Context, req plan
252252
return
253253
}
254254

255+
// skip if archs is set in the config
256+
if !req.ConfigValue.IsUnknown() && !req.ConfigValue.IsNull() {
257+
return
258+
}
259+
255260
var stateURL, planURL, stateSHA, planSHA types.String
261+
256262
// Get values from state and plan
257263
req.State.GetAttribute(ctx, path.Root("url"), &stateURL)
258264
req.Plan.GetAttribute(ctx, path.Root("url"), &planURL)
@@ -289,100 +295,115 @@ func (m *preserveAMD64ArchsModifier) PlanModifySet(ctx context.Context, req plan
289295
return
290296
}
291297

298+
tflog.Debug(ctx, "plan architectures", map[string]interface{}{
299+
"planArchsList": planArchsList,
300+
"stateArchsList": stateArchsList,
301+
"urlChanged": urlChanged,
302+
"shaChanged": shaChanged,
303+
"stateURL": stateURL,
304+
"planURL": planURL,
305+
"stateSHA": stateSHA,
306+
"planSHA": planSHA,
307+
})
292308
// Check if AMD64 is already in the plan
293-
for i, arch := range planArchsList {
309+
for _, arch := range planArchsList {
294310
if arch.Arch.ValueString() == "amd64" {
311+
tflog.Debug(ctx, "Found AMD64 architecture in plan", map[string]interface{}{
312+
"url": arch.URL.ValueString(),
313+
"sha": arch.Sha.ValueString(),
314+
"os": arch.OS.ValueString(),
315+
"arch": arch.Arch.ValueString(),
316+
})
317+
// If we found AMD64, update its URL and SHA if they are changing
295318
// If URL or SHA is changing, update the AMD64 arch to match
296319
if urlChanged {
297320
arch.URL = planURL
298321
}
299322
if shaChanged {
300323
arch.Sha = planSHA
301324
}
302-
// Update the plan architecture list with the modified AMD64 arch
303-
planArchsList[i] = arch
304325

305326
// Update the plan with the modified AMD64 arch
306327
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-
}
328+
attrValue := types.ObjectValueMust(
329+
archObjectType.AttrTypes,
330+
map[string]attr.Value{
331+
"url": arch.URL,
332+
"sha": arch.Sha,
333+
"os": arch.OS,
334+
"arch": arch.Arch,
335+
},
336+
)
320337

321-
resp.PlanValue = types.SetValueMust(archObjectType, attrValues)
338+
resp.PlanValue = types.SetValueMust(archObjectType, []attr.Value{attrValue})
322339
return
323340
}
324341
}
325342
}
326343

327-
// ValidateToolVersion provides common validation for tool version resources
328-
func ValidateToolVersion(ctx context.Context, url, sha types.String, archs types.Set, resourceType string) diag.Diagnostics {
329-
var diags diag.Diagnostics
330-
331-
urlPresent := !url.IsNull() && !url.IsUnknown()
332-
shaPresent := !sha.IsNull() && !sha.IsUnknown()
333-
334-
// If URL or SHA is not set, we will rely on the archs attribute
335-
if !urlPresent || !shaPresent {
336-
return diags
337-
}
338-
339-
// Check if archs is present
340-
if !archs.IsNull() && !archs.IsUnknown() {
341-
// Extract archs
342-
var archsList []ToolArchitecture
343-
archDiags := archs.ElementsAs(ctx, &archsList, false)
344-
if archDiags.HasError() {
345-
diags.Append(archDiags...)
346-
return diags
347-
}
348-
349-
// Check for AMD64 architecture
350-
var hasAMD64 bool
351-
for _, arch := range archsList {
352-
if arch.Arch.ValueString() == "amd64" {
353-
hasAMD64 = true
354-
355-
// If URL and SHA are set at top level, check they match AMD64 arch
356-
// Check URL matches
357-
if urlPresent && url.ValueString() != arch.URL.ValueString() {
358-
diags.AddError(
359-
fmt.Sprintf("Inconsistent %s URL values", resourceType),
360-
fmt.Sprintf("Top-level URL (%s) doesn't match AMD64 architecture URL (%s)",
361-
url.ValueString(), arch.URL.ValueString()),
362-
)
363-
}
364-
365-
// Check SHA matches
366-
if shaPresent && sha.ValueString() != arch.Sha.ValueString() {
367-
diags.AddError(
368-
fmt.Sprintf("Inconsistent %s SHA values", resourceType),
369-
fmt.Sprintf("Top-level SHA (%s) doesn't match AMD64 architecture SHA (%s)",
370-
sha.ValueString(), arch.Sha.ValueString()),
371-
)
372-
}
373-
374-
break
375-
}
376-
}
377-
378-
// If top-level URL/SHA are set and no AMD64 arch found, add error
379-
if !hasAMD64 && (!url.IsNull() || !sha.IsNull()) {
380-
diags.AddError(
381-
fmt.Sprintf("Missing AMD64 architecture in %s", resourceType),
382-
"When specifying both top-level URL/SHA and archs, an AMD64 architecture entry must be included",
383-
)
384-
}
385-
}
386-
387-
return diags
388-
}
344+
//
345+
// // ValidateToolVersion provides common validation for tool version resources
346+
// func ValidateToolVersion(ctx context.Context, url, sha types.String, archs types.Set, resourceType string) diag.Diagnostics {
347+
// var diags diag.Diagnostics
348+
349+
// urlPresent := !url.IsNull() && !url.IsUnknown()
350+
// shaPresent := !sha.IsNull() && !sha.IsUnknown()
351+
352+
// // If URL or SHA is not set, we will rely on the archs attribute
353+
// if !urlPresent || !shaPresent {
354+
// return diags
355+
// }
356+
357+
// // If archs aren't present, we can't validate against them
358+
// if archs.IsNull() || archs.IsUnknown() {
359+
// return diags
360+
// }
361+
362+
// // Extract archs
363+
// var archsList []ToolArchitecture
364+
// archDiags := archs.ElementsAs(ctx, &archsList, false)
365+
// if archDiags.HasError() {
366+
// diags.Append(archDiags...)
367+
// return diags
368+
// }
369+
370+
// // Check for AMD64 architecture
371+
// hasAMD64 := false
372+
// var amd64Arch ToolArchitecture
373+
// for _, arch := range archsList {
374+
// if arch.Arch.ValueString() == "amd64" {
375+
// hasAMD64 = true
376+
// amd64Arch = arch
377+
// break
378+
// }
379+
// }
380+
381+
// // If top-level URL/SHA are set and no AMD64 arch found, add error
382+
// if !hasAMD64 {
383+
// diags.AddError(
384+
// fmt.Sprintf("Missing AMD64 architecture in %s", resourceType),
385+
// "When specifying both top-level URL/SHA and archs, an AMD64 architecture entry must be included",
386+
// )
387+
// return diags
388+
// }
389+
390+
// // If URL and SHA are set at top level, check they match AMD64 arch
391+
// if url.ValueString() != amd64Arch.URL.ValueString() {
392+
// diags.AddError(
393+
// fmt.Sprintf("Inconsistent %s URL values", resourceType),
394+
// fmt.Sprintf("Top-level URL (%s) doesn't match AMD64 architecture URL (%s)",
395+
// url.ValueString(), amd64Arch.URL.ValueString()),
396+
// )
397+
// }
398+
399+
// // Check SHA matches
400+
// if sha.ValueString() != amd64Arch.Sha.ValueString() {
401+
// diags.AddError(
402+
// fmt.Sprintf("Inconsistent %s SHA values", resourceType),
403+
// fmt.Sprintf("Top-level SHA (%s) doesn't match AMD64 architecture SHA (%s)",
404+
// sha.ValueString(), amd64Arch.Sha.ValueString()),
405+
// )
406+
// }
407+
408+
// return diags
409+
// }

0 commit comments

Comments
 (0)