// 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 refresh import ( "log" "strconv" "github.com/jokelyo/argparse" "github.rackspace.com/DCM/gomigrate/core" "github.rackspace.com/DCM/gomigrate/shared" "github.rackspace.com/gorax/clients/migrationAPI" "github.rackspace.com/gorax/gokeys" ) func fromRVISubPlugin(p *argparse.Command) core.Plugin { cmd := p.NewCommand("virt", "refresh virt-specific data from RVI") args[argsRVI].MoveGroups = shared.ArgMoveGroups(cmd) args[argsRVI].Accounts = shared.ArgAccounts(cmd) args[argsRVI].Devices = shared.ArgDevices(cmd) return core.Plugin{CMD: cmd, Func: refreshRVI} } func refreshRVI(cfg gokeys.Settings) { if len(*args[argsRVI].MoveGroups) == 0 && len(*args[argsRVI].Accounts) == 0 && len(*args[argsRVI].Devices) == 0 { log.Fatal("must specify move-group(s) or account(s) or device(s)") } // Pull an identity token from the users gokeys keychain identKey, err := cfg.Identity(false) if err != nil { log.Fatalf("gokeys.Identity:%s", err) } // Pull a core token from the users gokeys keychain coreKey, err := cfg.Core(false) if err != nil { log.Fatalf("gokeys.Core:%s", err) } c := migrationAPI.New(identKey.Password, false) results := make([]migrationAPI.CollectResponse, 0) switch { case len(*args[argsRVI].MoveGroups) != 0: for _, mg := range *args[argsRVI].MoveGroups { resp, err := c.CollectFromRVI( shared.GetCollection(mg), migrationAPI.NewOptions().SetMoveGroup(mg), ) if err != nil { msg := "" if resp != nil && resp.Error != nil { msg = *resp.Error } log.Fatalf("%s: collect from_rvi error: %s, %s", mg, err.Error(), msg) } results = append(results, *resp) } case len(*args[argsRVI].Accounts) != 0: accts := *args[argsRVI].Accounts col := shared.GetAccountCollection(accts[0], coreKey.Password) for _, a := range *args[argsRVI].Accounts { acctID, err := strconv.Atoi(a) if err != nil { log.Fatal(err) } resp, err := c.CollectFromRVI( col, migrationAPI.NewOptions().SetAccount(acctID), ) if err != nil { msg := "" if resp != nil && resp.Error != nil { msg = *resp.Error } log.Fatalf("%d: collect from_rvi error: %s, %s", acctID, err.Error(), msg) } results = append(results, *resp) } case len(*args[argsRVI].Devices) != 0: devList := *args[argsRVI].Devices col := shared.GetDeviceCollection(devList[0], coreKey.Password) for _, d := range *args[argsRVI].Devices { resp, err := c.CollectFromRVI( col, migrationAPI.NewOptions().SetDevice(d), ) if err != nil { msg := "" if resp != nil && resp.Error != nil { msg = *resp.Error } log.Fatalf("%d: collect from_rvi error: %s, %s", d, err.Error(), msg) } results = append(results, *resp) } } pPrintCollect(results) }