add API for aliases/ansible

This commit is contained in:
Martin Geno 2016-05-14 13:21:10 +02:00
parent d1aa7ab4d7
commit 139c94f083
4 changed files with 80 additions and 65 deletions

View File

@ -1,43 +1,58 @@
package api
import (
"fmt"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/FreifunkBremen/respond-collector/models"
"fmt"
"net/http"
"encoding/json"
"github.com/julienschmidt/httprouter"
"github.com/FreifunkBremen/respond-collector/models"
)
type ApiAliases struct {
aliases *models.Aliases
config *models.Config
nodes *models.Nodes
aliases *models.Aliases
config *models.Config
nodes *models.Nodes
}
func NewAliases (config *models.Config, router *httprouter.Router,prefix string,nodes *models.Nodes) {
api := &ApiAliases{
aliases: models.NewAliases(config),
nodes: nodes,
config: config,
}
router.GET(prefix, api.GetAll)
router.GET(prefix+"/ansible", api.AnsibleDiff)
router.GET(prefix+"/alias/:nodeid", api.GetOne)
router.POST(prefix+"/alias/:nodeid", api.SaveOne)
api := &ApiAliases{
aliases: models.NewAliases(config),
nodes: nodes,
config: config,
}
router.GET(prefix, api.GetAll)
router.GET(prefix+"/ansible", api.AnsibleDiff)
router.GET(prefix+"/alias/:nodeid", api.GetOne)
router.POST(prefix+"/alias/:nodeid", api.SaveOne)
}
func (api *ApiAliases) GetAll(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
jsonOutput(w,api.aliases.List)
jsonOutput(w,api.aliases.List)
}
func (api *ApiAliases) GetOne(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if alias := api.aliases.List[ps.ByName("nodeid")]; alias !=nil{
jsonOutput(w,alias)
}
fmt.Fprint(w, "Not found: ", ps.ByName("nodeid"),"\n")
if alias := api.aliases.List[ps.ByName("nodeid")]; alias !=nil{
jsonOutput(w,alias)
return
}
fmt.Fprint(w, "Not found: ", ps.ByName("nodeid"),"\n")
}
func (api *ApiAliases) SaveOne(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
alias := &models.Alias{Hostname: ps.ByName("nodeid")}
api.aliases.Update(ps.ByName("nodeid"),alias)
api.GetOne(w,r,ps)
var alias models.Alias
err := json.NewDecoder(r.Body).Decode(&alias)
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)
}
func (api *ApiAliases) AnsibleDiff(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
diff := api.aliases.List
//TODO diff between List and api.nodes (for run not at all)
jsonOutput(w,models.GenerateAnsible(api.nodes,diff))
diff := api.aliases.List
//TODO diff between List and api.nodes (for run not at all)
jsonOutput(w,models.GenerateAnsible(api.nodes,diff))
}

View File

@ -1,17 +1,17 @@
package api
import (
"net/http"
"encoding/json"
"net/http"
"encoding/json"
)
func jsonOutput(w http.ResponseWriter,data interface{}){
js, err := json.Marshal(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
js, err := json.Marshal(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}

View File

@ -40,19 +40,19 @@ func (e *Aliases) Update(nodeID string, newalias *Alias) {
}
func (e *Aliases) load() {
path := e.config.Nodes.AliasesPath
log.Println("loading", path)
path := e.config.Nodes.AliasesPath
log.Println("loading", path)
if data, err := ioutil.ReadFile(path); err == nil {
if err := json.Unmarshal(data, e); err == nil {
log.Println("loaded", len(e.List), "aliases")
} else {
log.Println("failed to unmarshal nodes:", err)
}
if data, err := ioutil.ReadFile(path); err == nil {
if err := json.Unmarshal(data, e); err == nil {
log.Println("loaded", len(e.List), "aliases")
} else {
log.Println("failed to unmarshal nodes:", err)
}
} else {
log.Println("failed loading cached nodes:", err)
}
} else {
log.Println("failed loading cached nodes:", err)
}
}
// Periodically saves the cached DB to json file

View File

@ -1,30 +1,30 @@
package models
type Ansible struct {
Nodes []string `json:"nodes"`
Meta struct {
HostVars []*AnsibleHostVars `json:"hostvars"`
} `json:"_meta"`
Nodes []string `json:"nodes"`
Meta struct {
HostVars []*AnsibleHostVars `json:"hostvars"`
} `json:"_meta"`
}
type AnsibleHostVars struct {
Address string `json:"ansible_ssh_host"`
Hostname string `json:"node_name"`
Address string `json:"ansible_ssh_host"`
Hostname string `json:"node_name"`
}
func GenerateAnsible(nodes *Nodes,aliases map[string]*Alias) *Ansible{
ansible := &Ansible{Nodes:make([]string,0)}
for nodeid,alias := range aliases{
if node := nodes.List[nodeid]; node != nil {
ansible := &Ansible{Nodes:make([]string,0)}
for nodeid,alias := range aliases{
if node := nodes.List[nodeid]; node != nil {
ansible.Nodes = append(ansible.Nodes,nodeid)
ansible.Nodes = append(ansible.Nodes,nodeid)
vars := &AnsibleHostVars{
Address: node.Nodeinfo.Network.Addresses[0],
Hostname: alias.Hostname,
}
ansible.Meta.HostVars = append(ansible.Meta.HostVars,vars)
vars := &AnsibleHostVars{
Address: node.Nodeinfo.Network.Addresses[0],
Hostname: alias.Hostname,
}
ansible.Meta.HostVars = append(ansible.Meta.HostVars,vars)
}
}
return ansible
}
}
return ansible
}