Skip to content

Commit

Permalink
job: ensure that job is running
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigol-chan committed Oct 13, 2023
1 parent ad587f7 commit 2d2e53c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
14 changes: 14 additions & 0 deletions nomad/resource_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"log"
"reflect"
"slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -152,6 +153,12 @@ func resourceJob() *schema.Resource {
Type: schema.TypeString,
},

"status": {
Description: "The status of the job.",
Computed: true,
Type: schema.TypeString,
},

"region": {
Description: "The target region for the job, as derived from the jobspec.",
Computed: true,
Expand Down Expand Up @@ -604,6 +611,7 @@ func resourceJobRead(d *schema.ResourceData, meta interface{}) error {
} else {
d.Set("modify_index", "0")
}
d.Set("status", job.Status)

if d.Get("read_allocation_ids").(bool) {
allocStubs, _, err := client.Jobs().Allocations(id, false, opts)
Expand Down Expand Up @@ -639,9 +647,14 @@ func resourceJobCustomizeDiff(_ context.Context, d *schema.ResourceDiff, meta in
d.SetNewComputed("task_groups")
d.SetNewComputed("deployment_id")
d.SetNewComputed("deployment_status")
d.SetNewComputed("status")
return nil
}

if !slices.Contains([]string{"running", "pending"}, d.Get("status").(string)) {
d.SetNewComputed("status")
}

oldSpecRaw, newSpecRaw := d.GetChange("jobspec")

if jobspecEqual("jobspec", oldSpecRaw.(string), newSpecRaw.(string), d) {
Expand Down Expand Up @@ -696,6 +709,7 @@ func resourceJobCustomizeDiff(_ context.Context, d *schema.ResourceDiff, meta in
d.SetNew("type", job.Type)
d.SetNew("region", job.Region)
d.SetNew("datacenters", job.Datacenters)
d.SetNew("status", job.Status)

// If the identity has changed and the config asks us to deregister on identity
// change then the id field "forces new resource".
Expand Down
45 changes: 45 additions & 0 deletions nomad/resource_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3366,3 +3366,48 @@ job "example" {
})
}
}

func testResourceJob_externalStopCheck(t *testing.T) r.TestCheckFunc {
return func(s *terraform.State) error {
resourceState := s.Modules[0].Resources["nomad_job.test"]
if resourceState == nil {
return errors.New("resource not found in state")
}

instanceState := resourceState.Primary
if instanceState == nil {
return errors.New("resource has no primary instance")
}

jobID := instanceState.ID
providerConfig := testProvider.Meta().(ProviderConfig)
client := providerConfig.client
_, _, err := client.Jobs().Deregister(jobID, false, &api.WriteOptions{
Namespace: instanceState.Attributes["namespace"],
})
if err != nil {
return fmt.Errorf("error reading back job: %s", err)
}

return nil
}
}

func TestResourceJob_externalStop(t *testing.T) {
r.Test(t, r.TestCase{
Providers: testProviders,
PreCheck: func() { testAccPreCheck(t) },
Steps: []r.TestStep{
{
Config: testResourceJob_initialConfig,
Check: testResourceJob_initialCheck(t),
},
{
Config: testResourceJob_initialConfig,
Check: testResourceJob_externalStopCheck(t),
ExpectNonEmptyPlan: true,
},
},
CheckDestroy: testResourceJob_checkDestroy("foo"),
})
}

0 comments on commit 2d2e53c

Please sign in to comment.