package dp import ( "fmt" "net" "strconv" "github.rackspace.com/DCM/tools/migMeta" "go.mongodb.org/mongo-driver/bson" "github.rackspace.com/DCM/migration/pkg/collection" migrationMongo "github.rackspace.com/DCM/migration/pkg/collection/mongo" "github.rackspace.com/DCM/tools" ) // Update updates the existing device db records with 3rd-party-specific data func (c *Client) Update(devices []tools.Device, col string) (resp collection.UpdateResponse, err error) { chMissed := make(chan []collection.Missed) chInt := make(chan int) chunk := migrationMongo.Chunk(devices, routines) defer close(chMissed) defer close(chInt) backupNets := c.m.GetSubnets(migMeta.MetaKeyNetworkBackup) dracNets := c.m.GetSubnets(migMeta.MetaKeyNetworkDrac) for _, devChunk := range chunk { go run(c, devChunk, chMissed, chInt, col, backupNets, dracNets) } for range chunk { resp.Missed = append(resp.Missed, <-chMissed...) resp.Updated += <-chInt } return } func run(c *Client, devices []tools.Device, chMissed chan<- []collection.Missed, chInt chan<- int, col string, backupNets, dracNets []net.IPNet) { var resp collection.UpdateResponse for _, d := range devices { if d.Linked.ID == 0 { resp.Missed = append(resp.Missed, collection.Missed{Item: d.ID, Details: "linked ID not set"}) continue } if dpd, err := c.Collect(ByDevice(strconv.Itoa(d.Linked.ID)), WithBackupNets(backupNets), WithDracNets(dracNets)); err == nil { if len(dpd) > 0 { update := bson.D{ {"linked.account_name", dpd[0].Account.Name}, {"environment.current", dpd[0].Environment.Current}, {"network.address.drac_ips.source", dpd[0].Network.Address.DracIPs}, {"network.address.mbu_ips.source", dpd[0].Network.Address.MBUIPs}, } if mustInt(dpd[0].Device.ID) > 0 { update = append(update, bson.E{"linked.specific_id", mustInt(dpd[0].Device.ID)}) } // gm-993: pull migration ports from linked CMDB for devices not migrating to a RAX-standard DC if !c.m.RackspaceMigrationDC { dpd[0].Environment.Migration.Cabinet = d.Environment.Migration.Cabinet dpd[0].Environment.Migration.Container = d.Environment.Migration.Container dpd[0].Environment.Migration.StartingSpace = d.Environment.Migration.StartingSpace update = append(update, bson.E{"environment.migration", dpd[0].Environment.Migration}) } if uErr := c.db.UpdateOne( col, bson.D{{"_id", d.ID}}, bson.D{{"$set", update}}, false, ); uErr != nil { resp.Missed = append( resp.Missed, collection.Missed{Item: d.ID, Details: fmt.Sprintf("mongo.UpdateOne %v", uErr.Error())}, ) } resp.Updated++ } else { resp.Missed = append(resp.Missed, collection.Missed{Item: d.ID, Details: "device not found in myadmin"}) } } else { resp.Missed = append(resp.Missed, collection.Missed{Item: d.ID, Details: fmt.Sprintf("myadmin error: %v", err.Error())}) } } chMissed <- resp.Missed chInt <- resp.Updated }