yanic/api/aliases.go

104 lines
2.9 KiB
Go
Raw Normal View History

2016-05-14 12:31:43 +02:00
package api
import (
2016-05-14 13:21:10 +02:00
"encoding/json"
2016-05-29 21:41:58 +02:00
"fmt"
2016-05-14 13:21:10 +02:00
"github.com/FreifunkBremen/respond-collector/models"
2016-05-29 21:41:58 +02:00
"github.com/julienschmidt/httprouter"
"net/http"
2016-05-14 12:31:43 +02:00
)
2016-05-14 13:21:10 +02:00
2016-05-14 12:31:43 +02:00
type ApiAliases struct {
2016-05-14 13:21:10 +02:00
aliases *models.Aliases
2016-05-29 21:41:58 +02:00
config *models.Config
nodes *models.Nodes
2016-05-14 12:31:43 +02:00
}
2016-05-14 13:21:10 +02:00
2016-05-29 21:41:58 +02:00
func NewAliases(config *models.Config, router *httprouter.Router, prefix string, nodes *models.Nodes) {
2016-05-14 13:21:10 +02:00
api := &ApiAliases{
aliases: models.NewAliases(config),
2016-05-29 21:41:58 +02:00
nodes: nodes,
config: config,
2016-05-14 13:21:10 +02:00
}
router.GET(prefix, api.GetAll)
router.GET(prefix+"/ansible", api.AnsibleDiff)
2016-05-16 12:24:50 +02:00
router.GET(prefix+"/cleanup", api.Cleanup)
2016-05-29 21:41:58 +02:00
router.GET(prefix+"/auth", BasicAuth(api.Cleanup, []byte(config.Webserver.Api.Passphrase)))
router.GET(prefix+"/alias/:nodeid", api.GetOne)
router.POST(prefix+"/alias/:nodeid", BasicAuth(api.SaveOne, []byte(config.Webserver.Api.Passphrase)))
2016-05-16 12:24:50 +02:00
}
2016-05-29 21:41:58 +02:00
2016-05-17 10:37:15 +02:00
// clean up the aliases by correct values in nodes
2016-05-29 21:41:58 +02:00
func (api *ApiAliases) cleaner() {
for key, alias := range api.aliases.List {
if node := api.nodes.List[key]; node != nil {
if nodeinfo := node.Nodeinfo; nodeinfo != nil {
2016-05-17 10:37:15 +02:00
//counter for the diffrent attribute
2016-05-29 21:41:58 +02:00
count := 3
2016-05-17 10:37:15 +02:00
if alias.Hostname == nodeinfo.Hostname {
2016-05-17 10:47:01 +02:00
count -= 1
2016-05-17 10:37:15 +02:00
}
2016-05-29 22:49:40 +02:00
if alias.Location != nil && nodeinfo.Location != nil {
if alias.Location.Latitude == nodeinfo.Location.Latitude {
count -= 1
}
if alias.Location.Longtitude == nodeinfo.Location.Longtitude {
count -= 1
}
2016-05-29 21:41:58 +02:00
}
/*
if alias.Freq24.TxPower == nodeinfo.Freq24.TxPower {
count -= 1
}
if alias.Freq24.Channel == nodeinfo.Freq24.Channel {
count -= 1
}
if alias.Freq5.TxPower == nodeinfo.Freq5.TxPower {
count -= 1
}
if alias.Freq5.Channel == nodeinfo.Freq5.Channel {
count -= 1
}
*/
2016-05-17 10:37:15 +02:00
//delete element
if count <= 0 {
2016-05-29 21:41:58 +02:00
delete(api.aliases.List, key)
2016-05-17 10:37:15 +02:00
}
}
2016-05-17 10:30:31 +02:00
}
}
2016-05-14 12:31:43 +02:00
}
func (api *ApiAliases) GetAll(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
2016-05-29 21:41:58 +02:00
jsonOutput(w, r, api.aliases.List)
2016-05-14 12:31:43 +02:00
}
2016-05-14 13:21:10 +02:00
2016-05-14 12:31:43 +02:00
func (api *ApiAliases) GetOne(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
2016-05-29 21:41:58 +02:00
if alias := api.aliases.List[ps.ByName("nodeid")]; alias != nil {
jsonOutput(w, r, alias)
2016-05-14 13:21:10 +02:00
return
}
2016-05-29 21:41:58 +02:00
fmt.Fprint(w, "Not found: ", ps.ByName("nodeid"), "\n")
2016-05-14 12:31:43 +02:00
}
2016-05-14 13:21:10 +02:00
2016-05-14 12:31:43 +02:00
func (api *ApiAliases) SaveOne(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
2016-05-14 13:21:10 +02:00
var alias models.Alias
2016-05-16 12:24:50 +02:00
err := json.NewDecoder(r.Body).Decode(&alias)
2016-05-29 21:41:58 +02:00
if err != nil {
2016-05-14 13:21:10 +02:00
http.Error(w, err.Error(), http.StatusInternalServerError)
2016-05-29 21:41:58 +02:00
fmt.Fprint(w, "Decode: ", ps.ByName("nodeid"), "\n")
2016-05-14 13:21:10 +02:00
return
}
2016-05-29 21:41:58 +02:00
api.aliases.Update(ps.ByName("nodeid"), &alias)
jsonOutput(w, r, alias)
2016-05-14 12:31:43 +02:00
}
2016-05-14 13:21:10 +02:00
2016-05-16 12:24:50 +02:00
func (api *ApiAliases) Cleanup(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
api.cleaner()
2016-05-29 21:41:58 +02:00
jsonOutput(w, r, api.aliases.List)
2016-05-16 12:24:50 +02:00
}
2016-05-14 12:31:43 +02:00
func (api *ApiAliases) AnsibleDiff(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
2016-05-16 12:24:50 +02:00
api.cleaner()
2016-05-29 21:41:58 +02:00
jsonOutput(w, r, models.GenerateAnsible(api.nodes, api.aliases.List))
2016-05-14 12:31:43 +02:00
}