Skip to content

Commit 7bca552

Browse files
committed
update other resources to use the modifiers
1 parent 098a3f6 commit 7bca552

File tree

3 files changed

+48
-86
lines changed

3 files changed

+48
-86
lines changed

internal/provider/resource_tfe_sentinel_version.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,18 @@ func (r *sentinelVersionResource) Schema(ctx context.Context, req resource.Schem
6464
"url": schema.StringAttribute{
6565
Optional: true,
6666
Computed: true,
67+
PlanModifiers: []planmodifier.String{
68+
stringplanmodifier.UseStateForUnknown(),
69+
SyncTopLevelURLSHAWithAMD64(),
70+
},
6771
},
6872
"sha": schema.StringAttribute{
6973
Optional: true,
7074
Computed: true,
75+
PlanModifiers: []planmodifier.String{
76+
stringplanmodifier.UseStateForUnknown(),
77+
SyncTopLevelURLSHAWithAMD64(),
78+
},
7179
},
7280
"official": schema.BoolAttribute{
7381
Optional: true,
@@ -109,9 +117,12 @@ func (r *sentinelVersionResource) Schema(ctx context.Context, req resource.Schem
109117
},
110118
},
111119
},
112-
Computed: true,
113-
Optional: true,
114-
PlanModifiers: []planmodifier.Set{setplanmodifier.UseStateForUnknown()},
120+
Computed: true,
121+
Optional: true,
122+
PlanModifiers: []planmodifier.Set{
123+
setplanmodifier.UseStateForUnknown(),
124+
PreserveAMD64ArchsOnChange(),
125+
},
115126
},
116127
},
117128
}
@@ -279,7 +290,7 @@ func (r *sentinelVersionResource) Update(ctx context.Context, req resource.Updat
279290
opts := tfe.AdminSentinelVersionUpdateOptions{
280291
Version: tfe.String(sentinelVersion.Version.ValueString()),
281292
URL: stringOrNil(sentinelVersion.URL.ValueString()),
282-
SHA: tfe.String(sentinelVersion.SHA.ValueString()),
293+
SHA: stringOrNil(sentinelVersion.SHA.ValueString()),
283294
Official: tfe.Bool(sentinelVersion.Official.ValueBool()),
284295
Enabled: tfe.Bool(sentinelVersion.Enabled.ValueBool()),
285296
Beta: tfe.Bool(sentinelVersion.Beta.ValueBool()),
@@ -295,8 +306,6 @@ func (r *sentinelVersionResource) Update(ctx context.Context, req resource.Updat
295306
}(),
296307
}
297308

298-
tflog.Debug(ctx, "Updating sentinel version", map[string]interface{}{
299-
"id": sentinelVersion.ID.ValueString()})
300309
v, err := r.config.Client.Admin.SentinelVersions.Update(ctx, sentinelVersion.ID.ValueString(), opts)
301310
if err != nil {
302311
resp.Diagnostics.AddError(

internal/provider/resource_tfe_terraform_version.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,18 @@ func (r *terraformVersionResource) Schema(ctx context.Context, req resource.Sche
6565
"url": schema.StringAttribute{
6666
Optional: true,
6767
Computed: true,
68+
PlanModifiers: []planmodifier.String{
69+
stringplanmodifier.UseStateForUnknown(),
70+
SyncTopLevelURLSHAWithAMD64(),
71+
},
6872
},
6973
"sha": schema.StringAttribute{
7074
Optional: true,
7175
Computed: true,
76+
PlanModifiers: []planmodifier.String{
77+
stringplanmodifier.UseStateForUnknown(),
78+
SyncTopLevelURLSHAWithAMD64(),
79+
},
7280
},
7381
"official": schema.BoolAttribute{
7482
Optional: true,
@@ -110,9 +118,12 @@ func (r *terraformVersionResource) Schema(ctx context.Context, req resource.Sche
110118
},
111119
},
112120
},
113-
Computed: true,
114-
Optional: true,
115-
PlanModifiers: []planmodifier.Set{setplanmodifier.UseStateForUnknown()},
121+
Computed: true,
122+
Optional: true,
123+
PlanModifiers: []planmodifier.Set{
124+
setplanmodifier.UseStateForUnknown(),
125+
PreserveAMD64ArchsOnChange(),
126+
},
116127
},
117128
},
118129
}
@@ -315,6 +326,7 @@ func (r *terraformVersionResource) Update(ctx context.Context, req resource.Upda
315326

316327
tflog.Debug(ctx, "Updating Terraform version", map[string]interface{}{
317328
"id": tfVersion.ID.ValueString()})
329+
318330
v, err := r.config.Client.Admin.TerraformVersions.Update(ctx, tfVersion.ID.ValueString(), opts)
319331
if err != nil {
320332
resp.Diagnostics.AddError(

internal/provider/tool_helpers.go

Lines changed: 18 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -258,20 +258,21 @@ func (m *preserveAMD64ArchsModifier) PlanModifySet(ctx context.Context, req plan
258258
return
259259
}
260260

261-
var configURL, planURL, configSHA, planSHA types.String
261+
var configURL, stateURL, configSHA, stateSHA types.String
262262

263263
// Get values from state and plan
264264
req.Config.GetAttribute(ctx, path.Root("url"), &configURL)
265-
req.Plan.GetAttribute(ctx, path.Root("url"), &planURL)
266-
req.State.GetAttribute(ctx, path.Root("sha"), &configSHA)
267-
req.Plan.GetAttribute(ctx, path.Root("sha"), &planSHA)
265+
req.State.GetAttribute(ctx, path.Root("url"), &stateURL)
266+
req.Config.GetAttribute(ctx, path.Root("sha"), &configSHA)
267+
req.State.GetAttribute(ctx, path.Root("sha"), &stateSHA)
268268

269269
// Check if values are changing
270-
urlChanged := !configURL.Equal(planURL)
271-
shaChanged := !configSHA.Equal(planSHA)
270+
urlChanged := !configURL.Equal(stateURL)
271+
shaChanged := !configSHA.Equal(stateSHA)
272272

273273
// If neither URL nor SHA is changing, do nothing
274274
if !urlChanged && !shaChanged {
275+
tflog.Debug(ctx, "No changes to URL or SHA, skipping AMD64 architecture preservation")
275276
return
276277
}
277278

@@ -302,9 +303,9 @@ func (m *preserveAMD64ArchsModifier) PlanModifySet(ctx context.Context, req plan
302303
"urlChanged": urlChanged,
303304
"shaChanged": shaChanged,
304305
"stateURL": configURL,
305-
"planURL": planURL,
306+
"planURL": stateURL,
306307
"stateSHA": configSHA,
307-
"planSHA": planSHA,
308+
"planSHA": stateSHA,
308309
})
309310
// Check if AMD64 is already in the plan
310311
for _, arch := range planArchsList {
@@ -321,7 +322,7 @@ func (m *preserveAMD64ArchsModifier) PlanModifySet(ctx context.Context, req plan
321322
arch.URL = configURL
322323
}
323324
if shaChanged {
324-
arch.Sha = configURL
325+
arch.Sha = configSHA
325326
}
326327

327328
// Update the plan with the modified AMD64 arch
@@ -335,6 +336,12 @@ func (m *preserveAMD64ArchsModifier) PlanModifySet(ctx context.Context, req plan
335336
"arch": arch.Arch,
336337
},
337338
)
339+
tflog.Debug(ctx, "Updating AMD64 architecture in plan", map[string]interface{}{
340+
"url": arch.URL.ValueString(),
341+
"sha": arch.Sha.ValueString(),
342+
"os": arch.OS.ValueString(),
343+
"arch": arch.Arch.ValueString(),
344+
})
338345

339346
resp.PlanValue = types.SetValueMust(archObjectType, []attr.Value{attrValue})
340347
return
@@ -415,7 +422,8 @@ func (m *SyncTopLevelURLSHAWithAMD64Modifier) PlanModifyString(ctx context.Conte
415422
}
416423

417424
if amd64Arch.Arch.IsNull() || amd64Arch.Arch.IsUnknown() {
418-
tflog.Debug(ctx, "No AMD64 architecture found in config archs, skipping modification")
425+
// set the plan's top level URL/SHA to null
426+
resp.PlanValue = types.StringNull()
419427
return
420428
}
421429

@@ -435,70 +443,3 @@ func (m *SyncTopLevelURLSHAWithAMD64Modifier) PlanModifyString(ctx context.Conte
435443
}
436444

437445
}
438-
439-
//
440-
// // ValidateToolVersion provides common validation for tool version resources
441-
// func ValidateToolVersion(ctx context.Context, url, sha types.String, archs types.Set, resourceType string) diag.Diagnostics {
442-
// var diags diag.Diagnostics
443-
444-
// urlPresent := !url.IsNull() && !url.IsUnknown()
445-
// shaPresent := !sha.IsNull() && !sha.IsUnknown()
446-
447-
// // If URL or SHA is not set, we will rely on the archs attribute
448-
// if !urlPresent || !shaPresent {
449-
// return diags
450-
// }
451-
452-
// // If archs aren't present, we can't validate against them
453-
// if archs.IsNull() || archs.IsUnknown() {
454-
// return diags
455-
// }
456-
457-
// // Extract archs
458-
// var archsList []ToolArchitecture
459-
// archDiags := archs.ElementsAs(ctx, &archsList, false)
460-
// if archDiags.HasError() {
461-
// diags.Append(archDiags...)
462-
// return diags
463-
// }
464-
465-
// // Check for AMD64 architecture
466-
// hasAMD64 := false
467-
// var amd64Arch ToolArchitecture
468-
// for _, arch := range archsList {
469-
// if arch.Arch.ValueString() == "amd64" {
470-
// hasAMD64 = true
471-
// amd64Arch = arch
472-
// break
473-
// }
474-
// }
475-
476-
// // If top-level URL/SHA are set and no AMD64 arch found, add error
477-
// if !hasAMD64 {
478-
// diags.AddError(
479-
// fmt.Sprintf("Missing AMD64 architecture in %s", resourceType),
480-
// "When specifying both top-level URL/SHA and archs, an AMD64 architecture entry must be included",
481-
// )
482-
// return diags
483-
// }
484-
485-
// // If URL and SHA are set at top level, check they match AMD64 arch
486-
// if url.ValueString() != amd64Arch.URL.ValueString() {
487-
// diags.AddError(
488-
// fmt.Sprintf("Inconsistent %s URL values", resourceType),
489-
// fmt.Sprintf("Top-level URL (%s) doesn't match AMD64 architecture URL (%s)",
490-
// url.ValueString(), amd64Arch.URL.ValueString()),
491-
// )
492-
// }
493-
494-
// // Check SHA matches
495-
// if sha.ValueString() != amd64Arch.Sha.ValueString() {
496-
// diags.AddError(
497-
// fmt.Sprintf("Inconsistent %s SHA values", resourceType),
498-
// fmt.Sprintf("Top-level SHA (%s) doesn't match AMD64 architecture SHA (%s)",
499-
// sha.ValueString(), amd64Arch.Sha.ValueString()),
500-
// )
501-
// }
502-
503-
// return diags
504-
// }

0 commit comments

Comments
 (0)