From 5d57fd98b9b20acc8773790916bc276c2def6cb3 Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Sat, 9 Jul 2016 19:14:04 +0200 Subject: [PATCH] fix owner --- api/aliases.go | 52 +++++++++++++++++++++++++++++++++++++++-------- models/ansible.go | 4 +++- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/api/aliases.go b/api/aliases.go index 3550e1a..4ac0fa6 100644 --- a/api/aliases.go +++ b/api/aliases.go @@ -8,6 +8,18 @@ import ( "net/http" ) +// 7 nachkommerstellen sollten genug sein (7cm genau) +// http://blog.3960.org/post/7309573249/genauigkeit-bei-geo-koordinaten + +const GEOROUND = 0.0000001 + +func geoEqual(a, b float64) bool { + if (a-b) < GEOROUND && (b-a) < GEOROUND { + return true + } + return false +} + type ApiAliases struct { aliases *models.Aliases config *models.Config @@ -35,17 +47,34 @@ func (api *ApiAliases) cleaner() { //counter for the diffrent attribute count := 0 if nodeinfo := node.Nodeinfo; nodeinfo != nil { - count += 1 - if alias.Hostname == nodeinfo.Hostname { - count -= 1 - } - if alias.Location != nil && nodeinfo.Location != nil { - count += 2 - if alias.Location.Latitude == nodeinfo.Location.Latitude { + if len(alias.Hostname) > 0 { + count += 1 + if alias.Hostname == nodeinfo.Hostname { count -= 1 + alias.Hostname = "" } - if alias.Location.Longtitude == nodeinfo.Location.Longtitude { + } + if len(alias.Owner) > 0 { + count += 1 + if nodeinfo.Owner != nil && alias.Owner == nodeinfo.Owner.Contact { count -= 1 + alias.Owner = "" + } + } + if alias.Location != nil { + count += 2 + if nodeinfo.Location != nil { + if geoEqual(alias.Location.Latitude, nodeinfo.Location.Latitude) { + count -= 1 + if geoEqual(alias.Location.Longtitude, nodeinfo.Location.Longtitude) { + count -= 1 + alias.Location = nil + } + } else { + if geoEqual(alias.Location.Longtitude, nodeinfo.Location.Longtitude) { + count -= 1 + } + } } } if nodeinfo.Wireless != nil && alias.Wireless != nil { @@ -62,11 +91,16 @@ func (api *ApiAliases) cleaner() { if alias.Wireless.TxPower5 == nodeinfo.Wireless.TxPower5 { count -= 1 } + if alias.Wireless.Channel24 == nodeinfo.Wireless.Channel24 && alias.Wireless.TxPower24 == nodeinfo.Wireless.TxPower24 && alias.Wireless.Channel5 == nodeinfo.Wireless.Channel5 && alias.Wireless.TxPower5 == nodeinfo.Wireless.TxPower5 { + alias.Wireless = nil + } } } + //delete element if count <= 0 { delete(api.aliases.List, key) + fmt.Print("[api] node updated '", key, "'\n") } } } @@ -93,6 +127,7 @@ func (api *ApiAliases) SaveOne(w http.ResponseWriter, r *http.Request, ps httpro return } api.aliases.Update(ps.ByName("nodeid"), &alias) + fmt.Print("[api] node updated '", ps.ByName("nodeid"), "'\n") jsonOutput(w, r, alias) } @@ -101,6 +136,7 @@ func (api *ApiAliases) Cleanup(w http.ResponseWriter, r *http.Request, _ httprou jsonOutput(w, r, api.aliases.List) } func (api *ApiAliases) AnsibleDiff(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { + fmt.Print("[api] ansible\n") api.cleaner() jsonOutput(w, r, models.GenerateAnsible(api.nodes, api.aliases.List)) } diff --git a/models/ansible.go b/models/ansible.go index 4984813..f418c30 100644 --- a/models/ansible.go +++ b/models/ansible.go @@ -27,10 +27,12 @@ func GenerateAnsible(nodes *Nodes, aliases map[string]*Alias) *Ansible { ansible.Nodes = append(ansible.Nodes, nodeid) vars := &AnsibleHostVars{ - Address: node.Nodeinfo.Network.Addresses[0], Hostname: alias.Hostname, Owner: alias.Owner, } + if node.Nodeinfo.Network.Addresses != nil { + vars.Address = node.Nodeinfo.Network.Addresses[0] + } if alias.Wireless != nil { vars.Channel24 = alias.Wireless.Channel24 vars.TxPower24 = alias.Wireless.TxPower24