package unipede import ( "errors" "fmt" "net" "net/http" "github.rackspace.com/gorax/tools/util" ) type ( UpStaticRoute struct { Count int `json:"count" bson:"count"` Data []*StaticRouteData `json:"data" bson:"data"` } StaticRouteData struct { Chassis string `json:"chassis_model" bson:"chassis_model"` CIDR int `json:"cidr" bson:"cidr"` CoreId int `json:"core_id" bson:"core_id"` HostName string `json:"hostname" bson:"hostname"` ID int ManagementIP int64 `json:"management_ip" bson:"management_ip"` NetworkFirstAddr int64 `json:"network_first_addr" bson:"network_first_addr"` NetowrkLastAddr int64 `json:"network_last_addr" bson:"network_last_addr"` NextHop int64 `json:"nexthop_addr" bson:"nexthop_addr"` OsVersion string `json:"os_version" bson:"os_version"` PathNorth string `json:"path_north" bson:"path_north"` UpDevice int `json:"up_device_id" bson:"up_device_id"` } ) //Function to search unipede for next hop func (u *Client) UpRouteStatic(ip string, options ...*UnipedeOptions) (*UpStaticRoute, error) { ipaddr := net.ParseIP(ip) if ipaddr == nil { return nil, errors.New("The IP address is not valid.") } o := processOptions(nil, nil, options...) o.SetFilter(fmt.Sprintf("nexthop_addr:%d", util.InetAton(ipaddr))) req, err := u.newRequest(unipedeURL+"device_property/UpRoute::StaticRoute::Model", http.MethodGet, o, nil) if err != nil { return nil, err } sr := new(UpStaticRoute) _, err = u.do(req, o, sr) return sr, err } // method to return a list of unique next host hosts func (u *UpStaticRoute) AggrList() (aggrs []string) { for _, s := range u.Data { if !util.StrInSlice(s.HostName, aggrs) { aggrs = append(aggrs, s.HostName) } } return } // method to return a list of net.IPNet objects func (u *UpStaticRoute) RouteNetList() (routes []*net.IPNet) { for _, c := range u.RouteList() { if _, ipn, err := net.ParseCIDR(c); err == nil { routes = append(routes, ipn) } } return } // method to return a list of cidr strings func (u *UpStaticRoute) RouteList() (rl []string) { for _, s := range u.Data { rl = append(rl, fmt.Sprintf("%s/%d", util.InetNtoa(s.NetworkFirstAddr), s.CIDR)) } return }