Skip to content

Commit

Permalink
resource/job: remove attribute allocation_ids
Browse files Browse the repository at this point in the history
Storing the list of allocation IDs in a `nomad_job` resource can lead to
ever-changing plans since the list can change outsite of Terraform.

Retrieving this type of information is better suited for a data source.

The Terraform provider deprecation guide recommends attributes to be
first marked as deprecated before full removal in the next major
release.

Given that the next release will be a major release, this commit
introduces a breaking change in behaviour (allocation IDs are not
fetched by default anymore) but provides a fallback with the
`read_allocation_ids` attribute.

Both attributes are marked as deprecated and will be removed in the next
major release.
  • Loading branch information
lgfa29 committed Jul 26, 2023
1 parent cc2b3f9 commit 7d37eee
Showing 1 changed file with 24 additions and 10 deletions.
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

0 comments on commit 7d37eee

Please sign in to comment.