package pkg import ( "regexp" "github.rackspace.com/gorax/tools/util" ) var ( NETSELFRE = regexp.MustCompile(`(?i)net\s+self\s+(\S+)\s+{((.|\n)*?)\n}`) VLANNAMERE = regexp.MustCompile(`(?i)vlan\s+(\S+)`) UNITNUMRE = regexp.MustCompile(`(?i)unit\s+([0-9]+)`) FLOATINGRE = regexp.MustCompile(`(?i)floating\s+(\S+)`) //ALLOWSRVCDEFAULTRE = regexp.MustCompile(`(?i)allow-service\s+(default)`) ALLOWSRVCRE = regexp.MustCompile(`(?i)allow-service\s+{((.|\n)*?)}`) ROUTEDOMNUMRE = regexp.MustCompile(`%([0-9]+)`) ) type ( SelfIP struct { Name string `json:"name"` Address string `json:"address"` AllowedService *AllowedServices `json:"allowed_services"` Vlan *string `json:"vlan"` Floating bool `json:"floating"` Unit *int `json:"unit"` RouteDomain *int `json:"route_domain"` } AllowedServices struct { Enabled bool `json:"enabled"` Default bool `json:"default"` Services []*ServicePort `json:"services"` } ServicePort struct { Proto string `json:"proto"` Port string `json:"port"` } ) func ParseSelfIP(as bool, r []string) *SelfIP { return &SelfIP{ Address: func() string { addr := util.RegexpString(addressRegex, r[2]) if addr != "" { return addr } return r[1] }(), Vlan: util.RegexpFindStringPtr(VLANNAMERE, r[2]), Unit: util.RegexpAtoiPtr(UNITNUMRE, r[2]), Floating: FLOATINGRE.MatchString(r[2]), AllowedService: &AllowedServices{ Enabled: as, Default: as, Services: func() (sp []*ServicePort) { if as { return } for _, r := range ALLOWSRVCRE.FindAllStringSubmatch(r[2], -1) { sp = append(sp, &ServicePort{ Proto: r[1], Port: r[2], }) } return }(), }, RouteDomain: util.RegexpAtoiPtr(ROUTEDOMNUMRE, r[1]), } } func ParseSelfIPs(s string) ([]*SelfIP, error) { var i []*SelfIP for _, r := range NETSELFRE.FindAllStringSubmatch(s, -1) { as := ALLOWSRVCRE.MatchString(r[2]) l := ParseSelfIP(as, r) l.Name = r[1] i = append(i, l) } return i, nil } func FindSelfIPVlan(vlan string, si []*SelfIP) (string, *SelfIP) { for _, s := range si { if s.Vlan != nil { if *s.Vlan == vlan { return s.Name, s } } } return "", nil }