diff --git a/cmd/manager/chartrepos.go b/cmd/manager/chartrepos.go index 56112d65d..6b7e52c47 100644 --- a/cmd/manager/chartrepos.go +++ b/cmd/manager/chartrepos.go @@ -2,50 +2,140 @@ package main import ( "github.com/kubernetes/helm/cmd/manager/router" + "github.com/kubernetes/helm/pkg/httputil" + "github.com/kubernetes/helm/pkg/repo" "github.com/kubernetes/helm/pkg/util" + "net/http" + "net/url" + "regexp" ) func registerChartRepoRoutes(c *router.Context, h *router.Handler) { - h.Add("GET /chart_repositories", listChartRepositoriesHandlerFunc) - h.Add("POST /chart_repositories", addChartRepoHandlerFunc) - h.Add("DELETE /chart_repositories", removeChartRepoHandlerFunc) + h.Add("GET /repositories", listChartReposHandlerFunc) + h.Add("GET /repositories/*", getChartRepoHandlerFunc) + h.Add("GET /repositories/*/charts", listRepoChartsHandlerFunc) + h.Add("GET /repositories/*/charts/*", getRepoChartHandlerFunc) + h.Add("POST /repositories", addChartRepoHandlerFunc) + h.Add("DELETE /repositories", removeChartRepoHandlerFunc) } -func listChartRepositoriesHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { +func listChartReposHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { handler := "manager: list chart repositories" repos, err := c.Manager.ListChartRepos() if err != nil { return err } + util.LogHandlerExitWithJSON(handler, w, repos, http.StatusOK) return nil } func addChartRepoHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { handler := "manager: add chart repository" - name, err := pos(w, r, 2) + util.LogHandlerEntry(handler, r) + defer r.Body.Close() + cr := &repo.Repo{} + if err := httputil.Decode(w, r, cr); err != nil { + httputil.BadRequest(w, r, err) + return nil + } + + if err := c.Manager.AddChartRepo(cr); err != nil { + httputil.BadRequest(w, r, err) + return nil + } + + util.LogHandlerExitWithText(handler, w, "added", http.StatusOK) + return nil +} + +func removeChartRepoHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { + handler := "manager: remove chart repository" + util.LogHandlerEntry(handler, r) + URL, err := pos(w, r, 2) if err != nil { return err } - err = c.Manager.AddChartRepo(name) + + err = c.Manager.RemoveChartRepo(URL) if err != nil { return err } - util.LogHandlerExitWithJSON(handler, w, "added", http.StatusOK) + + util.LogHandlerExitWithText(handler, w, "removed", http.StatusOK) return nil } -func removeChartRepoHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: remove chart repository" - name, err := pos(w, r, 2) +func getChartRepoHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { + handler := "manager: get repository" + util.LogHandlerEntry(handler, r) + repoURL, err := pos(w, r, 2) if err != nil { return err } - err = c.Manager.RemoveChartRepo(name) + + cr, err := c.Manager.GetChartRepo(repoURL) + if err != nil { + httputil.BadRequest(w, r, err) + return nil + } + + util.LogHandlerExitWithJSON(handler, w, cr, http.StatusOK) + return nil +} + +func listRepoChartsHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { + handler := "manager: list repository charts" + util.LogHandlerEntry(handler, r) + repoURL, err := pos(w, r, 2) if err != nil { return err } - util.LogHandlerExitWithJSON(handler, w, "removed", http.StatusOK) + + values, err := url.ParseQuery(r.URL.RawQuery) + if err != nil { + httputil.BadRequest(w, r, err) + return nil + } + + var regex *regexp.Regexp + regexString := values.Get("regex") + if regexString != "" { + regex, err = regexp.Compile(regexString) + if err != nil { + httputil.BadRequest(w, r, err) + return nil + } + } + + repoCharts, err := c.Manager.ListRepoCharts(repoURL, regex) + if err != nil { + return err + } + + util.LogHandlerExitWithJSON(handler, w, repoCharts, http.StatusOK) + return nil +} + +func getRepoChartHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { + handler := "manager: get repository charts" + util.LogHandlerEntry(handler, r) + repoURL, err := pos(w, r, 2) + if err != nil { + return err + } + + chartName, err := pos(w, r, 4) + if err != nil { + return err + } + + repoChart, err := c.Manager.GetChartForRepo(repoURL, chartName) + if err != nil { + return err + } + + util.LogHandlerExitWithJSON(handler, w, repoChart, http.StatusOK) return nil } diff --git a/cmd/manager/chartrepos_test.go b/cmd/manager/chartrepos_test.go index fc8e9f8f2..8661cb59d 100644 --- a/cmd/manager/chartrepos_test.go +++ b/cmd/manager/chartrepos_test.go @@ -7,10 +7,10 @@ import ( func TestListChartRepositories(t *testing.T) { c := stubContext() - s := httpHarness(c, "GET /chart_repositories", listChartRepositoriesHandlerFunc) + s := httpHarness(c, "GET /repositories", listChartReposHandlerFunc) defer s.Close() - res, err := http.Get(s.URL + "/chart_repositories") + res, err := http.Get(s.URL + "/repositories") if err != nil { t.Errorf("Failed GET: %s", err) } else if res.StatusCode != http.StatusOK { diff --git a/cmd/manager/deployments.go b/cmd/manager/deployments.go index 4b706b857..0e67bc19d 100644 --- a/cmd/manager/deployments.go +++ b/cmd/manager/deployments.go @@ -26,7 +26,6 @@ import ( "net/http" "net/url" "os" - "regexp" "strings" "github.com/ghodss/yaml" @@ -71,13 +70,6 @@ func registerDeploymentRoutes(c *router.Context, h *router.Handler) { h.Add("GET /charts/*/repository", getRepoForChartHandlerFunc) h.Add("GET /charts/*/metadata", getMetadataForChartHandlerFunc) h.Add("GET /charts/*", getChartHandlerFunc) - - // TODO: Refactor these commented out routes - // h.Add("GET /repositories/*", getRepoHandlerFunc) - // h.Add("POST /repositories/*", createRepoHandlerFunc) - - h.Add("GET /repositories/*/charts", listRepositoryChartsHandlerFunc) - h.Add("GET /repositories/*/charts/*", getRepositoryChartHandlerFunc) h.Add("POST /credentials/*", createCredentialHandlerFunc) h.Add("GET /credentials/*", getCredentialHandlerFunc) } @@ -456,122 +448,6 @@ func getChartHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Conte return nil } -// TODO: Refactor this commented out code. - -/* - -func getRepoHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: get repository" - util.LogHandlerEntry(handler, r) - repoName, err := pos(w, r, 2) - if err != nil { - return err - } - - cr, err := c.Manager.GetRepo(repoName) - if err != nil { - httputil.BadRequest(w, r, err) - return nil - } - - util.LogHandlerExitWithJSON(handler, w, cr, http.StatusOK) - return nil -} - -func getRepo(w http.ResponseWriter, r *http.Request, handler string) *repo.Repo { - util.LogHandlerEntry(handler, r) - - t := &repo.Repo{} - if err := httputil.Decode(w, r, t); err != nil { - httputil.BadRequest(w, r, err) - return nil - } - return t -} - -func createRepoHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: create repository" - util.LogHandlerEntry(handler, r) - defer r.Body.Close() - repoName, err := pos(w, r, 2) - if err != nil { - return err - } - - reg := getRepo(w, r, handler) - if reg.Name != repoName { - e := fmt.Errorf("Repo name does not match %s != %s", reg.Name, repoName) - httputil.BadRequest(w, r, e) - return nil - } - if reg != nil { - err = c.Manager.CreateRepo(reg) - if err != nil { - httputil.BadRequest(w, r, err) - return nil - } - } - - util.LogHandlerExitWithJSON(handler, w, reg, http.StatusOK) - return nil -} -*/ - -func listRepositoryChartsHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: list repository charts" - util.LogHandlerEntry(handler, r) - repoName, err := pos(w, r, 2) - if err != nil { - return err - } - - values, err := url.ParseQuery(r.URL.RawQuery) - if err != nil { - httputil.BadRequest(w, r, err) - return nil - } - - var regex *regexp.Regexp - regexString := values.Get("regex") - if regexString != "" { - regex, err = regexp.Compile(regexString) - if err != nil { - httputil.BadRequest(w, r, err) - return nil - } - } - - repoCharts, err := c.Manager.ListRepoCharts(repoName, regex) - if err != nil { - return err - } - - util.LogHandlerExitWithJSON(handler, w, repoCharts, http.StatusOK) - return nil -} - -func getRepositoryChartHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: get repository charts" - util.LogHandlerEntry(handler, r) - repoName, err := pos(w, r, 2) - if err != nil { - return err - } - - chartName, err := pos(w, r, 4) - if err != nil { - return err - } - - repoChart, err := c.Manager.GetChartForRepo(repoName, chartName) - if err != nil { - return err - } - - util.LogHandlerExitWithJSON(handler, w, repoChart, http.StatusOK) - return nil -} - func getCredential(w http.ResponseWriter, r *http.Request, handler string) *repo.Credential { util.LogHandlerEntry(handler, r) t := &repo.Credential{} diff --git a/cmd/manager/manager/manager.go b/cmd/manager/manager/manager.go index d1f67de3d..9edf38ce3 100644 --- a/cmd/manager/manager/manager.go +++ b/cmd/manager/manager/manager.go @@ -50,8 +50,8 @@ type Manager interface { GetChart(chartName string) (*chart.Chart, error) // Repo Charts - ListRepoCharts(repoName string, regex *regexp.Regexp) ([]string, error) - GetChartForRepo(repoName, chartName string) (*chart.Chart, error) + ListRepoCharts(repoURL string, regex *regexp.Regexp) ([]string, error) + GetChartForRepo(repoURL, chartName string) (*chart.Chart, error) // Credentials CreateCredential(name string, c *repo.Credential) error @@ -59,8 +59,9 @@ type Manager interface { // Chart Repositories ListChartRepos() ([]string, error) - AddChartRepo(name string) error + AddChartRepo(addition repo.IRepo) error RemoveChartRepo(name string) error + GetChartRepo(URL string) (repo.IRepo, error) } type manager struct { @@ -335,19 +336,19 @@ func (m *manager) ListChartInstances(chartName string) ([]*common.ChartInstance, return m.repository.GetChartInstances(chartName) } -// GetRepoForChart returns the repository where a chart resides. -func (m *manager) GetRepoForChart(chartName string) (string, error) { - _, r, err := m.repoProvider.GetChartByReference(chartName) +// GetRepoForChart returns the repository where the referenced chart resides. +func (m *manager) GetRepoForChart(reference string) (string, error) { + _, r, err := m.repoProvider.GetChartByReference(reference) if err != nil { return "", err } - return r.GetName(), nil + return r.GetURL(), nil } -// GetMetadataForChart returns the metadata for a chart. -func (m *manager) GetMetadataForChart(chartName string) (*chart.Chartfile, error) { - c, _, err := m.repoProvider.GetChartByReference(chartName) +// GetMetadataForChart returns the metadata for the referenced chart. +func (m *manager) GetMetadataForChart(reference string) (*chart.Chartfile, error) { + c, _, err := m.repoProvider.GetChartByReference(reference) if err != nil { return nil, err } @@ -355,9 +356,9 @@ func (m *manager) GetMetadataForChart(chartName string) (*chart.Chartfile, error return c.Chartfile(), nil } -// GetChart returns a chart. -func (m *manager) GetChart(chartName string) (*chart.Chart, error) { - c, _, err := m.repoProvider.GetChartByReference(chartName) +// GetChart returns the referenced chart. +func (m *manager) GetChart(reference string) (*chart.Chart, error) { + c, _, err := m.repoProvider.GetChartByReference(reference) if err != nil { return nil, err } @@ -365,28 +366,24 @@ func (m *manager) GetChart(chartName string) (*chart.Chart, error) { return c, nil } -// ListChartRepos returns the list of chart repositories +// ListChartRepos returns the list of available chart repository URLs func (m *manager) ListChartRepos() ([]string, error) { - return m.service.List() + return m.service.ListRepos() } -// AddChartRepo adds a chart repository to list of available chart repositories -func (m *manager) AddChartRepo(name string) error { - //TODO: implement - return nil +// AddChartRepo adds a chart repository to the list +func (m *manager) AddChartRepo(addition repo.IRepo) error { + return m.service.CreateRepo(addition) } -// RemoveChartRepo removes a chart repository to list of available chart repositories -func (m *manager) RemoveChartRepo(name string) error { - return m.service.Delete(name) +// RemoveChartRepo removes a chart repository from the list by URL +func (m *manager) RemoveChartRepo(URL string) error { + return m.service.DeleteRepo(URL) } -func (m *manager) CreateRepo(pr repo.IRepo) error { - return m.service.Create(pr) -} - -func (m *manager) GetRepo(name string) (repo.IRepo, error) { - return m.service.Get(name) +// GetChartRepo returns the chart repository with the given URL +func (m *manager) GetChartRepo(URL string) (repo.IRepo, error) { + return m.service.GetRepoByURL(URL) } func generateManifestName() string { @@ -411,11 +408,11 @@ func getResourceErrors(c *common.Configuration) []string { return errs } -// ListRepoCharts lists charts in a given repository whose names +// ListRepoCharts lists charts in a given repository whose URLs // conform to the supplied regular expression, or all charts, if the regular // expression is nil. -func (m *manager) ListRepoCharts(repoName string, regex *regexp.Regexp) ([]string, error) { - r, err := m.repoProvider.GetRepoByName(repoName) +func (m *manager) ListRepoCharts(repoURL string, regex *regexp.Regexp) ([]string, error) { + r, err := m.repoProvider.GetRepoByURL(repoURL) if err != nil { return nil, err } @@ -423,9 +420,9 @@ func (m *manager) ListRepoCharts(repoName string, regex *regexp.Regexp) ([]strin return r.ListCharts(regex) } -// GetChartForRepo returns a chart from a given repository. -func (m *manager) GetChartForRepo(repoName, chartName string) (*chart.Chart, error) { - r, err := m.repoProvider.GetRepoByName(repoName) +// GetChartForRepo returns a chart by name from a given repository. +func (m *manager) GetChartForRepo(repoURL, chartName string) (*chart.Chart, error) { + r, err := m.repoProvider.GetRepoByURL(repoURL) if err != nil { return nil, err } diff --git a/cmd/manager/testutil.go b/cmd/manager/testutil.go index 6749a95d7..aa500123d 100644 --- a/cmd/manager/testutil.go +++ b/cmd/manager/testutil.go @@ -91,7 +91,7 @@ func (m *mockManager) GetChart(chartName string) (*chart.Chart, error) { return nil, nil } -func (m *mockManager) AddChartRepo(name string) error { +func (m *mockManager) AddChartRepo(addition repo.IRepo) error { return nil } @@ -103,6 +103,10 @@ func (m *mockManager) RemoveChartRepo(name string) error { return nil } +func (m *mockManager) GetChartRepo(URL string) (repo.IRepo, error) { + return nil, nil +} + func (m *mockManager) ListRepos() ([]*repo.Repo, error) { return []*repo.Repo{}, nil }