예제 #1
0
func (s *Server) GetInstances(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
	instances, err := s.Backend.GetClusterInstances(params.ByName("cluster_id"))
	if err != nil {
		httphelper.Error(w, err)
		return
	}
	if instances == nil {
		instances = []*Instance{}
	}
	httphelper.JSON(w, 200, struct {
		Data []*Instance `json:"data"`
	}{instances})
}
예제 #2
0
func (s *Server) CreateInstance(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
	var data struct {
		Data *Instance `json:"data"`
	}
	if err := httphelper.DecodeJSON(req, &data); err != nil {
		httphelper.Error(w, err)
		return
	}
	inst := data.Data
	inst.ClusterID = params.ByName("cluster_id")
	inst.CreatorIP = sourceIP(req)
	// TODO: validate with JSON schema

	status := http.StatusCreated
	if err := s.Backend.CreateInstance(inst); err == ErrExists {
		status = http.StatusConflict
	} else if err != nil {
		httphelper.Error(w, err)
		return
	}

	w.Header().Set("Location", fmt.Sprintf("%s/clusters/%s/instances/%s", s.URL, inst.ClusterID, inst.ID))
	httphelper.JSON(w, status, data)
}