configuration.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 // this is not used any more. leave it here for later.
  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. }