// 2020 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 servicenet import ( "fmt" "log" "sort" "strconv" "strings" "github.com/sirupsen/logrus" "github.rackspace.com/DCM/gomigrate/shared" "github.rackspace.com/DCM/tools" "github.rackspace.com/gorax/clients/dcxAutohost" "github.rackspace.com/gorax/gokeys" ) type ( client struct { dcx *dcxAutohost.Client devices []int coreToken string identityToken string envVlans map[string][]dcxAutohost.EnvironmentVlan } ) // pluginFunc function is executed from the gomigrate caller func pluginFunc(cfg gokeys.Settings) { // Set all the key from the arg parser c := client{ devices: *argD, envVlans: make(map[string][]dcxAutohost.EnvironmentVlan), } // Pull an identity token from the users gokeys keychain key, err := cfg.Identity(false) if err != nil { log.Fatal(err) } c.identityToken = key.Password // Pull a core token from the gokeys keychain coreKey, err := cfg.Core(false) if err != nil { log.Fatal(err) } c.coreToken = coreKey.Password // Get the device list from a list of move-groups or devices. devices, err := shared.MongoDevices([]string{}, nil, c.devices, "", coreKey.Password, make(map[string]interface{})) if err != nil { logrus.Fatal(err) } // Stop here if we did not find any devices. if len(devices) == 0 { logrus.Fatalf("no DB devices found in devices:(%v)", c.devices) } // Grab the client for the DCX API c.dcx, err = dcxAutohost.New(dcxAutohost.WithToken(c.identityToken), false) if err != nil { logrus.Fatal(err) } logrus.Infof("Allocating ServiceNet on %d devices...", len(devices)) for _, d := range devices { if d.Environment.Number == "" { logrus.Errorf("[%d] No Environment exist for device.", d.ID) continue } if err := c.allocateSNetIP(d); err != nil { logrus.Error(err) } } } func (c *client) allocateSNetIP(d tools.Device) error { if _, ok := c.envVlans[d.Environment.Number]; !ok { envVlans, err := c.dcx.EnvironmentVlansWithSecondary(d.Environment.Number) if err != nil { return err } c.envVlans[d.Environment.Number] = envVlans } priVlan := c.getPrimaryExNetVlan(d) if priVlan == nil { return fmt.Errorf("[%d] SNet:Assign:No IP or Vlan Port to determine the primary AggExNet vlan", d.ID) } logrus.Debugf("[%d] SNet:Assign:Using primary ExNet Vlan%d %s", d.ID, priVlan.Vlan, priVlan.Segment) for i, e := range c.envVlans[d.Environment.Number] { if e.VlanNumber != priVlan.Vlan { continue } if e.SegmentName != priVlan.Segment { continue } // Found the primary ExNet vlan for the device in the environment vlans. if err := c.assignSnet(d, i, priVlan.Method); err != nil { return err } } return nil } func (c *client) assignSnet(d tools.Device, i int, method string) error { for _, sVlan := range c.envVlans[d.Environment.Number][i].SecondaryVlans { if sVlan.AvailableCount == 0 { continue } req := dcxAutohost.IPAssign{ Addresses: []dcxAutohost.Address{ {Type: "servicenet", CIDR: sVlan.PrivateBlock}, }, DeviceID: d.ID, } res, err := c.dcx.IPAddressAssign([]dcxAutohost.IPAssign{req}) if err != nil { logrus.Errorf("[%d] SNet:IPAddressAssign:%s:%s", d.ID, method, err) continue } for _, r := range res { if len(r.Errors) != 0 { logrus.Errorf("[%d] SNet:IPAddressAssign:%s:%s", d.ID, method, strings.Join(r.Errors, ";")) continue } for _, a := range r.Addresses { logrus.Infof("[%d] SNet:Assign:%s:Assigned %s", d.ID, method, a.Address) return nil } } } curr := c.envVlans[d.Environment.Number][i] // If we get here, there isn't an SNet segment associated to the ExNet Segment. newSNet, err := c.dcx.AddSecondaryVlan(d.Environment.Number, curr.VlanNumber) if err != nil { return fmt.Errorf("[%d] NewSegmentSNet:%s:Create SNetSegment failed AggExVlan%d %s; %w", d.ID, method, curr.VlanNumber, curr.SegmentName, err) } // Add the new segment to the list of secondaries for the AggExVlans c.envVlans[d.Environment.Number][i].SecondaryVlans = append(c.envVlans[d.Environment.Number][i].SecondaryVlans, newSNet) req := dcxAutohost.IPAssign{ Addresses: []dcxAutohost.Address{ {Type: "servicenet", CIDR: newSNet.PrivateBlock}, }, DeviceID: d.ID, } res, err := c.dcx.IPAddressAssign([]dcxAutohost.IPAssign{req}) if err != nil { return fmt.Errorf("[%d] NewSegmentSNet:IPAddressAssign:%s:%s", d.ID, method, err) } for _, r := range res { if len(r.Errors) != 0 { return fmt.Errorf("[%d] NewSegmentSNet:IPAddressAssign:%s:%s", d.ID, method, strings.Join(r.Errors, ";")) } for _, a := range r.Addresses { logrus.Infof("[%d] NewSegmentSNet:Assign:%s:Assigned %s", d.ID, method, a.Address) return nil } } return fmt.Errorf("[%d] SNet:Assign:%s:No SNet Segment associated to AggExVlan%d %s", d.ID, method, curr.VlanNumber, curr.SegmentName) } func (c *client) getPrimaryExNetVlan(d tools.Device) *PortVlan { if portVlan := c.getPrimaryExNetVlanByIP(d); portVlan != nil { portVlan.Method = "ByIPAssigned" return portVlan } if portVlan := c.getPrimaryExNetVlanByPort(d); portVlan != nil { portVlan.Method = "ByPortAssociation" return portVlan } if portVlan := c.getPrimaryExNetVlanBySegment(d); portVlan != nil { portVlan.Method = "ByVlanAssociation" return portVlan } return nil } func (c *client) getPrimaryExNetVlanByPort(d tools.Device) *PortVlan { vlans, err := c.dcx.SwitchVlans(d.ID) if err != nil { logrus.Errorf("[%d] SNet:Assign:PrimaryExNetVlanByPort:%s", d.ID, err) return nil } if len(vlans.SwitchVlans) == 0 { return nil } total := make([]PortVlan, 0) for _, vlan := range vlans.SwitchVlans { for _, port := range vlan.SwitchPortData { if !strings.EqualFold(port.PortType, "AggExNet") { continue } for _, data := range port.PortVlanData { if idx := getIndex(data, total); idx != -1 { total[idx].Count++ } else { total = append(total, PortVlan{VlanData: *data, Count: 1}) } } } } if len(total) < 1 { return nil } sort.Sort(byCount(total)) return &total[len(total)-1] } func (c *client) getPrimaryExNetVlanByIP(d tools.Device) *PortVlan { ips, err := c.dcx.DeviceIPs(d.ID) if err != nil { logrus.Errorf("[%d] SNet:Assign:PrimaryExNetVlanByIP:%s", d.ID, err) return nil } for _, item := range ips.Items { if item.DeviceNumber != strconv.Itoa(d.ID) { continue } nattedIP := "" for _, pub := range item.PublicIPs { if pub.Label == "primary" && pub.NattedTo != "" { nattedIP = pub.NattedTo break } } // First attempt is to go through all the private AggExNet IPs to see if any of them // are labeled as Primary. If one is, this one will take precedence over any other logic // on determining the primary ExNet vlan. for _, priv := range item.PrivateIPs { // If it's not an AggExNet IP, we don't care about it. if !strings.Contains(strings.ToLower(priv.NetworkType), "exnet") { continue } if priv.Label == "primary" { return &PortVlan{ VlanData: dcxAutohost.VlanData{Segment: priv.NetworkName, Vlan: *priv.VLANNumber}, } } } // If we get here, there was not a private IP tagged as primary. The next step is to // find the one that is Natted to the public IP if it exist. Finally, we will // just take the first AggExNet private IP in the list as the primary vlan. for _, priv := range item.PrivateIPs { // If it's not an AggExNet IP, we don't care about it. if !strings.Contains(strings.ToLower(priv.NetworkType), "exnet") { continue } if nattedIP == "" { return &PortVlan{ VlanData: dcxAutohost.VlanData{Segment: priv.NetworkName, Vlan: *priv.VLANNumber}, } } if priv.IPAddress == nattedIP { return &PortVlan{ VlanData: dcxAutohost.VlanData{Segment: priv.NetworkName, Vlan: *priv.VLANNumber}, } } } } // Final step is to return nil. If we get here, we were unable to find any AggExNet vlan to use // as a segment for the primary AggEx. return nil } func (c *client) getPrimaryExNetVlanBySegment(d tools.Device) *PortVlan { vlans, err := c.dcx.EnvironmentVlans(d.Environment.Number) if err != nil { logrus.Errorf("[%d] SNet:Assign:getPrimaryExNetVlanBySegment:%s", d.ID, err) return nil } if len(vlans) == 0 { return nil } for _, vlan := range vlans { for _, vlanDevice := range vlan.DeviceNumbers { // Return the first vlan the device is associated with. if vlanDevice == strconv.Itoa(d.ID) { return &PortVlan{ VlanData: dcxAutohost.VlanData{Segment: vlan.SegmentName, Vlan: vlan.VlanNumber}, } } } } return nil } type ( // PortVlan is a embedded VlanData struct from DCX so we can // add a found count to it PortVlan struct { dcxAutohost.VlanData Count int Method string } byCount []PortVlan ) func getIndex(data *dcxAutohost.VlanData, vlans []PortVlan) int { for i := range vlans { if data.Vlan != vlans[i].Vlan { continue } if data.Segment != vlans[i].Segment { continue } return i } return -1 } // Len implementation for the custom sort method func (a byCount) Len() int { return len(a) } // Less implementation for the custom sort method func (a byCount) Less(i, j int) bool { return a[i].Count < a[j].Count } // Swap implementation for the custom sort method func (a byCount) Swap(i, j int) { a[i], a[j] = a[j], a[i] }