Exemple #1
0
func (ws *webServer) SetFlag(w http.ResponseWriter, r *http.Request) {
	_, name := path.Split(r.URL.Path)
	value := r.FormValue("value")

	macStr := r.FormValue("mac")
	var machine datasource.Machine
	var exist bool
	if macStr != "" {
		mac, err := net.ParseMAC(macStr)
		if err != nil {
			http.Error(w, `{"error": "Error while parsing the mac"}`, http.StatusInternalServerError)
			return
		}

		machine, exist = ws.ds.GetMachine(mac)
		if !exist {
			http.Error(w, `{"error": "Machine not found"}`, http.StatusInternalServerError)
			return
		}
	}

	var err error
	if machine != nil {
		err = machine.SetFlag(name, value)
	} else {
		// TODO deafult flags
		http.Error(w, `{"error": "Default flags not supported yet"}`, http.StatusInternalServerError)
	}

	if err != nil {
		http.Error(w, `{"error": "Error while setting value"}`, http.StatusInternalServerError)
		return
	}

	io.WriteString(w, `"OK"`)
}