// 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 ( "encoding/json" "log" "github.com/jokelyo/argparse" "github.com/sirupsen/logrus" "github.rackspace.com/DCM/gomigrate/core" "github.rackspace.com/DCM/gomigrate/shared" "github.rackspace.com/gorax/clients/dcxAutohost" "github.rackspace.com/gorax/gokeys" ) func fromLocationsPlugin(p *argparse.Command) core.Plugin { cmd := p.NewCommand("locations", "migrate switchport locations in DCX Location Manager") argLocDevices = shared.ArgDevices(cmd) argLocMG = shared.ArgMoveGroup(cmd) return core.Plugin{CMD: cmd, Func: migrateLocations} } // migrateLocations will prompt for a list of device, then run DCX migrate // locations for each device func migrateLocations(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) } devList := make([]int, 0) if *argLocMG != "" { devices, err := shared.MongoDevices([]string{*argLocMG}, nil, nil, "", coreKey.Password, nil) if err != nil { log.Fatalf("error retrieving devices for move group: %s", err) } logrus.Infof("%s: found %d devices with locations to migrate", *argLocMG, len(devices)) for _, d := range devices { devList = append(devList, d.ID) } } else { devList = *argLocDevices if len(devList) == 0 { log.Fatalf("no devices found in %v", devList) } } dcx, err := dcxAutohost.New(dcxAutohost.WithToken(iKey.Password), false) if err != nil { log.Fatalf(" dcxAutohost.NewWithToken:%s", err) } for _, d := range devList { migrate, err := dcx.MigrateLocation(d) if err != nil { var errData struct { Err []string `json:"errors"` } // Use json.Decode for reading streams of JSON data if uErr := json.Unmarshal([]byte(err.Error()), &errData); uErr == nil { logrus.Errorf("%d:%s", d, errData.Err) } else { logrus.Errorf("%d:%s", d, err) } continue } logrus.Infof("migrated %d to %s.%s successfully", d, migrate.Container, migrate.DataCenter) } }