// 2018 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 ebi import ( "fmt" "strconv" "strings" "github.rackspace.com/DCM/migration/pkg/collection" "github.rackspace.com/DCM/migration/pkg/tools/journal" "github.rackspace.com/DCM/tools/migMeta" "go.mongodb.org/mongo-driver/bson" ) // DMartUpdate method will kick off a series of go routines to post device information to the migration // database using data collected from GDCI EBI. func (c *Client) DMartUpdate(dl []Device, meta []migMeta.Meta, col string) (resp collection.UpdateResponse, err error) { chMissed := make(chan []collection.Missed) chChanges := make(chan []journal.Changes) chInt := make(chan int) chnk := Chunk(dl, routines) defer close(chMissed) defer close(chInt) defer close(chChanges) for _, item := range chnk { go routineDMartUpdate(c, item, col, meta, chMissed, chInt, chChanges) } for range chnk { resp.Changes = append(resp.Changes, <-chChanges...) resp.Missed = append(resp.Missed, <-chMissed...) resp.Updated += <-chInt } return } // routineDMartUpdate function is the internal function to kick off routines for DMartUpdate. func routineDMartUpdate(c *Client, dl []Device, col string, meta []migMeta.Meta, chMissed chan<- []collection.Missed, chInt chan<- int, chChanges chan<- []journal.Changes) { var resp collection.UpdateResponse for _, item := range dl { if shouldNotImport(item, col) { continue } doc := bson.D{ {"san", item.SAN}, {"has_das", item.DAS}, {"has_mbu", item.MBU}, {"has_sharepoint", item.SharePoint}, {"has_dex", item.DEX}, {"has_mssql", item.MSSQLSupport}, {"has_mysql", item.MySQLSupport}, {"has_skype", item.Skype}, {"has_oracle", item.OracleSupport}, {"has_rackconnect", item.RackConnect}, {"has_mssql_support", item.MSSQLSupport}, {"has_mysql_support", item.MySQLSupport}, {"has_oracle_support", item.OracleSupport}, {"has_doubletake", item.DoubleTake}, {"rcv2", item.RCv2}, {"rcv3", item.RCv3}, {"account.geographic_location", item.Account.GeographicLocation}, {"account.lead_tech", item.Account.LeadTech}, {"account.manager", item.Account.AM}, {"account.bdc", item.Account.BDC}, {"account.mrr", item.Account.CMRR}, {"account.name", item.Account.Name}, {"account.number", item.Account.Number}, {"account.status", item.Account.Status}, {"account.team_name", item.Account.TeamName}, {"account.type", item.Account.Type}, {"account.primary_contact", item.Account.PrimaryContact}, {"device.cmrr", item.Device.CMRR}, {"device.container_component_configuration_type_name", item.Device.ComponentConfigType}, {"device.hostname", item.Device.HostName}, {"device.make_model", item.Device.MakeModel}, {"device.online_status", item.Device.OnlineStatus}, {"device.datacenter", item.Device.DataCenter}, {"device.os", item.Device.OS}, {"device.os_name", item.Device.OSName}, {"device.status", item.Device.Status}, // We should be gathering IP information directly from DCX API // {"device.primary_ip", item.Device.PrimaryIP}, {"device.type", item.Device.Type}, {"device.sla", item.Device.SLA}, {"device.customer_managed", item.Device.CustomerManaged}, {"device.u_height", item.Device.UHeight}, {"environment.current.container", item.Environment.Current.Container}, {"environment.number", item.Environment.Number}, {"dp_device", item.DPDevice}, {"dp_account", item.DPAccount}, {"icon_url", item.IconURL}, {"linked.account_name", item.Linked.AccountName}, {"linked.account_number", item.Linked.AccountNumber}, {"linked._id", item.Linked.ID}, {"linked.specific_id", 0}, // value is set in dcx collect method {"linked.type", item.Linked.Type}, {"_db", collection.DBName}, {"_table", col}, } for _, m := range meta { doc = append(doc, bson.E{m.Key, m.Value}) } uErr := c.db.UpdateOne(col, bson.D{{"_id", item.ID}}, bson.D{{"$set", doc}}, true) if uErr != nil { resp.Missed = append(resp.Missed, collection.Missed{ Item: item.ID, Details: fmt.Sprintf("Mongo UpdateOne %v", uErr.Error()), }) continue } resp.Updated++ resp.Changes = append(resp.Changes, journal.Changes{ EventType: "DeviceNewAdded", AccountID: item.Account.Number, ID: strconv.Itoa(item.ID), Reason: fmt.Sprintf(item.Device.Status), TeamName: item.Account.TeamName, }) } chChanges <- resp.Changes chMissed <- resp.Missed chInt <- resp.Updated } func shouldNotImport(item Device, collectionName string) bool { // If the collection is defined as a test migration AND the account number is NOT our test account. return strings.HasPrefix(collectionName, "tst") && item.Account.Number != 5002028 }