// 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 migrate import ( "fmt" "log" "os" "github.rackspace.com/gorax/clients/migrationAPI" "github.com/jedib0t/go-pretty/table" "github.com/sirupsen/logrus" "github.com/jokelyo/argparse" "github.rackspace.com/DCM/gomigrate/core" "github.rackspace.com/DCM/gomigrate/shared" "github.rackspace.com/gorax/gokeys" ) type ( result struct { ID int MoveGroup string Result *migrationAPI.MigrateVlansResponse } ) func fromTranslationsPlugin(p *argparse.Command) core.Plugin { cmd := p.NewCommand("translations", "migrate VLAN translations in DCX Environment Manager") argXlateDevices = shared.ArgDevices(cmd) argXlateMG = shared.ArgMoveGroup(cmd) return core.Plugin{CMD: cmd, Func: migrateTranslations} } // migrateTranslations will prompt for a list of device, then run DCX migrate // translations for each device func migrateTranslations(cfg gokeys.Settings) { // Pull an Identity token from the users gokeys keychain iKey, err := cfg.Identity(false) if err != nil { log.Fatal(err) } // Pull a core token from the users gokeys keychain coreKey, err := cfg.Core(false) if err != nil { log.Fatal(err) } mgList := make(map[string][]int) if *argXlateMG != "" { devices, err := shared.MongoDevices([]string{*argXlateMG}, nil, nil, "", coreKey.Password, nil) if err != nil { log.Fatalf("error retrieving devices for move group: %s", err) } logrus.Infof("%s: found %d devices", *argXlateMG, len(devices)) mgList[*argXlateMG] = make([]int, 0) for _, d := range devices { mgList[*argXlateMG] = append(mgList[*argXlateMG], d.ID) } } else { devices, err := shared.MongoDevices(nil, nil, *argXlateDevices, "", coreKey.Password, nil) if err != nil { log.Fatalf("error retrieving devices: %s", err) } for _, d := range devices { if _, ok := mgList[d.MoveGroup]; !ok { mgList[d.MoveGroup] = make([]int, 0) } mgList[d.MoveGroup] = append(mgList[d.MoveGroup], d.ID) } } m := migrationAPI.New(iKey.Password, false) // change this to use migrateVlans method for mg, devices := range mgList { for _, d := range devices { r := result{ ID: d, MoveGroup: mg, Result: new(migrationAPI.MigrateVlansResponse), } r.Result, err = m.MigrateVlans(mg, d) if err != nil { errString := err.Error() r.Result = &migrationAPI.MigrateVlansResponse{ Error: &errString, } } pPrint(r) } } } func pPrint(r result) { t := table.NewWriter() t.SetOutputMirror(os.Stdout) t.AppendRow(table.Row{fmt.Sprintf("%d - %s", r.ID, r.MoveGroup)}) if r.Result.Error != nil { header := "Error" if *r.Result.Error == "environment vlans already migrated" { header = "Info" } t.AppendRow(table.Row{fmt.Sprintf("%s: %s", header, *r.Result.Error)}) } else if r.Result.Data != nil { out := "VLANs Migrated:\n" for _, v := range r.Result.Data.VlansMigrated { out += fmt.Sprintf("\t%s:Vlan%s\n", v.SourceZone, v.SourceVlan) } out += "\nVLANs Not Migrated:\n" for _, v := range r.Result.Data.VlansNotMigrated { out += fmt.Sprintf("\t%s:Vlan%s\n", v.SourceZone, v.SourceVlan) } t.AppendRow(table.Row{out}) } t.SetStyle(table.StyleLight) t.Style().Options.SeparateRows = true t.Render() }