package parsers // used with 'sh server bind' import ( "fmt" "regexp" log "github.com/sirupsen/logrus" "github.rackspace.com/gorax/tools/util" ) type ( // AdxBindInfo is the aggregate type for all server-bind info on // an ADX AdxBindInfo struct { Virtual []AdxBindVirtual `json:"bind_info"` } // AdxBindVirtual is the virtual address data items parsed into a struct AdxBindVirtual struct { Name string `json:"name"` Status string `json:"status"` IPAddress string `json:"ip_address"` SymmetricState string `json:"symmetric_vip_state"` Port []AdxBindVirtualPort `json:"ports"` } // AdxBindVirtual is the virtual port data items parsed into a struct AdxBindVirtualPort struct { Port string `json:"port"` Server []AdxBindServer `json:"servers"` } // AdxBindVirtual is the real server data items parsed into a struct AdxBindServer struct { Name string `json:"name"` IPAddress string `json:"ip_address"` Port string `json:"port"` State string `json:"state"` } ) var ( bindInfoRe = regexp.MustCompile(`(?i)(virtual\s+server|):\s+(\S+)\s+status:\s+(\S+)\s+IP:\s+(\S+)((.|\n)*?)(virtual server|$)`) symStateRe = regexp.MustCompile(`symmetric\s+VIP\s+state:\s+(\S+)`) bindVeRe = regexp.MustCompile(`\s+(\S+).*?->`) bindVePrtRe = regexp.MustCompile(`(\S+):\s+(\S+),\s+(\S+)\s+\((.*?)\)`) ) // Parse method is used to implement type population of a given string // using parsers to populate the method receiver. func (a *AdxBindInfo) Parse(s *string) { log.Debug("Parsing AdxBindInfo") for _, v := range bindInfoRe.FindAllStringSubmatch(*s, -1) { var i AdxBindVirtual i.Name = v[2] i.Status = v[3] i.IPAddress = v[4] i.parse(v[5]) a.Virtual = append(a.Virtual, i) } } func (i *AdxBindVirtual) parse(s string) { i.SymmetricState = util.RegexpFindString(symStateRe, s) for _, v := range bindVeRe.FindAllStringSubmatch(s, -1) { r := fmt.Sprintf("%s.*?->((.|\\n)*?)(->|$)", v[1]) b := regexpFindString(r, &s) if b != nil { var p AdxBindVirtualPort p.Port = v[1] p.parse(b) i.Port = append(i.Port, p) } } } func (i *AdxBindVirtualPort) parse(s *string) { for _, v := range bindVePrtRe.FindAllStringSubmatch(*s, -1) { i.Server = append(i.Server, AdxBindServer{Name: v[1], IPAddress: v[2], Port: v[3], State: v[4]}) } }