func (s *Server) DeleteSnapshot(rw http.ResponseWriter, req *http.Request) error { apiContext := api.GetApiContext(req) vars := mux.Vars(req) id, ok := vars["id"] if !ok { return fmt.Errorf("Snapshot id not supplied.") } snapshot, err := s.getSnapshot(apiContext, id) if err != nil { return err } if snapshot == nil { rw.WriteHeader(http.StatusNotFound) return nil } cmd := exec.Command("longhorn", "snapshots", "rm", snapshot.Id) cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout if err := cmd.Run(); err != nil { return err } rw.WriteHeader(http.StatusNoContent) return nil }
func (s *Server) RevertToSnapshot(rw http.ResponseWriter, req *http.Request) error { logrus.Infof("Reverting to snapshot") apiContext := api.GetApiContext(req) id := mux.Vars(req)["id"] if id != "1" { rw.WriteHeader(http.StatusNotFound) return nil } var input revertInput if err := apiContext.Read(&input); err != nil { return err } snap, err := s.getSnapshot(apiContext, input.Name) if err != nil { return err } if snap == nil { rw.WriteHeader(http.StatusBadRequest) return nil } _, err = s.controllerClient.RevertVolume(snap.Name) if err != nil { return err } return s.GetVolume(rw, req) }
func apiAuthenticator(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() logrus.Debugf("%#v", r.Header) policyRequest, err := http.NewRequest("GET", CATTLE_URL_API_POLICY, nil) if err != nil { panic(err) } policyRequest.Header.Set("Accept", "application/json") for _, cookie := range r.Cookies() { if cookie.Name != "CSRF" { policyRequest.AddCookie(cookie) } } resp, err := http.DefaultClient.Do(policyRequest) if err != nil { panic(err) } body, err := ioutil.ReadAll(resp.Body) if err != nil { panic(err) } policies := &Policies{} err = json.Unmarshal(body, policies) if err != nil { panic(err) } policy := policies.Data[0] logrus.Debugf("Policy: \n %#v", policy) context := api.GetApiContext(r) if context == nil { panic(fmt.Errorf("No context found:%v", r.URL.RawPath)) } context.Policy = policy w.Write(body) }
func (s *Server) writeResponse(err error, r *http.Request, data interface{}) error { if err != nil { return err } api.GetApiContext(r).Write(data) return nil }
func HandleError(s *client.Schemas, t func(http.ResponseWriter, *http.Request) error) http.Handler { return api.ApiHandler(s, http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { if err := t(rw, req); err != nil { apiContext := api.GetApiContext(req) apiContext.WriteErr(err) } })) }
func (s *Server) ListReplicas(rw http.ResponseWriter, req *http.Request) error { apiContext := api.GetApiContext(req) resp := client.GenericCollection{} resp.Data = append(resp.Data, s.Replica(apiContext)) apiContext.Write(&resp) return nil }
func (s *Server) RemoveDisk(rw http.ResponseWriter, req *http.Request) error { var input RemoveDiskInput apiContext := api.GetApiContext(req) if err := apiContext.Read(&input); err != nil { return err } return s.doOp(req, s.s.RemoveDiffDisk(input.Name)) }
func (s *Server) SetRebuilding(rw http.ResponseWriter, req *http.Request) error { var input RebuildingInput apiContext := api.GetApiContext(req) if err := apiContext.Read(&input); err != nil && err != io.EOF { return err } return s.doOp(req, s.s.SetRebuilding(input.Rebuilding)) }
func (s *Server) doOp(req *http.Request, err error) error { if err != nil { return err } apiContext := api.GetApiContext(req) apiContext.Write(s.Replica(apiContext)) return nil }
func (s *Server) RevertReplica(rw http.ResponseWriter, req *http.Request) error { var input RevertInput apiContext := api.GetApiContext(req) if err := apiContext.Read(&input); err != nil && err != io.EOF { return err } return s.doOp(req, s.s.Revert(input.Name)) }
//ListJournal flushes operation journal (replica read/write, ping, etc.) accumulated since previous flush func (s *Server) ListJournal(rw http.ResponseWriter, req *http.Request) error { var input JournalInput apiContext := api.GetApiContext(req) if err := apiContext.Read(&input); err != nil { return err } journal.PrintLimited(input.Limit) return nil }
func (s *Server) GetReplica(rw http.ResponseWriter, req *http.Request) error { apiContext := api.GetApiContext(req) r := s.Replica(apiContext) if mux.Vars(req)["id"] == r.Id { apiContext.Write(r) } else { rw.WriteHeader(http.StatusNotFound) } return nil }
func (s *Server) ListVolumes(rw http.ResponseWriter, req *http.Request) error { logrus.Infof("Listing volumes") apiContext := api.GetApiContext(req) apiContext.Write(&client.GenericCollection{ Data: []interface{}{ s.getVolume(apiContext), }, }) return nil }
func (s *Server) ListSnapshots(rw http.ResponseWriter, req *http.Request) error { apiContext := api.GetApiContext(req) snapshots, err := s.listSnapshot(apiContext) if err != nil { return err } apiContext.Write(&client.GenericCollection{ Data: []interface{}{ snapshots, }, }) return nil }
func (s *Server) SnapshotReplica(rw http.ResponseWriter, req *http.Request) error { var input SnapshotInput apiContext := api.GetApiContext(req) if err := apiContext.Read(&input); err != nil && err != io.EOF { return err } name := input.Name if name == "" { name = util.UUID() } return s.doOp(req, s.s.Snapshot(name)) }
func (s *Server) GetVolume(rw http.ResponseWriter, req *http.Request) error { logrus.Infof("Getting volumes") apiContext := api.GetApiContext(req) id := mux.Vars(req)["id"] if id != "1" { rw.WriteHeader(http.StatusNotFound) return nil } v := s.getVolume(apiContext) apiContext.Write(v) return nil }
//ReturnHTTPError handles sending out CatalogError response func ReturnHTTPError(w http.ResponseWriter, r *http.Request, httpStatus int, errorMessage string) { w.WriteHeader(httpStatus) err := model.CatalogError{ Resource: client.Resource{ Type: "error", }, Status: strconv.Itoa(httpStatus), Message: errorMessage, } api.CreateApiContext(w, r, schemas) api.GetApiContext(r).Write(&err) }
func (s *Server) StackList(rw http.ResponseWriter, r *http.Request) error { context := api.GetApiContext(r) context.Write(&client.GenericCollection{ Data: []interface{}{ client.Resource{ Id: "foo", Type: "stack", }, client.Resource{ Id: "bar", Type: "stack", }, }, }) return nil }
func (s *Server) CreateSnapshot(rw http.ResponseWriter, req *http.Request) error { apiContext := api.GetApiContext(req) var snapshot snapshot if err := apiContext.Read(&snapshot); err != nil { return err } name, err := s.controllerClient.Snapshot(snapshot.Name) if err != nil { return err } apiContext.Write(newSnapshot(apiContext, name)) return nil }
func (s *Server) Create(rw http.ResponseWriter, req *http.Request) error { var input CreateInput apiContext := api.GetApiContext(req) if err := apiContext.Read(&input); err != nil && err != io.EOF { return err } size := int64(0) if input.Size != "" { var err error size, err = strconv.ParseInt(input.Size, 10, 0) if err != nil { return err } } return s.doOp(req, s.s.Create(size)) }
func (s *Server) PrepareRemoveDisk(rw http.ResponseWriter, req *http.Request) error { var input PrepareRemoveDiskInput apiContext := api.GetApiContext(req) if err := apiContext.Read(&input); err != nil && err != io.EOF { return err } operations, err := s.s.PrepareRemoveDisk(input.Name) if err != nil { return err } apiContext.Write(&PrepareRemoveDiskOutput{ Resource: client.Resource{ Id: input.Name, Type: "prepareRemoveDiskOutput", }, Operations: operations, }) return nil }
func (s *Server) GetSnapshot(rw http.ResponseWriter, req *http.Request) error { apiContext := api.GetApiContext(req) vars := mux.Vars(req) id, ok := vars["id"] if !ok { return fmt.Errorf("Snapshot id not supplied.") } snap, err := s.getSnapshot(apiContext, id) if err != nil { return err } if snap == nil { rw.WriteHeader(http.StatusNotFound) return nil } apiContext.Write(snap) return nil }