You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
969 B

  1. package topology
  2. import (
  3. "encoding/xml"
  4. )
  5. type loc struct {
  6. dcName string
  7. rackName string
  8. }
  9. type rack struct {
  10. Name string `xml:"name,attr"`
  11. Ips []string `xml:"Ip"`
  12. }
  13. type dataCenter struct {
  14. Name string `xml:"name,attr"`
  15. Racks []rack `xml:"Rack"`
  16. }
  17. type topology struct {
  18. DataCenters []dataCenter `xml:"DataCenter"`
  19. }
  20. type Configuration struct {
  21. XMLName xml.Name `xml:"Configuration"`
  22. Topo topology `xml:"Topology"`
  23. ip2location map[string]loc
  24. }
  25. func (c *Configuration) String() string {
  26. if b, e := xml.MarshalIndent(c, " ", " "); e == nil {
  27. return string(b)
  28. }
  29. return ""
  30. }
  31. func (c *Configuration) Locate(ip string, dcName string, rackName string) (dc string, rack string) {
  32. if c != nil && c.ip2location != nil {
  33. if loc, ok := c.ip2location[ip]; ok {
  34. return loc.dcName, loc.rackName
  35. }
  36. }
  37. if dcName == "" {
  38. dcName = "DefaultDataCenter"
  39. }
  40. if rackName == "" {
  41. rackName = "DefaultRack"
  42. }
  43. return dcName, rackName
  44. }