Skip to content

Commit

Permalink
add allocation_ids to Nomad Job resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Baker committed May 15, 2019
1 parent 0461b16 commit 2532777
Show file tree
Hide file tree
Showing 17 changed files with 3,983 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

IMPROVEMENTS:
* **Target Nomad 0.9.1**: updated the nomad client to support Nomad API verison 0.9.1 ([#61](https://github.com/terraform-providers/terraform-provider-nomad/issues/61))
* Added `allocation_ids` as an attribute to Nomad Job resources. ([#63](https://github.com/terraform-providers/terraform-provider-nomad/issues/63))

INTERNAL:

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ require (
github.com/hashicorp/vault v0.10.4
github.com/hashicorp/yamux v0.0.0-20180917205041-7221087c3d28 // indirect
github.com/mitchellh/mapstructure v1.1.2
github.com/stretchr/testify v1.3.0
google.golang.org/grpc v1.19.0 // indirect
)
20 changes: 20 additions & 0 deletions nomad/resource_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ func resourceJob() *schema.Resource {
},
},

"allocation_ids": {
Description: "The IDs for allocations associated with this job.",
Computed: true,
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"task_groups": {
Computed: true,
Type: schema.TypeList,
Expand Down Expand Up @@ -208,11 +217,21 @@ func resourceJobRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("error checking for job: %#v", err)
}

allocStubs, _, err := client.Jobs().Allocations(id, false, nil)
if err != nil {
log.Printf("[WARN] Error listing allocations for Job %q, will return empty list", id)
}
allocIds := make([]string, 0, len(allocStubs))
for _, a := range allocStubs {
allocIds = append(allocIds, a.ID)
}

d.Set("name", job.ID)
d.Set("type", job.Type)
d.Set("region", job.Region)
d.Set("datacenters", job.Datacenters)
d.Set("task_groups", jobTaskGroupsRaw(job.TaskGroups))
d.Set("allocation_ids", allocIds)
if job.JobModifyIndex != nil {
d.Set("modify_index", strconv.FormatUint(*job.JobModifyIndex, 10))
} else {
Expand Down Expand Up @@ -289,6 +308,7 @@ func resourceJobCustomizeDiff(d *schema.ResourceDiff, meta interface{}) error {
// _somehow_, but we won't know how much it will increment until
// after we complete registration.
d.SetNewComputed("modify_index")
d.SetNewComputed("allocation_ids")

d.SetNew("task_groups", jobTaskGroupsRaw(job.TaskGroups))

Expand Down
90 changes: 57 additions & 33 deletions nomad/resource_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"reflect"
"regexp"
"strconv"
"strings"
"testing"
"time"
Expand All @@ -26,7 +28,7 @@ func TestResourceJob_basic(t *testing.T) {
Steps: []r.TestStep{
{
Config: testResourceJob_initialConfig,
Check: testResourceJob_initialCheck,
Check: testResourceJob_initialCheck(t),
},
},

Expand Down Expand Up @@ -71,7 +73,7 @@ func TestResourceJob_json(t *testing.T) {
Steps: []r.TestStep{
{
Config: testResourceJob_jsonConfig,
Check: testResourceJob_initialCheck,
Check: testResourceJob_initialCheck(t),
},
},

Expand All @@ -86,7 +88,7 @@ func TestResourceJob_refresh(t *testing.T) {
Steps: []r.TestStep{
{
Config: testResourceJob_initialConfig,
Check: testResourceJob_initialCheck,
Check: testResourceJob_initialCheck(t),
},

// This should successfully cause the job to be recreated,
Expand All @@ -107,7 +109,7 @@ func TestResourceJob_disableDestroyDeregister(t *testing.T) {
// create the resource
{
Config: testResourceJob_noDestroy,
Check: testResourceJob_initialCheck,
Check: testResourceJob_initialCheck(t),
},
// "Destroy" with 'deregister_on_destroy = false', check that it wasn't destroyed
{
Expand Down Expand Up @@ -140,7 +142,7 @@ func TestResourceJob_rename(t *testing.T) {
Steps: []r.TestStep{
{
Config: testResourceJob_initialConfig,
Check: testResourceJob_initialCheck,
Check: testResourceJob_initialCheck(t),
},
{
Config: testResourceJob_renameConfig,
Expand All @@ -162,7 +164,7 @@ func TestResourceJob_policyOverride(t *testing.T) {
Steps: []r.TestStep{
{
Config: testResourceJob_policyOverrideConfig(),
Check: testResourceJob_initialCheck,
Check: testResourceJob_initialCheck(t),
},
},
})
Expand Down Expand Up @@ -228,7 +230,7 @@ resource "nomad_job" "parameterized" {
cpu = 100
memory = 10
}
logs {
max_files = 3
max_file_size = 10
Expand All @@ -239,6 +241,7 @@ resource "nomad_job" "parameterized" {
EOT
}
`

var testResourceJob_initialConfig = `
resource "nomad_job" "test" {
jobspec = <<EOT
Expand All @@ -254,12 +257,12 @@ resource "nomad_job" "test" {
command = "/bin/sleep"
args = ["1"]
}
resources {
cpu = 100
memory = 10
}
logs {
max_files = 3
max_file_size = 10
Expand Down Expand Up @@ -354,12 +357,12 @@ resource "nomad_job" "test" {
command = "/bin/sleep"
args = ["30"]
}
resources {
cpu = 100
memory = 10
}
logs {
max_files = 3
max_file_size = 10
Expand All @@ -371,31 +374,52 @@ resource "nomad_job" "test" {
}
`

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

instanceState := resourceState.Primary
if instanceState == nil {
return errors.New("resource has no primary instance")
}
resourceState := s.Modules[0].Resources["nomad_job.test"]
if resourceState == nil {
return errors.New("resource not found in state")
}

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

providerConfig := testProvider.Meta().(ProviderConfig)
client := providerConfig.client
job, _, err := client.Jobs().Info(jobID, nil)
if err != nil {
return fmt.Errorf("error reading back job: %s", err)
}
jobID := instanceState.ID

if got, want := *job.ID, jobID; got != want {
return fmt.Errorf("jobID is %q; want %q", got, want)
}
providerConfig := testProvider.Meta().(ProviderConfig)
client := providerConfig.client
job, _, err := client.Jobs().Info(jobID, nil)
if err != nil {
return fmt.Errorf("error reading back job: %s", err)
}

return nil
if got, want := *job.ID, jobID; got != want {
return fmt.Errorf("jobID is %q; want %q", got, want)
}

wantAllocs, _, err := client.Jobs().Allocations(jobID, false, nil)
if err != nil {
return fmt.Errorf("error reading back job: %s", err)
}
wantAllocIds := make([]string, 0, len(wantAllocs))
for _, a := range wantAllocs {
wantAllocIds = append(wantAllocIds, a.ID)
}
numGotAllocs, _ := strconv.Atoi(instanceState.Attributes["allocation_ids.#"])
gotAllocs := make([]string, 0, numGotAllocs)
for i := 0; i < numGotAllocs; i++ {
id := instanceState.Attributes[fmt.Sprintf("allocation_ids.%d", i)]
gotAllocs = append(gotAllocs, id)
}
if !assert.ElementsMatch(t, gotAllocs, wantAllocIds) {
return fmt.Errorf("job 'allocation_ids' is '%v'; want '%v'", gotAllocs, wantAllocIds)
}

return nil
}
}

func testResourceJob_v086Check(s *terraform.State) error {
Expand Down Expand Up @@ -631,12 +655,12 @@ func TestResourceJob_vault(t *testing.T) {
Steps: []r.TestStep{
{
Config: testResourceJob_invalidVaultConfig,
Check: testResourceJob_initialCheck,
Check: testResourceJob_initialCheck(t),
ExpectError: re,
},
{
Config: testResourceJob_validVaultConfig,
Check: testResourceJob_initialCheck,
Check: testResourceJob_initialCheck(t),
},
},
CheckDestroy: testResourceJob_checkDestroy("test"),
Expand Down
27 changes: 27 additions & 0 deletions vendor/github.com/pmezard/go-difflib/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2532777

Please sign in to comment.