Skip to content

Commit

Permalink
jobs: update HCL2 variables even if not set
Browse files Browse the repository at this point in the history
Read HCL2 variables from job submission even if the `nomad_job` resource
does not speify an `hcl2` block.
  • Loading branch information
lgfa29 committed Dec 19, 2023
1 parent b34b8a6 commit 070092d
Showing 1 changed file with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions nomad/resource_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"github.com/hashicorp/nomad/jobspec2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"golang.org/x/exp/maps"

"github.com/hashicorp/terraform-provider-nomad/nomad/helper"
)

Expand Down Expand Up @@ -665,21 +667,48 @@ func resourceJobRead(d *schema.ResourceData, meta interface{}) error {
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)
} else {
err := resourceJobReadSubmission(sub, d, meta)
if err != nil {
log.Printf("[WARN] failed to update job submission: %v", err)
}
}

return nil
}

func resourceJobReadSubmission(sub *api.JobSubmission, d *schema.ResourceData, meta any) error {
if sub == nil {
return nil
}

if sub.Source != "" {
d.Set("jobspec", sub.Source)
}

if sub.Format == "hcl2" {
var err error
var hcl2Config HCL2JobParserConfig

// Update HCL2 variables if present.
hcl2, ok := d.GetOk("hcl2")
if sub.Format == "hcl2" && ok {
hcl2Config, err := parseHCL2JobParserConfig(hcl2)
if ok {
hcl2Config, err = parseHCL2JobParserConfig(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 fmt.Errorf("failed to parse HCL2 config: %v", err)
}
} else {
// Use default values if hcl2 is not set.
hcl2Config = HCL2JobParserConfig{
AllowFS: false,
Enabled: true,
}
}

// Only update hcl2 if there are changes to variables to avoid
// unnecessary updates if hcl2 is not set.
if !maps.Equal(sub.VariableFlags, hcl2Config.Vars) {
hcl2Config.Vars = sub.VariableFlags
d.Set("hcl2", flattenHCL2JobParserConfig(hcl2Config))
}
}

Expand Down

0 comments on commit 070092d

Please sign in to comment.