diff --git a/CHANGELOG.md b/CHANGELOG.md index e512e531..89ad4af7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ BACKWARDS INCOMPATIBILITIES: * provider: Terraform Plugin SDK upgraded to v2.10.1. **Terraform versions prior to 0.12 are no longer supported.** ([#339](https://github.com/hashicorp/terraform-provider-nomad/issues/339)) +* resource/nomad_job: Switch to HCL2 parsing by default. Jobs that require HCL1 parsing must set `hcl1 = true`. ([#343](https://github.com/hashicorp/terraform-provider-nomad/pull/343)) IMPROVEMENTS: * provider: add `skip_verify` configuration to skip TLS verification ([#319](https://github.com/hashicorp/terraform-provider-nomad/pull/319)) diff --git a/nomad/resource_job.go b/nomad/resource_job.go index 8ca788b9..a96e4413 100644 --- a/nomad/resource_job.go +++ b/nomad/resource_job.go @@ -91,6 +91,7 @@ func resourceJob() *schema.Resource { Schema: map[string]*schema.Schema{ "enabled": { Description: "If true, the `jobspec` will be parsed as HCL2 instead of HCL.", + Deprecated: "Starting with version 2.0.0 of the Nomad provider, jobs are parsed using HCL2 by default, so this field is no longer used and may be safely removed from your configuration files. Set 'hcl1 = true' if you must use HCL1 job parsing.", Type: schema.TypeBool, Optional: true, Default: false, @@ -110,6 +111,13 @@ func resourceJob() *schema.Resource { }, }, + "hcl1": { + Description: "If true, the `jobspec` will be parsed using the HCL1 format.", + Optional: true, + Default: false, + Type: schema.TypeBool, + }, + "json": { Description: "If true, the `jobspec` will be parsed as json instead of HCL.", Optional: true, @@ -298,6 +306,7 @@ func taskGroupSchema() *schema.Schema { // JobParserConfig stores configuration options for how to parse the jobspec. type JobParserConfig struct { JSON JSONJobParserConfig + HCL1 HCL1JobParserConfig HCL2 HCL2JobParserConfig } @@ -306,9 +315,13 @@ type JSONJobParserConfig struct { Enabled bool } +// HCL1JobParserConfig stores configuration options for the HCL1 jobspec parser. +type HCL1JobParserConfig struct { + Enabled bool +} + // HCL2JobParserConfig stores configuration options for the HCL2 jobspec parser. type HCL2JobParserConfig struct { - Enabled bool AllowFS bool Vars map[string]string } @@ -718,6 +731,12 @@ func parseJobParserConfig(d ResourceFieldGetter) (JobParserConfig, error) { Enabled: jsonEnabled, } + // Read HCL1 parser configuration. + hclEnabled := d.Get("hcl1").(bool) + config.HCL1 = HCL1JobParserConfig{ + Enabled: hclEnabled, + } + // Read HCL2 parser configuration. hcl2Config, err := parseHCL2JobParserConfig(d.Get("hcl2")) if err != nil { @@ -725,9 +744,9 @@ func parseJobParserConfig(d ResourceFieldGetter) (JobParserConfig, error) { } config.HCL2 = hcl2Config - // JSON and HCL2 parsing are conflicting options. - if config.JSON.Enabled && config.HCL2.Enabled { - return config, fmt.Errorf("invalid combination. json is %t and hcl2.enabled is %t", config.JSON.Enabled, config.HCL2.Enabled) + // JSON and HCL1 parsing are conflicting options. + if config.JSON.Enabled && config.HCL1.Enabled { + return config, fmt.Errorf("invalid combination. json is %t and hcl1 is %t", config.JSON.Enabled, config.HCL1.Enabled) } return config, nil @@ -754,9 +773,6 @@ func parseHCL2JobParserConfig(raw interface{}) (HCL2JobParserConfig, error) { } // Read map fields into parser config struct. - if enabled, ok := hcl2Map["enabled"].(bool); ok { - config.Enabled = enabled - } if allowFS, ok := hcl2Map["allow_fs"].(bool); ok { config.AllowFS = allowFS } @@ -777,10 +793,10 @@ func parseJobspec(raw string, config JobParserConfig, vaultToken *string, consul switch { case config.JSON.Enabled: job, err = parseJSONJobspec(raw) - case config.HCL2.Enabled: - job, err = parseHCL2Jobspec(raw, config.HCL2) - default: + case config.HCL1.Enabled: job, err = jobspec.Parse(strings.NewReader(raw)) + default: + job, err = parseHCL2Jobspec(raw, config.HCL2) } if err != nil { @@ -933,12 +949,12 @@ func jobspecDiffSuppress(k, old, new string, d *schema.ResourceData) bool { case jobParserConfig.JSON.Enabled: oldJob, oldErr = parseJSONJobspec(old) newJob, newErr = parseJSONJobspec(new) - case jobParserConfig.HCL2.Enabled: - oldJob, oldErr = parseHCL2Jobspec(old, jobParserConfig.HCL2) - newJob, newErr = parseHCL2Jobspec(new, jobParserConfig.HCL2) - default: + case jobParserConfig.HCL1.Enabled: oldJob, oldErr = jobspec.Parse(strings.NewReader(old)) newJob, newErr = jobspec.Parse(strings.NewReader(new)) + default: + oldJob, oldErr = parseHCL2Jobspec(old, jobParserConfig.HCL2) + newJob, newErr = parseHCL2Jobspec(new, jobParserConfig.HCL2) } if oldErr != nil { log.Println("error parsing old jobspec") diff --git a/nomad/resource_job_test.go b/nomad/resource_job_test.go index d004eccd..824af7d9 100644 --- a/nomad/resource_job_test.go +++ b/nomad/resource_job_test.go @@ -555,8 +555,12 @@ func TestResourceJob_hcl2(t *testing.T) { PreCheck: func() { testAccPreCheck(t); testCheckMinVersion(t, "1.0.0") }, Steps: []r.TestStep{ { - Config: testResourceJob_hcl2_and_json, - ExpectError: regexp.MustCompile("json is true and hcl2.enabled is true"), + Config: testResourceJob_hcl1_and_json, + ExpectError: regexp.MustCompile("json is true and hcl1 is true"), + }, + { + Config: testResourceJob_hcl1_hcl2_spec, + ExpectError: regexp.MustCompile("error parsing jobspec"), }, { Config: testResourceJob_hcl2_no_fs, @@ -3248,13 +3252,10 @@ EOT } ` -var testResourceJob_hcl2_and_json = ` +var testResourceJob_hcl1_hcl2_spec = ` resource "nomad_job" "hcl2" { - hcl2 { - enabled = true - } + hcl1 = true - json = true jobspec = < **Note:** This option should be enabled whenever possible as it includes - support for newer jobspec entries. +-> **Note:** HCL1 parsing is only provided for backwards compatibility with + older jobspecs and may not support newer job features and functionalities, so + it should be avoided whenever possible. ### Variables @@ -126,7 +126,6 @@ values must be provided as strings. ```hcl resource "nomad_job" "app" { hcl2 { - enabled = true vars = { "restart_attempts" = "5", "datacenters" = "[\"dc1\", \"dc2\"]", @@ -176,7 +175,6 @@ job "example" { EOT hcl2 { - enabled = true vars = { datacenter = random_pet.random_dc.id } @@ -192,10 +190,6 @@ job "example" { ... } EOT - - hcl2 { - enabled = true - } } ``` @@ -216,7 +210,6 @@ resource "nomad_job" "app" { jobspec = file("${path.module}/jobspec.hcl") hcl2 { - enabled = true allow_fs = true } } @@ -280,9 +273,13 @@ The following arguments are supported: - `json` `(boolean: false)` - Set this to `true` if your jobspec is structured with JSON instead of the default HCL. +- `hcl1` `(boolean: false)` - Set this to `true` to use the previous HCL1 + parser. This option is provided for backwards compatibility only and should + not be used unless absolutely necessary. + - `hcl2` `(block: optional)` - Options for the HCL2 jobspec parser. - - `enabled` `(boolean: false)` - Set this to `true` if your jobspec uses the HCL2 - format instead of the default HCL. + - `enabled` `(boolean: false)` - **Deprecated** All HCL jobs are parsed as + HCL2 by default. - `allow_fs` `(boolean: false)` - Set this to `true` to be able to use [HCL2 filesystem functions](#filesystem-functions)