Skip to content

Commit

Permalink
Add support for external parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
andrein committed Jun 26, 2020
1 parent 038578e commit 7b3f588
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
56 changes: 56 additions & 0 deletions jsonnet/data_source_jsonnet_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,38 @@ func dataSourceJsonnetTemplate() *schema.Resource {
Type: schema.TypeString,
},
},
"ext_var": {
Type: schema.TypeMap,
Description: "A map of Jsonnet external variables, as strings",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"ext_code": {
Type: schema.TypeMap,
Description: "A map of Jsonnet external variables, as code",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"tla_var": {
Type: schema.TypeMap,
Description: "A map of Jsonnet top-level arguments, as strings",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"tla_code": {
Type: schema.TypeMap,
Description: "A map of Jsonnet top-level arguments, as code",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"json": {
Type: schema.TypeString,
Description: "The JSON output",
Expand All @@ -42,6 +74,30 @@ func dataSourceJsonnetTemplateRead(d *schema.ResourceData, _ interface{}) error
vm.Importer(&importer)
}

if extVar := d.Get("ext_var").(map[string]interface{}); extVar != nil {
for key, val := range extVar {
vm.ExtVar(key, val.(string))
}
}

if extCode := d.Get("ext_code").(map[string]interface{}); extCode != nil {
for key, val := range extCode {
vm.ExtCode(key, val.(string))
}
}

if tlaVar := d.Get("tla_var").(map[string]interface{}); tlaVar != nil {
for key, val := range tlaVar {
vm.TLAVar(key, val.(string))
}
}

if tlaCode := d.Get("tla_code").(map[string]interface{}); tlaCode != nil {
for key, val := range tlaCode {
vm.TLACode(key, val.(string))
}
}

json, err := vm.EvaluateSnippet("input", d.Get("jsonnet").(string))
if err != nil {
return err
Expand Down
80 changes: 80 additions & 0 deletions jsonnet/data_source_jsonnet_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,38 @@ func TestAccJsonnet_jpath(t *testing.T) {
})
}

func TestAccJsonnet_ext(t *testing.T) {
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccJsonnetTemplate_ext_var,
Check: testCheckResourceJsonAttr("data.jsonnet_template.test", "json", `{"result": "test"}`),
},
{
Config: testAccJsonnetTemplate_ext_code,
Check: testCheckResourceJsonAttr("data.jsonnet_template.test", "json", `{"result": "test"}`),
},
},
})
}

func TestAccJsonnet_tla(t *testing.T) {
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccJsonnetTemplate_tla_var,
Check: testCheckResourceJsonAttr("data.jsonnet_template.test", "json", `{"result": "test"}`),
},
{
Config: testAccJsonnetTemplate_tla_code,
Check: testCheckResourceJsonAttr("data.jsonnet_template.test", "json", `{"result": "test"}`),
},
},
})
}

func testCheckResourceJsonAttr(name, key, value string) resource.TestCheckFunc {
return func(s *terraform.State) error {
actual := s.RootModule().Resources[name].Primary.Attributes[key]
Expand Down Expand Up @@ -81,3 +113,51 @@ data "jsonnet_template" "test" {
jpath = ["test-fixtures"]
}
`

var testAccJsonnetTemplate_ext_var = `
data "jsonnet_template" "test" {
jsonnet = <<-EOF
{ result: std.extVar('result') }
EOF
ext_var = {
result = "test"
}
}
`

var testAccJsonnetTemplate_ext_code = `
data "jsonnet_template" "test" {
jsonnet = <<-EOF
{ result: std.extVar('result') }
EOF
ext_code = {
result = "'test'"
}
}
`

var testAccJsonnetTemplate_tla_var = `
data "jsonnet_template" "test" {
jsonnet = <<-EOF
function(result) {
result: result
}
EOF
tla_var = {
result = "test"
}
}
`

var testAccJsonnetTemplate_tla_code = `
data "jsonnet_template" "test" {
jsonnet = <<-EOF
function(result) {
result: result
}
EOF
tla_code = {
result = "'test'"
}
}
`

0 comments on commit 7b3f588

Please sign in to comment.