Skip to content

Commit

Permalink
initial refactoring of plan.lint with lintModifyPlan
Browse files Browse the repository at this point in the history
  • Loading branch information
BBBmau committed Oct 24, 2024
1 parent 2bafedf commit 256a726
Showing 1 changed file with 58 additions and 7 deletions.
65 changes: 58 additions & 7 deletions helm-framework/helm/resource_helm_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,61 @@ func suppressDescription() planmodifier.String {
return suppressDescriptionPlanModifier{}
}

type lintPlanModifier struct {
r *HelmRelease
}

func (m lintPlanModifier) Description(ctx context.Context) string {
return "Suppress changes if the new description is an empty string"
}

func (m lintPlanModifier) MarkdownDescription(ctx context.Context) string {
return m.Description(ctx)
}

func (m lintPlanModifier) PlanModifyBool(ctx context.Context, req planmodifier.BoolRequest, resp *planmodifier.BoolResponse) {
var plan HelmReleaseModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}

var state *HelmReleaseModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

actionConfig, err := m.r.meta.GetHelmConfiguration(ctx, state.Namespace.ValueString())
if err != nil {
resp.Diagnostics.AddError("Failed to get Helm configuration", err.Error())
return
}

client := action.NewInstall(actionConfig)
cpo, _, diags := chartPathOptions(&plan, m.r.meta, &client.ChartPathOptions)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

if req.PlanValue.ValueBool() {
diags := resourceReleaseValidate(ctx, &plan, m.r.meta, cpo)
if diags.HasError() {
for _, diag := range diags {
resp.Diagnostics.Append(diag)
}
return
}
}

tflog.Debug(ctx, fmt.Sprintf("%s Release validated", "LintModifier"))
}

func lintDescription() planmodifier.Bool {
return lintPlanModifier{}
}

type suppressDevelPlanModifier struct{}

func (m suppressDevelPlanModifier) Description(ctx context.Context) string {
Expand Down Expand Up @@ -339,6 +394,9 @@ func (r *HelmRelease) Schema(ctx context.Context, req resource.SchemaRequest, re
Computed: true,
Default: booldefault.StaticBool(defaultAttributes["lint"].(bool)),
Description: "Run helm lint when planning",
PlanModifiers: []planmodifier.Bool{
lintDescription(),
},
},
"manifest": schema.StringAttribute{
Description: "The rendered manifest as JSON.",
Expand Down Expand Up @@ -1709,13 +1767,6 @@ func (r *HelmRelease) ModifyPlan(ctx context.Context, req resource.ModifyPlanReq
}
}

if plan.Lint.ValueBool() {
diags := resourceReleaseValidate(ctx, &plan, meta, cpo)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
}
}
tflog.Debug(ctx, fmt.Sprintf("%s Release validated", logID))

if meta.ExperimentEnabled("manifest") {
Expand Down

0 comments on commit 256a726

Please sign in to comment.