github.com/yasker/longhorn-engine@v0.0.0-20160621014712-6ed6cfca0729/agent/controller/rest/volume.go (about)

     1  package rest
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/Sirupsen/logrus"
     7  	"github.com/gorilla/mux"
     8  
     9  	"github.com/rancher/go-rancher/api"
    10  	"github.com/rancher/go-rancher/client"
    11  )
    12  
    13  func (s *Server) ListVolumes(rw http.ResponseWriter, req *http.Request) error {
    14  	logrus.Infof("Listing volumes")
    15  
    16  	apiContext := api.GetApiContext(req)
    17  
    18  	apiContext.Write(&client.GenericCollection{
    19  		Data: []interface{}{
    20  			s.getVolume(apiContext),
    21  		},
    22  	})
    23  	return nil
    24  }
    25  
    26  func (s *Server) GetVolume(rw http.ResponseWriter, req *http.Request) error {
    27  	logrus.Infof("Getting volumes")
    28  
    29  	apiContext := api.GetApiContext(req)
    30  	id := mux.Vars(req)["id"]
    31  
    32  	if id != "1" {
    33  		rw.WriteHeader(http.StatusNotFound)
    34  		return nil
    35  	}
    36  
    37  	v := s.getVolume(apiContext)
    38  	apiContext.Write(v)
    39  	return nil
    40  }
    41  
    42  func (s *Server) RevertToSnapshot(rw http.ResponseWriter, req *http.Request) error {
    43  	logrus.Infof("Reverting to snapshot")
    44  
    45  	apiContext := api.GetApiContext(req)
    46  	id := mux.Vars(req)["id"]
    47  
    48  	if id != "1" {
    49  		rw.WriteHeader(http.StatusNotFound)
    50  		return nil
    51  	}
    52  
    53  	var input revertInput
    54  	if err := apiContext.Read(&input); err != nil {
    55  		return err
    56  	}
    57  
    58  	snap, err := s.getSnapshot(apiContext, input.Name)
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	if snap == nil {
    64  		rw.WriteHeader(http.StatusBadRequest)
    65  		return nil
    66  	}
    67  
    68  	_, err = s.controllerClient.RevertVolume(snap.Name)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	return s.GetVolume(rw, req)
    74  }
    75  
    76  func (s *Server) getVolume(context *api.ApiContext) *volume {
    77  	return &volume{
    78  		Resource: client.Resource{Id: "1"},
    79  		Name:     "volume",
    80  	}
    81  }