Commit b61ff1a6 authored by jackgr's avatar jackgr

Wire up routes to chart repo methods

parent e065ebcc
......@@ -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
}
......@@ -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 {
......
......@@ -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{}
......
......@@ -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
}
......
......@@ -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
}
......
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