// 2017 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 ctk import ( "encoding/json" "fmt" "strconv" "strings" "github.rackspace.com/gorax/tools/util" "github.com/sirupsen/logrus" "github.rackspace.com/DCM/migration/pkg/collection" "github.rackspace.com/DCM/migration/pkg/collection/ebi" ) var ( attributes = []interface{}{ "number", "platform_type", "platform_name", "platform_model", "datacenter.symbol", "status.name", "name", "os", "os_name", "os_type", "account.has_dsan_devices", "account.service_level_name", "account.primary_lead_tech.name", "support_team.name", "account.account_exec.name", "account.business_development.name", "account.primary_contact.name", "account.latest_cmrr", "account.rack_connect_configs", "account.number", "account.type.name", "platform.icon_path", "service_level_name", "primary_ip", "account.name", "ports.display_name", "failover_device.number", "dracnet_ip.ip_address", "account.status.name", "ip_addresses", "parts.sku_number", "account.mbu_devices.number", "account.has_sharepoint", map[string]interface{}{ "id": "sku_attribute.description", "attribute": "parts.partributes", "subattributes": map[string]string{ "value": "value", }, }, } dcMap = map[string]int{ "IAD2": 21, "ORD1": 22, "IAD3": 32, "DFW1": 5, "DFW2": 25, "DFW3": 41, "LON3": 13, "LON5": 40, "LON7": 109, "HKG1": 14, "SYD2": 31, "SYD4": 42, "SJC": 47, "FRA1": 48, } onlineStatuses = []int{ 9, // Segment Configuration 12, // Online/Complete 15, // Migration Server 70, // Support Maintenance } ) // CollectFromCTK method will gather all device information that is housed in CORE/CTK DB. func (c *Client) CollectFromCTK(devices []int, mrrMap map[string]ebi.DeviceMRR) (ctkDeviceList []Device, err error) { req, err := json.Marshal(ctk{ Class: "Computer.Computer", LoadArg: loadArg{ Values: []interface{}{"number", "in", devices}, Class: "Computer.ComputerWhere", }, Attributes: attributes, }) if err != nil { return nil, err } res, err := c.ctk.Query(req) if err != nil { return nil, err } var r ctkQuery if err = json.Unmarshal(res, &[]interface{}{&r}); err != nil { return nil, err } for _, d := range r.Result { newDevice := ctkToMongoMap(d) // Set the MRR if the device exist in the list newDevice.Device.CMRR = func() string { if val, ok := mrrMap[strconv.Itoa(newDevice.ID)]; ok { return val.USD } return "0.00" }() ctkDeviceList = append(ctkDeviceList, newDevice) } return } // CTKCollectAndUpdateByDC method will gather all device information that is housed in CORE/CTK DB. func (c *Client) CTKCollectAndUpdateByDC(col string, test bool) (ctkDeviceList []Device, err error) { // Pull MRR from Datamart. If we encounter an error, we will still continue processing, // just log that an error occurred. mrrMap, err := c.dMart.DMartMRR(strings.ToUpper(c.db.CollectionDC())) if err != nil { logrus.Errorf("error collecting MRR from DMART: %s", err) } limit := 250 if test { limit = 10 } // Build the original payload payload := ctk{ Class: "Computer.Computer", LoadArg: loadArg{ Values: []interface{}{ []interface{}{"datacenter", "=", dcMap[strings.ToUpper(c.db.CollectionDC())]}, []interface{}{"status", ">=", 7}, }, Class: "Computer.ComputerWhere", Limit: limit, }, Attributes: attributes, } var ( // Set a boolean to catch the last iteration when // the returned result is less than max. last = false // Base offset to start with offset = 0 ) for { // Set the initial offset payload.LoadArg.Offset = offset // Increment the offset to the next iteration offset += 250 req, err := json.Marshal(payload) if err != nil { return nil, err } logrus.Infof("collecting %d devices from CTK in %s", len(ctkDeviceList), c.db.CollectionDC()) res, err := c.ctk.Query(req) if err != nil { if strings.Contains(err.Error(), "No such session") { return nil, err } logrus.Errorf("CTKCollectAndUpdateByDC:ctk.Query:%s", err) continue } var r ctkQuery if err = json.Unmarshal(res, &[]interface{}{&r}); err != nil { return nil, err } // If the returned count is less than the max returned, // then that means we are on our last iteration. if r.Count < 250 { last = true } // Range through the result and build the ctkDeviceList for _, d := range r.Result { newDevice := ctkToMongoMap(d) // Set the MRR if the device exist in the list newDevice.Device.CMRR = func() string { if val, ok := mrrMap[strconv.Itoa(newDevice.ID)]; ok { return val.USD } return "0.00" }() ctkDeviceList = append(ctkDeviceList, newDevice) } logrus.Infof("updating %d devices for %s collection", len(ctkDeviceList), col) if _, err := c.UpdateMigrationDBForDC(ctkDeviceList, col); err != nil { logrus.Errorf("error updating DB %s", err) } logrus.Infof("update to %s complete for %d devices", col, len(ctkDeviceList)) // If the last flag was set, break out of the loop if last { break } } return } // CollectIDSFromCTK method will gather all device information that is housed in CORE/CTK DB. func (c *Client) CollectIDSFromCTK(devices []string, datacenter string) (ctkDeviceList []Device, err error) { if _, ok := dcMap[strings.ToUpper(datacenter)]; !ok { err = fmt.Errorf("%s not found as a valid DC", datacenter) return } var ( payload = ctk{ Class: "Computer.Computer", Attributes: attributes, } // Set a boolean to catch the last iteration when // the returned result is less than max. last = false // Base offset to start with offset = 0 // limit = 200 ) if len(devices) > 0 { payload.LoadArg = loadArg{ Values: []interface{}{ []interface{}{"status", "in", onlineStatuses}, "&", []interface{}{"number", "in", devices}, "&", []interface{}{"datacenter", "=", dcMap[strings.ToUpper(datacenter)]}, }, Class: "Computer.ComputerWhere", Limit: limit, } } else { payload.LoadArg = loadArg{ Values: []interface{}{ []interface{}{"status", "in", onlineStatuses}, "&", []interface{}{"sku", "in", []int{107439, 106266}}, "&", []interface{}{"datacenter", "=", dcMap[strings.ToUpper(datacenter)]}, }, Class: "Computer.ComputerWhere", Limit: limit, } } for { // Set the initial offset payload.LoadArg.Offset = offset req, err := json.Marshal(payload) if err != nil { return nil, err } res, err := c.ctk.Query(req) if err != nil { return nil, err } var r ctkQuery if err = json.Unmarshal(res, &[]interface{}{&r}); err != nil { return nil, err } // Increment the offset to the next iteration offset += limit // If the returned count is less than the max returned, // then that means we are on our last iteration. if r.Count < limit { last = true } // Range through the result and build the ctkDeviceList for _, d := range r.Result { ctkDeviceList = append(ctkDeviceList, ctkToMongoMap(d)) } logrus.Infof("collected %d devices", len(ctkDeviceList)) // If the last flag was set, break out of the loop if last { break } } return } // ctkToMongoMap is a shared function that maps the data returned from CTK into the type we use to // interface with Mongo. func ctkToMongoMap(d ctkResult) Device { nd := Device{ ID: d.Number, PrimaryIP: d.PrimaryIP, RackConnect: d.RCConfigs != "", SAN: d.SAN, SharePoint: d.SharePoint, MBU: len(d.MBUDevices) != 0, IconURL: d.Icon, } nd.Device.PrimaryIP = d.PrimaryIP nd.Device.MakeModel = d.PlatformName nd.Device.ComponentConfigType = d.PlatformModel nd.Device.Type = d.PlatformType nd.Device.Status = d.Status nd.Device.OS = d.OS // set as customer managed if any of these skus are assigned to the device nd.Device.CustomerManaged = isCustomerManaged(d.SKUNumbers) // Check SKUs for any matching MSSQL Support types nd.MSSQLSupport = hasSKU(d.SKUNumbers, collection.SKUsMSSQLSupport) // Check SKUs for any matching MySQL Support types nd.MySQLSupport = hasSKU(d.SKUNumbers, collection.SKUsMySQLSupport) // Check SKUs for any matching Oracle Support types nd.OracleSupport = hasSKU(d.SKUNumbers, collection.SKUsOracle) // Check SKUs for any matching Dedicated Exchange types nd.DEX = hasSKU(d.SKUNumbers, collection.SKUsDEX) // Check SKUs for any matching DoubleTake types nd.DoubleTake = hasSKU(d.SKUNumbers, collection.SKUsDoubleTake) // Check SKUs for any matching Skype For Business types nd.Skype = hasSKU(d.SKUNumbers, collection.SKUsSkypeForBusiness) // Setting specific flags for the RC versions found // in associated RC configurations linked to the account. nd.RCv2, nd.RCv3 = getRCConfigs(d) // Support for the os_name data that originated from data warehouse. It appears that information // is pulled from the os_type attribute in CTK, and manipulating the data a little. We are replicating // that duplication here. See: GM-236 nd.Device.OSName = func() string { if d.OSType == "Microsoft Windows" { return "Microsoft" } if d.OSType == "Misc" { return "Other" } if d.OSType == "" || d.OSType == "None" { return "Unknown" } return d.OSType }() nd.Device.DataCenter = d.DC nd.Device.SLA = d.DeviceSLA nd.Device.HostName = d.Name nd.Account.Name = d.AccountName nd.Account.Status = d.AccountStatus nd.Account.Number = util.Atoi(d.AccountNumber) nd.Account.PrimaryContact = d.PrimaryContact nd.Account.TeamName = d.TeamName nd.Account.AM = d.AM nd.Account.Type = d.AccountSLA nd.Account.BDC = d.BDC nd.Account.LeadTech = d.LeadTech nd.Account.CMRR = strconv.Itoa(int(d.AccountMRR)) nd.DPAccount = d.SKUs.SubAttributes.DPAccount.Value nd.DPDevice = d.SKUs.SubAttributes.DPDevice.Value return nd } // getRCConfigs function will unmarshal the RackConnect Configurations struct and // range through its data to determine the status of RCv2 or RCv3 customers based on the // RC Configurations and matching Region -> DC // // This function is left in as a logic placeholer, however, we are not going to implement this // dynamic data just yet. func getRCConfigs(d ctkResult) (hasRCv2, hasRCv3 bool) { var allRCConfigs []rackConnectCfg // For some reason, the rack_connect_configs element brings back a json payload // that has been escaped into a single string. We need format it back into // json to unmarshal into a struct. if byt, err := json.RawMessage(d.RCConfigs).MarshalJSON(); err == nil { // Now finally back into the RC cfg struct if err = json.Unmarshal(byt, &allRCConfigs); err != nil { return } } // Ranging through the RC configs to find ones that match the device region for _, rcc := range allRCConfigs { if strings.Contains(strings.ToLower(d.DC), strings.ToLower(rcc.Region)) { if rcc.RackConnectVersion == 3 { hasRCv3 = true } if rcc.RackConnectVersion == 2 { hasRCv2 = true } } } return } func hasSKU(deviceSKUs, targetSKUs []int) bool { for _, s := range deviceSKUs { for _, s2 := range targetSKUs { if s == s2 { return true } } } return false } func isCustomerManaged(skus []int) bool { for _, s := range skus { for _, s2 := range collection.SKUsCustomerManaged { if s == s2 { return true } } } return false }