package parsers import ( "fmt" "regexp" "strings" "github.rackspace.com/gorax/tools/util" ) var ( vlanRe = regexp.MustCompile(`(?i)vlan\s+([0-9]+)\s+name\s+(\S+)\s+by\s+port((.|\n)*?)!`) vlanRouterIntRE = regexp.MustCompile(`(?i)router-interface\s+ve\s+([0-9]+)`) taggedEthRe = regexp.MustCompile(`(?i)tagged\s+ethe\s+([0-9]+)`) unTaggedEthRe = regexp.MustCompile(`(?i)untagged\s+ethe\s+([0-9]+)`) ) type ( // Vlans is the parent type to describe a listing of vlans on an ADX Vlans struct { Vlans []Vlan `json:"vlans"` } // Vlan type contains all the attributes for a Vlan configuration Vlan struct { Vlan int `json:"vlan"` Name string `json:"name"` Interfaces []VlanInt `json:"interfaces"` RouterInterface *int `json:"router_interface"` } // VlanInt type describes all the data about a interface within a Vlan config. VlanInt struct { Tagged bool `json:"tagged"` Eth int `json:"ethernet"` } ) // Parse method is used to implement type population of a given string // using parsers to populate the method receiver. func (v *Vlans) Parse(s string) { for _, ve := range vlanRe.FindAllStringSubmatch(s, -1) { v.Vlans = append(v.Vlans, Vlan{ Vlan: util.Atoi(ve[1]), Name: ve[2], RouterInterface: util.RegexpAtoiPtr(vlanRouterIntRE, ve[3]), Interfaces: func() (el []VlanInt) { for _, l := range strings.Split(ve[3], "\n") { ic := unTaggedEthRe.FindStringSubmatch(l) if len(ic) != 0 { el = append(el, VlanInt{ Eth: util.Atoi(ic[1]), }) continue } ic = taggedEthRe.FindStringSubmatch(l) if len(ic) != 0 { el = append(el, VlanInt{ Eth: util.Atoi(ic[1]), Tagged: true, }) } } return }(), }) } } func (v *VlanInt) Name() *string { n := fmt.Sprintf("ethernet%d", v.Eth) return &n } func (v *VlanInt) Type() string { if v.Tagged { return "trunk" } return "access" }