// 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 dp allows for the integration of MyDataPipe data into the Migration Database. package dp import ( "math" "net/http" "regexp" "strconv" "sync" "github.rackspace.com/DCM/tools/migMeta" "github.rackspace.com/DCM/migration/pkg/collection/mongo" "github.rackspace.com/DCM/tools" "github.rackspace.com/gorax/clients/mydp" "github.rackspace.com/gorax/clients/passwordsafe" "github.rackspace.com/gorax/clients/unipede" "github.rackspace.com/gorax/libs/switchlib/swClient" ) // concurrent go routines used in Update() const routines = 3 type ( // Client type represents client references to third-party resources required // for the methods within this package. Client struct { sync.RWMutex db *mongo.Client dp *mydp.Client m migMeta.Migration u *unipede.Client switchMap map[string]*swClient.Switch eth2Map map[string]mydp.Device } // Device struct is a representation of a SpecificDevice that we get back // from the GDCI implementation of a EBI-ODS SpecificDevice Device struct { ID int `json:"_id"` RSID int `json:"rackspace_id"` Account Account `json:"account"` Device SpecificDevice `json:"SpecificDevice"` Environment tools.EnvironmentData `json:"environment,omitempty"` Type string `json:"type"` Icon string `json:"icon_url"` DC string `json:"dc"` SAN bool `json:"san"` DAS bool `json:"has_das"` MBU bool `json:"has_mbu"` PrimaryIP string `json:"primary_ip"` Network struct { Address struct { DracIPs []tools.IPAddressDetails `json:"drac_ips"` MBUIPs []tools.IPAddressDetails `json:"mbu_ips"` } `json:"address"` Public struct { Zone string `json:"zone"` } `json:"public"` } `json:"network"` } // Account type defines the account related parameters // from a datapipe SpecificDevice. Account struct { Number int `json:"number"` Name string `json:"name"` Type string `json:"type"` LeadTech string `json:"lead_tech,omitempty"` Status string `json:"status"` TeamName string `json:"team_name"` GeographicLocation string `json:"geographic_location,omitempty"` CMRR string `json:"cmrr,omitempty"` AM string `json:"account_manager"` BDC string `json:"account_bdc"` PrimaryContact string `json:"primary_contact"` } // SpecificDevice type gathers the information about the specific // device details in admin.mydatapipe SpecificDevice struct { ID string `json:"id"` OS string `json:"os,omitempty"` HostName string `json:"hostname"` DataCenter string `json:"datacenter"` Type string `json:"type,omitempty"` OnlineStatus string `json:"online_status,omitempty"` Status string `json:"status"` PrimaryIP string `json:"primary_ip"` CMRR string `json:"cmrr"` MakeModel string `json:"make_model"` FailoverID string `json:"failover_id,omitempty"` OSName string `json:"os_name"` ComponentConfigType string `json:"container_component_configuration_type_name,omitempty"` SLA string `json:"device_sla"` CustomerManaged bool `json:"customer_managed"` } ) var privIP = regexp.MustCompile(`([pP]\[\d+])(.*)`) var rackU = regexp.MustCompile(`^\D*([0-9.]+)`) // 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 { return &Client{ db: mongo.NewContext(r), dp: mydp.FromContext(r.Context()), u: unipede.FromContext(r.Context()), switchMap: make(map[string]*swClient.Switch), eth2Map: make(map[string]mydp.Device), } } // New function will take a credential to create a myDP client func New(c passwordsafe.Credential, db *mongo.Client, uni *unipede.Client) (*Client, error) { dpClient, err := mydp.New(c.Credential.Username, c.Credential.Password, false) if err != nil { return nil, err } return &Client{ db: db, dp: dpClient, u: uni, switchMap: make(map[string]*swClient.Switch), eth2Map: make(map[string]mydp.Device), }, nil } // 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 } // SetMigrationMetadata sets the migration metadata for the client instance func (c *Client) SetMigrationMetadata(m migMeta.Migration) *Client { c.m = m return c } func getRackU(s string) float32 { m := rackU.FindStringSubmatch(s) if len(m) == 2 { return mustFloat32(m[1]) } return 0 } func mustFloat32(s string) float32 { i, _ := strconv.Atoi(s) return float32(i) } func mustInt(s string) int { i, _ := strconv.Atoi(s) return i } func mustString(s *string) string { if s == nil { return "" } return *s }