Skip to content

Commit

Permalink
Merge pull request #357 from hashicorp/f-job-remove-alloc-ids
Browse files Browse the repository at this point in the history
resource/job: remove attribute allocation_ids
  • Loading branch information
lgfa29 authored Jul 26, 2023
2 parents cc2b3f9 + 3663266 commit 355efe5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
BACKWARDS INCOMPATIBILITIES:
* provider: Terraform Plugin SDK upgraded to v2.10.1. **Terraform versions prior to 0.12 are no longer supported.** ([#339](https://github.com/hashicorp/terraform-provider-nomad/issues/339))
* resource/nomad_job: Switch to HCL2 parsing by default. Jobs that require HCL1 parsing must set `hcl1 = true`. ([#343](https://github.com/hashicorp/terraform-provider-nomad/pull/343))
* resource/nomad_job: Deprecate field `allocation_ids` and do not retrieve the job's allocations by default. Set `read_allocation_ids` to `true` if you must retain existing behavior, but consider using the `nomad_allocations` data source instead. ([#357](https://github.com/hashicorp/terraform-provider-nomad/pull/357))

DEPRECATIONS:
* resource/nomad_volume: The `nomad_volume` resource has been deprecated. Use the new `nomad_csi_volume_registration` resource instead. ([#344](https://github.com/hashicorp/terraform-provider-nomad/pull/344))
Expand Down
34 changes: 24 additions & 10 deletions nomad/resource_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,16 @@ func resourceJob() *schema.Resource {
},
},

"read_allocation_ids": {
Description: "",
Deprecated: "Retrieving allocation IDs from the job resource is deprecated and will be removed in a future release. Use the nomad_allocations data source instead.",
Optional: true,
Default: false,
Type: schema.TypeBool,
},

"allocation_ids": {
Deprecated: "Retrieving allocation IDs from the job resource is deprecated and will be removed in a future release. Use the nomad_allocations data source instead.",
Description: "The IDs for allocations associated with this job.",
Computed: true,
Type: schema.TypeList,
Expand Down Expand Up @@ -580,28 +589,33 @@ func resourceJobRead(d *schema.ResourceData, meta interface{}) error {
}
log.Printf("[DEBUG] found job %q in namespace %q", *job.Name, *job.Namespace)

allocStubs, _, err := client.Jobs().Allocations(id, false, opts)
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)
d.Set("namespace", job.Namespace)
if job.JobModifyIndex != nil {
d.Set("modify_index", strconv.FormatUint(*job.JobModifyIndex, 10))
} else {
d.Set("modify_index", "0")
}

if d.Get("read_allocation_ids").(bool) {
allocStubs, _, err := client.Jobs().Allocations(id, false, opts)
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("allocation_ids", allocIDs)
} else {
d.Set("allocation_ids", nil)
}

return nil
}

Expand Down
20 changes: 0 additions & 20 deletions nomad/resource_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"io/ioutil"
"reflect"
"regexp"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -187,7 +186,6 @@ func TestResourceJob_batchNoDetach(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "deployment_id", ""),
resource.TestCheckResourceAttr(resourceName, "deployment_status", ""),
resource.TestCheckResourceAttrSet(resourceName, "allocation_ids.#"),
),
},
},
Expand Down Expand Up @@ -1187,24 +1185,6 @@ func testResourceJob_initialCheckNS(t *testing.T, expectedNamespace string) r.Te
return fmt.Errorf("job namespace 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
}
}
Expand Down

0 comments on commit 355efe5

Please sign in to comment.