// 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 envdiscover is the translations script used to run environment true-up on a // customer environment. package envdiscover import ( "bytes" "context" "encoding/json" "fmt" "log" "net/http" "net/url" "strings" "time" "github.com/jokelyo/argparse" "github.com/labstack/gommon/color" "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.rackspace.com/DCM/discover" "github.rackspace.com/DCM/gomigrate/core" "github.rackspace.com/DCM/gomigrate/shared" migrationMongo "github.rackspace.com/DCM/migration/pkg/collection/mongo" "github.rackspace.com/gorax/clients/migrationAPI" "github.rackspace.com/gorax/gokeys" ) const ( discoverTimeout = 600 ) var ( argAccount *int argDevices *[]int argDevicesExcluded *[]int argVlanExcluded *[]int argMoveGroup *string flagMakeChanges *bool ) // Plugin function will return a argparse.Command type back to the parent parser // nolint:typecheck func Plugin(p *core.Parser) core.Plugin { // Create a argument for the DCX Translations logic cmd := p.NewCommand("discover", "Network discovery and EM/NA builder") argAccount = cmd.Int("a", "account", &argparse.Options{Help: "CORE Account number"}) argDevices = shared.ArgDevices(cmd) argMoveGroup = shared.ArgMoveGroup(cmd) argDevicesExcluded = cmd.Ints("e", "devices-excluded", &argparse.Options{Help: "CORE Device IDs to exclude from discovery (used with -a option)", Nargs: "+"}) argVlanExcluded = cmd.Ints("v", "vlans-excluded", &argparse.Options{Help: "Vlan IDs to exclude from discovery process (i.e. shared backup vlans)", Nargs: "+"}) flagMakeChanges = shared.ArgFlag(cmd, "update", "attempt to build EM environments/NA segments") return core.Plugin{CMD: cmd, Func: pluginFunc} } // pluginFunc function will prompt for a list of move-groups, then query the migration database // for a listing of network devices to run the failover_device call on. func pluginFunc(cfg gokeys.Settings) { // Pull a Identity token from the users gokeys keychain iKey, err := cfg.Identity(false) if err != nil { log.Fatal(err) } cKey, err := cfg.Core(false) if err != nil { log.Fatal(err) } sso, err := cfg.SSO() if err != nil { log.Fatal(err) } if len(*argDevices) == 0 && *argAccount == 0 { log.Fatal("must specify either devices or an account") } if len(*argDevices) != 0 && *argAccount != 0 { log.Fatal("devices and account args are mutually exclusive") } if *argAccount != 0 && *argMoveGroup == "" { log.Fatal("must define -m move-group when using -a account") } deviceGroups := make(map[string][]int) if *argAccount != 0 { filter := map[string]interface{}{"linked._id": map[string]int{"$ne": 0}} devList, err := shared.MongoDevices([]string{*argMoveGroup}, []int{*argAccount}, nil, "", cKey.Password, filter) if err != nil { log.Fatal(err) } deviceGroups["Account"] = make([]int, 0) for _, d := range devList { if d.Linked.ID == 0 { logrus.Warnf("[%d] Does not have a Linked ID; Will not be able to discover", d.ID) continue } match := false for _, e := range *argDevicesExcluded { if e == d.ID { match = true break } } if match { logrus.Debugf("excluding device %d", d.ID) continue } deviceGroups["Account"] = append(deviceGroups["Account"], d.Linked.ID) } } else if len(*argDevices) != 0 { filter := map[string]interface{}{"linked._id": map[string]int{"$ne": 0}} devList, err := shared.MongoDevices(nil, nil, *argDevices, "", cKey.Password, filter) if err != nil { log.Fatal(err) } deviceGroups["Devices"] = make([]int, 0) for _, d := range devList { if d.Linked.ID == 0 { logrus.Warnf("[%d] Does not have a Linked ID; Will not be able to discover", d.ID) continue } deviceGroups["Devices"] = append(deviceGroups["Devices"], d.Linked.ID) } } else { log.Fatal("no device data found") } if *flagMakeChanges { logrus.Warnf("Discovery with production changes!!") } else { logrus.Info("Discovery with no changes") } for groupType, devices := range deviceGroups { if len(devices) == 0 { logrus.Info("No devices found.") } req := request{ Devices: devices, VlansExcluded: *argVlanExcluded, } logrus.Infof("%s discovery with %d devices: %v ...", groupType, len(req.Devices), req.Devices) result := runDiscovery(req, iKey.Password) prettyPrint(result.Data, sso.Username, cKey.Password, iKey.Password) } } // prettyPrint func will take a structured type of response data and render a table // output to stdout. func prettyPrint(data discover.Result, username, coreToken, identityToken string) { for _, disc := range data.Discoveries { for _, t := range disc.Environments { fmt.Printf(color.Magenta(fmt.Sprintf("[++ %d NEW ENVIRONMENT %s ++]\n", disc.Account, t.Number))) fmt.Printf(color.Magenta(strings.Repeat("-", 40) + "\n")) for _, d := range t.Devices { fmt.Printf("\tDEVICE [%s => %s] IsGateway=%v Model=%s\n", color.Green(d.ID), color.Green(d.LinkedID), d.IsGateway, d.Model) for _, x := range d.PortMaps.PortMaps { fmt.Printf("\t\t%s[%s] (%s) => %s[%d] (%s)\n", strings.ToUpper(x.Source.Switch.Number), x.Source.Name, x.Source.SwitchPortType, strings.ToUpper(x.Migration.Switch.Number), x.Migration.PortNumber, x.Migration.SwitchPortType) } if d.PortMaps.UnmappedSourcePorts != "" { for _, up := range strings.Split(d.PortMaps.UnmappedSourcePorts, ",") { fmt.Printf("\t\t%s => UNMAPPED SRC PORT\n", up) } } } for _, d := range t.Translations { fmt.Printf("\tVLAN [%s:Vlan%s Segment=%v Block=%s]", color.Cyan(d.ConfiguredVlan.ZoneName), color.Cyan(d.ConfiguredVlan.Vlan), d.ConfiguredVlan.NetworkSegments, d.ConfiguredVlan.CIDR) if d.MigrationVlan.Vlan != 0 { fmt.Printf(" => [%s:Vlan%s]", color.Cyan(d.MigrationVlan.ZoneName), color.Cyan(d.MigrationVlan.Vlan)) } fmt.Printf("\n") } for _, r := range t.PrivateBlocks { fmt.Printf("\tPRIV BLOCK [%s]\n", color.Yellow(r)) } for _, r := range t.PublicBlocks { fmt.Printf("\tPUBL BLOCK [%s]\n", color.Cyan(r)) } } fmt.Println() for _, l := range disc.Logs { fmt.Printf(color.Yellow(fmt.Sprintf("\t%s\n", l))) } } for _, vl := range data.SVIs { fmt.Printf(color.Green("[NA SEGMENT] Name=%s\n"), vl.Name) } for _, e := range data.Errors { fmt.Printf(color.Red("[ERRORS] %s\n"), e) } if *flagMakeChanges { // post results to status endpoint logrus.Debug("Recording results to DB ...") statusUpdate(data, username, coreToken, identityToken) } } type ( request struct { Devices []int `json:"devices"` VlansExcluded []int `json:"vlans_excluded"` } response struct { Data discover.Result `json:"data"` Error string `json:"error"` } ) // runDiscovery function will POST the list of devices to the discover endpoint and return // the results func runDiscovery(deviceReq request, token string) response { var ( headers = map[string]string{ "Accept": "application/json", "Connection": "close", "X-Auth-Token": token, } result = response{} fullURL = fmt.Sprintf("%s/discover/datapipe", shared.MigAPI) ) js, err := json.Marshal(deviceReq) if err != nil { logrus.Errorf("runDiscovery:json.Marshal:%v:%s", deviceReq, err) return result } ctx, cancel := context.WithTimeout(context.Background(), time.Second*discoverTimeout) defer cancel() // Build the request req, err := http.NewRequestWithContext(ctx, http.MethodPost, fullURL, bytes.NewReader(js)) if err != nil { logrus.Errorf("runDiscovery:NewRequest:%v:%s", deviceReq, err) return result } if *flagMakeChanges { par := url.Values{ "update": []string{"true"}, } req.URL.RawQuery = par.Encode() } for key, value := range headers { req.Header.Set(key, value) } // Send the request via a client // Do sends an HTTP request and // returns an HTTP response r, err := http.DefaultClient.Do(req) if err != nil { logrus.Errorf("runDiscovery:Do:%s", err) return result } if r != nil { defer func() { if defErr := r.Body.Close(); defErr != nil { err = errors.Wrap(defErr, "defer error") } }() } if r.StatusCode > http.StatusNoContent { logrus.Errorf("resp_err:%s", r.Status) return result } // Use json.Decode for reading streams of JSON data if err := json.NewDecoder(r.Body).Decode(&result); err != nil { logrus.Errorf("runDiscovery:Decode:%s", err) return result } return result } func statusUpdate(result discover.Result, username, coreToken, identityToken string) { if len(result.Discoveries) == 0 { return } collection, mg := shared.GetAccountMetadata(result.Discoveries[0].Account, coreToken) payload := migrationMongo.StatusDiscover{ ID: result.Discoveries[0].Account, Username: username, Timestamp: time.Now().Unix(), Result: result, } m := migrationAPI.New(identityToken, false) err := m.StatusUpdateDiscover(collection, mg, &payload) if err != nil { logrus.Errorf("error updating status results: %s", err.Error()) } }