Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add post renderer args #869

Merged
merged 6 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 2.6.0

* Upgrade helm to 3.9.0
* Add args attribute in post-render block

## 2.5.1 (April 11, 2022)

Expand Down
42 changes: 38 additions & 4 deletions helm/resource_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,12 @@ func resourceRelease() *schema.Resource {
Required: true,
Description: "The command binary path.",
},
"args": {
Type: schema.TypeList,
Optional: true,
Description: "an argument to the post-renderer (can specify multiple)",
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
Expand Down Expand Up @@ -535,9 +541,18 @@ func resourceReleaseCreate(ctx context.Context, d *schema.ResourceData, meta int
client.Replace = d.Get("replace").(bool)
client.Description = d.Get("description").(string)
client.CreateNamespace = d.Get("create_namespace").(bool)

if cmd := d.Get("postrender.0.binary_path").(string); cmd != "" {
pr, err := postrender.NewExec(cmd)
av := d.Get("postrender.0.args")
var args []string
for _, arg := range av.([]interface{}) {
if arg == nil {
continue
}
args = append(args, arg.(string))
}

pr, err := postrender.NewExec(cmd, args...)

if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -646,7 +661,16 @@ func resourceReleaseUpdate(ctx context.Context, d *schema.ResourceData, meta int
client.Description = d.Get("description").(string)

if cmd := d.Get("postrender.0.binary_path").(string); cmd != "" {
pr, err := postrender.NewExec(cmd)
av := d.Get("postrender.0.args")
var args []string
for _, arg := range av.([]interface{}) {
if arg == nil {
continue
}
args = append(args, arg.(string))
}

pr, err := postrender.NewExec(cmd, args...)

if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -795,7 +819,17 @@ func resourceDiff(ctx context.Context, d *schema.ResourceDiff, meta interface{})
client.Description = d.Get("description").(string)

if cmd := d.Get("postrender.0.binary_path").(string); cmd != "" {
pr, err := postrender.NewExec(cmd)
av := d.Get("postrender.0.args")
var args []string
for _, arg := range av.([]interface{}) {
if arg == nil {
continue
}
args = append(args, arg.(string))
}

pr, err := postrender.NewExec(cmd, args...)

if err != nil {
return err
}
Expand Down
17 changes: 13 additions & 4 deletions helm/resource_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ func TestAccResourceRelease_updateExistingFailed(t *testing.T) {
}

func TestAccResourceRelease_postrender(t *testing.T) {
// TODO: Add Test Fixture to return real YAML here

namespace := createRandomNamespace(t)
defer deleteNamespace(t, namespace)

Expand All @@ -497,19 +499,25 @@ func TestAccResourceRelease_postrender(t *testing.T) {
CheckDestroy: testAccCheckHelmReleaseDestroy(namespace),
Steps: []resource.TestStep{
{
Config: testAccHelmReleaseConfigPostrender(testResourceName, namespace, testResourceName, "cat"),
Config: testAccHelmReleaseConfigPostrender(testResourceName, namespace, testResourceName, "echo"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("helm_release.test", "status", release.StatusDeployed.String()),
),
},
{
Config: testAccHelmReleaseConfigPostrender(testResourceName, namespace, testResourceName, "date"),
Config: testAccHelmReleaseConfigPostrender(testResourceName, namespace, testResourceName, "echo", "this will not work!", "Wrong", "Code"),
ExpectError: regexp.MustCompile("error validating data"),
},
{
Config: testAccHelmReleaseConfigPostrender(testResourceName, namespace, testResourceName, "foobardoesnotexist"),
ExpectError: regexp.MustCompile("unable to find binary"),
},
{
Config: testAccHelmReleaseConfigPostrender(testResourceName, namespace, testResourceName, "true", "Hello", "World", "!"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("helm_release.test", "status", release.StatusDeployed.String()),
),
},
},
})
}
Expand Down Expand Up @@ -917,7 +925,7 @@ func testAccCheckHelmReleaseDestroy(namespace string) resource.TestCheckFunc {
}
}

func testAccHelmReleaseConfigPostrender(resource, ns, name, binaryPath string) string {
func testAccHelmReleaseConfigPostrender(resource, ns, name, binaryPath string, args ...string) string {
return fmt.Sprintf(`
resource "helm_release" "%s" {
name = %q
Expand All @@ -928,6 +936,7 @@ func testAccHelmReleaseConfigPostrender(resource, ns, name, binaryPath string) s

postrender {
binary_path = %q
args = %s
}

set {
Expand All @@ -939,7 +948,7 @@ func testAccHelmReleaseConfigPostrender(resource, ns, name, binaryPath string) s
value = 1337
}
}
`, resource, name, ns, testRepositoryURL, binaryPath)
`, resource, name, ns, testRepositoryURL, binaryPath, fmt.Sprintf(`["%s"]`, strings.Join(args, `","`)))
}

func TestAccResourceRelease_LintFailValues(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion website/docs/r/release.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ The `set` and `set_sensitive` blocks support:
* `value` - (Required) value of the variable to be set.
* `type` - (Optional) type of the variable to be set. Valid options are `auto` and `string`.

The `postrender` block supports a single attribute:
The `postrender` block supports two attributes:

* `binary_path` - (Required) relative or full path to command binary.
* `args` - (Optional) a list of arguments to supply to the post-renderer.



## Attributes Reference
Expand Down