From d2597982ad798a5d8b163dc513de6071f33f5a49 Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Sun, 13 Sep 2015 19:55:47 -0700 Subject: [PATCH] api: use stub structs --- api/allocations.go | 45 +++++++++++++++++++++++++++++++++++++++++++-- api/evaluations.go | 4 ++-- api/jobs.go | 23 +++++++++++++++++++---- api/jobs_test.go | 5 ++--- api/nodes.go | 26 ++++++++++++++++++++++---- api/nodes_test.go | 2 +- 6 files changed, 89 insertions(+), 16 deletions(-) diff --git a/api/allocations.go b/api/allocations.go index f247fed2a..32fbf8714 100644 --- a/api/allocations.go +++ b/api/allocations.go @@ -1,5 +1,9 @@ package api +import ( + "time" +) + // Allocations is used to query the alloc-related endpoints. type Allocations struct { client *Client @@ -11,8 +15,8 @@ func (c *Client) Allocations() *Allocations { } // List returns a list of all of the allocations. -func (a *Allocations) List(q *QueryOptions) ([]*Allocation, *QueryMeta, error) { - var resp []*Allocation +func (a *Allocations) List(q *QueryOptions) ([]*AllocationListStub, *QueryMeta, error) { + var resp []*AllocationListStub qm, err := a.client.query("/v1/allocations", &resp, q) if err != nil { return nil, nil, err @@ -32,6 +36,41 @@ func (a *Allocations) Info(allocID string, q *QueryOptions) (*Allocation, *Query // Allocation is used for serialization of allocations. type Allocation struct { + ID string + EvalID string + Name string + NodeID string + JobID string + Job *Job + TaskGroup string + Resources *Resources + TaskResources map[string]*Resources + Metrics *AllocationMetric + DesiredStatus string + DesiredDescription string + ClientStatus string + ClientDescription string + CreateIndex uint64 + ModifyIndex uint64 +} + +// AllocationMetric is used to deserialize allocation metrics. +type AllocationMetric struct { + NodesEvaluated int + NodesFiltered int + ClassFiltered map[string]int + ConstraintFiltered map[string]int + NodesExhausted int + ClassExhausted map[string]int + DimensionExhaused map[string]int + Scores map[string]float64 + AllocationTime time.Duration + CoalescedFailures int +} + +// AllocationListStub is used to return a subset of an allocation +// during list operations. +type AllocationListStub struct { ID string EvalID string Name string @@ -42,4 +81,6 @@ type Allocation struct { DesiredDescription string ClientStatus string ClientDescription string + CreateIndex uint64 + ModifyIndex uint64 } diff --git a/api/evaluations.go b/api/evaluations.go index 2956664b4..52a0db970 100644 --- a/api/evaluations.go +++ b/api/evaluations.go @@ -36,8 +36,8 @@ func (e *Evaluations) Info(evalID string, q *QueryOptions) (*Evaluation, *QueryM // Allocations is used to retrieve a set of allocations given // an evaluation ID. -func (e *Evaluations) Allocations(evalID string, q *QueryOptions) ([]*Allocation, *QueryMeta, error) { - var resp []*Allocation +func (e *Evaluations) Allocations(evalID string, q *QueryOptions) ([]*AllocationListStub, *QueryMeta, error) { + var resp []*AllocationListStub qm, err := e.client.query("/v1/evaluation/"+evalID+"/allocations", &resp, q) if err != nil { return nil, nil, err diff --git a/api/jobs.go b/api/jobs.go index 6898b6278..ffc279788 100644 --- a/api/jobs.go +++ b/api/jobs.go @@ -32,8 +32,8 @@ 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(q *QueryOptions) ([]*Job, *QueryMeta, error) { - var resp []*Job +func (j *Jobs) List(q *QueryOptions) ([]*JobListStub, *QueryMeta, error) { + var resp []*JobListStub qm, err := j.client.query("/v1/jobs", &resp, q) if err != nil { return nil, qm, err @@ -53,8 +53,8 @@ func (j *Jobs) Info(jobID string, q *QueryOptions) (*Job, *QueryMeta, error) { } // Allocations is used to return the allocs for a given job ID. -func (j *Jobs) Allocations(jobID string, q *QueryOptions) ([]*Allocation, *QueryMeta, error) { - var resp []*Allocation +func (j *Jobs) Allocations(jobID string, q *QueryOptions) ([]*AllocationListStub, *QueryMeta, error) { + var resp []*AllocationListStub qm, err := j.client.query("/v1/job/"+jobID+"/allocations", &resp, q) if err != nil { return nil, nil, err @@ -105,6 +105,21 @@ type Job struct { Meta map[string]string Status string StatusDescription string + CreateIndex uint64 + ModifyIndex uint64 +} + +// JobListStub is used to return a subset of information about +// jobs during list operations. +type JobListStub struct { + ID string + Name string + Type string + Priority int + Status string + StatusDescription string + CreateIndex uint64 + ModifyIndex uint64 } // NewServiceJob creates and returns a new service-style job diff --git a/api/jobs_test.go b/api/jobs_test.go index 8b9d1366c..8f576c635 100644 --- a/api/jobs_test.go +++ b/api/jobs_test.go @@ -42,8 +42,7 @@ func TestJobs_Register(t *testing.T) { assertQueryMeta(t, qm) // Check that we got the expected response - expect := []*Job{job} - if !reflect.DeepEqual(resp, expect) { + if len(resp) != 1 || resp[0].ID != job.ID { t.Fatalf("bad: %#v", resp[0]) } } @@ -76,7 +75,7 @@ func TestJobs_Info(t *testing.T) { assertQueryMeta(t, qm) // Check that the result is what we expect - if !reflect.DeepEqual(result, job) { + if result == nil || result.ID != job.ID { t.Fatalf("expect: %#v, got: %#v", job, result) } } diff --git a/api/nodes.go b/api/nodes.go index 2e40baafc..77c98e662 100644 --- a/api/nodes.go +++ b/api/nodes.go @@ -15,8 +15,8 @@ func (c *Client) Nodes() *Nodes { } // List is used to list out all of the nodes -func (n *Nodes) List(q *QueryOptions) ([]*Node, *QueryMeta, error) { - var resp []*Node +func (n *Nodes) List(q *QueryOptions) ([]*NodeListStub, *QueryMeta, error) { + var resp []*NodeListStub qm, err := n.client.query("/v1/nodes", &resp, q) if err != nil { return nil, nil, err @@ -45,8 +45,8 @@ func (n *Nodes) ToggleDrain(nodeID string, drain bool, q *WriteOptions) (*WriteM } // Allocations is used to return the allocations associated with a node. -func (n *Nodes) Allocations(nodeID string, q *QueryOptions) ([]*Allocation, *QueryMeta, error) { - var resp []*Allocation +func (n *Nodes) Allocations(nodeID string, q *QueryOptions) ([]*AllocationListStub, *QueryMeta, error) { + var resp []*AllocationListStub qm, err := n.client.query("/v1/node/"+nodeID+"/allocations", &resp, q) if err != nil { return nil, nil, err @@ -66,6 +66,24 @@ func (n *Nodes) ForceEvaluate(nodeID string, q *WriteOptions) (string, *WriteMet // Node is used to deserialize a node entry. type Node struct { + ID string + Datacenter string + Name string + Attributes map[string]string + Resources *Resources + Reserved *Resources + Links map[string]string + NodeClass string + Drain bool + Status string + StatusDescription string + CreateIndex uint64 + ModifyIndex uint64 +} + +// NodeListStub is a subset of information returned during +// node list operations. +type NodeListStub struct { ID string Datacenter string Name string diff --git a/api/nodes_test.go b/api/nodes_test.go index 9a850c66f..5d71b8304 100644 --- a/api/nodes_test.go +++ b/api/nodes_test.go @@ -16,7 +16,7 @@ func TestNodes_List(t *testing.T) { nodes := c.Nodes() var qm *QueryMeta - var out []*Node + var out []*NodeListStub var err error testutil.WaitForResult(func() (bool, error) {