Skip to content

Commit 070092d

Browse files
committed
jobs: update HCL2 variables even if not set
Read HCL2 variables from job submission even if the `nomad_job` resource does not speify an `hcl2` block.
1 parent b34b8a6 commit 070092d

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

nomad/resource_job.go

+39-10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"github.com/hashicorp/nomad/jobspec2"
2020
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
2121
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
22+
"golang.org/x/exp/maps"
23+
2224
"github.com/hashicorp/terraform-provider-nomad/nomad/helper"
2325
)
2426

@@ -665,21 +667,48 @@ func resourceJobRead(d *schema.ResourceData, meta interface{}) error {
665667
sub, _, err := client.Jobs().Submission(*job.ID, int(*job.Version), opts)
666668
if err != nil {
667669
log.Printf("[WARN] failed to read job submission: %v", err)
668-
} else if sub != nil {
669-
if sub.Source != "" {
670-
d.Set("jobspec", sub.Source)
670+
} else {
671+
err := resourceJobReadSubmission(sub, d, meta)
672+
if err != nil {
673+
log.Printf("[WARN] failed to update job submission: %v", err)
671674
}
675+
}
676+
677+
return nil
678+
}
679+
680+
func resourceJobReadSubmission(sub *api.JobSubmission, d *schema.ResourceData, meta any) error {
681+
if sub == nil {
682+
return nil
683+
}
684+
685+
if sub.Source != "" {
686+
d.Set("jobspec", sub.Source)
687+
}
688+
689+
if sub.Format == "hcl2" {
690+
var err error
691+
var hcl2Config HCL2JobParserConfig
672692

673-
// Update HCL2 variables if present.
674693
hcl2, ok := d.GetOk("hcl2")
675-
if sub.Format == "hcl2" && ok {
676-
hcl2Config, err := parseHCL2JobParserConfig(hcl2)
694+
if ok {
695+
hcl2Config, err = parseHCL2JobParserConfig(hcl2)
677696
if err != nil {
678-
log.Printf("[WARN] failed to parse HCL2 config: %v", err)
679-
} else {
680-
hcl2Config.Vars = sub.VariableFlags
681-
d.Set("hcl2", flattenHCL2JobParserConfig(hcl2Config))
697+
return fmt.Errorf("failed to parse HCL2 config: %v", err)
682698
}
699+
} else {
700+
// Use default values if hcl2 is not set.
701+
hcl2Config = HCL2JobParserConfig{
702+
AllowFS: false,
703+
Enabled: true,
704+
}
705+
}
706+
707+
// Only update hcl2 if there are changes to variables to avoid
708+
// unnecessary updates if hcl2 is not set.
709+
if !maps.Equal(sub.VariableFlags, hcl2Config.Vars) {
710+
hcl2Config.Vars = sub.VariableFlags
711+
d.Set("hcl2", flattenHCL2JobParserConfig(hcl2Config))
683712
}
684713
}
685714

0 commit comments

Comments
 (0)