diff --git a/cmd/manager/deployments.go b/cmd/manager/deployments.go index 39764121c..e3537fc0e 100644 --- a/cmd/manager/deployments.go +++ b/cmd/manager/deployments.go @@ -38,15 +38,14 @@ import ( "github.com/kubernetes/deployment-manager/cmd/manager/repository/transient" "github.com/kubernetes/deployment-manager/cmd/manager/router" "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/util" ) var deployments = []Route{ - {"ListDeployments", "/deployments", "GET", listDeploymentsHandlerFunc, ""}, - {"GetDeployment", "/deployments/{deployment}", "GET", getDeploymentHandlerFunc, ""}, {"CreateDeployment", "/deployments", "POST", createDeploymentHandlerFunc, "JSON"}, - {"DeleteDeployment", "/deployments/{deployment}", "DELETE", deleteDeploymentHandlerFunc, ""}, + {"DeleteDeplyment", "/deployments/{deployment}", "DELETE", deleteDeploymentHandlerFunc, ""}, {"PutDeployment", "/deployments/{deployment}", "PUT", putDeploymentHandlerFunc, "JSON"}, {"ListManifests", "/deployments/{deployment}/manifests", "GET", listManifestsHandlerFunc, ""}, {"GetManifest", "/deployments/{deployment}/manifests/{manifest}", "GET", getManifestHandlerFunc, ""}, @@ -83,6 +82,8 @@ func registerRoutes(c *router.Context, h *router.Handler) { re := regexp.MustCompile("{[a-z]+}") h.Add("GET /healthz", healthz) + h.Add("GET /deployments", listDeploymentsHandlerFunc) + h.Add("GET /deployments/*", getDeploymentHandlerFunc) // TODO: Replace these routes with updated ones. for _, d := range deployments { @@ -186,13 +187,13 @@ func makeEnvVariableName(str string) string { 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" util.LogHandlerEntry(handler, r) l, err := backend.ListDeployments() if err != nil { util.LogAndReturnError(handler, http.StatusInternalServerError, err, w) - return + return nil } var names []string for _, d := range l { @@ -200,23 +201,25 @@ func listDeploymentsHandlerFunc(w http.ResponseWriter, r *http.Request) { } 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" util.LogHandlerEntry(handler, r) name, err := getPathVariable(w, r, "deployment", handler) if err != nil { - return + return nil } d, err := backend.GetDeployment(name) if err != nil { util.LogAndReturnError(handler, http.StatusBadRequest, err, w) - return + return nil } util.LogHandlerExitWithJSON(handler, w, d, http.StatusOK) + return nil } func createDeploymentHandlerFunc(w http.ResponseWriter, r *http.Request) { @@ -240,7 +243,7 @@ func deleteDeploymentHandlerFunc(w http.ResponseWriter, r *http.Request) { handler := "manager: delete deployment" util.LogHandlerEntry(handler, r) 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 { return } @@ -258,7 +261,7 @@ func putDeploymentHandlerFunc(w http.ResponseWriter, r *http.Request) { handler := "manager: update deployment" util.LogHandlerEntry(handler, r) 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 { 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) { vars := mux.Vars(r) 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) { handler := "manager: list manifests" util.LogHandlerEntry(handler, r) - deploymentName, err := getPathVariable(w, r, "deployment", handler) + deploymentName, err := pos(w, r, 2) if err != nil { return } @@ -337,12 +349,12 @@ func listManifestsHandlerFunc(w http.ResponseWriter, r *http.Request) { func getManifestHandlerFunc(w http.ResponseWriter, r *http.Request) { handler := "manager: get manifest" util.LogHandlerEntry(handler, r) - deploymentName, err := getPathVariable(w, r, "deployment", handler) + deploymentName, err := pos(w, r, 2) if err != nil { return } - manifestName, err := getPathVariable(w, r, "manifest", handler) + manifestName, err := pos(w, r, 4) if err != nil { return } @@ -390,7 +402,7 @@ func listTypesHandlerFunc(w http.ResponseWriter, r *http.Request) { func listTypeInstancesHandlerFunc(w http.ResponseWriter, r *http.Request) { handler := "manager: list instances" util.LogHandlerEntry(handler, r) - typeName, err := getPathVariable(w, r, "type", handler) + typeName, err := pos(w, r, 2) if err != nil { return } @@ -407,7 +419,7 @@ func listTypeInstancesHandlerFunc(w http.ResponseWriter, r *http.Request) { func getRegistryForTypeHandlerFunc(w http.ResponseWriter, r *http.Request) { handler := "manager: get type registry" util.LogHandlerEntry(handler, r) - typeName, err := getPathVariable(w, r, "type", handler) + typeName, err := pos(w, r, 2) if err != nil { return } @@ -424,7 +436,7 @@ func getRegistryForTypeHandlerFunc(w http.ResponseWriter, r *http.Request) { func getMetadataForTypeHandlerFunc(w http.ResponseWriter, r *http.Request) { handler := "manager: get type metadata" util.LogHandlerEntry(handler, r) - typeName, err := getPathVariable(w, r, "type", handler) + typeName, err := pos(w, r, 2) if err != nil { return } @@ -454,7 +466,7 @@ func listRegistriesHandlerFunc(w http.ResponseWriter, r *http.Request) { func getRegistryHandlerFunc(w http.ResponseWriter, r *http.Request) { handler := "manager: get registry" util.LogHandlerEntry(handler, r) - registryName, err := getPathVariable(w, r, "registry", handler) + registryName, err := pos(w, r, 2) if err != nil { return } @@ -489,7 +501,7 @@ func createRegistryHandlerFunc(w http.ResponseWriter, r *http.Request) { handler := "manager: create registry" util.LogHandlerEntry(handler, r) defer r.Body.Close() - registryName, err := getPathVariable(w, r, "registry", handler) + registryName, err := pos(w, r, 2) if err != nil { return } @@ -514,7 +526,7 @@ func createRegistryHandlerFunc(w http.ResponseWriter, r *http.Request) { func listRegistryTypesHandlerFunc(w http.ResponseWriter, r *http.Request) { handler := "manager: list registry types" util.LogHandlerEntry(handler, r) - registryName, err := getPathVariable(w, r, "registry", handler) + registryName, err := pos(w, r, 2) if err != nil { return } @@ -547,12 +559,12 @@ func listRegistryTypesHandlerFunc(w http.ResponseWriter, r *http.Request) { func getDownloadURLsHandlerFunc(w http.ResponseWriter, r *http.Request) { handler := "manager: get download URLs" util.LogHandlerEntry(handler, r) - registryName, err := getPathVariable(w, r, "registry", handler) + registryName, err := pos(w, r, 2) if err != nil { return } - typeName, err := getPathVariable(w, r, "type", handler) + typeName, err := pos(w, r, 4) if err != nil { return } @@ -579,7 +591,7 @@ func getDownloadURLsHandlerFunc(w http.ResponseWriter, r *http.Request) { func getFileHandlerFunc(w http.ResponseWriter, r *http.Request) { handler := "manager: get file" util.LogHandlerEntry(handler, r) - registryName, err := getPathVariable(w, r, "registry", handler) + registryName, err := pos(w, r, 2) if err != nil { return } @@ -619,7 +631,7 @@ func createCredentialHandlerFunc(w http.ResponseWriter, r *http.Request) { handler := "manager: create credential" util.LogHandlerEntry(handler, r) defer r.Body.Close() - credentialName, err := getPathVariable(w, r, "credential", handler) + credentialName, err := pos(w, r, 2) if err != nil { return } @@ -639,7 +651,7 @@ func createCredentialHandlerFunc(w http.ResponseWriter, r *http.Request) { func getCredentialHandlerFunc(w http.ResponseWriter, r *http.Request) { handler := "manager: get credential" util.LogHandlerEntry(handler, r) - credentialName, err := getPathVariable(w, r, "credential", handler) + credentialName, err := pos(w, r, 2) if err != nil { return } diff --git a/pkg/httputil/httperrors.go b/pkg/httputil/httperrors.go index 1d9f0d56e..c13a2a6bf 100644 --- a/pkg/httputil/httperrors.go +++ b/pkg/httputil/httperrors.go @@ -29,6 +29,8 @@ const ( LogNotFound = "Not Found: %s %s" // LogFatal is for logging 500 errors. Form: Internal Server Error r.Method r.URL message 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. @@ -38,6 +40,12 @@ func NotFound(w http.ResponseWriter, r *http.Request) { 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. // // Additional arguments are past into the the formatter as params to msg.