diff --git a/cmd/helm/get.go b/cmd/helm/get.go new file mode 100644 index 000000000..e5dcfe921 --- /dev/null +++ b/cmd/helm/get.go @@ -0,0 +1,33 @@ +package main + +import ( + "errors" + + "github.com/codegangsta/cli" + "github.com/deis/helm-dm/dm" + "github.com/deis/helm-dm/format" +) + +func getCmd() cli.Command { + return cli.Command{ + Name: "get", + Usage: "Retrieves the supplied deployment", + Action: func(c *cli.Context) { run(c, get) }, + } +} + +func get(c *cli.Context) error { + args := c.Args() + if len(args) < 1 { + return errors.New("First argument, deployment name, is required. Try 'helm get --help'") + } + name := args[0] + host := c.GlobalString("host") + client := dm.NewClient(host).SetDebug(c.GlobalBool("debug")) + + deployment, err := client.GetDeployment(name) + if err != nil { + return err + } + return format.YAML(deployment) +} diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 498693b55..aeb04e51f 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -181,6 +181,7 @@ func commands() []cli.Command { Name: "search", }, listCmd(), + getCmd(), } } diff --git a/cmd/helm/list.go b/cmd/helm/list.go index a9cd6d996..24f200a5b 100644 --- a/cmd/helm/list.go +++ b/cmd/helm/list.go @@ -1,8 +1,6 @@ package main import ( - "os" - "github.com/codegangsta/cli" "github.com/deis/helm-dm/dm" "github.com/deis/helm-dm/format" @@ -10,19 +8,15 @@ import ( func listCmd() cli.Command { return cli.Command{ - Name: "list", - Usage: "Lists the deployments in the cluster", - Action: func(c *cli.Context) { - if err := list(c.GlobalString("host")); err != nil { - format.Err("%s (Is the cluster running?)", err) - os.Exit(1) - } - }, + Name: "list", + Usage: "Lists the deployments in the cluster", + Action: func(c *cli.Context) { run(c, list) }, } } -func list(host string) error { - client := dm.NewClient(host).SetDebug(isDebugging) +func list(c *cli.Context) error { + host := c.GlobalString("host") + client := dm.NewClient(host).SetDebug(c.GlobalBool("debug")) list, err := client.ListDeployments() if err != nil { return err diff --git a/dm/client.go b/dm/client.go index 3749451c0..7fa57d303 100644 --- a/dm/client.go +++ b/dm/client.go @@ -11,6 +11,8 @@ import ( "path/filepath" "strings" "time" + + "github.com/kubernetes/deployment-manager/common" ) // The default HTTP timeout @@ -125,10 +127,34 @@ func (c *Client) callHTTP(path, method, action string, reader io.ReadCloser) (st return string(body), nil } +// DefaultServerURL converts a host, host:port, or URL string to the default base server API path +// to use with a Client +func DefaultServerURL(host string) (*url.URL, error) { + if host == "" { + return nil, fmt.Errorf("host must be a URL or a host:port pair") + } + base := host + hostURL, err := url.Parse(base) + if err != nil { + return nil, err + } + if hostURL.Scheme == "" { + hostURL, err = url.Parse(DefaultHTTPProtocol + "://" + base) + if err != nil { + return nil, err + } + } + if len(hostURL.Path) > 0 && !strings.HasSuffix(hostURL.Path, "/") { + hostURL.Path = hostURL.Path + "/" + } + + return hostURL, nil +} + // ListDeployments lists the deployments in DM. func (c *Client) ListDeployments() ([]string, error) { var l []string - if err := c.CallService("deployments", "GET", "foo", &l, nil); err != nil { + if err := c.CallService("deployments", "GET", "list deployments", &l, nil); err != nil { return nil, err } @@ -181,26 +207,11 @@ func (c *Client) DeployChart(filename, deployname string) error { return nil } -// DefaultServerURL converts a host, host:port, or URL string to the default base server API path -// to use with a Client -func DefaultServerURL(host string) (*url.URL, error) { - if host == "" { - return nil, fmt.Errorf("host must be a URL or a host:port pair") - } - base := host - hostURL, err := url.Parse(base) - if err != nil { +// GetDeployment retrieves the supplied deployment +func (c *Client) GetDeployment(name string) (*common.Deployment, error) { + var deployment *common.Deployment + if err := c.CallService(filepath.Join("deployments", name), "GET", "get deployment", &deployment, nil); err != nil { return nil, err } - if hostURL.Scheme == "" { - hostURL, err = url.Parse(DefaultHTTPProtocol + "://" + base) - if err != nil { - return nil, err - } - } - if len(hostURL.Path) > 0 && !strings.HasSuffix(hostURL.Path, "/") { - hostURL.Path = hostURL.Path + "/" - } - - return hostURL, nil + return deployment, nil } diff --git a/dm/client_test.go b/dm/client_test.go index d4660e4ab..61e2e0036 100644 --- a/dm/client_test.go +++ b/dm/client_test.go @@ -4,6 +4,8 @@ import ( "net/http" "net/http/httptest" "testing" + + "github.com/kubernetes/deployment-manager/common" ) func TestDefaultServerURL(t *testing.T) { @@ -96,3 +98,23 @@ func TestListDeployments(t *testing.T) { t.Fatal("expected a single deployment") } } + +func TestGetDeployment(t *testing.T) { + fc := &fakeClient{ + response: []byte(`{"name":"guestbook.yaml","id":0,"createdAt":"2016-02-08T12:17:49.251658308-08:00","deployedAt":"2016-02-08T12:17:49.251658589-08:00","modifiedAt":"2016-02-08T12:17:51.177518098-08:00","deletedAt":"0001-01-01T00:00:00Z","state":{"status":"Deployed"},"latestManifest":"manifest-1454962670728402229"}`), + } + defer fc.teardown() + + d, err := fc.setup().GetDeployment("guestbook.yaml") + if err != nil { + t.Fatal(err) + } + + if d.Name != "guestbook.yaml" { + t.Fatalf("expected deployment name 'guestbook.yaml', got '%s'", d.Name) + } + + if d.State.Status != common.DeployedStatus { + t.Fatalf("expected deployment status 'Deployed', got '%s'", d.State.Status) + } +}