// 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 is designed to integrate CORE CTK related actions into the MigrationDB collections and updates. package ctk import ( "math" "net/http" "github.rackspace.com/DCM/migration/pkg/collection/ebi" "github.rackspace.com/DCM/migration/pkg/collection/mongo" "github.rackspace.com/gorax/clients/goctk" ) type ( // Client type represents client references to third-party resources required // for the methods within this package. Client struct { ctk *goctk.Client db *mongo.Client dMart *ebi.Client } // Device struct is a representation of a device that we get back // from the GDCI implementation of a EBI-ODS device Device struct { ID int `json:"_id" bson:"_id"` Account struct { Number int `json:"number" bson:"number"` Name string `json:"name" bson:"name"` Type string `json:"type" bson:"type"` LeadTech string `json:"lead_tech" bson:"lead_tech"` Status string `json:"status" bson:"status"` TeamName string `json:"team_name" bson:"team_name"` GeographicLocation string `json:"geographic_location" bson:"geographic_location"` CMRR string `json:"cmrr" bson:"cmrr"` AM string `json:"account_manager" bson:"account_manager"` BDC string `json:"account_bdc" bson:"account_bdc"` PrimaryContact string `json:"primary_contact" bson:"primary_contact"` } `json:"account" bson:"account"` Device struct { OS string `json:"os" bson:"os"` HostName string `json:"hostname" bson:"hostname"` DataCenter string `json:"datacenter" bson:"datacenter"` Type string `json:"type" bson:"type"` OnlineStatus string `json:"online_status" bson:"online_status"` Status string `json:"status" bson:"status"` PrimaryIP string `json:"primary_ip" bson:"primary_ip"` CMRR string `json:"cmrr" bson:"cmrr"` MakeModel string `json:"make_model" bson:"make_model"` OSName string `json:"os_name" bson:"os_name"` ComponentConfigType string `json:"container_component_configuration_type_name" bson:"container_component_configuration_type_name"` SLA string `json:"device_sla" bson:"device_sla"` CustomerManaged bool `json:"customer_managed" bson:"customer_managed"` } `json:"device" bson:"device"` Environment struct { Current Location `json:"current,omitempty" bson:"current,omitempty"` Migration Location `json:"migration,omitempty" bson:"migration,omitempty"` } `json:"environment,omitempty" bson:"environment,omitempty"` SAN bool `json:"san" bson:"san"` DAS bool `json:"has_das" bson:"has_das"` SharePoint bool `json:"has_sharepoint" bson:"has_sharepoint"` MBU bool `json:"has_mbu" bson:"has_mbu"` DEX bool `json:"has_dex" bson:"has_dex"` DoubleTake bool `json:"has_doubletake" bson:"has_doubletake"` Skype bool `json:"has_skype" bson:"has_skype"` RackConnect bool `json:"has_rackconnect" bson:"has_rackconnect"` MSSQLSupport bool `json:"has_mssql_support" bson:"has_mssql_support"` MySQLSupport bool `json:"has_mysql_support" bson:"has_mysql_support"` OracleSupport bool `json:"has_oracle_support" bson:"has_oracle_support"` PrimaryIP string `json:"primary_ip" bson:"primary_ip"` IconURL string `json:"icon_url" bson:"icon_url"` RCv2 bool `json:"rcv2" bson:"rcv2"` RCv3 bool `json:"rcv3" bson:"rcv3"` DPDevice string `json:"datapipe_device" bson:"datapipe_device"` DPAccount string `json:"datapipe_account" bson:"datapipe_account"` } // Location type is a representation of the physical assignment of // a device in the datacenter. Location struct { Cabinet string `json:"cabinet,omitempty" bson:"cabinet,omitempty"` Location string `json:"location,omitempty" bson:"location,omitempty"` DataCenter string `json:"data_center" bson:"data_center"` Container string `json:"container" bson:"container"` StartingSpace int `json:"starting_space" bson:"starting_space"` SwitchPorts []string `json:"switch_ports" bson:"switch_ports"` } ctkQuery struct { Count int `json:"count" bson:"count"` Class string `json:"class" bson:"class"` Limit int `json:"limit" bson:"limit"` Result []ctkResult `json:"result" bson:"result"` Offset int `json:"offset" bson:"offset"` } ctk struct { Class string `json:"class" bson:"class"` LoadArg loadArg `json:"load_arg" bson:"load_arg"` Attributes []interface{} `json:"attributes" bson:"attributes"` } loadArg struct { Values []interface{} `json:"values" bson:"values"` Class string `json:"class" bson:"class"` Limit int `json:"limit,omitempty" bson:"limit,omitempty"` Offset int `json:"offset,omitempty" bson:"offset,omitempty"` } ctkResult struct { Number int `json:"number" bson:"number"` PlatformType string `json:"platform_type" bson:"platform_type"` PlatformName string `json:"platform_name" bson:"platform_name"` PlatformModel string `json:"platform_model" bson:"platform_model"` DC string `json:"datacenter.symbol" bson:"datacenter.symbol"` Status string `json:"status.name" bson:"status.name"` Name string `json:"name" bson:"name"` OS string `json:"os" bson:"os"` OSName string `json:"os_name" bson:"os_name"` OSType string `json:"os_type" bson:"os_type"` LeadTech string `json:"account.primary_lead_tech.name" bson:"account.primary_lead_tech.name"` TeamName string `json:"support_team.name" bson:"support_team.name"` AM string `json:"account.account_exec.name" bson:"account.account_exec.name"` BDC string `json:"account.business_development.name" bson:"account.business_development.name"` PrimaryContact string `json:"account.primary_contact.name" bson:"account.primary_contact.name"` AccountMRR float64 `json:"account.latest_cmrr" bson:"account.latest_cmrr"` RCConfigs string `json:"account.rack_connect_configs" bson:"account.rack_connect_configs"` AccountNumber string `json:"account.number" bson:"account.number"` SAN bool `json:"account.has_dsan_devices" bson:"account.has_dsan_devices"` AccountStatus string `json:"account.status.name" bson:"account.status.name"` AccountType string `json:"account.type.name" bson:"account.type.name"` AccountSLA string `json:"account.service_level_name" bson:"account.service_level_name"` Icon string `json:"platform.icon_path" bson:"platform.icon_path"` DeviceSLA string `json:"service_level_name" bson:"service_level_name"` PrimaryIP string `json:"primary_ip" bson:"primary_ip"` AccountName string `json:"account.name" bson:"account.name"` MBUDevices []int `json:"account.mbu_devices.number" bson:"account.mbu_devices.number"` SharePoint bool `json:"account.has_sharepoint" bson:"account.has_sharepoint"` SKUs skuData `json:"parts.partributes" bson:"parts.partributes"` SKUNumbers []int `json:"parts.sku_number" bson:"parts.sku_number"` } skuData struct { SubAttributes struct { DPAccount struct { Value string `json:"value" bson:"value"` } `json:"Account" bson:"Account"` DPDevice struct { Value string `json:"value" bson:"value"` } `json:"Device(s)" bson:"Device(s)"` DPValid struct { Value string `json:"value" bson:"value"` } `json:"Product status"` } `json:"subattributes" bson:"subattributes"` } rackConnectCfg struct { ConfigurationID string `json:"ConfigurationId" bson:"ConfigurationId"` ConfigurationName string `json:"ConfigurationName" bson:"ConfigurationName"` Region string `json:"Region" bson:"Region"` RackConnectVersion int `json:"RackConnectVersion" bson:"RackConnectVersion"` HybridControlDeepLink string `json:"HybridControlDeepLink" bson:"HybridControlDeepLink"` CoreAccountNumber int `json:"CoreAccountNumber" bson:"CoreAccountNumber"` CloudAccounts []struct { CloudAccountID string `json:"CloudAccountId" bson:"CloudAccountId"` CloudDDIAccount int `json:"CloudDDIAccount" bson:"CloudDDIAccount"` Username string `json:"Username" bson:"Username"` } `json:"CloudAccounts" bson:"CloudAccounts"` } ) // New function will take the direct references to the clients required for this package. // This is under the assumption that you have initiated your own client and are passing by // reference. func New(ctk *goctk.Client, db *mongo.Client) *Client { return &Client{ ctk: ctk, db: db, } } // NewContext function will take a pointer to a Request, and pull the client references // from the Context() offered within the default net/http package. func NewContext(r *http.Request) (*Client, error) { d, err := ebi.NewDMART(r) return &Client{ ctk: goctk.FromContext(r.Context()), db: mongo.NewContext(r), dMart: &d, }, err } // Chunk is a small internal function to break a list of Device types into // smaller chunks that we can send to go routines. func Chunk(devList []Device, size int) (chunk [][]Device) { float := float64(len(devList)) / float64(size) chunkSize := int(math.Ceil(float)) for i := 0; i < len(devList); i += chunkSize { end := i + chunkSize if end > len(devList) { end = len(devList) } chunk = append(chunk, devList[i:end]) } return } // ChunkString is a small internal function to break a list of Device types into // smaller chunks that we can send to go routines. func ChunkString(devList []int, size int) (chunk [][]int) { float := float64(len(devList)) / float64(size) chunkSize := int(math.Ceil(float)) for i := 0; i < len(devList); i += chunkSize { end := i + chunkSize if end > len(devList) { end = len(devList) } chunk = append(chunk, devList[i:end]) } return }