Commit 1e741afb authored by Adam Reese's avatar Adam Reese

feat(get): add get verb

parent 57008336
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)
}
......@@ -181,6 +181,7 @@ func commands() []cli.Command {
Name: "search",
},
listCmd(),
getCmd(),
}
}
......
package main
import (
"os"
"github.com/codegangsta/cli"
"github.com/deis/helm-dm/dm"
"github.com/deis/helm-dm/format"
......@@ -12,17 +10,13 @@ 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)
}
},
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
......
......@@ -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
}
......@@ -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)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment