diff --git a/jsonnet/data_source_jsonnet_template.go b/jsonnet/data_source_jsonnet_template.go index ca8a121..330f270 100644 --- a/jsonnet/data_source_jsonnet_template.go +++ b/jsonnet/data_source_jsonnet_template.go @@ -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", @@ -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 diff --git a/jsonnet/data_source_jsonnet_template_test.go b/jsonnet/data_source_jsonnet_template_test.go index 1927a89..9b5d5ff 100644 --- a/jsonnet/data_source_jsonnet_template_test.go +++ b/jsonnet/data_source_jsonnet_template_test.go @@ -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] @@ -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'" + } +} +`