Exemple #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})
}
Exemple #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)
}
Exemple #3
0
func (s *Server) CreateCluster(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
	cluster := &Cluster{
		CreatorIP:        sourceIP(req),
		CreatorUserAgent: req.Header.Get("User-Agent"),
	}

	if len(cluster.CreatorUserAgent) > 1000 {
		cluster.CreatorUserAgent = cluster.CreatorUserAgent[:1000]
	}

	if err := s.Backend.CreateCluster(cluster); err != nil {
		httphelper.Error(w, err)
		return
	}

	w.Header().Set("Location", fmt.Sprintf("%s/clusters/%s", s.URL, cluster.ID))
	w.WriteHeader(http.StatusCreated)
}