From 642764fda4648092ccd4a2dfa8e2200823ff80db Mon Sep 17 00:00:00 2001 From: Thomas Bartelmess Date: Sat, 1 Jul 2017 20:50:36 -0400 Subject: [PATCH] Added a Allocation#for_job API --- lib/nomad/api/allocation.rb | 12 ++++++++++++ spec/integration/api/allocation_spec.rb | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/nomad/api/allocation.rb b/lib/nomad/api/allocation.rb index d821a77..2e5fbbf 100644 --- a/lib/nomad/api/allocation.rb +++ b/lib/nomad/api/allocation.rb @@ -32,6 +32,18 @@ def read(id, **options) json = client.get("/v1/allocation/#{CGI.escape(id)}", options) return Alloc.decode(json) end + + # Read all allocations for a given job + # + # @param [String] job_id The Job ID of the job + # + # @return Array + def for_job(job_id, **options) + json = client.get("/v1/job/#{CGI.escape(job_id)}/allocations", options) + return json.map do |alloc_json| + Alloc.decode(alloc_json) + end + end end class Alloc < Response diff --git a/spec/integration/api/allocation_spec.rb b/spec/integration/api/allocation_spec.rb index 9524e52..4910d04 100644 --- a/spec/integration/api/allocation_spec.rb +++ b/spec/integration/api/allocation_spec.rb @@ -69,5 +69,15 @@ module Nomad expect(result.task_states).to be_a(Hash) end end + + describe "#for_job" do + it "reads allocations for a specific job" do + result = subject.for_job "job" + expect(result).to be + expect(result).to be_a(Array) + expect(result.count).to eq(3) + expect(result.first).to be_a(Alloc) + end + end end end