Skip to content

Commit

Permalink
Remove blocks from helm_release resource and data source schemas (#1507)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhouston authored Oct 23, 2024
1 parent 43749a8 commit 8e884fd
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 127 deletions.
71 changes: 31 additions & 40 deletions helm-framework/helm/data_source_helm_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strings"
"time"

"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
Expand All @@ -31,8 +30,10 @@ import (
"sigs.k8s.io/yaml"
)

var _ datasource.DataSource = &HelmTemplate{}
var _ datasource.DataSourceWithConfigure = &HelmTemplate{}
var (
_ datasource.DataSource = &HelmTemplate{}
_ datasource.DataSourceWithConfigure = &HelmTemplate{}
)

func NewHelmTemplate() datasource.DataSource {
return &HelmTemplate{}
Expand Down Expand Up @@ -79,7 +80,7 @@ type HelmTemplateModel struct {
Replace types.Bool `tfsdk:"replace"`
Description types.String `tfsdk:"description"`
CreateNamespace types.Bool `tfsdk:"create_namespace"`
Postrender types.List `tfsdk:"postrender"`
Postrender types.Object `tfsdk:"postrender"`
ApiVersions types.List `tfsdk:"api_versions"`
IncludeCrds types.Bool `tfsdk:"include_crds"`
IsUpgrade types.Bool `tfsdk:"is_upgrade"`
Expand Down Expand Up @@ -306,12 +307,10 @@ func (d *HelmTemplate) Schema(ctx context.Context, req datasource.SchemaRequest,
Optional: true,
Description: "Kubernetes version used for Capabilities.KubeVersion.",
},
},
Blocks: map[string]schema.Block{
// Definitions for set, set_list, set_sensitive, set_string, and postrender as previously modified
"set": schema.SetNestedBlock{
Description: "Custom values to be merged with the values.",
NestedObject: schema.NestedBlockObject{
"set": schema.SetNestedAttribute{
Description: "Custom values to be merged with the values",
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Required: true,
Expand All @@ -321,16 +320,18 @@ func (d *HelmTemplate) Schema(ctx context.Context, req datasource.SchemaRequest,
},
"type": schema.StringAttribute{
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.OneOf("auto", "string"),
},
},
},
},
},
"set_list": schema.ListNestedBlock{
Description: "Custom sensitive values to be merged with the values.",
NestedObject: schema.NestedBlockObject{
"set_list": schema.ListNestedAttribute{
Description: "Custom sensitive values to be merged with the values",
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Required: true,
Expand All @@ -342,9 +343,10 @@ func (d *HelmTemplate) Schema(ctx context.Context, req datasource.SchemaRequest,
},
},
},
"set_sensitive": schema.SetNestedBlock{
Description: "Custom sensitive values to be merged with the values.",
NestedObject: schema.NestedBlockObject{
"set_sensitive": schema.SetNestedAttribute{
Description: "Custom sensitive values to be merged with the values",
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Required: true,
Expand All @@ -362,30 +364,18 @@ func (d *HelmTemplate) Schema(ctx context.Context, req datasource.SchemaRequest,
},
},
},
"set_string": schema.SetNestedBlock{
Description: "Custom string values to be merged with the values.",
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Required: true,
},
"value": schema.StringAttribute{
Required: true,
},
"postrender": schema.SingleNestedAttribute{
Description: "Postrender command config",
Optional: true,
Attributes: map[string]schema.Attribute{
"args": schema.ListAttribute{
Optional: true,
Description: "An argument to the post-renderer (can specify multiple)",
ElementType: types.StringType,
},
},
},
"postrender": schema.ListNestedBlock{
Description: "Postrender command configuration",
Validators: []validator.List{
listvalidator.SizeAtMost(1),
},
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"binary_path": schema.StringAttribute{
Required: true,
Description: "The command binary path.",
},
"binary_path": schema.StringAttribute{
Required: true,
Description: "The common binary path",
},
},
},
Expand All @@ -409,7 +399,7 @@ func (d *HelmTemplate) Read(ctx context.Context, req datasource.ReadRequest, res
return
}

//setting default values to false is attributes are not provided in the config
// setting default values to false is attributes are not provided in the config
if state.Description.IsNull() || state.Description.ValueString() == "" {
state.Description = types.StringValue("")
}
Expand Down Expand Up @@ -739,6 +729,7 @@ func getTemplateValues(ctx context.Context, model *HelmTemplateModel) (map[strin

return base, logDataValues(ctx, base, model)
}

func getDataSourceListValue(ctx context.Context, base map[string]interface{}, set SetListValue) diag.Diagnostics {
var diags diag.Diagnostics

Expand Down
153 changes: 66 additions & 87 deletions helm-framework/helm/resource_helm_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"
"time"

"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
Expand Down Expand Up @@ -75,7 +74,7 @@ type HelmReleaseModel struct {
Name types.String `tfsdk:"name"`
Namespace types.String `tfsdk:"namespace"`
PassCredentials types.Bool `tfsdk:"pass_credentials"`
Postrender types.List `tfsdk:"postrender"`
Postrender types.Object `tfsdk:"postrender"`
RecreatePods types.Bool `tfsdk:"recreate_pods"`
Replace types.Bool `tfsdk:"replace"`
RenderSubchartNotes types.Bool `tfsdk:"render_subchart_notes"`
Expand Down Expand Up @@ -511,11 +510,10 @@ func (r *HelmRelease) Schema(ctx context.Context, req resource.SchemaRequest, re
Default: booldefault.StaticBool(defaultAttributes["wait_for_jobs"].(bool)),
Description: "If wait is enabled, will wait until all Jobs have been completed before marking the release as successful.",
},
},
Blocks: map[string]schema.Block{
"set": schema.SetNestedBlock{
"set": schema.SetNestedAttribute{
Description: "Custom values to be merged with the values",
NestedObject: schema.NestedBlockObject{
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Required: true,
Expand All @@ -534,9 +532,10 @@ func (r *HelmRelease) Schema(ctx context.Context, req resource.SchemaRequest, re
},
},
},
"set_list": schema.ListNestedBlock{
"set_list": schema.ListNestedAttribute{
Description: "Custom sensitive values to be merged with the values",
NestedObject: schema.NestedBlockObject{
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Required: true,
Expand All @@ -548,9 +547,10 @@ func (r *HelmRelease) Schema(ctx context.Context, req resource.SchemaRequest, re
},
},
},
"set_sensitive": schema.SetNestedBlock{
"set_sensitive": schema.SetNestedAttribute{
Description: "Custom sensitive values to be merged with the values",
NestedObject: schema.NestedBlockObject{
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Required: true,
Expand All @@ -568,23 +568,18 @@ func (r *HelmRelease) Schema(ctx context.Context, req resource.SchemaRequest, re
},
},
},
// single nested
"postrender": schema.ListNestedBlock{
Validators: []validator.List{
listvalidator.SizeAtMost(1),
},
"postrender": schema.SingleNestedAttribute{
Description: "Postrender command config",
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"args": schema.ListAttribute{
Optional: true,
Description: "An argument to the post-renderer (can specify multiple)",
ElementType: types.StringType,
},
"binary_path": schema.StringAttribute{
Required: true,
Description: "The common binary path",
},
Optional: true,
Attributes: map[string]schema.Attribute{
"args": schema.ListAttribute{
Optional: true,
Description: "An argument to the post-renderer (can specify multiple)",
ElementType: types.StringType,
},
"binary_path": schema.StringAttribute{
Required: true,
Description: "The common binary path",
},
},
},
Expand Down Expand Up @@ -732,35 +727,29 @@ func (r *HelmRelease) Create(ctx context.Context, req resource.CreateRequest, re

if !state.Postrender.IsNull() {
tflog.Debug(ctx, "Postrender is not null")
// Extract the list of postrender configurations
var postrenderList []postrenderModel
postrenderDiags := state.Postrender.ElementsAs(ctx, &postrenderList, false)
var postrenderConfig postrenderModel
postrenderDiags := state.Postrender.As(ctx, &postrenderConfig, basetypes.ObjectAsOptions{})
resp.Diagnostics.Append(postrenderDiags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, fmt.Sprintf("Postrender list extracted: %+v", postrenderList))
// Since postrender is defined as a list but can only have one element, we fetch the first item
if len(postrenderList) > 0 {
tflog.Debug(ctx, fmt.Sprintf("Postrender config: %+v", postrenderConfig))

prModel := postrenderList[0]
binaryPath := postrenderConfig.BinaryPath.ValueString()
argsList := postrenderConfig.Args.Elements()

binaryPath := prModel.BinaryPath.ValueString()
argsList := prModel.Args.Elements()

var args []string
for _, arg := range argsList {
args = append(args, arg.(basetypes.StringValue).ValueString())
}
tflog.Debug(ctx, fmt.Sprintf("Creating post-renderer with binary path: %s and args: %v", binaryPath, args))
pr, err := postrender.NewExec(binaryPath, args...)
if err != nil {
resp.Diagnostics.AddError("Error creating post-renderer", fmt.Sprintf("Could not create post-renderer: %s", err))
return
}

client.PostRenderer = pr
var args []string
for _, arg := range argsList {
args = append(args, arg.(basetypes.StringValue).ValueString())
}
tflog.Debug(ctx, fmt.Sprintf("Creating post-renderer with binary path: %s and args: %v", binaryPath, args))
pr, err := postrender.NewExec(binaryPath, args...)
if err != nil {
resp.Diagnostics.AddError("Error creating post-renderer", fmt.Sprintf("Could not create post-renderer: %s", err))
return
}

client.PostRenderer = pr
}

rel, err := client.Run(c, values)
Expand Down Expand Up @@ -948,35 +937,28 @@ func (r *HelmRelease) Update(ctx context.Context, req resource.UpdateRequest, re
client.Description = plan.Description.ValueString()

if !plan.Postrender.IsNull() {
// Extract the list of postrender configurations
var postrenderList []postrenderModel
postrenderDiags := plan.Postrender.ElementsAs(ctx, &postrenderList, false)
var postrenderConfig postrenderModel
postrenderDiags := plan.Postrender.As(ctx, &postrenderConfig, basetypes.ObjectAsOptions{})
resp.Diagnostics.Append(postrenderDiags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, fmt.Sprintf("Initial postrender values update method: %+v", postrenderList))

// Since postrender is defined as a list but can only have one element, we fetch the first item
if len(postrenderList) > 0 {
prModel := postrenderList[0]
tflog.Debug(ctx, fmt.Sprintf("Initial postrender values update method: %+v", postrenderConfig))

binaryPath := prModel.BinaryPath.ValueString()
argsList := prModel.Args.Elements()
binaryPath := postrenderConfig.BinaryPath.ValueString()
argsList := postrenderConfig.Args.Elements()

var args []string
for _, arg := range argsList {
args = append(args, arg.(basetypes.StringValue).ValueString())
}
tflog.Debug(ctx, fmt.Sprintf("Binary path update method: %s, Args: %v", binaryPath, args))
pr, err := postrender.NewExec(binaryPath, args...)
if err != nil {
resp.Diagnostics.AddError("Error creating post-renderer", fmt.Sprintf("Could not create post-renderer: %s", err))
return
}

client.PostRenderer = pr
var args []string
for _, arg := range argsList {
args = append(args, arg.(basetypes.StringValue).ValueString())
}
tflog.Debug(ctx, fmt.Sprintf("Binary path update method: %s, Args: %v", binaryPath, args))
pr, err := postrender.NewExec(binaryPath, args...)
if err != nil {
resp.Diagnostics.AddError("Error creating post-renderer", fmt.Sprintf("Could not create post-renderer: %s", err))
return
}
client.PostRenderer = pr
}
values, valuesDiags := getValues(ctx, &plan)
resp.Diagnostics.Append(valuesDiags...)
Expand Down Expand Up @@ -1748,31 +1730,28 @@ func (r *HelmRelease) ModifyPlan(ctx context.Context, req resource.ModifyPlanReq
var postRenderer postrender.PostRenderer
if !plan.Postrender.IsNull() {
// Extract the list of postrender configurations
var postrenderList []postrenderModel
postrenderDiags := plan.Postrender.ElementsAs(ctx, &postrenderList, false)
var postrenderConfig postrenderModel
postrenderDiags := plan.Postrender.As(ctx, &postrenderConfig, basetypes.ObjectAsOptions{})
resp.Diagnostics.Append(postrenderDiags...)
if resp.Diagnostics.HasError() {
return
}
if len(postrenderList) > 0 {
prModel := postrenderList[0]

binaryPath := prModel.BinaryPath.ValueString()
argsList := prModel.Args.Elements()

var args []string
for _, arg := range argsList {
args = append(args, arg.(basetypes.StringValue).ValueString())
}
binaryPath := postrenderConfig.BinaryPath.ValueString()
argsList := postrenderConfig.Args.Elements()

pr, err := postrender.NewExec(binaryPath, args...)
if err != nil {
resp.Diagnostics.AddError("Error creating post-renderer", fmt.Sprintf("Could not create post-renderer: %s", err))
return
}
var args []string
for _, arg := range argsList {
args = append(args, arg.(basetypes.StringValue).ValueString())
}

client.PostRenderer = pr
pr, err := postrender.NewExec(binaryPath, args...)
if err != nil {
resp.Diagnostics.AddError("Error creating post-renderer", fmt.Sprintf("Could not create post-renderer: %s", err))
return
}

client.PostRenderer = pr
}
if state == nil {
install := action.NewInstall(actionConfig)
Expand Down

0 comments on commit 8e884fd

Please sign in to comment.