// 2019 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 publicxlate serves as a helper to generate the SVI bridges and swings needed for a // migration. package publicxlate import ( "bytes" "encoding/json" "fmt" "os" "regexp" "strings" "github.com/jedib0t/go-pretty/v6/table" "github.com/sirupsen/logrus" "github.rackspace.com/DCM/gomigrate/shared" "github.rackspace.com/DCM/tools" "github.rackspace.com/gorax/clients/dcxAutohost" "github.rackspace.com/gorax/clients/dcxSwitchapi" "github.rackspace.com/gorax/clients/mongodbAPI" "github.rackspace.com/gorax/clients/unipede" "github.rackspace.com/gorax/gokeys" ) var ( // tmpes is a mapping of datacenter to tmpe switch name. The // datacenter is derived from the move-group name. tmpes = map[string]string{ "ord1": "tmpe1.ord1", "hkg1": "tmpe1.hkg5", } // polledSwitch is a marked to help us determine if a switch // has already been polled for uplinked neighbors polledSwitch = make(map[string]bool) // zoneDistRE = regexp.MustCompile(`(?i)(dist\d+)-\d+.*`) ) type ( // Client contains all the pkg clients and variables that are // needed throughout multiple methods within this package. Client struct { dcx *dcxAutohost.Client uni *unipede.Client swAPI *dcxSwitchapi.Client mgo *mongodbAPI.Client coreAuth gokeys.Credential raxAuth gokeys.Credential ssoAuth gokeys.Credential zoneSVI map[string][]unipede.UpConnectedRouteItem } // zoneInfo type represents the migration vlan, type and note data for // a given network public zone. zoneInfo struct { tools.VlanTranslation MoveGroup string Type string Notes string } // zoneMap type is the representation of the response from the migration/ZoneMapping collections zoneMap struct { ID string `json:"_id"` MigZone string `json:"migration_zone"` SrcVlan int `json:"source_vlan"` SrcZone string `json:"source_zone"` } ) // initClient function will return the client initializations that are // required for this package. If we encounter an error here, we should // exit the program as we cannot continue without this data. func initClient(cfg gokeys.Settings) Client { // Pull a core token from the users gokeys keychain cKey, err := cfg.Core(false) if err != nil { logrus.Fatal(err) } // Pull a Rackspace Identity token from the users gokeys keychain iKey, err := cfg.Identity(false) if err != nil { logrus.Fatal(err) } // Pull a SSO info from the users gokeys keychain sKey, err := cfg.SSO() if err != nil { logrus.Fatal(err) } // Initialize the unipede client uni, err := unipede.New(sKey.Username, sKey.Password, "") if err != nil { logrus.Fatal(err) } // Initialize the DCX API client dcx, err := dcxAutohost.NewWithToken(iKey.Password, false) if err != nil { logrus.Fatal(err) } // Initialize the DCX FireEngine client swAPI, err := dcxSwitchapi.NewClient(sKey.Username, sKey.Password) if err != nil { logrus.Fatal(err) } m := mongodbAPI.New(mongodbAPI.WithToken(cKey.Password), "migration", false) return Client{ dcx: dcx, uni: uni, swAPI: swAPI, mgo: m, coreAuth: cKey, raxAuth: iKey, ssoAuth: sKey, zoneSVI: make(map[string][]unipede.UpConnectedRouteItem), } } // pluginFunc function will prompt for a list of move-groups, then query the migration database // along with the zone-mapping collection, for a listing of public vlans for the specified move-groups. // The return data will be a type that contains the public zone:vlan translations. func pluginFunc(cfg gokeys.Settings) { c := initClient(cfg) moveGroupList := *argM if len(moveGroupList) == 0 { logrus.Fatalf("no move-groups found in %v", moveGroupList) } // Determine the mongo collection we should be querying based on the move-group name. collection := shared.GetCollection(moveGroupList[0]) dc := shared.GetDCFromMoveGroup(moveGroupList[0]) if collection == "" || dc == "" { logrus.Fatalf("unable to parse collection or dc name from %s", moveGroupList[0]) } logrus.Debug("getting move-group devices from the MigrationDB") devices, err := c.MoveGroupDevices(moveGroupList, collection) if err != nil { logrus.Fatal(err) } logrus.Debugf("Found %d devices from %v in the MigrationDB", len(devices), moveGroupList) var bridgeDomains shared.BridgeGroup if val, ok := tmpes[dc]; ok { logrus.Debugf("pulling bridge-domains from %s", val) bridgeDomains, err = c.BridgeDomains(val) if err != nil { logrus.Errorf("Unable to find bridge-domains from %s. Check results for already bridged vlans!!\n - %s", tmpes[dc], err) } } // Gather a list of the source zones that probably need a translation xlates := c.translations(devices) // Range though the devices and see if we find any zones that // do not exist in the translations we just built. var noXlate = make([]string, 0) NoXlateLoop: for _, d := range devices { if d.Network.Public.Zone == "" { continue } pubZone := PublicZoneForDevice(d) if _, exist := xlates[pubZone]; !exist { for _, x := range noXlate { if pubZone == x { continue NoXlateLoop } } noXlate = append(noXlate, pubZone) } } bridgesToAdd := make([]zoneInfo, 0) for zone, xlate := range xlates { mgs, err := c.MoveGroupsForZone(zone, collection) if err != nil { logrus.Error(err) continue } xlate.Notes = strings.Join(mgs, ",") if _, exist := bridgeDomains[zone]; exist { xlate.Type = "Already Bridged" xlate.Notes = bridgeDomains[zone].BridgeID xlates[zone] = xlate continue } if len(mgs) > 1 { bridgesToAdd = append(bridgesToAdd, xlate) xlate.Type = "NEW Bridge" } xlates[zone] = xlate } logrus.Debugf("getting info from the %sZoneMappings collection", collection) zoneMaps, err := c.zoneMappings(collection) if err != nil { logrus.Fatal(err) } prettyPrint(xlates, zoneMaps, noXlate) if *argZM && len(bridgesToAdd) != 0 { c.AddBridgesToExclusionDB(bridgesToAdd) } } // prettyPrint func will take a structured type of response data and render a table // output to stdout. func prettyPrint(xlates map[string]zoneInfo, zoneMaps []zoneMap, noXlate []string) { //var r = strings.NewReplacer( // fmt.Sprintf("%s:public:", migrDC), "", // fmt.Sprintf("%s:publicnet:", migrDC), "", // "zone", "aggr", // "omega", "internal", //) t := table.NewWriter() t.SetOutputMirror(os.Stdout) t.AppendHeader(table.Row{"MG", "Source Zone", "Migration Zone", "Mapping", "Type", "Notes"}) for zone, x := range xlates { migZone := normalize(x.Migration.ZoneName, x.Migration.VlanNumber) mapping := "N/A" for _, m := range zoneMaps { if m.ID == zone { mapping = m.MigZone break } } if migZone == "" && x.Type != "Already Bridged" { migZone = "None" x.Type = "N/A" } t.AppendRow(table.Row{x.MoveGroup, zone, migZone, mapping, x.Type, shared.Truncate(x.Notes, 50)}) } if len(noXlate) > 0 { for _, xl := range noXlate { t.AppendRow(table.Row{"N/A", xl, "N/A", "N/A", "N/A", "ERROR: No Translation"}) } } t.SortBy([]table.SortBy{{Name: "MG", Mode: table.Asc}, {Name: "Source Zone", Mode: table.Asc}}) t.SetStyle(table.StyleDefault) t.Render() } // zoneMappings will call out to Mongo to pull a list of zone-mappings for a given datacenter // from the zone-mappings collection. func (c Client) zoneMappings(collection string) ([]zoneMap, error) { // Make an empty response var var response = make([]zoneMap, 0) // Initiate the call out to the migration/Find resource byt, err := c.mgo.Find(&mongodbAPI.FindQuery{}, fmt.Sprintf("%sZoneMapping", collection)) if err != nil { return response, fmt.Errorf("zoneMappings:mongodbAPI.Find:%s", err) } // Decode the json response back var payload struct { Result []zoneMap `json:"result"` } jd := json.NewDecoder(strings.NewReader(string(byt))) if err = jd.Decode(&payload); err != nil { return response, err } return payload.Result, nil } // MoveGroupDevices will call out to Mongo to pull a list of public vlans for a given set of move-groups // from the migrations collection. func (c Client) MoveGroupDevices(moveGroups []string, collection string) ([]tools.Device, error) { // Initiate the call out to the migration/Find resource byt, err := c.mgo.Find(&mongodbAPI.FindQuery{ Query: map[string]interface{}{ "move_group": map[string][]string{ "$in": moveGroups, }, "deleted": map[string]bool{ "$ne": true, }, "network.public.zone": map[string]interface{}{ "$nin": []interface{}{nil, ""}, }, }, Projection: []string{ "network.vlans", "network.public", "move_group", "environment.number", "environment.current.switch_ports_parsed", }, }, collection) if err != nil { return nil, fmt.Errorf("moveGroupDevices:mongodbAPI.Find:%s", err) } // Decode the json response back var payload struct { Result []tools.Device `json:"result"` } jd := json.NewDecoder(strings.NewReader(string(byt))) if err = jd.Decode(&payload); err != nil { return nil, err } return payload.Result, nil } // MoveGroupsForZone will call out to Mongo to pull a list of move-groups for a public zone // from the migrations collection. func (c Client) MoveGroupsForZone(zone, collection string) ([]string, error) { // Make an empty response var var moveGroups = make([]string, 0) // Create a new Mongo Client mgoClient := mongodbAPI.New(mongodbAPI.WithToken(c.coreAuth.Password), "migration", false) // Initiate the call out to the migration/Find resource byt, err := mgoClient.Find(&mongodbAPI.FindQuery{ Query: map[string]string{"network.public.zone": zone}, Projection: []string{"move_group"}, }, collection) if err != nil { return moveGroups, fmt.Errorf("getZoneMappings:mongodbAPI.Find:%s", err) } // Decode the json response back var payload struct { Result []tools.Device `json:"result"` } jd := json.NewDecoder(strings.NewReader(string(byt))) if err = jd.Decode(&payload); err != nil { return moveGroups, err } Result: for _, r := range payload.Result { if r.MoveGroup == "" { continue Result } if tools.IsNotMovingGroup(r.MoveGroup) { continue Result } for _, m := range moveGroups { if m == r.MoveGroup { continue Result } } moveGroups = append(moveGroups, r.MoveGroup) } return moveGroups, nil } // BridgeDomains function will reach out to the TMPE's to gather the bridge-domain info for a given DC // and return a listing of the parsed domains. func (c Client) BridgeDomains(switchName string) (shared.BridgeGroup, error) { var bg = make(shared.BridgeGroup) data, err := c.swAPI.GetBridgeDomains(switchName) if err != nil { return nil, err } for _, bd := range data { for k, v := range shared.ParseBridgeGroup(bd.ID) { bg[k] = v } } return bg, nil } // translations method is where the actual translations get built. This method will range through the devices // and pull aggr connected_route info for each switchport uplink, then run the connected routes through the // list of vlan translations to determine if a L3 interface is in the translations list. func (c Client) translations(devices []tools.Device) map[string]zoneInfo { var r = make(map[string]zoneInfo) for _, d := range devices { // For each port assigned to the device, go through and pull any connected routes that // exist on its up-linked aggr. for _, p := range d.Environment.Current.PortsParsed { // Get the connected routes. If we run into an error, just log it to terminal. This may cause a miss // in SVI, but it should crash the program entirely. if err := c.aggrConnectedRoutes(fmt.Sprintf("%s.%s", p.SwitchName, p.DataCenter), 3); err != nil { logrus.Errorf("%s: %s", err, p.SwitchName) } } } for _, d := range devices { // At this point, we "should" have all the L3 interfaces for each uplinked aggr, so we can now // range through the translations and try to determine if the vlan id has a L3 interface in the zone. for _, v := range d.Network.Vlans { if v.Source.ZoneName == "" { continue } zone := normalize(v.Source.ZoneName, v.Source.VlanNumber) if _, exist := r[zone]; exist { continue } hasSVI := false if val, ok := c.zoneSVI[v.Source.ZoneName]; ok { if strings.Contains(strings.ToLower(v.Source.ZoneName), "servicenet") { logrus.Infof("%s:Contains servicenet", v.Source.ZoneName) continue } if strings.Contains(strings.ToLower(v.Source.NetworkSegments), "drac") { logrus.Infof("%s:Contains drac", v.Source.NetworkSegments) continue } for _, route := range val { if strings.EqualFold(route.Interface, fmt.Sprintf("vlan%d", v.Source.VlanNumber)) { logrus.Debugf("Found SVI %s:%s in %s", route.Interface, route.Network, v.Source.ZoneName) hasSVI = true break } } } if hasSVI { logrus.Debugf("%d::Adding xlate for %s => %s", d.ID, v.Source.ZoneName, v.Migration.ZoneName) r[zone] = zoneInfo{VlanTranslation: v, MoveGroup: d.MoveGroup, Type: "Swing", Notes: ""} } } } return r } // aggrConnectedRoutes will pull the first north uplink for the given switch, then // gather the connected routes from the uplinked aggr func (c Client) aggrConnectedRoutes(switchName string, uplinkCount int) error { if _, ok := polledSwitch[switchName]; ok { return nil } polledSwitch[switchName] = true logrus.Debugf("Checking for neighbors on %s", switchName) neighbors, err := c.uni.UpNeighbors(switchName, nil) if err != nil { return err } nonAggrNorths := make([]string, 0) for _, neighbor := range neighbors.Items { if !strings.EqualFold("north", neighbor.Direction) { continue } if !strings.Contains(strings.ToLower(neighbor.RemoteHostname), "aggr") && !strings.Contains(strings.ToLower(neighbor.RemoteHostname), "dist") { nonAggrNorths = append(nonAggrNorths, neighbor.RemoteHostname) continue } infraDevice, err := c.dcx.InfrastructureDevice(neighbor.RemoteHostname) if err != nil { manZn := ManualZone(neighbor.RemoteHostname) if manZn == "" { return fmt.Errorf("infrastructureDevice:%s", err) } infraDevice = &dcxAutohost.InfraDevice{ZoneName: manZn} } if _, ok := c.zoneSVI[infraDevice.ZoneName]; ok { return nil } c.zoneSVI[infraDevice.ZoneName] = make([]unipede.UpConnectedRouteItem, 0) routes, err := c.uni.UpConnectedRoutes(neighbor.RemoteHostname) if err != nil { return err } c.zoneSVI[infraDevice.ZoneName] = routes.Items return nil } if uplinkCount != 0 { for _, neighbor := range nonAggrNorths { logrus.Debugf("Couldn't find up-link aggr on %s, checking up-link neighbor %s", switchName, neighbor) return c.aggrConnectedRoutes(neighbor, uplinkCount-1) } } return fmt.Errorf("could not find an aggr for %s", switchName) } // ManualZone is a catch for Datapipe when the source zone does not exist in the // DCX tooling, "we make a new zone" func ManualZone(hostname string) string { // SourceZone will pull a zone for each of the vlans mapped in the xlates. r := hostname if !strings.Contains(r, ".") { return r } split := strings.Split(r, ".") if len(split) > 1 { r = fmt.Sprintf("%s:Zone%s", strings.ToUpper(split[1]), strings.ToUpper(split[0])) } return r } // PublicZoneForDevice is a Datapipe hack to get the zone name for a given device func PublicZoneForDevice(d tools.Device) string { for _, v := range d.Network.Vlans { if strings.Contains(strings.ToLower(v.Source.ZoneName), "dist") { return normalize(v.Source.ZoneName, v.Source.VlanNumber) } } return d.Network.Public.Zone } // Exclusions type is the format of the data returned from the // vlansExclusionsDB type Exclusions struct { ID string `json:"_id"` VlanNumber string `json:"vlan_number"` ZoneName string `json:"zone_name"` } // AddBridgesToExclusionDB method will insert a document into the vlansExcluded DB for bridges we are going to have // to setup. func (c Client) AddBridgesToExclusionDB(bridges []zoneInfo) { confirmMsg := fmt.Sprintf("Found %d source vlans flagged as 'NEW Bridges', do you want to add them to the exlusionDB?", len(bridges)) if !shared.ConfirmPrompt(confirmMsg) { return } for _, b := range bridges { errorPrompt := fmt.Sprintf("Cannot check if %s:Vlan%d exist in DB. Do you want to continue?", b.Source.ZoneName, b.Source.VlanNumber) byt, err := c.mgo.Find(&mongodbAPI.FindQuery{Query: map[string]interface{}{"vlan_number": b.Source.VlanNumber, "zone_name": b.Source.ZoneName}}, "vlanTranslationExcluded") if err != nil { if !shared.ConfirmPrompt(errorPrompt) { os.Exit(0) } } // Decode the json response back var p struct { Result []Exclusions `json:"result"` } if err = json.NewDecoder(bytes.NewReader(byt)).Decode(&p); err != nil { if !shared.ConfirmPrompt(errorPrompt) { os.Exit(0) } } if len(p.Result) != 0 { logrus.Infof("Found exclusion already added for %s:Vlan%s. Skipping...", p.Result[0].ZoneName, p.Result[0].VlanNumber) continue } insByt, err := c.mgo.InsertOne(&mongodbAPI.InsertOneQuery{ Document: map[string]interface{}{ "vlan_number": b.Source.VlanNumber, "zone_name": b.Source.ZoneName, }, }, "vlanTranslationExcluded") if err != nil { logrus.Errorf("Error inserting new record in vlansExcludedDB:%s", err) } logrus.Infof("%s:Vlan%d added %s", b.Source.ZoneName, b.Source.VlanNumber, insByt.InsertedID) } } func normalize(zoneName string, vlanID int) string { str := regexp.MustCompile(`(?i).*zone(.*)$`).FindStringSubmatch(zoneName) if len(str) != 2 { return "" } name := strings.ToLower(str[1]) if strings.Contains(name, "dist") { return fmt.Sprintf("%s:Vlan%d", name, vlanID) } return fmt.Sprintf("aggr%s:Vlan%d", name, vlanID) } // nolint:deadcode,unused func contains(s string, subStrings []string) bool { for _, sub := range subStrings { if strings.Contains(strings.ToLower(s), sub) { return true } } return false }