Exemple #1
0
// PutRegion accepts a request to retrieve information about a particular region.
//
//     PUT /api/regions/:region
//
func PutRegion(c web.C, w http.ResponseWriter, r *http.Request) {
	var (
		ctx    = context.FromC(c)
		idStr  = c.URLParams["region"]
		region model.Region
	)

	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	if !regionFromRequest(c, w, r, &region) {
		return
	}
	region.ID = id

	err = datastore.UpdateRegion(ctx, &region)
	if err != nil {
		log.FromContext(ctx).WithField("err", err).Error("Error updating region")
		w.WriteHeader(http.StatusNotFound)
		return
	}

	json.NewEncoder(w).Encode(&region)
}
Exemple #2
0
// ListRegions accepts a request to retrieve a list of regions.
//
//     GET /api/regions
//
func ListRegions(c web.C, w http.ResponseWriter, r *http.Request) {
	var (
		ctx    = context.FromC(c)
		limit  = ToLimit(r)
		offset = ToOffset(r)
	)

	regions, err := datastore.ListRegions(ctx, limit, offset)
	if err != nil {
		log.FromContext(ctx).WithField("err", err).Error("Error listing regions")
		w.WriteHeader(http.StatusNotFound)
		return
	}

	json.NewEncoder(w).Encode(regions)
}
Exemple #3
0
// GetHost accepts a request to retrieve information about a particular host.
//
//     GET /api/hosts/:host
//
func GetHost(c web.C, w http.ResponseWriter, r *http.Request) {
	var (
		ctx   = context.FromC(c)
		idStr = c.URLParams["host"]
	)

	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	host, err := datastore.GetHost(ctx, id)
	if err != nil {
		log.FromContext(ctx).WithField("err", err).Error("Error getting host")
		w.WriteHeader(http.StatusNotFound)
		return
	}

	json.NewEncoder(w).Encode(host)
}
Exemple #4
0
// DeleteRegion accepts a request to delete a region.
//
//     DELETE /api/regions/:region
//
func DeleteRegion(c web.C, w http.ResponseWriter, r *http.Request) {
	var (
		ctx   = context.FromC(c)
		idStr = c.URLParams["region"]
	)

	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	err = datastore.DeleteRegion(ctx, id)
	if err != nil {
		log.FromContext(ctx).WithField("err", err).Error("Error deleting region")
		w.WriteHeader(http.StatusNotFound)
		return
	}

	w.WriteHeader(http.StatusNoContent)
}