diff --git a/CHANGELOG.md b/CHANGELOG.md index d7df7452..1d0dc540 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ IMPROVEMENTS: * data source/nomad_jwks: add EdDSA (Ed25519) key support ([#583](https://github.com/hashicorp/terraform-provider-nomad/pull/583)) +* data source/nomad_job_parser: add `variables` parameter to pass HCL2 variables to the job parser ([#582](https://github.com/hashicorp/terraform-provider-nomad/pull/582)) * resource/nomad_acl_policy: make `job_id` optional in `job_acl` block to allow policies that apply to all jobs in a namespace ([#580](https://github.com/hashicorp/terraform-provider-nomad/pull/580)) * resource/nomad_namespace: add `vault_config` and `consul_config` blocks to configure Vault and Consul cluster permissions (Nomad Enterprise only) ([#581](https://github.com/hashicorp/terraform-provider-nomad/pull/581)) * **New Data Source**: `nomad_node` to look up a single Nomad node by ID ([#579](https://github.com/hashicorp/terraform-provider-nomad/pull/579)) diff --git a/nomad/data_source_job_parser.go b/nomad/data_source_job_parser.go index e8f0c6e4..9f0121f5 100644 --- a/nomad/data_source_job_parser.go +++ b/nomad/data_source_job_parser.go @@ -9,6 +9,7 @@ import ( "log" "strings" + "github.com/hashicorp/nomad/api" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -27,6 +28,13 @@ func dataSourceJobParser() *schema.Resource { Optional: true, Default: false, }, + "variables": { + Description: "HCL2 variables to pass to the job parser. Interpreted as the content of a variables file.", + Type: schema.TypeString, + Optional: true, + Default: "", + }, + "json": { Description: "The parsed job as JSON string.", Type: schema.TypeString, @@ -42,9 +50,17 @@ func dataSourceJobParserRead(d *schema.ResourceData, meta interface{}) error { hcl := d.Get("hcl").(string) canonicalize := d.Get("canonicalize").(bool) + variables := d.Get("variables").(string) log.Printf("[DEBUG] Parsing Job with Canonicalize set to %t", canonicalize) - job, err := client.Jobs().ParseHCL(hcl, canonicalize) + + req := &api.JobsParseRequest{ + JobHCL: hcl, + Canonicalize: canonicalize, + Variables: variables, + } + + job, err := client.Jobs().ParseHCLOpts(req) if err != nil { return fmt.Errorf("error parsing job: %#v", err) } @@ -57,8 +73,6 @@ func dataSourceJobParserRead(d *schema.ResourceData, meta interface{}) error { jobJSONString := string(jobJSON) d.SetId(*job.ID) - d.Set("hcl", strings.TrimSpace(hcl)) - d.Set("canonicalize", canonicalize) d.Set("json", strings.TrimSpace(jobJSONString)) return nil diff --git a/nomad/data_source_job_parser_test.go b/nomad/data_source_job_parser_test.go index 1fdbbdae..e14084e7 100644 --- a/nomad/data_source_job_parser_test.go +++ b/nomad/data_source_job_parser_test.go @@ -9,7 +9,6 @@ import ( "fmt" "reflect" "regexp" - "strings" "testing" "github.com/hashicorp/nomad/api" @@ -26,13 +25,7 @@ func TestAccDataSourceNomadJobParser_Basic(t *testing.T) { Steps: []resource.TestStep{ { Config: testJobParserConfig(), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - resourceName, "hcl", strings.TrimSpace(testDataSourceJobParserHCL)), - resource.TestCheckResourceAttr( - resourceName, "canonicalize", "false"), - checkJobFromString(resourceName, testDataSourceJobParserJSON), - ), + Check: checkJobFromString(resourceName, testDataSourceJobParserJSON), }, }, }) @@ -288,3 +281,159 @@ data "nomad_job_parser" "test_job" { const testDataSourceJobParserMissingHCLConfig = ` data "nomad_job_parser" "test_job" { }` + +func TestAccDataSourceNomadJobParser_WithVariables(t *testing.T) { + resourceName := "data.nomad_job_parser.test_job" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testProviders, + Steps: []resource.TestStep{ + { + Config: testJobParserWithVariablesConfig(), + Check: checkJobFromString(resourceName, testDataSourceJobParserWithVariablesJSON), + }, + }, + }) +} + +func testJobParserWithVariablesConfig() string { + return fmt.Sprintf(` +data "nomad_job_parser" "test_job" { + hcl = <)` - The HCL definition of the job. +- `canonicalize` `(boolean: false)` - Flag to enable setting any unset fields to their default values. +- `variables` `(string: "")` - HCL2 variables to pass to the job parser. Interpreted as the content of a variables file. + ## Attribute Reference The following attributes are exported: -- `hcl` `(string)` - the HCL definition of the job. -- `canonicalize` `(boolean: true)` - flag to enable setting any unset fields to their default values. -- `json` `(string)` - the parsed job as JSON string. +- `json` `(string)` - The parsed job as JSON string.