Wire up routes to chart repo methods

pull/446/head
jackgr 9 years ago
parent e065ebcc19
commit b61ff1a655

@ -2,50 +2,140 @@ package main
import ( import (
"github.com/kubernetes/helm/cmd/manager/router" "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" "github.com/kubernetes/helm/pkg/util"
"net/http" "net/http"
"net/url"
"regexp"
) )
func registerChartRepoRoutes(c *router.Context, h *router.Handler) { func registerChartRepoRoutes(c *router.Context, h *router.Handler) {
h.Add("GET /chart_repositories", listChartRepositoriesHandlerFunc) h.Add("GET /repositories", listChartReposHandlerFunc)
h.Add("POST /chart_repositories", addChartRepoHandlerFunc) h.Add("GET /repositories/*", getChartRepoHandlerFunc)
h.Add("DELETE /chart_repositories", removeChartRepoHandlerFunc) 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" handler := "manager: list chart repositories"
repos, err := c.Manager.ListChartRepos() repos, err := c.Manager.ListChartRepos()
if err != nil { if err != nil {
return err return err
} }
util.LogHandlerExitWithJSON(handler, w, repos, http.StatusOK) util.LogHandlerExitWithJSON(handler, w, repos, http.StatusOK)
return nil return nil
} }
func addChartRepoHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { func addChartRepoHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error {
handler := "manager: add chart repository" 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 { if err != nil {
return err return err
} }
err = c.Manager.AddChartRepo(name)
err = c.Manager.RemoveChartRepo(URL)
if err != nil { if err != nil {
return err return err
} }
util.LogHandlerExitWithJSON(handler, w, "added", http.StatusOK)
util.LogHandlerExitWithText(handler, w, "removed", http.StatusOK)
return nil return nil
} }
func removeChartRepoHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { func getChartRepoHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error {
handler := "manager: remove chart repository" handler := "manager: get repository"
name, err := pos(w, r, 2) util.LogHandlerEntry(handler, r)
repoURL, err := pos(w, r, 2)
if err != nil {
return err
}
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 { if err != nil {
return err return err
} }
err = c.Manager.RemoveChartRepo(name)
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 { if err != nil {
return err return err
} }
util.LogHandlerExitWithJSON(handler, w, "removed", http.StatusOK)
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 return nil
} }

