// 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 dp import ( "fmt" net2 "net" "regexp" "strconv" "strings" "golang.org/x/text/cases" "golang.org/x/text/language" "github.rackspace.com/DCM/tools" "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.rackspace.com/DCM/discover/common" "github.rackspace.com/gorax/clients/mydp" "github.rackspace.com/gorax/libs/switchlib/swClient" ) const ( onlineComplete = "Online/Complete" cnla = "Deleted" iconURL = "https://28616ea5aab979dce92c-69a5bc1f8c3c585c4f658970ef961bb7.ssl.cf2.rackcdn.com/" ) var ( errNoInvalid = errors.New("an account number or SpecificDevice number needs to be specified") //errInvalidResp = errors.New("invalid response returned from upstream") reNamelessIF = regexp.MustCompile(`^(\d+)\/`) ) type ( // Opt defines the different types of options passed into the collection function Opt func(c *Options) // Options defines the types of Options you can perform with the Collect // function Options struct { AccountID, DCFilter, DeviceID string backupNets []net2.IPNet dracNets []net2.IPNet getPortConfig bool searchLimit int } ) // ByAccount sets the search by account number // nolint:unused func ByAccount(accountID string) Opt { return func(c *Options) { c.AccountID = accountID } } // FilterDC sets the DC filter // nolint:unused func FilterDC(dc string) Opt { return func(c *Options) { c.DCFilter = dc } } // ByDevice sets the search by SpecificDevice number // nolint:unused func ByDevice(deviceID string) Opt { return func(c *Options) { c.DeviceID = deviceID } } // WithBackupNets sets the list of backup nets // nolint:unused func WithBackupNets(nets []net2.IPNet) Opt { return func(c *Options) { c.backupNets = nets } } // WithDracNets sets the list of backup nets // nolint:unused func WithDracNets(nets []net2.IPNet) Opt { return func(c *Options) { c.dracNets = nets } } // WithPortConfig sets the option to get the port configurations // nolint:unused func WithPortConfig() Opt { return func(c *Options) { c.getPortConfig = true } } // WithLimit sets the option to get the port configurations // nolint:unused func WithLimit(b int) Opt { return func(c *Options) { c.searchLimit = b } } // Collect method will gather all SpecificDevice information that is housed in admin.mydatapipe and structure // the data into a format for mongo func (c *Client) Collect(opts ...Opt) ([]Device, error) { result := new(mydp.DeviceList) ret := make([]Device, 0) // Apply default options and generate a new options type. opt := &Options{ getPortConfig: false, searchLimit: 1000, backupNets: make([]net2.IPNet, 0), dracNets: make([]net2.IPNet, 0), } // Apply all options for _, o := range opts { o(opt) } // If a DC filer is specified, we cannot also accept a search limit as the search limit would // request that we do not query admin.mydatapipe for a full set of data, however, we require a full // set of data to know all of the devices in a DC. if opt.DCFilter != "" && opt.searchLimit != 1000 { return ret, fmt.Errorf("cannot use a search limit when filtering for all devices in a DC") } // Pull the devices based on one of the options passed in, if no valid option was passed in // return an error switch { case opt.AccountID != "": accountID, err := strconv.Atoi(opt.AccountID) if err != nil { return ret, fmt.Errorf("[admin.mydatapipe.com] account id is not a valid integer:%s", err) } offset := 0 for { dbc, err := c.dp.DevicesByCustomer(accountID, opt.searchLimit, offset, "DeviceID", "everything") if err != nil { return ret, err } // If there is not a DC filter passed in, then append the entire SpecificDevice list to the // Devices response and break out of the switch. if opt.DCFilter == "" { result.Devices = append(result.Devices, dbc.Devices...) break } // Otherwise, we need to range through the devices and only append on devices // where the DC matches the DC Filter. for _, d := range dbc.Devices { if strings.Contains(strings.ToLower(d.DatacenterTitle), strings.ToLower(opt.DCFilter)) { result.Devices = append(result.Devices, d) } } // If a searchLimit got passed in, and the length of devices is already at the // searchLimit, break out of the loop. if len(result.Devices) >= opt.searchLimit && opt.searchLimit != 1000 { break } // If the row count returned is not the maximum, then we can break out of the // for loop. If it is the maximum, then we need to increment the offset and // iterate again. if dbc.RowCount != "1000" { break } offset += opt.searchLimit } case opt.DeviceID != "": devID, err := strconv.Atoi(opt.DeviceID) if err != nil { return ret, fmt.Errorf("[admin.mydatapipe.com] device id [%s] is not a valid integer:%s", opt.DeviceID, err) } d, err := c.dp.DeviceByDeviceID(devID) if err != nil { return ret, err } // If there is not a DC filter passed in, then append the entire SpecificDevice list to the // Devices response and break out of the switch. if opt.DCFilter != "" && !strings.Contains(strings.ToLower(d.DatacenterTitle), strings.ToLower(opt.DCFilter)) { break } result.Devices = append(result.Devices, *d) default: return ret, errNoInvalid } for _, item := range result.Devices { // 1.) Get that integer for the device ID devID, err := strconv.Atoi(item.DeviceID) if err != nil { logrus.Errorf("DeviceID %s: Collect:range.DeviceID:Atoi:%s", opt.DeviceID, err) continue } // 2.) Create the device type with as much data as we can at this point. If we fail at // any point in the following processes, we will at least have this data on the device. d := Device{ ID: devID, RSID: func() int { if i, err := strconv.Atoi(item.RSCoreDeviceID); err == nil { return i } logrus.Errorf("LinkedID: %s, Collect:range.RSCoreDeviceID:Atoi:%s", item.DeviceID, err) return -1 }(), Type: item.SpecificDeviceType, Account: Account{ Name: item.CustomerName, Status: "Active", TeamName: "Datapipe", Number: func() int { if i, err := strconv.Atoi(item.CustomerID); err == nil { return i } logrus.Errorf("LinkedID: %s, Collect:range.CustomerID:Atoi:%s", item.DeviceID, err) return -1 }(), GeographicLocation: item.LocationTitle, BDC: item.SalesEngineerName, }, Device: SpecificDevice{ ID: item.SpecificDeviceID, Type: item.SpecificDeviceType, DataCenter: item.DatacenterTitle, HostName: item.Name, PrimaryIP: item.PrimaryIP, Status: func() string { if item.Disabled == "1" { return cnla } return onlineComplete }(), }, Environment: tools.EnvironmentData{ Current: tools.Location{ DataCenter: strings.ToUpper(c.m.DataCenter), Cabinet: item.RackTitle, Container: item.RackTitle, Location: item.LocationTitle, }, }, } // prevent null values in DB d.Network.Address.DracIPs = make([]tools.IPAddressDetails, 0) d.Network.Address.MBUIPs = make([]tools.IPAddressDetails, 0) // 3.) Attempt to gather more specific device information querying the specific device type // APIs from admin.mydatapipe if err := c.specificDevice(&d); err != nil { logrus.Errorf("LinkedID: %s, Collect:range:specificDevice:%s", item.DeviceID, err) } // 4.) Try to set a device icon that would align with the Rackspace icon sets. d.Icon = icon(item.SpecificDeviceType, d.Device.MakeModel, d.Device.OS) // 5.) Pull IP assignments to look for backup and OOB IPs; don't abort on error if err := c.ipAddresses(devID, &d, opt.backupNets, opt.dracNets); err != nil { logrus.Errorf("LinkedID: %s, Collect:range:IPsFromDeviceID:%s", item.DeviceID, err) } // 6.) Ignore errors here because we still want the rest of the data if // the port map has issues. if err := c.portAssignments(&d); err != nil { logrus.Errorf("LinkedID: %s, Collect:range.Devices:EtherListByDevice:%s", item.DeviceID, err) } // 7.) If the getPortConfig flag is set, attempt to pull the switchport configurations // into the type. if opt.getPortConfig { d.Environment.Current.SwitchPortConfigs = c.portConfigs(d.Environment.Current.SwitchPortsMyDP) } // remove any relevant null slice values if d.Environment.Current.SwitchPorts == nil { d.Environment.Current.SwitchPorts = make([]string, 0) } if d.Environment.Current.PortsParsed == nil { d.Environment.Current.PortsParsed = make([]tools.PortParsed, 0) } // 8.) Finally, append what we have onto the return structure ret = append(ret, d) } return ret, nil } func (c *Client) ipAddresses(devID int, d *Device, backupNets, dracNets []net2.IPNet) error { ip, err := c.dp.IPFromDeviceID(devID) if err == nil { for _, i := range ip.IP { m := privIP.FindStringSubmatch(i.Address) if len(m) == 3 { ipAddr := m[2] ipDetails := tools.IPAddressDetails{ IPAddress: ipAddr, Netmask: "", Gateway: "", Vlan: 0, Zone: m[1], } m = privIP.FindStringSubmatch(i.IPsSubnet) if len(m) == 3 { _, net, err := net2.ParseCIDR(m[2]) if err == nil && len(net.Mask) == 4 { ipDetails.Netmask = fmt.Sprintf("%d.%d.%d.%d", net.Mask[0], net.Mask[1], net.Mask[2], net.Mask[3]) } } if c.m.IPInSubnets(backupNets, ipAddr) { d.Network.Address.MBUIPs = append(d.Network.Address.MBUIPs, ipDetails) } else if c.m.IPInSubnets(dracNets, ipAddr) { d.Network.Address.DracIPs = append(d.Network.Address.DracIPs, ipDetails) } } } } return err } func (c *Client) portAssignments(d *Device) error { //d.Environment.Current.SwitchPortsMyDP = make([]mydp.EtherPort, 0) d.Environment.Current.SwitchPorts = make([]string, 0) d.Environment.Current.PortsParsed = make([]tools.PortParsed, 0) if !c.m.RackspaceMigrationDC { d.Environment.Migration.SwitchPorts = make([]string, 0) d.Environment.Migration.PortsParsed = make([]tools.PortParsed, 0) } eths, err := c.dp.EtherListByDevice(d.ID) if err != nil { return err } for i := range eths { c.RLock() _, ok := c.eth2Map[eths[i].Device2ID] c.RUnlock() if !ok { id, err := strconv.Atoi(eths[i].Device2ID) if err != nil { logrus.Errorf("portAssignments:Device2ID:%s:%s", eths[i].Device2ID, err) continue } d, err := c.dp.DeviceByDeviceID(id) if err != nil { logrus.Errorf("portAssignments:DeviceByDeviceID:%d:%s", id, err) continue } c.Lock() c.eth2Map[eths[i].Device2ID] = *d c.Unlock() } c.RLock() devType := c.eth2Map[eths[i].Device2ID].SpecificDeviceType c.RUnlock() if devType == networkSwitchType { eths[i].End2Port = portNormalizeForSwitch(eths[i].Device2Name, eths[i].End2Port) eths[i].End2Port = PortNormalize(eths[i].End2Port) switchName := eths[i].Device2Name portType := cases.Title(language.English).String(strings.ToLower(eths[i].Class)) dc := strings.ToUpper(c.m.DataCenter) // Separate the switch name from the DC if strings.HasSuffix(eths[i].Device2Name, fmt.Sprintf(".%s", strings.ToLower(c.m.DataCenter))) { switchName = strings.TrimSuffix(eths[i].Device2Name, fmt.Sprintf(".%s", strings.ToLower(c.m.DataCenter))) } else { split := strings.Split(eths[i].Device2Name, ".") if len(split) == 2 { switchName = strings.ToUpper(split[0]) dc = strings.ToUpper(split[1]) } } portListName := fmt.Sprintf("%s[%s] (%s)", switchName, eths[i].End2Port, portType) // Set a Rackspace looking switch port assignment num := strings.Split(eths[i].End2Port, "/") if c.m.RackspaceMigrationDC || (!c.m.RackspaceMigrationDC && isSourcePort(eths[i], c.m.DataCenter)) { d.Environment.Current.SwitchPorts = append(d.Environment.Current.SwitchPorts, portListName) d.Environment.Current.PortsParsed = append(d.Environment.Current.PortsParsed, tools.PortParsed{ PortNumber: common.Atoi(num[len(num)-1]), PortName: eths[i].End2Port, PortType: portType, DevicePort: strings.ToUpper(eths[i].End1Port), SwitchName: switchName, MigrationPort: false, DataCenter: dc, SwitchNum: common.Atoi(eths[i].Device2ID), }) } else { d.Environment.Migration.DataCenter = dc d.Environment.Migration.SwitchPorts = append(d.Environment.Migration.SwitchPorts, portListName) d.Environment.Migration.PortsParsed = append(d.Environment.Migration.PortsParsed, tools.PortParsed{ PortNumber: common.Atoi(num[len(num)-1]), PortName: eths[i].End2Port, PortType: portType, DevicePort: strings.Replace(strings.ToUpper(eths[i].End1Port), "NEW ", "", 1), SwitchName: switchName, MigrationPort: true, DataCenter: dc, SwitchNum: common.Atoi(eths[i].Device2ID), }) } } } return nil } func (c *Client) portConfigs(eth []mydp.EtherPort) []tools.SwitchPortConfig { swConfigs := make([]tools.SwitchPortConfig, 0) for _, port := range eth { err := c.getUnipedeConfig(port.Device2Name) if err != nil { logrus.Errorf("dpCollect:portConfigs:getUnipedeConfig:%s", err) continue } var rawConfig string p := c.switchMap[port.Device2Name].PortConfigByName(port.End2Port) if p != nil { rawConfig = p.String() } swConfigs = append( swConfigs, tools.SwitchPortConfig{ Name: port.Device2Name, Interface: port.End2Port, RawConfig: rawConfig, }) } return swConfigs } // getUnipedeConfig method will pull the switch's config from unipede func (c *Client) getUnipedeConfig(switchName string) error { if _, ok := c.switchMap[switchName]; ok { return nil } sw, err := swClient.New(c.u, "", switchName) if err != nil { return err } c.Lock() c.switchMap[switchName] = sw c.Unlock() return sw.UnipedeSwitchConfig(nil) } // PortNormalize will try to standardize the short names used in admin.mydp func PortNormalize(portName string) string { portName = strings.Replace(portName, "GE", "GigabitEthernet", 1) portName = strings.Replace(portName, "FE", "FastEthernet", 1) portName = strings.Replace(portName, "TE", "TenGigabitEthernet", 1) // remove leading zeros return strings.Replace(portName, "/0", "/", -1) } func portNormalizeForSwitch(switchName, portName string) string { switch strings.ToLower(switchName) { case "access1-3.nyc1": m := reNamelessIF.FindStringSubmatch(portName) if len(m) == 2 { num, err := strconv.Atoi(m[1]) if err != nil { return portName } if num < 8 { return "FastEthernet" + portName } return "GigabitEthernet" + portName } case "backup2.nyc1": if reNamelessIF.MatchString(portName) { return "GigabitEthernet" + portName } } return portName } // icon function does some dirty logic to get the correct icon depending on the // SpecificDevice type, hardware platform and running OS. func icon(specificType, hardware, os string) string { switch specificType { case firewallType: return iconURL + "asalogo.gif" case serverType: hw := strings.ToLower(hardware) switch { case strings.Contains(hw, "webmux"): return iconURL + "load_balancer.gif" case strings.Contains(hw, "dell poweredge"): if strings.Contains(strings.ToLower(os), "windows") { return iconURL + "dell_win.gif" } return iconURL + "dell_linux.gif" case strings.Contains(hw, "hp proliant") || strings.Contains(hw, "hp dl"): if strings.Contains(strings.ToLower(os), "windows") { return iconURL + "hp_win.gif" } return iconURL + "hp_linux.gif" default: return iconURL + "unknown.gif" } case netscalerType, serverIronType, f5Type: return iconURL + "load_balancer.gif" case storageArrayType: return iconURL + "managed_storage.gif" case networkSwitchType: return iconURL + "router.gif" default: return iconURL + "unknown.gif" } } //nolint:gosimple func isSourcePort(port mydp.EtherPort, sourceDC string) bool { // if switch dc name matches source DC, then true if strings.HasSuffix(strings.ToLower(port.Device2Name), fmt.Sprintf(".%s", strings.ToLower(sourceDC))) { return true } // assume switches without a <.dc> are source ports split := strings.Split(port.Device2Name, ".") if len(split) < 2 { return true } // assume migration if device port name contains 'NEW ' if strings.Contains(strings.ToUpper(port.End1Port), "NEW ") { return false } return false }