package dcxSwitchapi import ( "encoding/json" "strings" ) type ( SwitchApiDynRoutes struct { Items []*SwitchApiDynRoute `json:"items"` Links json.RawMessage `json:"links"` // Links SwitchApiDynLinks `json:"links"` } SwitchApiDynRoute struct { Destination string `json:"destination"` Distance json.RawMessage `json:"distance"` KnownVia json.RawMessage `json:"known_via"` LastUpdate string `json:"last_update"` LastUpdateFrom string `json:"last_update_from"` Links json.RawMessage `json:"links"` // Links SwitchApiDynLink `json:"links"` Metric json.RawMessage `json:"metric"` NextHop string `json:"nexthop"` Proto json.RawMessage `json:"Protocol"` } SwitchApiDynLink struct { Self SwitchApiLink `json:"self"` } SwitchApiDynLinks struct { Self SwitchApiLink `json:"self"` Switch SwitchApiLink `json:"switch"` } SwitchApiDynEmptyRoutes struct { Items []*SwitchApiEmptyDynRoute `json:"items"` Links json.RawMessage `json:"links"` // Links SwitchApiDynLinks `json:"links"` } SwitchApiEmptyDynRoute struct { Destination string `json:"destination"` Distance json.RawMessage `json:"distance"` KnownVia json.RawMessage `json:"known_via"` LastUpdate json.RawMessage `json:"last_update"` LastUpdateFrom json.RawMessage `json:"last_update_from"` Links json.RawMessage `json:"links"` // Links SwitchApiDynLink `json:"links"` Metric json.RawMessage `json:"metric"` NextHop []string `json:"nexthop"` Proto json.RawMessage `json:"Protocol"` } ) func (s *Client) DynRoutes(dc, ip string) (*SwitchApiDynRoutes, error) { dc = strings.ToLower(dc) body, err := s.get("route-views1." + dc + "/dynroutes/forip/" + ip) if err != nil { return nil, err } var sr SwitchApiDynRoutes if err = json.Unmarshal(*body, &sr); err == nil { return &sr, nil } var ed SwitchApiDynEmptyRoutes if err = json.Unmarshal(*body, &ed); err != nil { return nil, err } return &SwitchApiDynRoutes{ Links: ed.Links, Items: func() (i []*SwitchApiDynRoute) { for _, r := range ed.Items { i = append(i, &SwitchApiDynRoute{ Destination: r.Destination, }) } return }(), }, nil } func (s *SwitchApiDynRoutes) MatchDestination(block string) *SwitchApiDynRoute { for _, r := range s.Items { if r.Destination == block { return r } } return nil }