Skip to content

Commit

Permalink
resource_chart: new values field and value renamed to set
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed Sep 4, 2017
1 parent b1a5f90 commit 5d5bad8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 29 deletions.
9 changes: 5 additions & 4 deletions docs/chart.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ The following arguments are supported:
* `repository` - (Optional) Repository where to locate the requested chart. If is an URL the chart is installed without install the repository.
* `chart` - (Required) Chart name to be installed.
* `version` - (Optional) Specify the exact chart version to install. If this is not specified, the latest version is installed.
* `value` - (Optional) Value block with custom values to be merge with the values.yaml.
* `values` - (Optional) Values in raw yaml file to pass to helm.
* `set` - (Optional) Value block with custom values to be merge with the values.yaml.
* `namespace` - (Optional) Namespace to install the release into.
* `repository_url` - (Optional) Repository URL where to locate the requested chart without install the repository.
* `verify` - (Optional) Verify the package before installing it.
Expand All @@ -39,10 +40,10 @@ The following arguments are supported:
* `force_update` - (Optional) Force resource update through delete/recreate if needed.
* `recreate_pods` - (Optional) On update performs pods restart for the resource if applicable.

The `value` block supports:
The `set` block supports:

* `content` - (Required)
* `name` - (Required)
* `name` - (Required) fullname of the variable to be set.
* `value` - (Required) value of the variable to be set.


## Attributes Reference
Expand Down
35 changes: 18 additions & 17 deletions helm/resource_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,22 @@ func resourceChart() *schema.Resource {
Optional: true,
Description: "Specify the exact chart version to install. If this is not specified, the latest version is installed.",
},
"raw_values": {
"values": {
Type: schema.TypeString,
Optional: true,
Description: "Set raw yaml file to pass to helm.",
Description: "Values in raw yaml file to pass to helm.",
},
"value": {
"set": {
Type: schema.TypeSet,
Optional: true,
Description: "Custom values to be merge with the values.yaml.",
Description: "Custom values to be merge with the values.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"content": {
"value": {
Type: schema.TypeString,
Required: true,
},
Expand Down Expand Up @@ -330,29 +330,30 @@ func getChart(d *schema.ResourceData, m *Meta) (c *chart.Chart, path string, err
func getValues(d *schema.ResourceData) ([]byte, error) {
base := map[string]interface{}{}

raw_values := d.Get("raw_values").(string)
if raw_values != "" {
if err := yaml.Unmarshal([]byte(raw_values), &base); err != nil {
values := d.Get("values").(string)
if values != "" {
if err := yaml.Unmarshal([]byte(values), &base); err != nil {
return nil, err
}
}

for _, raw := range d.Get("value").(*schema.Set).List() {
value := raw.(map[string]interface{})
for _, raw := range d.Get("set").(*schema.Set).List() {
set := raw.(map[string]interface{})

name := value["name"].(string)
content := value["content"].(string)
name := set["name"].(string)
value := set["value"].(string)

if err := strvals.ParseInto(fmt.Sprintf("%s=%s", name, content), base); err != nil {
return nil, fmt.Errorf("failed parsing key %q with value %s, %s", name, content, err)
if err := strvals.ParseInto(fmt.Sprintf("%s=%s", name, value), base); err != nil {
return nil, fmt.Errorf("failed parsing key %q with value %s, %s", name, value, err)
}
}

values, err := yaml.Marshal(base)
yaml, err := yaml.Marshal(base)
if err == nil {
log.Printf("---[ values.yaml ]-----------------------------------\n%s\n", string(values))
log.Printf("---[ values.yaml ]-----------------------------------\n%s\n", yaml)
}
return values, err

return yaml, err
}

var all = []release.Status_Code{
Expand Down
48 changes: 40 additions & 8 deletions helm/resource_chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"regexp"
"testing"

yaml "gopkg.in/yaml.v1"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"k8s.io/helm/pkg/helm"
Expand Down Expand Up @@ -83,9 +85,9 @@ func TestAccResourceChart_updateAfterFail(t *testing.T) {
resource "helm_chart" "test" {
name = "malformed"
chart = "stable/nginx-ingress"
value {
name = "controller.podAnnotations.\"prometheus\\.io/scrape\""
content = "true"
set {
name = "controller.podAnnotations.\"prometheus\\.io/scrape\""
value = "true"
}
}
`
Expand Down Expand Up @@ -116,19 +118,44 @@ func testAccHelmChartConfigBasic(ns, name, version string) string {
chart = "stable/mariadb"
version = %q
value {
set {
name = "foo"
content = "qux"
value = "qux"
}
value {
set {
name = "qux.bar"
content = 1
value = 1
}
}
`, name, ns, version)
}

func TestGetValues(t *testing.T) {
d := resourceChart().Data(nil)
d.Set("values", `foo: bar`)
d.Set("set", []interface{}{
map[string]interface{}{"name": "foo", "value": "qux"},
})

values, err := getValues(d)
if err != nil {
t.Fatalf("error getValues: %s", err)
return
}

base := map[string]string{}
err = yaml.Unmarshal([]byte(values), &base)
if err != nil {
t.Fatalf("error parsing returned yaml: %s", err)
return
}

if base["foo"] != "qux" {
t.Fatalf("error merging values, expected %q, got %q", "qux", base["foo"])
}
}

func testAccHelmChartConfigRepository(ns, name string) string {
return fmt.Sprintf(`
resource "helm_repository" "incubator" {
Expand All @@ -146,7 +173,12 @@ func testAccHelmChartConfigRepository(ns, name string) string {
}

func testAccCheckHelmChartDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*Meta).GetHelmClient()
m := testAccProvider.Meta()
if m == nil {
return fmt.Errorf("provider not properly initialized")
}

client := m.(*Meta).GetHelmClient()

res, err := client.ListReleases(
helm.ReleaseListNamespace(testNamespace),
Expand Down

0 comments on commit 5d5bad8

Please sign in to comment.