Merge pull request #1090 from hashicorp/f-inspect-submittable

Make inspect output submittable via HTTP API
This commit is contained in:
Alex Dadgar
2016-04-13 17:18:54 -07:00
4 changed files with 12 additions and 79 deletions

View File

@@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
@@ -35,9 +34,6 @@ type QueryOptions struct {
// If set, used as prefix for resource list searches
Prefix string
// If set, pretty print the response json.
Pretty bool
}
// WriteOptions are used to parameterize a write
@@ -160,9 +156,6 @@ func (r *request) setQueryOptions(q *QueryOptions) {
if q.Prefix != "" {
r.params.Set("prefix", q.Prefix)
}
if q.Pretty {
r.params.Set("pretty", "true")
}
}
// durToMsec converts a duration to a millisecond specified string
@@ -272,29 +265,6 @@ func (c *Client) query(endpoint string, out interface{}, q *QueryOptions) (*Quer
return qm, nil
}
// rawQuery is used to do a GET request against an endpoint and return the raw
// string result.
func (c *Client) rawQuery(endpoint string, q *QueryOptions) (string, *QueryMeta, error) {
r := c.newRequest("GET", endpoint)
r.setQueryOptions(q)
rtt, resp, err := requireOK(c.doRequest(r))
if err != nil {
return "", nil, err
}
defer resp.Body.Close()
qm := &QueryMeta{}
parseQueryMeta(resp, qm)
qm.RequestTime = rtt
raw, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", nil, err
}
return string(raw), qm, nil
}
// write is used to do a PUT request against an endpoint
// and serialize/deserialized using the standard Nomad conventions.
func (c *Client) write(endpoint string, in, out interface{}, q *WriteOptions) (*WriteMeta, error) {

View File

@@ -63,20 +63,6 @@ func (j *Jobs) Info(jobID string, q *QueryOptions) (*Job, *QueryMeta, error) {
return &resp, qm, nil
}
// RawJob is used to retrieve information about a particular
// job given its unique ID and return the raw json.
func (j *Jobs) RawJob(jobID string, q *QueryOptions) (string, *QueryMeta, error) {
if q == nil {
q = &QueryOptions{}
}
q.Pretty = true
raw, qm, err := j.client.rawQuery("/v1/job/"+jobID, q)
if err != nil {
return "", nil, err
}
return raw, qm, nil
}
// Allocations is used to return the allocs for a given job ID.
func (j *Jobs) Allocations(jobID string, q *QueryOptions) ([]*AllocationListStub, *QueryMeta, error) {
var resp []*AllocationListStub

View File

@@ -83,39 +83,6 @@ func TestJobs_Info(t *testing.T) {
}
}
func TestJobs_RawJob(t *testing.T) {
c, s := makeClient(t, nil, nil)
defer s.Stop()
jobs := c.Jobs()
// Trying to retrieve a job by ID before it exists
// returns an error
_, _, err := jobs.RawJob("job1", nil)
if err == nil || !strings.Contains(err.Error(), "not found") {
t.Fatalf("expected not found error, got: %#v", err)
}
// Register the job
job := testJob()
_, wm, err := jobs.Register(job, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
assertWriteMeta(t, wm)
// Query the job again and ensure it exists
result, qm, err := jobs.RawJob("job1", nil)
if err != nil {
t.Fatalf("err: %s", err)
}
assertQueryMeta(t, qm)
// Check that the result is what we expect
if result == "" || !strings.Contains(result, job.ID) {
t.Fatalf("expect: %#v, got: %#v", job, result)
}
}
func TestJobs_PrefixList(t *testing.T) {
c, s := makeClient(t, nil, nil)
defer s.Stop()

View File

@@ -1,8 +1,11 @@
package command
import (
"encoding/json"
"fmt"
"strings"
"github.com/hashicorp/nomad/api"
)
type InspectCommand struct {
@@ -74,13 +77,20 @@ func (c *InspectCommand) Run(args []string) int {
}
// Prefix lookup matched a single job
job, _, err := client.Jobs().RawJob(jobs[0].ID, nil)
job, _, err := client.Jobs().Info(jobs[0].ID, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error inspecting job: %s", err))
return 1
}
// Print the contents of the job
c.Ui.Output(job)
req := api.RegisterJobRequest{job}
buf, err := json.MarshalIndent(req, "", " ")
if err != nil {
c.Ui.Error(fmt.Sprintf("Error converting job: %s", err))
return 1
}
c.Ui.Output(string(buf))
return 0
}