Beispiel #1
0
func updateAgent(w http.ResponseWriter, r *http.Request) (*httpapi.APIResponse, error) {
	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
	if err != nil {
		return nil, httpapi.NewError("", "Request too large", 413)
	}

	if err := r.Body.Close(); err != nil {
		return nil, errors.New("")
	}

	agent := new(spec.Agent)
	if err := json.Unmarshal(body, &agent); err != nil {
		return nil, httpapi.NewError(err.Error(), "Invalid Request", 400)
	}

	vars := mux.Vars(r)

	aID, err := strconv.ParseInt(vars["agentID"], 10, 64)
	if err != nil {
		return nil, httpapi.NewError(err.Error(), "Invalid Agent ID", 400)
	}

	agent.ID = int(aID)

	if err := manager.UpdateAgent(agent); err != nil {
		return nil, httpapi.NewError(err.Error(), "Could not update agent", 400)
	}

	return &httpapi.APIResponse{HTTPCode: 200}, nil
}
Beispiel #2
0
// AddAgent inserts an agent into the DB
func (d *SQLite) AddAgent(s *spec.Agent) error {
	sql := "INSERT INTO " + agentsTable + " (name, address, publicKey) values (?, ?, ?)"

	result, err := d.Connection.Exec(sql, s.Name, s.Address, s.PublicKey)
	if err != nil {
		return err
	}

	id, err := result.LastInsertId()
	if err != nil {
		return err
	}

	s.ID = int(id)
	return nil
}