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 }
// 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 }
// AddAgent simply adds an agent to the system func AddAgent(agent *spec.Agent) error { key, err := getAgentKey(agent) if err != nil { return err } agent.PublicKey = key ks, err := keys.DecodePublicKeyString(key) if err != nil { return err } ip := strings.Split(agent.Address, ":") keyManager.PublicKeys[ip[0]] = ks return gDb.AddAgent(agent) }