Skip to content

Commit

Permalink
set.value: required -> optional (#1572)
Browse files Browse the repository at this point in the history
* required -> optional

* add resource test

* add data source test

* set_list.value is required

* docs set_list required

* changelog entry
  • Loading branch information
BBBmau authored Feb 24, 2025
1 parent 420fb40 commit b6bb55a
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .changelog/1572.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
change `set.value` && `set_list.value` to optional instead of required
```
2 changes: 1 addition & 1 deletion docs/data-sources/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ Required:
Required:

- `name` (String)
- `value` (String)

Optional:

- `type` (String)
- `value` (String)


<a id="nestedblock--set_list"></a>
Expand Down
5 changes: 2 additions & 3 deletions docs/resources/release.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,11 @@ Optional:
Required:

- `name` (String)
- `value` (String)

Optional:

- `type` (String)

- `value` (String)

<a id="nestedblock--set_list"></a>
### Nested Schema for `set_list`
Expand Down Expand Up @@ -289,7 +288,7 @@ resource "helm_release" "example" {
The `set`, `set_list`, and `set_sensitive` blocks support:

* `name` - (Required) full name of the variable to be set.
* `value` - (Required) value of the variable to be set.
* `value` - (Required; Optional for `set`) value of the variable to be set.
* `type` - (Optional) type of the variable to be set. Valid options are `auto` and `string`.

Since Terraform Utilizes HCL as well as Helm using the Helm Template Language, it's necessary to escape the `{}`, `[]`, `.`, and `,` characters twice in order for it to be parsed. `name` should also be set to the `value path`, and `value` is the desired value that will be set.
Expand Down
4 changes: 2 additions & 2 deletions helm/data_helm_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func (d *HelmTemplate) Schema(ctx context.Context, req datasource.SchemaRequest,
Required: true,
},
"value": schema.StringAttribute{
Required: true,
Optional: true,
},
"type": schema.StringAttribute{
Optional: true,
Expand All @@ -311,7 +311,7 @@ func (d *HelmTemplate) Schema(ctx context.Context, req datasource.SchemaRequest,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Required: true,
Optional: true,
},
"value": schema.ListAttribute{
Required: true,
Expand Down
46 changes: 46 additions & 0 deletions helm/data_helm_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,24 @@ func TestAccDataTemplate_kubeVersion(t *testing.T) {
})
}

func TestAccDataTemplate_configSetNull(t *testing.T) {
name := randName("basic")
namespace := randName(testNamespacePrefix)

datasourceAddress := fmt.Sprintf("data.helm_template.%s", testResourceName)

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{{
Config: testAccDataHelmTemplateConfigNullSet(testResourceName, namespace, name, "1.2.3"),
Check: resource.ComposeAggregateTestCheckFunc(
checkResourceAttrNotSet(datasourceAddress, "set.0.value"),
checkResourceAttrNotSet(datasourceAddress, "set.1.value"),
),
}},
})
}

func testAccDataHelmTemplateConfigBasic(resource, ns, name, version string) string {
return fmt.Sprintf(`
data "helm_template" "%s" {
Expand Down Expand Up @@ -290,6 +308,34 @@ func testAccDataHelmTemplateConfigTemplates(resource, ns, name, version string)
`, resource, name, ns, testRepositoryURL, version)
}

func testAccDataHelmTemplateConfigNullSet(resource, ns, name, version string) string {
return fmt.Sprintf(`
data "helm_template" "%s" {
name = %q
namespace = %q
description = "Test"
repository = %q
chart = "test-chart"
version = %q
set = [
{
name = "foo"
},
{
name = "fizz"
value = null
}
]
show_only = [
"templates/configmaps.yaml",
""
]
}
`, resource, name, ns, testRepositoryURL, version)
}

func testAccDataHelmTemplateKubeVersion(resource, ns, name, version, kubeVersion string) string {
return fmt.Sprintf(`
data "helm_template" "%s" {
Expand Down
2 changes: 1 addition & 1 deletion helm/resource_helm_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ func (r *HelmRelease) Schema(ctx context.Context, req resource.SchemaRequest, re
Required: true,
},
"value": schema.StringAttribute{
Required: true,
Optional: true,
},
"type": schema.StringAttribute{
Optional: true,
Expand Down
66 changes: 66 additions & 0 deletions helm/resource_helm_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,29 @@ func TestAccResourceRelease_updateExistingFailed(t *testing.T) {
},
})
}
func TestAccResourceRelease_SetNull(t *testing.T) {
name := randName("test-update-set-value")
namespace := createRandomNamespace(t)
defer deleteNamespace(t, namespace)

// Ensure that value is null
resource.Test(t, resource.TestCase{
//PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: protoV6ProviderFactories(),
//CheckDestroy: testAccCheckHelmReleaseDestroy(namespace),
Steps: []resource.TestStep{
{
Config: testAccHelmReleaseConfigSetNull(
testResourceName, namespace, name, "1.2.3",
),
Check: resource.ComposeAggregateTestCheckFunc(
checkResourceAttrNotSet("helm_release.test", "set.0.value"),
checkResourceAttrNotSet("helm_release.test", "set.1.value"),
),
},
},
})
}
func TestAccResourceRelease_updateSetValue(t *testing.T) {
name := randName("test-update-set-value")
namespace := createRandomNamespace(t)
Expand Down Expand Up @@ -602,6 +625,26 @@ func checkResourceAttrExists(name, key string) resource.TestCheckFunc {
return fmt.Errorf("%s: Attribute '%s' expected to be set", name, key)
}
}
func checkResourceAttrNotSet(name, key string) resource.TestCheckFunc {
return func(s *terraform.State) error {
ms := s.RootModule()
rs, ok := ms.Resources[name]
if !ok {
return fmt.Errorf("Not found: %s in %s", name, ms.Path)
}

is := rs.Primary
if is == nil {
return fmt.Errorf("No primary instance: %s in %s", name, ms.Path)
}

if _, ok := is.Attributes[key]; !ok {
return nil // Success - attribute does not exist
}
return fmt.Errorf("%s: Attribute '%s' exists but was expected to be unset", name, key)
}
}

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

Expand Down Expand Up @@ -898,6 +941,29 @@ func testAccHelmReleaseConfigSet(resource, ns, name, version, setValue string) s
`, resource, name, ns, testRepositoryURL, version, setValue)
}

func testAccHelmReleaseConfigSetNull(resource, ns, name, version string) string {
return fmt.Sprintf(`
resource "helm_release" "%s" {
name = %q
namespace = %q
description = "Test"
repository = %q
chart = "test-chart"
version = %q
set = [
{
name = "foo"
value = null
},
{
name = "fizz"
}
]
}
`, resource, name, ns, testRepositoryURL, version)
}

// func TestGetValues(t *testing.T) {
// // Initialize a new HelmReleaseResource
// r := NewHelmReleaseResource().Data(nil)
Expand Down

0 comments on commit b6bb55a

Please sign in to comment.