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