api: allow query options everywhere

This commit is contained in:
Ryan Uber 2015-09-09 13:18:50 -07:00
parent aff13fc84c
commit a807612091
4 changed files with 30 additions and 20 deletions

View file

@ -11,15 +11,25 @@ func (c *Client) Allocations() *Allocations {
}
// List returns a list of all of the allocations.
func (a *Allocations) List() ([]*Allocation, *QueryMeta, error) {
func (a *Allocations) List(q *QueryOptions) ([]*Allocation, *QueryMeta, error) {
var resp []*Allocation
qm, err := a.client.query("/v1/allocations", &resp, nil)
qm, err := a.client.query("/v1/allocations", &resp, q)
if err != nil {
return nil, nil, err
}
return resp, qm, nil
}
// Info is used to retrieve a single allocation.
func (a *Allocations) Info(allocID string, q *QueryOptions) (*Allocation, *QueryMeta, error) {
var resp Allocation
qm, err := a.client.query("/v1/allocation/"+allocID, &resp, q)
if err != nil {
return nil, nil, err
}
return &resp, qm, nil
}
// Allocation is used for serialization of allocations.
type Allocation struct {
ID string

View file

@ -10,7 +10,7 @@ func TestAllocations_List(t *testing.T) {
a := c.Allocations()
// Querying when no allocs exist returns nothing
allocs, qm, err := a.List()
allocs, qm, err := a.List(nil)
if err != nil {
t.Fatalf("err: %s", err)
}
@ -36,7 +36,7 @@ func TestAllocations_List(t *testing.T) {
}
// List the allocations again
allocs, qm, err = a.List()
allocs, qm, err = a.List(nil)
if err != nil {
t.Fatalf("err: %s", err)
}

View file

@ -24,9 +24,9 @@ func (j *Jobs) Register(job *Job, q *WriteOptions) (string, *WriteMeta, error) {
}
// List is used to list all of the existing jobs.
func (j *Jobs) List() ([]*Job, *QueryMeta, error) {
func (j *Jobs) List(q *QueryOptions) ([]*Job, *QueryMeta, error) {
var resp []*Job
qm, err := j.client.query("/v1/jobs", &resp, nil)
qm, err := j.client.query("/v1/jobs", &resp, q)
if err != nil {
return nil, qm, err
}
@ -35,9 +35,9 @@ func (j *Jobs) List() ([]*Job, *QueryMeta, error) {
// Info is used to retrieve information about a particular
// job given its unique ID.
func (j *Jobs) Info(jobID string) (*Job, *QueryMeta, error) {
func (j *Jobs) Info(jobID string, q *QueryOptions) (*Job, *QueryMeta, error) {
var resp Job
qm, err := j.client.query("/v1/job/"+jobID, &resp, nil)
qm, err := j.client.query("/v1/job/"+jobID, &resp, q)
if err != nil {
return nil, nil, err
}
@ -45,9 +45,9 @@ func (j *Jobs) Info(jobID string) (*Job, *QueryMeta, error) {
}
// Allocations is used to return the allocs for a given job ID.
func (j *Jobs) Allocations(jobID string) ([]*Allocation, *QueryMeta, error) {
func (j *Jobs) Allocations(jobID string, q *QueryOptions) ([]*Allocation, *QueryMeta, error) {
var resp []*Allocation
qm, err := j.client.query("/v1/job/"+jobID+"/allocations", &resp, nil)
qm, err := j.client.query("/v1/job/"+jobID+"/allocations", &resp, q)
if err != nil {
return nil, nil, err
}
@ -56,9 +56,9 @@ func (j *Jobs) Allocations(jobID string) ([]*Allocation, *QueryMeta, error) {
// Evaluations is used to query the evaluations associated with
// the given job ID.
func (j *Jobs) Evaluations(jobID string) ([]*Evaluation, *QueryMeta, error) {
func (j *Jobs) Evaluations(jobID string, q *QueryOptions) ([]*Evaluation, *QueryMeta, error) {
var resp []*Evaluation
qm, err := j.client.query("/v1/job/"+jobID+"/evaluations", &resp, nil)
qm, err := j.client.query("/v1/job/"+jobID+"/evaluations", &resp, q)
if err != nil {
return nil, nil, err
}

View file

@ -12,7 +12,7 @@ func TestJobs_Register(t *testing.T) {
jobs := c.Jobs()
// Listing jobs before registering returns nothing
resp, qm, err := jobs.List()
resp, qm, err := jobs.List(nil)
if err != nil {
t.Fatalf("err: %s", err)
}
@ -35,7 +35,7 @@ func TestJobs_Register(t *testing.T) {
assertWriteMeta(t, wm)
// Query the jobs back out again
resp, qm, err = jobs.List()
resp, qm, err = jobs.List(nil)
if err != nil {
t.Fatalf("err: %s", err)
}
@ -55,7 +55,7 @@ func TestJobs_Info(t *testing.T) {
// Trying to retrieve a job by ID before it exists
// returns an error
_, _, err := jobs.Info("job1")
_, _, err := jobs.Info("job1", nil)
if err == nil || !strings.Contains(err.Error(), "not found") {
t.Fatalf("expected not found error, got: %#v", err)
}
@ -69,7 +69,7 @@ func TestJobs_Info(t *testing.T) {
assertWriteMeta(t, wm)
// Query the job again and ensure it exists
result, qm, err := jobs.Info("job1")
result, qm, err := jobs.Info("job1", nil)
if err != nil {
t.Fatalf("err: %s", err)
}
@ -87,7 +87,7 @@ func TestJobs_Allocations(t *testing.T) {
jobs := c.Jobs()
// Looking up by a non-existent job returns nothing
allocs, qm, err := jobs.Allocations("job1")
allocs, qm, err := jobs.Allocations("job1", nil)
if err != nil {
t.Fatalf("err: %s", err)
}
@ -108,7 +108,7 @@ func TestJobs_Evaluations(t *testing.T) {
jobs := c.Jobs()
// Looking up by a non-existent job ID returns nothing
evals, qm, err := jobs.Evaluations("job1")
evals, qm, err := jobs.Evaluations("job1", nil)
if err != nil {
t.Fatalf("err: %s", err)
}
@ -129,7 +129,7 @@ func TestJobs_Evaluations(t *testing.T) {
assertWriteMeta(t, wm)
// Look up the evaluations again.
evals, qm, err = jobs.Evaluations("job1")
evals, qm, err = jobs.Evaluations("job1", nil)
if err != nil {
t.Fatalf("err: %s", err)
}
@ -169,7 +169,7 @@ func TestJobs_Delete(t *testing.T) {
assertWriteMeta(t, wm3)
// Check that the job is really gone
result, qm, err := jobs.List()
result, qm, err := jobs.List(nil)
if err != nil {
t.Fatalf("err: %s", err)
}