From 139c94f083b3b53c14d363d6d59bebfbddca575a Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Sat, 14 May 2016 13:21:10 +0200 Subject: [PATCH] add API for aliases/ansible --- api/aliases.go | 69 ++++++++++++++++++++++++++++------------------- api/lib.go | 18 ++++++------- models/aliases.go | 22 +++++++-------- models/ansible.go | 36 ++++++++++++------------- 4 files changed, 80 insertions(+), 65 deletions(-) diff --git a/api/aliases.go b/api/aliases.go index e7be5b1..96b2e9e 100644 --- a/api/aliases.go +++ b/api/aliases.go @@ -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)) } diff --git a/api/lib.go b/api/lib.go index 38a7c47..2f5aa2f 100644 --- a/api/lib.go +++ b/api/lib.go @@ -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) } diff --git a/models/aliases.go b/models/aliases.go index 635c386..bd842ee 100644 --- a/models/aliases.go +++ b/models/aliases.go @@ -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 diff --git a/models/ansible.go b/models/ansible.go index cc21f58..ecc62a3 100644 --- a/models/ansible.go +++ b/models/ansible.go @@ -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 }