// 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 fromLinkedSubPlugin(p *argparse.Command) core.Plugin { cmd := p.NewCommand("linked", "refresh linked-specific source data from linked CMDB") args[argsLinked].MoveGroups = shared.ArgMoveGroups(cmd) args[argsLinked].Accounts = shared.ArgAccounts(cmd) args[argsLinked].Devices = shared.ArgDevices(cmd) return core.Plugin{CMD: cmd, Func: refreshLinked} } func refreshLinked(cfg gokeys.Settings) { if len(*args[argsLinked].MoveGroups) == 0 && len(*args[argsLinked].Accounts) == 0 && len(*args[argsLinked].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[argsLinked].MoveGroups) != 0: for _, mg := range *args[argsLinked].MoveGroups { resp, err := c.CollectFromLinkedCMDB( 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_linked error: %s, %s", mg, err.Error(), msg) } results = append(results, *resp) } case len(*args[argsLinked].Accounts) != 0: accts := *args[argsLinked].Accounts col := shared.GetAccountCollection(accts[0], coreKey.Password) for _, a := range *args[argsLinked].Accounts { acctID, err := strconv.Atoi(a) if err != nil { log.Fatal(err) } resp, err := c.CollectFromLinkedCMDB( col, migrationAPI.NewOptions().SetAccount(acctID), ) if err != nil { msg := "" if resp != nil && resp.Error != nil { msg = *resp.Error } log.Fatalf("%d: collect from_linked error: %s, %s", acctID, err.Error(), msg) } results = append(results, *resp) } case len(*args[argsLinked].Devices) != 0: devList := *args[argsLinked].Devices col := shared.GetDeviceCollection(devList[0], coreKey.Password) for _, d := range *args[argsLinked].Devices { resp, err := c.CollectFromLinkedCMDB( col, migrationAPI.NewOptions().SetDevice(d), ) if err != nil { msg := "" if resp != nil && resp.Error != nil { msg = *resp.Error } log.Fatalf("%d: collect from_linked error: %s, %s", d, err.Error(), msg) } results = append(results, *resp) } } pPrintCollect(results) }