fix(manager): rewrite URL param grabbing

This updates path grabbing to match the new URI matcher.
pull/363/head
Matt Butcher 9 years ago
parent ce32351bec
commit 527a4f9c9d

@ -38,15 +38,14 @@ import (
"github.com/kubernetes/deployment-manager/cmd/manager/repository/transient" "github.com/kubernetes/deployment-manager/cmd/manager/repository/transient"
"github.com/kubernetes/deployment-manager/cmd/manager/router" "github.com/kubernetes/deployment-manager/cmd/manager/router"
"github.com/kubernetes/deployment-manager/pkg/common" "github.com/kubernetes/deployment-manager/pkg/common"
"github.com/kubernetes/deployment-manager/pkg/httputil"
"github.com/kubernetes/deployment-manager/pkg/registry" "github.com/kubernetes/deployment-manager/pkg/registry"
"github.com/kubernetes/deployment-manager/pkg/util" "github.com/kubernetes/deployment-manager/pkg/util"
) )
var deployments = []Route{ var deployments = []Route{
{"ListDeployments", "/deployments", "GET", listDeploymentsHandlerFunc, ""},
{"GetDeployment", "/deployments/{deployment}", "GET", getDeploymentHandlerFunc, ""},
{"CreateDeployment", "/deployments", "POST", createDeploymentHandlerFunc, "JSON"}, {"CreateDeployment", "/deployments", "POST", createDeploymentHandlerFunc, "JSON"},
{"DeleteDeployment", "/deployments/{deployment}", "DELETE", deleteDeploymentHandlerFunc, ""}, {"DeleteDeplyment", "/deployments/{deployment}", "DELETE", deleteDeploymentHandlerFunc, ""},
{"PutDeployment", "/deployments/{deployment}", "PUT", putDeploymentHandlerFunc, "JSON"}, {"PutDeployment", "/deployments/{deployment}", "PUT", putDeploymentHandlerFunc, "JSON"},
{"ListManifests", "/deployments/{deployment}/manifests", "GET", listManifestsHandlerFunc, ""}, {"ListManifests", "/deployments/{deployment}/manifests", "GET", listManifestsHandlerFunc, ""},
{"GetManifest", "/deployments/{deployment}/manifests/{manifest}", "GET", getManifestHandlerFunc, ""}, {"GetManifest", "/deployments/{deployment}/manifests/{manifest}", "GET", getManifestHandlerFunc, ""},
@ -83,6 +82,8 @@ func registerRoutes(c *router.Context, h *router.Handler) {
re := regexp.MustCompile("{[a-z]+}") re := regexp.MustCompile("{[a-z]+}")
h.Add("GET /healthz", healthz) h.Add("GET /healthz", healthz)
h.Add("GET /deployments", listDeploymentsHandlerFunc)
h.Add("GET /deployments/*", getDeploymentHandlerFunc)
// TODO: Replace these routes with updated ones. // TODO: Replace these routes with updated ones.
for _, d := range deployments { for _, d := range deployments {
@ -186,13 +187,13 @@ func makeEnvVariableName(str string) string {
return strings.ToUpper(strings.Replace(str, "-", "_", -1)) return strings.ToUpper(strings.Replace(str, "-", "_", -1))
} }
func listDeploymentsHandlerFunc(w http.ResponseWriter, r *http.Request) { func listDeploymentsHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error {
handler := "manager: list deployments" handler := "manager: list deployments"
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
l, err := backend.ListDeployments() l, err := backend.ListDeployments()
if err != nil { if err != nil {
util.LogAndReturnError(handler, http.StatusInternalServerError, err, w) util.LogAndReturnError(handler, http.StatusInternalServerError, err, w)
return return nil
} }
var names []string var names []string
for _, d := range l { for _, d := range l {
@ -200,23 +201,25 @@ func listDeploymentsHandlerFunc(w http.ResponseWriter, r *http.Request) {
} }
util.LogHandlerExitWithJSON(handler, w, names, http.StatusOK) util.LogHandlerExitWithJSON(handler, w, names, http.StatusOK)
return nil
} }
func getDeploymentHandlerFunc(w http.ResponseWriter, r *http.Request) { func getDeploymentHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error {
handler := "manager: get deployment" handler := "manager: get deployment"
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
name, err := getPathVariable(w, r, "deployment", handler) name, err := getPathVariable(w, r, "deployment", handler)
if err != nil { if err != nil {
return return nil
} }
d, err := backend.GetDeployment(name) d, err := backend.GetDeployment(name)
if err != nil { if err != nil {
util.LogAndReturnError(handler, http.StatusBadRequest, err, w) util.LogAndReturnError(handler, http.StatusBadRequest, err, w)
return return nil
} }
util.LogHandlerExitWithJSON(handler, w, d, http.StatusOK) util.LogHandlerExitWithJSON(handler, w, d, http.StatusOK)
return nil
} }
func createDeploymentHandlerFunc(w http.ResponseWriter, r *http.Request) { func createDeploymentHandlerFunc(w http.ResponseWriter, r *http.Request) {
@ -240,7 +243,7 @@ func deleteDeploymentHandlerFunc(w http.ResponseWriter, r *http.Request) {
handler := "manager: delete deployment" handler := "manager: delete deployment"
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
defer r.Body.Close() defer r.Body.Close()
name, err := getPathVariable(w, r, "deployment", handler) name, err := pos(w, r, 2) //getPathVariable(w, r, "deployment", handler)
if err != nil { if err != nil {
return return
} }
@ -258,7 +261,7 @@ func putDeploymentHandlerFunc(w http.ResponseWriter, r *http.Request) {
handler := "manager: update deployment" handler := "manager: update deployment"
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
defer r.Body.Close() defer r.Body.Close()
name, err := getPathVariable(w, r, "deployment", handler) name, err := pos(w, r, 2) //getPathVariable(w, r, "deployment", handler)
if err != nil { if err != nil {
return return
} }
@ -275,6 +278,15 @@ func putDeploymentHandlerFunc(w http.ResponseWriter, r *http.Request) {
} }
} }
func pos(w http.ResponseWriter, r *http.Request, i int) (string, error) {
parts := strings.Split(r.URL.Path, "/")
if len(parts) < i-1 {
httputil.BadRequest(w, r)
return "", fmt.Errorf("No index for %d", i)
}
return parts[i], nil
}
func getPathVariable(w http.ResponseWriter, r *http.Request, variable, handler string) (string, error) { func getPathVariable(w http.ResponseWriter, r *http.Request, variable, handler string) (string, error) {
vars := mux.Vars(r) vars := mux.Vars(r)
escaped, ok := vars[variable] escaped, ok := vars[variable]
@ -315,7 +327,7 @@ func getTemplate(w http.ResponseWriter, r *http.Request, handler string) *common
func listManifestsHandlerFunc(w http.ResponseWriter, r *http.Request) { func listManifestsHandlerFunc(w http.ResponseWriter, r *http.Request) {
handler := "manager: list manifests" handler := "manager: list manifests"
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
deploymentName, err := getPathVariable(w, r, "deployment", handler) deploymentName, err := pos(w, r, 2)
if err != nil { if err != nil {
return return
} }
@ -337,12 +349,12 @@ func listManifestsHandlerFunc(w http.ResponseWriter, r *http.Request) {
func getManifestHandlerFunc(w http.ResponseWriter, r *http.Request) { func getManifestHandlerFunc(w http.ResponseWriter, r *http.Request) {
handler := "manager: get manifest" handler := "manager: get manifest"
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
deploymentName, err := getPathVariable(w, r, "deployment", handler) deploymentName, err := pos(w, r, 2)
if err != nil { if err != nil {
return return
} }
manifestName, err := getPathVariable(w, r, "manifest", handler) manifestName, err := pos(w, r, 4)
if err != nil { if err != nil {
return return
} }
@ -390,7 +402,7 @@ func listTypesHandlerFunc(w http.ResponseWriter, r *http.Request) {
func listTypeInstancesHandlerFunc(w http.ResponseWriter, r *http.Request) { func listTypeInstancesHandlerFunc(w http.ResponseWriter, r *http.Request) {
handler := "manager: list instances" handler := "manager: list instances"
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
typeName, err := getPathVariable(w, r, "type", handler) typeName, err := pos(w, r, 2)
if err != nil { if err != nil {
return return
} }
@ -407,7 +419,7 @@ func listTypeInstancesHandlerFunc(w http.ResponseWriter, r *http.Request) {
func getRegistryForTypeHandlerFunc(w http.ResponseWriter, r *http.Request) { func getRegistryForTypeHandlerFunc(w http.ResponseWriter, r *http.Request) {
handler := "manager: get type registry" handler := "manager: get type registry"
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
typeName, err := getPathVariable(w, r, "type", handler) typeName, err := pos(w, r, 2)
if err != nil { if err != nil {
return return
} }
@ -424,7 +436,7 @@ func getRegistryForTypeHandlerFunc(w http.ResponseWriter, r *http.Request) {
func getMetadataForTypeHandlerFunc(w http.ResponseWriter, r *http.Request) { func getMetadataForTypeHandlerFunc(w http.ResponseWriter, r *http.Request) {
handler := "manager: get type metadata" handler := "manager: get type metadata"
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
typeName, err := getPathVariable(w, r, "type", handler) typeName, err := pos(w, r, 2)
if err != nil { if err != nil {
return return
} }
@ -454,7 +466,7 @@ func listRegistriesHandlerFunc(w http.ResponseWriter, r *http.Request) {
func getRegistryHandlerFunc(w http.ResponseWriter, r *http.Request) { func getRegistryHandlerFunc(w http.ResponseWriter, r *http.Request) {
handler := "manager: get registry" handler := "manager: get registry"
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
registryName, err := getPathVariable(w, r, "registry", handler) registryName, err := pos(w, r, 2)
if err != nil { if err != nil {
return return
} }
@ -489,7 +501,7 @@ func createRegistryHandlerFunc(w http.ResponseWriter, r *http.Request) {
handler := "manager: create registry" handler := "manager: create registry"
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
defer r.Body.Close() defer r.Body.Close()
registryName, err := getPathVariable(w, r, "registry", handler) registryName, err := pos(w, r, 2)
if err != nil { if err != nil {
return return
} }
@ -514,7 +526,7 @@ func createRegistryHandlerFunc(w http.ResponseWriter, r *http.Request) {
func listRegistryTypesHandlerFunc(w http.ResponseWriter, r *http.Request) { func listRegistryTypesHandlerFunc(w http.ResponseWriter, r *http.Request) {
handler := "manager: list registry types" handler := "manager: list registry types"
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
registryName, err := getPathVariable(w, r, "registry", handler) registryName, err := pos(w, r, 2)
if err != nil { if err != nil {
return return
} }
@ -547,12 +559,12 @@ func listRegistryTypesHandlerFunc(w http.ResponseWriter, r *http.Request) {
func getDownloadURLsHandlerFunc(w http.ResponseWriter, r *http.Request) { func getDownloadURLsHandlerFunc(w http.ResponseWriter, r *http.Request) {
handler := "manager: get download URLs" handler := "manager: get download URLs"
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
registryName, err := getPathVariable(w, r, "registry", handler) registryName, err := pos(w, r, 2)
if err != nil { if err != nil {
return return
} }
typeName, err := getPathVariable(w, r, "type", handler) typeName, err := pos(w, r, 4)
if err != nil { if err != nil {
return return
} }
@ -579,7 +591,7 @@ func getDownloadURLsHandlerFunc(w http.ResponseWriter, r *http.Request) {
func getFileHandlerFunc(w http.ResponseWriter, r *http.Request) { func getFileHandlerFunc(w http.ResponseWriter, r *http.Request) {
handler := "manager: get file" handler := "manager: get file"
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
registryName, err := getPathVariable(w, r, "registry", handler) registryName, err := pos(w, r, 2)
if err != nil { if err != nil {
return return
} }
@ -619,7 +631,7 @@ func createCredentialHandlerFunc(w http.ResponseWriter, r *http.Request) {
handler := "manager: create credential" handler := "manager: create credential"
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
defer r.Body.Close() defer r.Body.Close()
credentialName, err := getPathVariable(w, r, "credential", handler) credentialName, err := pos(w, r, 2)
if err != nil { if err != nil {
return return
} }
@ -639,7 +651,7 @@ func createCredentialHandlerFunc(w http.ResponseWriter, r *http.Request) {
func getCredentialHandlerFunc(w http.ResponseWriter, r *http.Request) { func getCredentialHandlerFunc(w http.ResponseWriter, r *http.Request) {
handler := "manager: get credential" handler := "manager: get credential"
util.LogHandlerEntry(handler, r) util.LogHandlerEntry(handler, r)
credentialName, err := getPathVariable(w, r, "credential", handler) credentialName, err := pos(w, r, 2)
if err != nil { if err != nil {
return return
} }

@ -29,6 +29,8 @@ const (
LogNotFound = "Not Found: %s %s" LogNotFound = "Not Found: %s %s"
// LogFatal is for logging 500 errors. Form: Internal Server Error r.Method r.URL message // LogFatal is for logging 500 errors. Form: Internal Server Error r.Method r.URL message
LogFatal = "Internal Server Error: %s %s %s" LogFatal = "Internal Server Error: %s %s %s"
// LogBadRequest logs 400 errors.
LogBadRequest = "Bad Request: %s %s"
) )
// NotFound writes a 404 error to the client and logs an error. // NotFound writes a 404 error to the client and logs an error.
@ -38,6 +40,12 @@ func NotFound(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "File Not Found") fmt.Fprintln(w, "File Not Found")
} }
func BadRequest(w http.ResponseWriter, r *http.Request) {
log.Printf(LogNotFound, r.Method, r.URL)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, "Bad Request")
}
// Fatal writes a 500 response to the client and logs the message. // Fatal writes a 500 response to the client and logs the message.
// //
// Additional arguments are past into the the formatter as params to msg. // Additional arguments are past into the the formatter as params to msg.

Loading…
Cancel
Save