@ -7,10 +7,10 @@ import (
func TestListChartRepositories(t *testing.T) { func TestListChartRepositories(t *testing.T) {
c := stubContext() c := stubContext()
s := httpHarness(c, "GET /chart_repositories", listChartRepositoriesHandlerFunc) s := httpHarness(c, "GET /repositories", listChartReposHandlerFunc)
defer s.Close() defer s.Close()
res, err := http.Get(s.URL + "/chart_repositories") res, err := http.Get(s.URL + "/repositories")
if err != nil { if err != nil {
t.Errorf("Failed GET: %s", err) t.Errorf("Failed GET: %s", err)
} else if res.StatusCode != http.StatusOK { } else if res.StatusCode != http.StatusOK {

@ -26,7 +26,6 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"regexp"
"strings" "strings"
"github.com/ghodss/yaml" "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/*/repository", getRepoForChartHandlerFunc)
h.Add("GET /charts/*/metadata", getMetadataForChartHandlerFunc) h.Add("GET /charts/*/metadata", getMetadataForChartHandlerFunc)
h.Add("GET /charts/*", getChartHandlerFunc) 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("POST /credentials/*", createCredentialHandlerFunc)
h.Add("GET /credentials/*", getCredentialHandlerFunc) h.Add("GET /credentials/*", getCredentialHandlerFunc)
} }
@ -456,122 +448,6 @@ func getChartHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Conte
return nil 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 { func getCredential(w http.ResponseWriter, r *http.Request, handler string) *repo.Credential {
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
t := &repo.Credential{} t := &repo.Credential{}

@ -50,8 +50,8 @@ type Manager interface {
GetChart(chartName string) (*chart.Chart, error) GetChart(chartName string) (*chart.Chart, error)
// Repo Charts // Repo Charts
ListRepoCharts(repoName string, regex *regexp.Regexp) ([]string, error) ListRepoCharts(repoURL string, regex *regexp.Regexp) ([]string, error)
GetChartForRepo(repoName, chartName string) (*chart.Chart, error) GetChartForRepo(repoURL, chartName string) (*chart.Chart, error)
// Credentials // Credentials
CreateCredential(name string, c *repo.Credential) error CreateCredential(name string, c *repo.Credential) error
@ -59,8 +59,9 @@ type Manager interface {
// Chart Repositories // Chart Repositories
ListChartRepos() ([]string, error) ListChartRepos() ([]string, error)
AddChartRepo(name string) error AddChartRepo(addition repo.IRepo) error
RemoveChartRepo(name string) error RemoveChartRepo(name string) error
GetChartRepo(URL string) (repo.IRepo, error)
} }
type manager struct { type manager struct {
@ -335,19 +336,19 @@ func (m *manager) ListChartInstances(chartName string) ([]*common.ChartInstance,
return m.repository.GetChartInstances(chartName) return m.repository.GetChartInstances(chartName)
} }
// GetRepoForChart returns the repository where a chart resides. // GetRepoForChart returns the repository where the referenced chart resides.
func (m *manager) GetRepoForChart(chartName string) (string, error) { func (m *manager) GetRepoForChart(reference string) (string, error) {
_, r, err := m.repoProvider.GetChartByReference(chartName) _, r, err := m.repoProvider.GetChartByReference(reference)
if err != nil { if err != nil {
return "", err return "", err
} }
return r.GetName(), nil return r.GetURL(), nil
} }
// GetMetadataForChart returns the metadata for a chart. // GetMetadataForChart returns the metadata for the referenced chart.
func (m *manager) GetMetadataForChart(chartName string) (*chart.Chartfile, error) { func (m *manager) GetMetadataForChart(reference string) (*chart.Chartfile, error) {
c, _, err := m.repoProvider.GetChartByReference(chartName) c, _, err := m.repoProvider.GetChartByReference(reference)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -355,9 +356,9 @@ func (m *manager) GetMetadataForChart(chartName string) (*chart.Chartfile, error
return c.Chartfile(), nil return c.Chartfile(), nil
} }
// GetChart returns a chart. // GetChart returns the referenced chart.
func (m *manager) GetChart(chartName string) (*chart.Chart, error) { func (m *manager) GetChart(reference string) (*chart.Chart, error) {
c, _, err := m.repoProvider.GetChartByReference(chartName) c, _, err := m.repoProvider.GetChartByReference(reference)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -365,28 +366,24 @@ func (m *manager) GetChart(chartName string) (*chart.Chart, error) {
return c, nil 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) { 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 // AddChartRepo adds a chart repository to the list
func (m *manager) AddChartRepo(name string) error { func (m *manager) AddChartRepo(addition repo.IRepo) error {
//TODO: implement return m.service.CreateRepo(addition)
return nil
} }
// RemoveChartRepo removes a chart repository to list of available chart repositories // RemoveChartRepo removes a chart repository from the list by URL
func (m *manager) RemoveChartRepo(name string) error { func (m *manager) RemoveChartRepo(URL string) error {
return m.service.Delete(name) return m.service.DeleteRepo(URL)
} }
func (m *manager) CreateRepo(pr repo.IRepo) error { // GetChartRepo returns the chart repository with the given URL
return m.service.Create(pr) func (m *manager) GetChartRepo(URL string) (repo.IRepo, error) {
} return m.service.GetRepoByURL(URL)
func (m *manager) GetRepo(name string) (repo.IRepo, error) {
return m.service.Get(name)
} }
func generateManifestName() string { func generateManifestName() string {
@ -411,11 +408,11 @@ func getResourceErrors(c *common.Configuration) []string {
return errs 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 // conform to the supplied regular expression, or all charts, if the regular
// expression is nil. // expression is nil.
func (m *manager) ListRepoCharts(repoName string, regex *regexp.Regexp) ([]string, error) { func (m *manager) ListRepoCharts(repoURL string, regex *regexp.Regexp) ([]string, error) {
r, err := m.repoProvider.GetRepoByName(repoName) r, err := m.repoProvider.GetRepoByURL(repoURL)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -423,9 +420,9 @@ func (m *manager) ListRepoCharts(repoName string, regex *regexp.Regexp) ([]strin
return r.ListCharts(regex) return r.ListCharts(regex)
} }
// GetChartForRepo returns a chart from a given repository. // GetChartForRepo returns a chart by name from a given repository.
func (m *manager) GetChartForRepo(repoName, chartName string) (*chart.Chart, error) { func (m *manager) GetChartForRepo(repoURL, chartName string) (*chart.Chart, error) {
r, err := m.repoProvider.GetRepoByName(repoName) r, err := m.repoProvider.GetRepoByURL(repoURL)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -91,7 +91,7 @@ func (m *mockManager) GetChart(chartName string) (*chart.Chart, error) {
return nil, nil return nil, nil
} }
func (m *mockManager) AddChartRepo(name string) error { func (m *mockManager) AddChartRepo(addition repo.IRepo) error {
return nil return nil
} }
@ -103,6 +103,10 @@ func (m *mockManager) RemoveChartRepo(name string) error {
return nil return nil
} }
func (m *mockManager) GetChartRepo(URL string) (repo.IRepo, error) {
return nil, nil
}
func (m *mockManager) ListRepos() ([]*repo.Repo, error) { func (m *mockManager) ListRepos() ([]*repo.Repo, error) {
return []*repo.Repo{}, nil return []*repo.Repo{}, nil
} }

Loading…
Cancel
Save