api: starting on jobs
This commit is contained in:
parent
4ff1753466
commit
0f31b16fae
|
@ -0,0 +1,57 @@
|
|||
package api
|
||||
|
||||
// Jobs is used to access the job-specific endpoints.
|
||||
type Jobs struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
// Jobs returns a handle on the jobs endpoints.
|
||||
func (c *Client) Jobs() *Jobs {
|
||||
return &Jobs{client: c}
|
||||
}
|
||||
|
||||
// Register is used to register a new job. It returns the ID
|
||||
// of the evaluation, along with any errors encountered.
|
||||
func (j *Jobs) Register(job *Job, q *WriteOptions) (string, *WriteMeta, error) {
|
||||
var resp registerJobResponse
|
||||
|
||||
req := ®isterJobRequest{job}
|
||||
wm, err := j.client.write("/v1/jobs", req, &resp, q)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
return resp.EvalID, wm, nil
|
||||
}
|
||||
|
||||
// List is used to list all of the existing jobs.
|
||||
func (j *Jobs) List() ([]*Job, error) {
|
||||
var resp []*Job
|
||||
_, err := j.client.query("/v1/jobs", &resp, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// Job is used to serialize a job.
|
||||
type Job struct {
|
||||
ID string
|
||||
Name string
|
||||
Type string
|
||||
Priority int
|
||||
AllAtOnce bool
|
||||
Datacenters []string
|
||||
Meta map[string]string
|
||||
Status string
|
||||
StatusDescription string
|
||||
}
|
||||
|
||||
// registerJobRequest is used to serialize a job registration
|
||||
type registerJobRequest struct {
|
||||
Job *Job
|
||||
}
|
||||
|
||||
// registerJobResponse is used to deserialize a job response
|
||||
type registerJobResponse struct {
|
||||
EvalID string
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestJobs_Register(t *testing.T) {
|
||||
c, s := makeClient(t, nil, nil)
|
||||
defer s.Stop()
|
||||
jobs := c.Jobs()
|
||||
|
||||
// Listing jobs before registering returns nothing
|
||||
resp, err := jobs.List()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if n := len(resp); n != 0 {
|
||||
t.Fatalf("expected 0 jobs, got: %d", n)
|
||||
}
|
||||
|
||||
// Create a job and attempt to register it
|
||||
job := &Job{
|
||||
ID: "job1",
|
||||
Name: "Job #1",
|
||||
Type: "service",
|
||||
Priority: 1,
|
||||
}
|
||||
eval, _, err := jobs.Register(job, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if eval == "" {
|
||||
t.Fatalf("missing eval id")
|
||||
}
|
||||
|
||||
// Query the jobs back out again
|
||||
resp, err = jobs.List()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
// Check that we got the expected response
|
||||
expect := []*Job{job}
|
||||
if !reflect.DeepEqual(resp, expect) {
|
||||
t.Fatalf("bad: %#v", resp[0])
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue