// 2020 Rackspace, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"): you may // not use this file except in compliance with the License. You may obtain // a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. // Package discover contains the business logic to pull switchport data for a device and // interface with Rackspace systems to add to an environment. package cmdb import ( "fmt" "strconv" "strings" "github.com/sirupsen/logrus" "github.rackspace.com/DCM/discover/common" "github.rackspace.com/DCM/tools" "github.rackspace.com/gorax/clients/mydp" ) // portAssignments method is used to reach out to myadmin and gather switchport location // information (Ether Assignments) func (c *DP) portAssignments(d *Model) error { // Allocate memory to the structs needed d.Environment.Current.SwitchPortsMyDP = make([]mydp.EtherPort, 0) d.Environment.Current.SwitchPorts = make([]string, 0) d.Environment.Current.PortsParsed = make([]tools.PortParsed, 0) d.Environment.Current.DirectPorts = make([]tools.DirectPort, 0) eths := c.DeviceMap[strconv.Itoa(d.Linked.ID)].Eths for i := range eths { // Fetch the device data for the end2 device. return the cached data if its // still in memory. end2Device, err := c.DeviceInfo(eths[i].Device2ID) if err != nil { logrus.Errorf("portAssignments:DeviceByDeviceID:%s:%s", eths[i].Device2ID, err) continue } switch end2Device.SpecificDeviceType { // This is the best way I can figure out if the connected device is a network tap. // I don't know what else to do here. case hubType: // Still add this as a device-to-device mapping, since it is something we need to know about. d.AddDirectPort(eths[i], end2Device) // This prevents things like NGTM's or Log Managers getting the TAP port added to them, since they // are likely going to be connected to the TAPs as well. if strings.EqualFold(d.Device.Device.Type, "server") { continue } if !strings.Contains(strings.ToLower(end2Device.Name), "tap") { continue } // If the connected device is a tap, then get the ethernet connections on the tap, // and append them to the devices connections. This should allow us to treat the // directly connected device as something in the network environment. for _, eth := range end2Device.Eths { end3Device, err := c.DeviceInfo(eth.Device2ID) if err != nil { logrus.Errorf("portAssignments:DeviceByDeviceID:%s:%s", eth.Device2ID, err) // Default to add the port as a switchport if we can't find the End2 Device Info. // Worst case scenario is that is causes errors in network discovery. d.AddNetworkPort(eth, true) continue } if end3Device.SpecificDeviceType == networkSwitchType { d.AddNetworkPort(eth, true) } } case serverType: // Still add this as a device-to-device mapping, since it is something we need to know about. d.AddDirectPort(eths[i], end2Device) if !strings.Contains(strings.ToLower(end2Device.Name), "imperva") && !strings.Contains(strings.ToLower(end2Device.Name), "waf") { continue } // If the connected device is a WAF, then get the ethernet connections on the WAF, // and append them to the devices connections. This should allow us to treat the // directly connected device as something in the network environment. for _, eth := range end2Device.Eths { end3Device, err := c.DeviceInfo(eth.Device2ID) if err != nil { logrus.Errorf("portAssignments:DeviceByDeviceID:%s:%s", eth.Device2ID, err) // Default to add the port as a switchport if we can't find the End2 Device Info. // Worst case scenario is that is causes errors in network discovery. d.AddNetworkPort(eth, true) continue } if end3Device.SpecificDeviceType == networkSwitchType { d.AddNetworkPort(eth, true) } } // If it's a regular switchport, go through the regular AddNetworkPort methods case networkSwitchType: d.AddNetworkPort(eths[i], false) // Finally, if the connection doesn't match any type of network port, then add it as // a direct server-to-server mapping. default: d.AddDirectPort(eths[i], end2Device) } } // Now get the migration ports. In the Datapipe scenario, the current ports will act as migration ports. d.Environment.Migration.PortsParsed = make([]tools.PortParsed, 0) migrations, err := c.DCX.MigrationsByDevice(d.ID) if err != nil { return err } migPorts := migrations.Current if migrations.Migration != nil { if len(migrations.Migration.SwitchPorts) != 0 { migPorts = migrations.Migration } } if migPorts == nil { return nil } dc := "" swPorts, err := c.DCX.SwitchPorts(strconv.Itoa(d.ID)) if err != nil { return err } for _, p := range swPorts.SwitchPorts { _, dc = ParseSwitch(p.SwitchPortData.SwitchName) if dc != "" { break } } d.Environment.Migration.SwitchPorts = migPorts.SwitchPorts d.Environment.Migration.DataCenter = dc d.Environment.Migration.StartingSpace = migPorts.StartingSpace for _, p := range migPorts.ParsedSwitchPorts { d.Environment.Migration.PortsParsed = append(d.Environment.Migration.PortsParsed, tools.PortParsed{ Cab: p.Cab, DataCenter: dc, MigrationPort: false, PortNumber: p.PortNumber, PortType: p.PortType, SwitchName: p.SwitchName, SwitchNum: p.SwitchNum, }) } return nil } // AddNetworkPort will take a devices eth connection and normalize it into something // similar to Rackspace formatting, then append it onto the devices environment ports. func (d *Model) AddNetworkPort(eth mydp.EtherPort, mockPort bool) { eth.End2Port = NormalizeDPPort(eth.End2Port) // Set a Rackspace looking switch port assignment num := strings.Split(eth.End2Port, "/") switchName, dc := ParseSwitch(eth.Device2Name) d.Environment.Current.SwitchPorts = append( d.Environment.Current.SwitchPorts, fmt.Sprintf("%s[%s]", eth.Device2Name, num[len(num)-1]), ) d.Environment.Current.SwitchPortsMyDP = append(d.Environment.Current.SwitchPortsMyDP, eth) d.Environment.Current.PortsParsed = append(d.Environment.Current.PortsParsed, tools.PortParsed{ DataCenter: strings.ToUpper(dc), MockPort: mockPort, PortNumber: common.Atoi(num[len(num)-1]), DevicePort: eth.End1Port, PortName: eth.End2Port, PortType: strings.Title(eth.Class), SwitchName: switchName, SwitchNum: common.Atoi(eth.Device2ID), }) } // AddDirectPort will take a devices eth connection and normalize it into something // similar to Rackspace formatting, then append it onto the devices direct mappings. func (d *Model) AddDirectPort(eth mydp.EtherPort, end2 DeviceInfo) { d.Environment.Current.DirectPorts = append(d.Environment.Current.DirectPorts, tools.DirectPort{ DevicePort: eth.End1Port, DataCenter: "", OtherDevice: tools.OtherDevice{ DevicePort: eth.End2Port, ID: common.Atoi(end2.RSCoreDeviceID), Type: end2.SpecificDeviceType, LinkedID: end2.DeviceID, }, }) } // NormalizeDPPort will try to standardize the short names used in admin.mydp func NormalizeDPPort(portName string) string { if strings.Contains(portName, "GE") { portName = strings.Replace(portName, "GE", "GigabitEthernet", 1) } if strings.Contains(portName, "FE") { portName = strings.Replace(portName, "FE", "FastEthernet", 1) } if strings.Contains(portName, "TE") { portName = strings.Replace(portName, "TE", "TenGigabitEthernet", 1) } // remove leading zeros if strings.Contains(portName, "/0/") { return portName } return strings.Replace(portName, "/0", "/", -1) } // ParseSwitch function will take a full Rackspace switch hostname and // parse it into the switch name and switch DC. func ParseSwitch(s string) (switchName, switchDC string) { // Separate the switch name from the DC split := strings.Split(s, ".") if len(split) == 2 { switchName = strings.ToUpper(split[0]) switchDC = strings.ToUpper(split[1]) return } else if len(split) == 1 { return split[0], "" } return s, "" } // DeviceInfo will fetch a list of ethernet ports from the Device2ID connected device // from the passed in ethernet interface. Basically used to get the details about the // device connected at the other end. func (c *DP) DeviceInfo(deviceID string) (DeviceInfo, error) { c.Lock() defer c.Unlock() // If we have not already gathered information about the switch, hit the admin.mydatapipe api // for that information. if _, ok := c.DeviceMap[deviceID]; ok { return c.DeviceMap[deviceID], nil } logrus.Debugf("Gathering device info for %s", deviceID) // Add blank entries here. This will prevent subsequent requests from tying to // fetch the failed device from myadmin again. devInfo := DeviceInfo{} idInt, err := strconv.Atoi(deviceID) if err != nil { logrus.Errorf("portAssignments:End2Info:Device2ID:%s:%s", deviceID, err) return devInfo, err } d, err := c.MyDP.DeviceByDeviceID(idInt) if err != nil { logrus.Errorf("portAssignments:End2Info:DeviceByDeviceID:%s:%s", deviceID, err) return devInfo, err } devInfo.Device = *d devInfo.Eths, err = c.MyDP.EtherListByDevice(idInt) if err != nil { logrus.Errorf("portAssignments:End2Info:EtherListByDevice:%s:%s", deviceID, err) return devInfo, err } c.DeviceMap[deviceID] = devInfo return c.DeviceMap[deviceID], nil }