package parsers import ( "regexp" "strings" "github.rackspace.com/gorax/tools/util" ) var ( routeConfRe = regexp.MustCompile(`(?i)ip\s+route\s+(\S+)\s+(\S+)\s+(\S+)`) routeShowRe = regexp.MustCompile(`(?i)([0-9]+)\s+([0-9]\S+)\s+([0-9]\S+)\s+([0-9]\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)`) ) type ( // RoutesConfig is an aggregate type to define all the routes // within an ADX configuration RoutesConfig struct { Routes []RouteConfig `json:"route_configuration"` } // RouteConfig is the parsed items of a single route in the ADX configuration. RouteConfig struct { Network string `json:"network"` Netmask string `json:"netmask"` NextHop string `json:"next_hop"` } // RouteTable is an aggregate type to define all the routing // table entries with an ADXs routing table RouteTable struct { Routes []RouteEntry `json:"route_table"` } // RouteEntry is the parsed items within a single routing-table entry RouteEntry struct { Order int `json:"order"` Destination string `json:"destination"` Netmask string `json:"netmask"` Gateway string `json:"gateway"` Port string `josn:"port"` Cost string `json:"cost"` Type string `json:"type"` Uptime string `json:"uptime"` } ) // Parse method is used to implement type population of a given string // using parsers to populate the method receiver. func (R *RoutesConfig) Parse(s *string) { for _, r := range strings.Split(*s, "\n") { re := routeConfRe.FindStringSubmatch(r) if len(re) != 0 { R.Routes = append(R.Routes, RouteConfig{ Network: re[1], Netmask: re[2], NextHop: re[3], }) } } } // Parse method is used to implement type population of a given string // using parsers to populate the method receiver. func (R *RouteTable) Parse(s *string) { for _, r := range strings.Split(*s, "\n") { re := routeShowRe.FindStringSubmatch(r) if len(re) != 0 { R.Routes = append(R.Routes, RouteEntry{ Order: util.Atoi(re[1]), Destination: re[2], Netmask: re[3], Gateway: re[4], Port: re[5], Cost: re[6], Type: re[7], Uptime: re[8], }) } } } // RouteType method is an action against a RouteEntry that // will determine if the route is connected or static func (r *RouteEntry) RouteType() string { if strings.HasPrefix(r.Type, "D") { return "connected" } if strings.HasPrefix(r.Type, "S") { return "static" } return "" } // NextHop method will return the next-hop IP address // of the routing entry's method receiver func (r *RouteEntry) NextHop() string { switch r.RouteType() { case "connected": return "self" case "static": return r.Gateway } return "" }