Merge pull request #4 from adamreese/list-deployments

List deployments
pull/291/head
Adam Reese 9 years ago
commit ca43497cd3

@ -17,6 +17,16 @@ func main() {
app.Usage = `Deploy and manage packages.`
app.Commands = commands()
// TODO: make better
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "host,u",
Usage: "The URL of the DM server.",
EnvVar: "HELM_HOST",
Value: "https://localhost:8181/FIXME_NOT_RIGHT",
},
}
app.Run(os.Args)
}
@ -89,7 +99,7 @@ func commands() []cli.Command {
d.Input = os.Stdin
}
if err := deploy(d, c.String("host"), c.Bool("dry-run")); err != nil {
if err := deploy(d, c.GlobalString("host"), c.Bool("dry-run")); err != nil {
format.Error("%s (Try running 'helm doctor')", err)
os.Exit(1)
}
@ -119,16 +129,11 @@ func commands() []cli.Command {
Usage: "The default repository",
Value: "kubernetes/application-dm-templates",
},
cli.StringFlag{
Name: "host,u",
Usage: "The URL of the DM server.",
EnvVar: "HELM_HOST",
Value: "https://localhost:8181/FIXME_NOT_RIGHT",
},
},
},
{
Name: "search",
},
listCmd(),
}
}

@ -0,0 +1,28 @@
package main
import (
"os"
"github.com/codegangsta/cli"
"github.com/deis/helm-dm/dm"
"github.com/deis/helm-dm/format"
)
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.Error("%s (Is the cluster running?)", err)
os.Exit(1)
}
},
}
}
func list(host string) error {
client := dm.NewClient(host)
client.Protocol = "http"
return client.ListDeployments()
}

@ -162,8 +162,8 @@ func getGitRegistry(reg string) (registry.Registry, error) {
}
if s[0] == "helm" {
return registry.NewGithubPackageRegistry(s[0], s[1]), nil
return registry.NewGithubPackageRegistry(s[0], s[1], nil), nil
} else {
return registry.NewGithubRegistry(s[0], s[1], path), nil
return registry.NewGithubRegistry(s[0], s[1], path, nil), nil
}
}

@ -22,6 +22,8 @@ type Client struct {
Host string
// The protocol. Currently only http and https are supported.
Protocol string
// Transport
Transport http.RoundTripper
}
// NewClient creates a new DM client. Host name is required.
@ -30,6 +32,7 @@ func NewClient(host string) *Client {
HTTPTimeout: DefaultHTTPTimeout,
Protocol: "https",
Host: host,
Transport: NewDebugTransport(nil),
}
}
@ -70,7 +73,8 @@ func (c *Client) callHttp(path, method, action string, reader io.ReadCloser) (st
request.Header.Add("Content-Type", "application/json")
client := http.Client{
Timeout: time.Duration(time.Duration(DefaultHTTPTimeout) * time.Second),
Timeout: time.Duration(time.Duration(DefaultHTTPTimeout) * time.Second),
Transport: c.Transport,
}
response, err := client.Do(request)
@ -92,3 +96,13 @@ func (c *Client) callHttp(path, method, action string, reader io.ReadCloser) (st
return string(body), nil
}
func (c *Client) ListDeployments() error {
var d interface{}
if err := c.CallService("deployments", "GET", "foo", &d, nil); err != nil {
return err
}
fmt.Printf("%#v\n", d)
return nil
}

@ -0,0 +1,65 @@
package dm
import (
"fmt"
"io"
"net/http"
"net/http/httputil"
"os"
)
type debugTransport struct {
// Writer is the logging destination
Writer io.Writer
http.RoundTripper
}
func NewDebugTransport(rt http.RoundTripper) http.RoundTripper {
return debugTransport{
RoundTripper: rt,
Writer: os.Stderr,
}
}
func (tr debugTransport) CancelRequest(req *http.Request) {
type canceler interface {
CancelRequest(*http.Request)
}
if cr, ok := tr.transport().(canceler); ok {
cr.CancelRequest(req)
}
}
func (tr debugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
tr.logRequest(req)
resp, err := tr.transport().RoundTrip(req)
if err != nil {
return nil, err
}
tr.logResponse(resp)
return resp, err
}
func (tr debugTransport) transport() http.RoundTripper {
if tr.RoundTripper != nil {
return tr.RoundTripper
}
return http.DefaultTransport
}
func (tr debugTransport) logRequest(req *http.Request) {
dump, err := httputil.DumpRequestOut(req, true)
if err != nil {
fmt.Fprintf(tr.Writer, "%s: %s\n", "could not dump request", err)
}
fmt.Fprint(tr.Writer, string(dump))
}
func (tr debugTransport) logResponse(resp *http.Response) {
dump, err := httputil.DumpResponse(resp, true)
if err != nil {
fmt.Fprintf(tr.Writer, "%s: %s\n", "could not dump response", err)
}
fmt.Fprint(tr.Writer, string(dump))
}

@ -0,0 +1,49 @@
package dm
import (
"bytes"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestDebugTransport(t *testing.T) {
handler := func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"status":"awesome"}`))
}
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
var output bytes.Buffer
client := &http.Client{
Transport: debugTransport{
Writer: &output,
},
}
_, err := client.Get(server.URL)
if err != nil {
t.Fatal(err.Error())
}
expected := []string{
"GET / HTTP/1.1",
"Accept-Encoding: gzip",
"HTTP/1.1 200 OK",
"Content-Length: 20",
"Content-Type: application/json",
`{"status":"awesome"}`,
}
actual := output.String()
for _, match := range expected {
if !strings.Contains(actual, match) {
t.Errorf("Expected %s to contain %s", actual, match)
}
}
}
Loading…
Cancel
Save