From 0f31b16fae10209fd68215841a1d62d1de766df3 Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Tue, 8 Sep 2015 16:24:26 -0700 Subject: [PATCH] api: starting on jobs --- api/job.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ api/job_test.go | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 api/job.go create mode 100644 api/job_test.go diff --git a/api/job.go b/api/job.go new file mode 100644 index 000000000..beec9910b --- /dev/null +++ b/api/job.go @@ -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 +} diff --git a/api/job_test.go b/api/job_test.go new file mode 100644 index 000000000..bf9c38d40 --- /dev/null +++ b/api/job_test.go @@ -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]) + } +}