package dcxSwitchapi import ( "encoding/json" "net" ) type ( // GetRoutes structure is the return value for the GET /routes resource GetRoutes struct { Items []struct { Destination string `json:"destination"` NextHop string `json:"nexthop"` Preference int `json:"preference,omitempty"` Vrf string `json:"vrf"` Type string `json:"type"` } `json:"items"` } // Route struct is the data defined in the route/delta calls to create/delete // routes from a switch Route struct { Destination string `json:"destination"` NextHop net.IP `json:"nexthop"` VRF string `json:"vrf,omitempty"` } routeDelete struct { Delete []Route `json:"delete"` } ) // GetRoutes method takes a switch argument and returns a struct of routes defined on the switch func (s *Client) GetRoutes(sw string) (routes GetRoutes, err error) { body, err := s.getv2(sw + "/routes") if err != nil { return } if err = json.Unmarshal(*body, &routes); err != nil { return } return } // CreateRoutes method will call the deltas endpoint in switchapi to create a list of routes // on a given switch. Response from switch api will be a 204 no content or error, this // method will only return an error func (s *Client) CreateRoutes(sw string, routes []Route) error { request := struct { Create []Route `json:"create"` }{Create: routes} payload, err := json.Marshal(&request) if err != nil { return err } _, err = s.postv2(sw+"/routes/delta", payload) if err != nil { return err } return nil } // DeleteRoutes method will call the deltas endpoint in switchapi to delete a list of routes // on a given switch. Response from switch api will be a 204 no content or error, this // method will only return an error func (s *Client) DeleteRoutes(sw string, routes []Route) error { request := struct { Delete []Route `json:"delete"` }{Delete: routes} payload, err := json.Marshal(&request) if err != nil { return err } _, err = s.postv2(sw+"/routes/delta", payload) if err != nil { return err } return nil }