func getConfigurationHandlerFunc(w http.ResponseWriter, r *http.Request) { handler := "resourcifier: get configuration" util.LogHandlerEntry(handler, r) rtype, err := getPathVariable(w, r, "type", handler) if err != nil { return } rname, err := getPathVariable(w, r, "name", handler) if err != nil { return } c := &common.Configuration{ Resources: []*common.Resource{ {Name: rname, Type: rtype}, }, } output, err := backend.Configure(c, configurator.GetOperation) if err != nil { util.LogAndReturnError(handler, http.StatusBadRequest, err, w) return } util.LogHandlerExit(handler, http.StatusOK, output, w) util.WriteYAML(handler, w, []byte(output), http.StatusOK) }
func deleteConfigurationHandlerFunc(w http.ResponseWriter, r *http.Request) { handler := "resourcifier: delete configuration" util.LogHandlerEntry(handler, r) defer r.Body.Close() c := getConfiguration(w, r, handler) if c != nil { if _, err := backend.Configure(c, configurator.DeleteOperation); err != nil { e := errors.New("cannot delete configuration: " + err.Error() + "\n") util.LogAndReturnError(handler, http.StatusBadRequest, e, w) return } w.WriteHeader(http.StatusNoContent) util.LogHandlerExit(handler, http.StatusNoContent, "No Content", w) return } util.LogHandlerExit(handler, http.StatusOK, "OK", w) }
// NewService encapsulates code to open an HTTP server on the given address:port that serves the // expansion API using the given Expander backend to do the actual expansion. After calling // NewService, call ListenAndServe to start the returned service. func NewService(address string, port int, backend Expander) *Service { restful.EnableTracing(true) webService := new(restful.WebService) webService.Consumes(restful.MIME_JSON) webService.Produces(restful.MIME_JSON) handler := func(req *restful.Request, resp *restful.Response) { util.LogHandlerEntry("expansion service", req.Request) request := &ServiceRequest{} if err := req.ReadEntity(&request); err != nil { badRequest(resp, err.Error()) return } reqMsg := fmt.Sprintf("\nhandling request:\n%s\n", util.ToYAMLOrError(request)) util.LogHandlerText("expansion service", reqMsg) response, err := backend.ExpandChart(request) if err != nil { badRequest(resp, fmt.Sprintf("error expanding chart: %s", err)) return } util.LogHandlerExit("expansion service", http.StatusOK, "OK", resp.ResponseWriter) respMsg := fmt.Sprintf("\nreturning response:\n%s\n", util.ToYAMLOrError(response.Resources)) util.LogHandlerText("expansion service", respMsg) resp.WriteEntity(response) } webService.Route( webService.POST("/expand"). To(handler). Doc("Expand a chart."). Reads(&ServiceRequest{}). Writes(&ServiceResponse{})) container := restful.DefaultContainer container.Add(webService) server := &http.Server{ Addr: fmt.Sprintf("%s:%d", address, port), Handler: container, } return &Service{ webService: webService, server: server, container: container, } }
func putConfigurationHandlerFunc(w http.ResponseWriter, r *http.Request) { handler := "resourcifier: update configuration" util.LogHandlerEntry(handler, r) defer r.Body.Close() c := getConfiguration(w, r, handler) if c != nil { if _, err := backend.Configure(c, configurator.ReplaceOperation); err != nil { e := errors.New("cannot replace configuration: " + err.Error() + "\n") util.LogAndReturnError(handler, http.StatusBadRequest, e, w) return } util.LogHandlerExitWithYAML(handler, w, c, http.StatusCreated) return } util.LogHandlerExit(handler, http.StatusOK, "OK", w) }
func createConfigurationHandlerFunc(w http.ResponseWriter, r *http.Request) { handler := "resourcifier: create configuration" util.LogHandlerEntry(handler, r) defer r.Body.Close() c := getConfiguration(w, r, handler) if c != nil { _, err := backend.Configure(c, configurator.CreateOperation) if err != nil { util.LogAndReturnError(handler, http.StatusBadRequest, err, w) return } util.LogHandlerExitWithYAML(handler, w, c, http.StatusCreated) return } util.LogHandlerExit(handler, http.StatusOK, "OK", w) }
func badRequest(resp *restful.Response, message string) { statusCode := http.StatusBadRequest util.LogHandlerExit("expansion service", statusCode, message, resp.ResponseWriter) resp.WriteError(statusCode, errors.New(message)) }