github.com/yasker/longhorn-engine@v0.0.0-20160621014712-6ed6cfca0729/controller/rest/volume.go (about) 1 package rest 2 3 import ( 4 "net/http" 5 6 "github.com/gorilla/mux" 7 "github.com/rancher/go-rancher/api" 8 "github.com/rancher/go-rancher/client" 9 ) 10 11 func (s *Server) ListVolumes(rw http.ResponseWriter, req *http.Request) error { 12 apiContext := api.GetApiContext(req) 13 apiContext.Write(&client.GenericCollection{ 14 Data: []interface{}{ 15 s.listVolumes(apiContext)[0], 16 }, 17 }) 18 return nil 19 } 20 21 func (s *Server) GetVolume(rw http.ResponseWriter, req *http.Request) error { 22 apiContext := api.GetApiContext(req) 23 id := mux.Vars(req)["id"] 24 25 v := s.getVolume(apiContext, id) 26 if v == nil { 27 rw.WriteHeader(http.StatusNotFound) 28 return nil 29 } 30 31 apiContext.Write(v) 32 return nil 33 } 34 35 func (s *Server) ShutdownVolume(rw http.ResponseWriter, req *http.Request) error { 36 apiContext := api.GetApiContext(req) 37 id := mux.Vars(req)["id"] 38 39 v := s.getVolume(apiContext, id) 40 if v == nil { 41 rw.WriteHeader(http.StatusNotFound) 42 return nil 43 } 44 45 if err := s.c.Shutdown(); err != nil { 46 return err 47 } 48 49 return s.GetVolume(rw, req) 50 } 51 52 func (s *Server) RevertVolume(rw http.ResponseWriter, req *http.Request) error { 53 apiContext := api.GetApiContext(req) 54 id := mux.Vars(req)["id"] 55 56 v := s.getVolume(apiContext, id) 57 if v == nil { 58 rw.WriteHeader(http.StatusNotFound) 59 return nil 60 } 61 62 var input RevertInput 63 if err := apiContext.Read(&input); err != nil { 64 return err 65 } 66 67 if err := s.c.Revert(input.Name); err != nil { 68 return err 69 } 70 71 return s.GetVolume(rw, req) 72 } 73 74 func (s *Server) SnapshotVolume(rw http.ResponseWriter, req *http.Request) error { 75 apiContext := api.GetApiContext(req) 76 id := mux.Vars(req)["id"] 77 78 v := s.getVolume(apiContext, id) 79 if v == nil { 80 rw.WriteHeader(http.StatusNotFound) 81 return nil 82 } 83 84 var input SnapshotInput 85 if err := apiContext.Read(&input); err != nil { 86 return err 87 } 88 89 name, err := s.c.Snapshot(input.Name) 90 if err != nil { 91 return err 92 } 93 94 apiContext.Write(&SnapshotOutput{ 95 client.Resource{ 96 Id: name, 97 Type: "snapshotOutput", 98 }, 99 }) 100 return nil 101 } 102 103 func (s *Server) StartVolume(rw http.ResponseWriter, req *http.Request) error { 104 apiContext := api.GetApiContext(req) 105 id := mux.Vars(req)["id"] 106 107 v := s.getVolume(apiContext, id) 108 if v == nil { 109 rw.WriteHeader(http.StatusNotFound) 110 return nil 111 } 112 113 var input StartInput 114 if err := apiContext.Read(&input); err != nil { 115 return err 116 } 117 118 if err := s.c.Start(input.Replicas...); err != nil { 119 return err 120 } 121 122 return s.GetVolume(rw, req) 123 } 124 125 func (s *Server) listVolumes(context *api.ApiContext) []*Volume { 126 return []*Volume{ 127 NewVolume(context, s.c.Name, len(s.c.ListReplicas())), 128 } 129 } 130 131 func (s *Server) getVolume(context *api.ApiContext, id string) *Volume { 132 for _, v := range s.listVolumes(context) { 133 if v.Id == id { 134 return v 135 } 136 } 137 return nil 138 }