api aliases delete cleanup
This commit is contained in:
		
							parent
							
								
									f127bed990
								
							
						
					
					
						commit
						f76f6b34a8
					
				@ -34,75 +34,10 @@ func NewAliases(config *models.Config, router *httprouter.Router, prefix string,
 | 
			
		||||
	}
 | 
			
		||||
	router.GET(prefix, api.GetAll)
 | 
			
		||||
	router.GET(prefix+"/ansible", api.AnsibleDiff)
 | 
			
		||||
	router.GET(prefix+"/cleanup", api.Cleanup)
 | 
			
		||||
	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)))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// clean up the aliases by correct values in nodes
 | 
			
		||||
func (api *ApiAliases) cleaner() {
 | 
			
		||||
	for key, alias := range api.aliases.List {
 | 
			
		||||
		if node := api.nodes.List[key]; node != nil {
 | 
			
		||||
			//counter for the diffrent attribute
 | 
			
		||||
			count := 0
 | 
			
		||||
			if nodeinfo := node.Nodeinfo; nodeinfo != nil {
 | 
			
		||||
				if len(alias.Hostname) > 0 {
 | 
			
		||||
					count += 1
 | 
			
		||||
					if alias.Hostname == nodeinfo.Hostname {
 | 
			
		||||
						count -= 1
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
				if len(alias.Owner) > 0 {
 | 
			
		||||
					count += 1
 | 
			
		||||
					if nodeinfo.Owner != nil && alias.Owner == nodeinfo.Owner.Contact {
 | 
			
		||||
						count -= 1
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
				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 {
 | 
			
		||||
					count += 4
 | 
			
		||||
					if alias.Wireless.Channel24 == nodeinfo.Wireless.Channel24 {
 | 
			
		||||
						count -= 1
 | 
			
		||||
					}
 | 
			
		||||
					if alias.Wireless.TxPower24 == nodeinfo.Wireless.TxPower24 {
 | 
			
		||||
						count -= 1
 | 
			
		||||
					}
 | 
			
		||||
					if alias.Wireless.Channel5 == nodeinfo.Wireless.Channel5 {
 | 
			
		||||
						count -= 1
 | 
			
		||||
					}
 | 
			
		||||
					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")
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
func (api *ApiAliases) GetAll(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
 | 
			
		||||
	jsonOutput(w, r, api.aliases.List)
 | 
			
		||||
}
 | 
			
		||||
@ -128,13 +63,7 @@ func (api *ApiAliases) SaveOne(w http.ResponseWriter, r *http.Request, ps httpro
 | 
			
		||||
	fmt.Print("[api] node updated '", ps.ByName("nodeid"), "'\n")
 | 
			
		||||
	jsonOutput(w, r, alias)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (api *ApiAliases) Cleanup(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
 | 
			
		||||
	api.cleaner()
 | 
			
		||||
	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))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user