Exemple #1
0
func ServerCreate(w http.ResponseWriter, r *http.Request) {
	if authentication.IsAllowed(w, r) {
		var server pulp.Server
		body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
		if err != nil {
			logger.Log("could not read POST body, Error: "+err.Error(), logger.ERROR)
		}

		if err := r.Body.Close(); err != nil {
			logger.Log("could not close POST body, Error: "+err.Error(), logger.ERROR)
		}

		if err := json.Unmarshal(body, &server); err != nil {
			w.Header().Set("Content-Type", "application/json; charset=UTF-8")
			w.WriteHeader(422) // unprocessable entity
			if err := json.NewEncoder(w).Encode(err); err != nil {
				logger.Log("could not json/encode error, Error: "+err.Error(), logger.ERROR)
			}
		}
		session, collection := db.InitServerCollection()
		defer session.Close()
		server.Added = time.Now()
		err = collection.Insert(server)
		if err != nil {
			logger.Log("could not insert server to DB, Error: "+err.Error(), logger.ERROR)
		}
		w.Header().Set("Content-Type", "application/json; charset=UTF=8")
		w.WriteHeader(http.StatusCreated)
		if err := json.NewEncoder(w).Encode(server); err != nil {
			panic(err)
		}
	}
}
Exemple #2
0
func ServerShow(w http.ResponseWriter, r *http.Request) {
	oid := getOID("serverId", r)

	session, collection := db.InitServerCollection()
	defer session.Close()
	server := pulp.Server{}

	db.SearchOne(bson.M{"_id": oid}, collection, &server)

	if err := json.NewEncoder(w).Encode(server); err != nil {
		logger.Log("could not encode server struct, Error: "+err.Error(), logger.ERROR)
	}
}
Exemple #3
0
func ServerIndex(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusOK)

	session, collection := db.InitServerCollection()
	defer session.Close()
	servers := pulp.Servers{}

	db.SearchAll(bson.M{}, collection, &servers)

	if err := json.NewEncoder(w).Encode(servers); err != nil {
		logger.Log("could not encode servers struct, Error: "+err.Error(), logger.ERROR)
	}
}
Exemple #4
0
func ServerDelete(w http.ResponseWriter, r *http.Request) {
	if authentication.IsAllowed(w, r) {
		oid := getOID("serverId", r)
		session, collection := db.InitServerCollection()
		defer session.Close()
		err := collection.Remove(bson.M{"_id": oid})
		if err != nil {
			w.Header().Set("Content-Type", "application/json; charset=UTF-8")
			w.WriteHeader(http.StatusInternalServerError)
			if err := json.NewEncoder(w).Encode(err); err != nil {
				logger.Log("could not json/encode error, Error: "+err.Error(), logger.ERROR)
			}
		}
	}
}