package unipede import ( "log" "net/http" "net/url" "strings" "github.rackspace.com/gorax/tools/util" ) type ( UpInterfaceAssignments struct { Count int `json:"count" bson:"count"` Items []*UpInterfaceAssignment `json:"items" bson:"items"` Links *map[string]Links `json:"links" bson:"links"` } UpInterfaceAssignment struct { Id int `json:"id" bson:"id"` UpDeviceId int `json:"up_device_id" bson:"up_device_id"` FePort int `json:"fe_port" bson:"fe_port"` CoreAccount *int `json:"core_account" bson:"core_account"` CoreDevice *int `json:"core_device" bson:"core_device"` Created string `json:"created" bson:"created"` Updated string `json:"updated" bson:"updated"` } UpInterfaces struct { Count int `json:"count" bson:"count"` Items []*UpInterface `json:"items" bson:"items"` Links *map[string]Links `json:"links" bson:"links"` } UpInterface struct { Id int `json:"id" bson:"id"` Name string `json:"name" bson:"name"` Description string `json:"description" bson:"description"` FeAggTo *int `json:"fe_aggregated_to" bson:"fe_aggregated_to"` FePort *int `json:"fe_port" bson:"fe_port"` FeInterface int `json:"fe_interface" bson:"fe_interface"` Link string `json:"link" bson:"link"` Speed string `json:"speed" bson:"speed"` Duplex string `json:"duplex" bson:"duplex"` Admin string `json:"admin" bson:"admin"` State string `json:"state" bson:"state"` Trunking bool `json:"trunking" bson:"trunking"` Created string `json:"created" bson:"created"` Updated string `json:"updated" bson:"updated"` UpDeviceId int `json:"up_device_id" bson:"up_device_id"` } InterfaceProperties struct { Count int `json:"count" bson:"count"` Items []InterfaceProperty `json:"items" bson:"items"` } InterfaceDataProperties struct { Count int `json:"count" bson:"count"` Data []InterfaceProperty `json:"data" bson:"data"` } InterfaceProperty struct { AccessVlan int `json:"access_vlan" bson:"access_vlan"` Admin string `json:"admin" bson:"admin"` Bia int `json:"bia" bson:"bia"` ConfiguredDuplex string `json:"configured_duplex" bson:"configured_duplex"` ConfiguredSpeed string `json:"configured_speed" bson:"configured_speed"` CoreAccount int `json:"core_account" bson:"core_account"` CoreDevice int `json:"core_device" bson:"core_device"` Created string `json:"created" bson:"created"` Description string `json:"description" bson:"description"` Direction string `json:"direction" bson:"direction"` Duplex string `json:"duplex" bson:"duplex"` EEE bool `json:"eee" bson:"eee"` FEAggregatedTo int `json:"fe_aggregated_to" bson:"fe_aggregated_to"` FEInterface int `json:"fe_interface" bson:"fe_interface"` FEPort int `json:"fe_port" bson:"fe_port"` HasConnector bool `json:"has_connector" bson:"has_connector"` ID int `json:"id" bson:"id"` Link string `json:"link" bson:"link"` Mode string `json:"mode" bson:"mode"` Name string `json:"name" bson:"name"` NativeVlan int `json:"native_vlan" bson:"native_vlan"` PortSecurity bool `json:"port_security" bson:"port_security"` PortType string `json:"port_type" bson:"port_type"` PortFast bool `json:"portfast" bson:"portfast"` PortFastTrunk bool `json:"portfast_trunk" bson:"portfast_trunk"` Protocol string `json:"protocol" bson:"protocol"` RemoteDeviceID int `json:"remote_device_id" bson:"remote_device_id"` RemoteHostname string `json:"remote_hostname" bson:"remote_hostname"` RemoteInterface string `json:"remote_interface" bson:"remote_interface"` Shutdown bool `json:"shutdown" bson:"shutdown"` Speed string `json:"speed" bson:"speed"` State string `json:"state" bson:"state"` Status string `json:"status" bson:"status"` Trunking bool `json:"trunking" bson:"trunking"` UpDeviceID int `json:"up_device_id" bson:"up_device_id"` Updated string `json:"updated" bson:"updated"` } UpInterfaceView struct { Description string `json:"description" bson:"description"` FEInterface int `json:"fe_interface" bson:"fe_interface"` FEAggregatedTo int `json:"fe_aggregated_to" bson:"fe_aggregated_to"` Duplex string `json:"duplex" bson:"duplex"` Link string `json:"link" bson:"link"` Admin string `json:"admin" bson:"admin"` HasConnector bool `json:"has_connector" bson:"has_connector"` Mode string `json:"mode" bson:"mode"` Speed string `json:"speed" bson:"speed"` CoreAccount int `json:"core_account" bson:"core_account"` CoreDevice int `json:"core_device" bson:"core_device"` PortType string `json:"port_type" bson:"port_type"` PortFast bool `json:"portfast" bson:"portfast"` PortFastTrunk bool `json:"portfast_trunk" bson:"portfast_trunk"` AccessVlan int `json:"access_vlan" bson:"access_vlan"` ConfiguredDuplex string `json:"configured_duplex" bson:"configured_duplex"` ConfiguredSpeed string `json:"configured_speed" bson:"configured_speed"` Shutdown bool `json:"shutdown" bson:"shutdown"` Trunking bool `json:"trunking" bson:"trunking"` PortSecurity bool `json:"port_security" bson:"port_security"` UpDeviceID int `json:"up_device_id" bson:"up_device_id"` FEPort int `json:"fe_port" bson:"fe_port"` NativeVlan int `json:"native_vlan" bson:"native_vlan"` Name string `json:"name" bson:"name"` UpNeighborID int `json:"up_neighbor_id" bson:"up_neighbor_id"` } UpInterfacesMerge struct { UpInterfaceMerge []*UpInterfaceMerge `json:"up_interface_merge" bson:"up_interface_merge"` } UpInterfaceMerge struct { Name string `json:"name" bson:"name"` Description string `json:"description" bson:"description"` FePort int `json:"fe_port" bson:"fe_port"` FeInt int `json:"fe_interface" bson:"fe_interface"` UpDeviceId int `json:"up_device_id" bson:"up_device_id"` Link string `json:"link" bson:"link"` Speed string `json:"speed" bson:"speed"` Duplex string `json:"duplex" bson:"duplex"` Admin string `json:"admin" bson:"admin"` FeAggTo *int `json:"fe_aggregated_to" bson:"fe_aggregated_to"` CoreAccount *int `json:"core_account" bson:"core_account"` CoreDevice *int `json:"core_device" bson:"core_device"` } ) func (u *UpInterface) PortNum() int { if u.FePort != nil { return *u.FePort } return -1 } func (u *Client) UpInterfacesMerge(switchName string, refresh *int, options ...*UnipedeOptions) (*UpInterfacesMerge, error) { ui, err := u.UpInterfaces(switchName, refresh, options...) if err != nil { return nil, err } ua, err := u.UpInterfaceAssignment(switchName, refresh, options...) if err != nil { return nil, err } um := new(UpInterfacesMerge) for _, iu := range ui.Items { if iu.FePort != nil { ia := ua.FePortFind(*iu.FePort) um.MergePorts(ia, iu) } } return um, nil } func (u *UpInterfacesMerge) MergePorts(ia *UpInterfaceAssignment, iu *UpInterface) { u.UpInterfaceMerge = append(u.UpInterfaceMerge, &UpInterfaceMerge{ Name: iu.Name, Description: iu.Description, FePort: *iu.FePort, FeInt: iu.FeInterface, UpDeviceId: iu.UpDeviceId, FeAggTo: iu.FeAggTo, CoreAccount: func() *int { if ia != nil { return ia.CoreAccount } return nil }(), CoreDevice: func() *int { if ia != nil { return ia.CoreDevice } return nil }(), Speed: iu.Speed, Duplex: iu.Duplex, Admin: iu.Admin, }) } func (u *UpInterfacesMerge) PortByName(port string) *UpInterfaceMerge { port = strings.ToLower(port) for _, Port := range u.UpInterfaceMerge { if Port == nil { continue } if port == strings.ToLower(Port.Name) { return Port } } return nil } func (u *UpInterfacesMerge) PortByNum(port int) *UpInterfaceMerge { for _, Port := range u.UpInterfaceMerge { if Port == nil { continue } if port == Port.FePort { return Port } } return nil } func (u *UpInterfaceView) PortByNum(port int) UpInterfaceView { if port == u.FEPort { return *u } return UpInterfaceView{} } func (u *Client) UpInterfaces(switchName string, refresh *int, options ...*UnipedeOptions) (*UpInterfaces, error) { o := processOptions(refresh, nil, options...) req, err := u.newRequest(unipedeURL+"devices/"+switchName+"/UpInterface::Interface", http.MethodGet, o, nil) if err != nil { return nil, err } ui := new(UpInterfaces) _, err = u.do(req, o, ui) return ui, err } func (u *Client) UpInterfaceAssignment(switchName string, refresh *int, options ...*UnipedeOptions) (*UpInterfaceAssignments, error) { o := processOptions(refresh, nil, options...) req, err := u.newRequest(unipedeURL+"devices/"+switchName+"/UpInterface::Assignment", http.MethodGet, o, nil) if err != nil { return nil, err } ui := new(UpInterfaceAssignments) _, err = u.do(req, o, ui) return ui, err } func (u *Client) UpInterfaceDeviceView(params url.Values, switchName string, refresh *int, options ...*UnipedeOptions) (InterfaceProperties, error) { view := new(InterfaceProperties) o := processOptions(refresh, ¶ms, options...) req, err := u.newRequest(unipedeURL+"devices/"+switchName+"/interface_view", http.MethodGet, o, nil) if err != nil { return *view, err } _, err = u.do(req, o, view) return *view, err } func (u *Client) UpInterfaceView(params url.Values, refresh *int, options ...*UnipedeOptions) (InterfaceDataProperties, error) { view := new(InterfaceDataProperties) o := processOptions(refresh, ¶ms, options...) req, err := u.newRequest(unipedeURL+"property/interface_view", http.MethodGet, o, nil) if err != nil { return *view, err } _, err = u.do(req, o, view) return *view, err } func (u *UpInterfaceAssignments) DevicesByAccount() map[int][]*UpInterfaceAssignment { acc := make(map[int][]*UpInterfaceAssignment) for _, i := range u.Items { acc[*i.CoreAccount] = append(acc[*i.CoreDevice], i) } return acc } func UpIntDeviceList(acc []*UpInterfaceAssignment) (dl []int) { for _, i := range acc { dl = append(dl, *i.CoreDevice) } return util.UniqueInts(dl) } func (u *UpInterfaceAssignments) PortByNum(port int) *UpInterfaceAssignment { for _, Port := range u.Items { if port == Port.FePort { return Port } } return nil } func (u *UpInterfaceAssignments) FePortFind(port int) *UpInterfaceAssignment { return u.PortByNum(port) } func (u *UpInterfaces) PortByName(port string) *UpInterface { port = strings.ToLower(port) for _, Port := range u.Items { log.Println(port, Port.Name) if port == strings.ToLower(Port.Name) { return Port } } return nil } func (u *UpInterfaces) PortByNum(port int) *UpInterface { for _, Port := range u.Items { if Port.FePort != nil { if port == *Port.FePort { return Port } } } return nil }