diff --git a/nomad/resource_job.go b/nomad/resource_job.go index 522f6f01..3d0b7f52 100644 --- a/nomad/resource_job.go +++ b/nomad/resource_job.go @@ -344,6 +344,11 @@ type HCL1JobParserConfig struct { type HCL2JobParserConfig struct { AllowFS bool Vars map[string]string + + // Deprecated: Starting in v2.0.0 the provider assumes HCL2 parsing by + // default. This field should only be used to update the `hcl2` attribute + // in state without causing a diff. + Enabled bool } // ResourceFieldGetter are able to retrieve field values. @@ -643,6 +648,28 @@ func resourceJobRead(d *schema.ResourceData, meta interface{}) error { d.Set("allocation_ids", nil) } + // Update jobspec submission data if available. + // Safely ignore errors as this is an optional step. + sub, _, err := client.Jobs().Submission(*job.ID, int(*job.Version), opts) + if err != nil { + log.Printf("[WARN] failed to read job submission: %v", err) + } else if sub != nil { + if sub.Source != "" { + d.Set("jobspec", sub.Source) + } + + // Update HCL2 variables if present. + if sub.Format == "hcl2" { + hcl2Config, err := parseHCL2JobParserConfig(d.Get("hcl2")) + if err != nil { + log.Printf("[WARN] failed to parse HCL2 config: %v", err) + } else { + hcl2Config.Vars = sub.VariableFlags + d.Set("hcl2", flattenHCL2JobParserConfig(hcl2Config)) + } + } + } + return nil } @@ -829,6 +856,9 @@ func parseHCL2JobParserConfig(raw interface{}) (HCL2JobParserConfig, error) { if allowFS, ok := hcl2Map["allow_fs"].(bool); ok { config.AllowFS = allowFS } + if enabled, ok := hcl2Map["enabled"].(bool); ok { + config.Enabled = enabled + } if vars, ok := hcl2Map["vars"].(map[string]interface{}); ok { config.Vars = make(map[string]string) for k, v := range vars { @@ -839,6 +869,14 @@ func parseHCL2JobParserConfig(raw interface{}) (HCL2JobParserConfig, error) { return config, nil } +func flattenHCL2JobParserConfig(c HCL2JobParserConfig) []any { + return []any{map[string]any{ + "allow_fs": c.AllowFS, + "enabled": c.Enabled, + "vars": c.Vars, + }} +} + func parseJobspec(raw string, config JobParserConfig, vaultToken *string, consulToken *string) (*api.Job, error) { var job *api.Job var err error diff --git a/website/docs/r/job.html.markdown b/website/docs/r/job.html.markdown index 01e11a67..3fb43b8b 100644 --- a/website/docs/r/job.html.markdown +++ b/website/docs/r/job.html.markdown @@ -249,6 +249,13 @@ EOF } ``` +## Tracking Jobspec Changes + +The Nomad API allows [submitting the raw jobspec when registering and updating +jobs](https://developer.hashicorp.com/nomad/api-docs/jobs#submission). If +available, the job submission source is used to detect changes to the `jobspec` +and `hcl2.vars` arguments. + ## Argument Reference The following arguments are supported: