package pkg import ( "regexp" "strings" "github.rackspace.com/gorax/tools/util" ) type Virtual struct { Name string `json:"name"` IPAddress string `json:"address"` Port string `json:"port"` Protocol string `json:"ip-protocol"` NetMask string `json:"netmask"` Pool string `json:"pool"` Partition string `json:"partition"` Persist string `json:"persist"` Profiles []string `json:"profiles"` Rules []string `json:"rules"` Source string `json:"source"` Mirror bool `json:"mirror"` IPForward bool `json:"ip-forward"` TranslateAddress bool `json:"translate-address"` TranslatePort bool `json:"translate-port"` } func ParseVirtuals(s string) ([]*Virtual, error) { var v []*Virtual var LTMVIRTUALALLRE = regexp.MustCompile(`ltm\s+virtual\s+(\S+)\s+{((.|\n)*?\n)}`) var VIRTUALIPADDRRE = regexp.MustCompile(`\s+destination\s+(\S+?):\S+`) var VIRTUALPORTRE = regexp.MustCompile(`\s+destination\s+\S+?:(\S+)`) var VIRTUALPROTORE = regexp.MustCompile(`\s+ip[ -]protocol\s+(\S+)\s*\n`) var VIRTUALNETMASKRE = regexp.MustCompile(`\s+mask\s+(\S+)\s*\n`) // 4 spaces before pool to prevent matching on snat pool var VIRTUALPOOLRE = regexp.MustCompile(`\n\s{4}pool\s+(\S+)\s*\n`) var VIRTUALPARTITIONRE = regexp.MustCompile(`\s+partition\s+(\S+)\s*\n`) var VIRTUALPERSISTRE = regexp.MustCompile(`\s+persist\s+{\s+(\S+)\s+{`) var VIRTUALPROFILESRE = regexp.MustCompile(`\s+profiles\s+{((.|\n)*?}\s+)}`) var VIRTUALPROFILERE = regexp.MustCompile(`\s*(\S+)\s+{(?:.|\n)+?}`) var VIRTUALRULESRE = regexp.MustCompile(`\s+rules\s+{((.|\n)*?)}`) var VIRTUALSOURCERE = regexp.MustCompile(`\s+source\s+(\S+)`) for _, x := range LTMVIRTUALALLRE.FindAllStringSubmatch(s, -1) { v = append(v, &Virtual{ Name: x[1], IPAddress: util.RegexpFindString(VIRTUALIPADDRRE, x[2]), Port: util.RegexpFindString(VIRTUALPORTRE, x[2]), Protocol: util.RegexpFindString(VIRTUALPROTORE, x[2]), NetMask: util.RegexpFindString(VIRTUALNETMASKRE, x[2]), Pool: util.RegexpFindString(VIRTUALPOOLRE, x[2]), Partition: util.RegexpFindString(VIRTUALPARTITIONRE, x[2]), Persist: util.RegexpFindString(VIRTUALPERSISTRE, x[2]), Profiles: func() (profiles []string) { y := util.RegexpFindString(VIRTUALPROFILESRE, x[2]) if y == "" { return nil } for _, profile := range VIRTUALPROFILERE.FindAllStringSubmatch(y, -1) { profiles = append(profiles, profile[1]) } return }(), Rules: func() (rules []string) { y := util.RegexpFindStringPtr(VIRTUALRULESRE, x[2]) if y == nil { return nil } for _, r := range strings.Split(*y, "\n") { if regexp.MustCompile(`^\s*$`).Match([]byte(r)) { continue } rule := strings.Trim(r, " ") rules = append(rules, rule) } return }(), Source: util.RegexpFindString(VIRTUALSOURCERE, x[2]), Mirror: strings.Contains(x[2], "mirror enabled"), IPForward: strings.Contains(x[2], "ip-forward"), TranslateAddress: strings.Contains(x[2], "translate-address enabled"), TranslatePort: strings.Contains(x[2], "translate-port enabled"), }) } return v, nil }