// 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" "os" "strconv" "github.com/jedib0t/go-pretty/table" "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 fromBQSubPlugin(p *argparse.Command) core.Plugin { cmd := p.NewCommand("bq", "refresh device/account CORE data from BigQuery") args[argsBQ].Accounts = shared.ArgAccounts(cmd) args[argsBQ].Devices = shared.ArgDevices(cmd) return core.Plugin{CMD: cmd, Func: refreshBQ} } func refreshBQ(cfg gokeys.Settings) { if len(*args[argsBQ].Accounts) == 0 && len(*args[argsBQ].Devices) == 0 { log.Fatal("must specify 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.AuditResponse, 0) switch { /* case len(*moveGroups) != 0: for _, mg := range *moveGroups { metadata, err := shared.GetMetadata(mg, coreKey.Password) if err != nil { log.Fatalf("%s: error getting metadata: %s", mg, err.Error()) } resp, err := c.CollectFromBigQuery( metadata.Collection, metadata.DataCenter, migrationAPI.NewOptions().SetMoveGroup(mg), ) if err != nil { msg := "" if resp != nil && resp.Error != nil { msg = *resp.Error } log.Fatalf("%s: collect from_bigquery error: %s, %s", mg, err.Error(), msg) } results = append(results, *resp) }*/ case len(*args[argsBQ].Accounts) != 0: accts := *args[argsBQ].Accounts acctID, err := strconv.Atoi(accts[0]) if err != nil { log.Fatal(err) } col, mg := shared.GetAccountMetadata(acctID, coreKey.Password) dc := shared.GetDCFromMoveGroup(mg) for _, a := range *args[argsBQ].Accounts { acctID, err := strconv.Atoi(a) if err != nil { log.Fatal(err) } resp, err := c.CollectFromBigQuery( col, dc, migrationAPI.NewOptions().SetAccount(acctID), ) if err != nil { msg := "" if resp != nil && resp.Error != nil { msg = *resp.Error } log.Fatalf("%d: collect from_bigquery error: %s, %s", acctID, err.Error(), msg) } results = append(results, *resp) } case len(*args[argsBQ].Devices) != 0: devList := *args[argsBQ].Devices col := shared.GetDeviceCollection(devList[0], coreKey.Password) dc := shared.GetDCFromCollection(col) for _, d := range *args[argsBQ].Devices { resp, err := c.CollectFromBigQuery( col, dc, migrationAPI.NewOptions().SetDevice(d), ) if err != nil { msg := "" if resp != nil && resp.Error != nil { msg = *resp.Error } log.Fatalf("%d: collect from_bigquery error: %s, %s", d, err.Error(), msg) } results = append(results, *resp) } } pPrintBQ(results) } //// pPrint func will take a structured type of response data and render a table //// output to stdout. func pPrintBQ(data []migrationAPI.AuditResponse) { t := table.NewWriter() t.SetOutputMirror(os.Stdout) t.AppendHeader(table.Row{"Device ID", "Account", "Status"}) for _, row := range data { for _, d := range row.Data { t.AppendRow(table.Row{d.ID, d.AccountID, d.Reason}) } } t.SortBy([]table.SortBy{{Name: "Device ID", Mode: table.AscNumeric}}) t.Render() }