// 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 ecm import ( "fmt" "net" "strconv" "strings" "github.com/sirupsen/logrus" "github.rackspace.com/DCM/discover/common" "github.rackspace.com/DCM/discover/log" "github.rackspace.com/DCM/netparse" "github.rackspace.com/gorax/clients/dcxAutohost" ) // Update method takes a Modeled device and performs the logic on how, what and where we should update // the environment management system. // This function is designed to take a list of devices belonging to the same account. func (c *DCX) Update(config Config, modify bool) log.Collector { logger := log.Init() // 2.) BUILD DCX NetEnv: Range through the environments we've created and attempt to build // them out in environment manager using the DCX API for env := range config.Environments { // 2.a.) FIND EXISTING DCX NetEnv: Range through the devices and gateways to get the environment // info from each device. This will help us determine if an environment already exist. // A success will be 0 or 1 environment. 2 or more will result in an error. envNumber, err := c.ENVNumber(config.Environments[env]) if err != nil { logger.Error("Update:ENVNumber", config.Environments[env].Alias(), err) continue } // 2.b.) CREATE DCX NetEnv: If we cannot find an environment, run the EnvironmentCreate method to // build a blank environment with only the gateway devices defined, store the new // environment number in the envNumber variable. if envNumber == "" && modify { if envNumber, err = c.dcx.EnvironmentCreate(config.Environments[env].GatewayDevices()); err != nil { logger.Error("Update:EnvironmentCreate", config.Environments[env].Alias(), err) continue } logger.Info("MODIFIED-Update:EnvironmentCreate", envNumber) } else if envNumber != "" { logger.Info("WOULD-UseExisting: DCX Environment", envNumber, fmt.Sprintf("%v", config.Environments[env].GatewayDevices())) } else { logger.Info("WOULD-Create: DCX ENV with Gateways", config.Environments[env].Alias(), fmt.Sprintf("%v", config.Environments[env].GatewayDevices())) } config.Environments[env].Number = envNumber // 2.c.) VLAN XLATE GATHER: Pull vlan translations from the DB and compare them with the // vlans that we discovered. If there are matches, set the migration vlans so we don't // duplicate the vlan in DCX tooling. xlates, err := c.db.GetTranslations(envNumber) if err != nil { c.Error("Update:GetTranslations", envNumber, err) } config.Environments[env].SetTranslationsFromDB(xlates) // 2.d.) DCX NetEnv GATHER: Run the method to gather all DCX data we will need for an environment. // If this is a newly created environment, this will be mostly empty struct elements. envData := c.GetDCXEnv(envNumber) // 2.e.) ADD VLANS TO NetEnv: At this point, we definitely have an environment in DCX for this data, now // we can start with populating the missing data, starting with the VLANs. for i, vl := range config.Environments[env].Translations { // 2.e.1) Now lets check if we have stored a translation already for this vlan. If we // have, we can assume the vlan already exist in the DCX environment. if vl.MigrationVlan.Vlan > 2 { continue } // 2.e.2) If there is a configured vlan zone name, we likely set it because this is a public // vlan and it needs to be added to the NA zone, not to environment manager. if strings.HasPrefix(vl.MigrationVlan.ZoneName, "ZN-") { continue } // 2.e.3) If the vlan doesn't exist, lets create that now. vlanReq := dcxAutohost.VlanRequest{ Segment: dcxAutohost.NetworkSegment{ Name: vl.ConfiguredVlan.Segment(), CIDR: vl.ConfiguredVlan.CIDR, }, } // Check the modify flag, and just log a change if modify=false if !modify { logger.Info("WOULD-Update:AddVlan", config.Environments[env].Alias(), fmt.Sprintf("Vlan%d %s", vl.ConfiguredVlan.Vlan, vl.ConfiguredVlan.Segment())) continue } logrus.Infof("Adding Vlan: %v", vlanReq.Segment) vlanRes, err := c.dcx.EnvironmentAddVlan(envNumber, vlanReq, map[string]string{"vlan_number": strconv.Itoa(vl.ConfiguredVlan.Vlan)}) if err != nil { vlanRes, err = c.dcx.EnvironmentAddVlan(envNumber, vlanReq, nil) if err != nil { logger.Error(fmt.Sprintf("Update:AddVlan%d", vl.ConfiguredVlan.Vlan), envNumber, err) continue } } logger.Info("MODIFIED-Update:AddVlan", envNumber, fmt.Sprintf("Vlan%d %s", vl.ConfiguredVlan.Vlan, vl.ConfiguredVlan.Segment())) // 2.e.4) We were able to create the new vlan, lets add it to the translations list. if err := config.Environments[env].Translations[i].AddTranslation(vlanRes); err != nil { logger.Error(fmt.Sprintf("Update:AddMigrationVlan%d", vl.ConfiguredVlan.Vlan), envNumber, err) } } // 2.f.) ADD DEVICES TO NetEnv: Now we need to join any devices that aren't already associated to // the environment. for _, dv := range config.Environments[env].Devices { // 2.f.1) ADD TO NetEnv: At this point, we've validate the device does not exist in the // environment, so now we need to add it. // Check the modify flag, and just log a change if modify=false if !envData.HasDevice(dv) { if !modify { logger.Info("WOULD-Update:AddDevice", config.Environments[env].Alias(), fmt.Sprintf("%d", dv.ID)) } else { if err := c.dcx.EnvironmentAddDevice(dv.ID, envNumber, dv.DeviceType()); err != nil { logger.Error("Update:AddDevice", dv.ID, err) continue } logger.Info("MODIFIED-Update:AddDevice", envNumber, fmt.Sprintf("%d", dv.ID)) if err := c.db.WritePortMaps(dv.ID, dv.PortMap); err != nil { logger.Error("Update:AddDevice:WritePortMaps", dv.ID, err) } } } // 2.f.2) DEVICE+VLAN ASSOC: Once we successfully add the device, now we need to // join the vlans to the device using the vlan translations logic. for _, vx := range config.Environments[env].DeviceVlanAssigner(dv) { // Check the modify flag, and just log a change if modify=false if !modify { logger.Info("WOULD-Update:JoinExNetVlan", config.Environments[env].Alias(), fmt.Sprintf("%d+Vlan%d", dv.ID, vx)) continue } if err := c.dcx.DeviceJoinExNetVlan(dv.ID, vx); err != nil { logger.Error("Update:JoinExNetVlan", dv.ID, err) continue } logger.Info("MODIFIED-Update:JoinExNetVlan", envNumber, fmt.Sprintf("%d+Vlan%d", dv.ID, vx)) } // 2.f.3) PORT+SEGMENT ASSOC: This process will generate a list of vlans that we need to associate to the // port in environment manager, then it will call DCX to create the port+segment association. for _, s := range config.Environments[env].PortSegmentAssigner(dv) { // If we do not have a translations, no reason to proceed. if s.VlanNumber == 0 { continue } // Check the modify flag, and just log a change if modify=false if !modify { logger.Info("WOULD-Update:PortSegment", config.Environments[env].Alias(), fmt.Sprintf("%d %s[%d] + Vlan%d", dv.ID, s.Switch, s.SwitchPort, s.VlanNumber)) continue } if err := c.dcx.PutSwitchVlans(dv.ID, s); err != nil { logger.Error("Update:PutSwitchVlans", dv.ID, err) continue } logger.Info("MODIFIED-Update:PortSegment", envNumber, fmt.Sprintf("%d %s[%d] + Vlan%d", dv.ID, s.Switch, s.SwitchPort, s.VlanNumber)) } } // 2.g.) ADD IP BLOCKS TO NetEnv: This next process will loop through the routed blocks in the environment // and determine if we need to allocate, carve, and assign IP blocks to the account. for _, rt := range config.Environments[env].RoutedBlocks { for _, dt := range rt.Dest { // Check if this IP block already exist in the DCX NetEnv info. If so, // move along sir. if envData.HasIPBlock(dt) { continue } // We're going to add this block to the env list manually now because whether we // experience an issue or not, we don't need to continue to try. envData.IPBlockCIDR.Public = append(envData.IPBlockCIDR.Public, dt.CIDR()) // Check the modify flag, and just log a change if modify=false if !modify { logger.Info("WOULD-Update:AddBlock", config.Environments[env].Alias(), dt.CIDR()) continue } // 2.g.1.) ASSIGN/CARVE: Now we know we have block that we need to add to the environment, we need // to first carve the block if it's not already carved and allocate it. if _, err := c.dcx.PublicIPBlocksCarve(config.Environments[env].IPBlockAssigner(dt, envNumber)); err != nil { logger.Error("Update:PublicIPBlocksCarve", dt.CIDR(), err) continue } logger.Info("MODIFIED-Update:AddBlock", envNumber, dt.CIDR()) } } // 2.h.) Now we're at a point where we have almost all the information about the environments. We need to // go through the list now and trace IP->mac-addresses so we can get proper IP/NAT assignments. for _, assign := range config.Environments[env].IPAddressAssigner(config.Environments[env].EnvNATs()) { if len(assign.Addresses) == 0 { continue } // Check the modify flag, and just log a change if modify=false if !modify { for _, ad := range assign.Addresses { logger.Info("WOULD-Update:IPAddressAssign", assign.DeviceID, fmt.Sprintf("%s=%s %s", ad.Role, ad.Address, ad.NAT.Address)) } continue } // 2.h.1.) We want to break up the assignments by device so we can generate a log message for each // one. Also, this may be a large payload in some cases, so we don't want it to take forever. if _, err := c.dcx.IPAddressAssign([]dcxAutohost.IPAssign{assign}); err != nil { logger.Error("Update:IPAddressAssign", assign.DeviceID, err) } } } // 3.) BUILD NETWORK ARCHITECT SEGMENTS: Now we need to take any of the public SVIs we found and // build them in network-architect for _, vlan := range config.PublicVlans { if vlan.SVIConfig == nil { continue } if vlan.MigrationZone == "" { c.Error("Update:AddSVI:ParseCIDR", vlan.Vlan, fmt.Errorf("no migration zone")) continue } if xlVlan := c.PublicXlate(vlan); xlVlan != nil { for i := range config.Environments { config.Environments[i].SetVlanTranslation(vlan.Vlan, *xlVlan) } logger.Info("Update:AddSVI:AlreadyExist", vlan.SegmentName(), fmt.Sprintf("%s:%v", vlan.Name, xlVlan)) continue } for _, svi := range vlan.SVIConfig.IPAddress { if svi.Secondary { continue } _, ipNet, err := net.ParseCIDR(svi.CIDR()) if err != nil { c.Error("Update:AddSVI:ParseCIDR", vlan.Vlan, err) continue } pre, _ := ipNet.Mask.Size() sviReq := dcxAutohost.SVI{ ZoneNumber: vlan.MigrationZone, NetworkAddress: ipNet.IP.String(), PrefixLength: pre, } if !modify { logger.Warn("WOULD-Update:AddSVI", vlan.SegmentName(), fmt.Sprintf("%s:%v", vlan.Name, sviReq)) continue } res, err := c.dcx.IPBlocksSVI(sviReq) if err != nil { logrus.Errorf("Update:AddSVI:IPBlocksSVI:%s", err) c.Error("Update:AddSVI:IPBlocksSVI", vlan.Vlan, err) continue } if common.Atoi(res.VlanNumber) < 2 { continue } vlanx := common.VlanX{ Vlan: common.Atoi(res.VlanNumber), ZoneName: vlan.MigrationZone, NetworkSegments: []string{res.Name}, CIDR: ipNet.String(), } // Add the newly created segment as a translation for i := range config.Environments { config.Environments[i].SetVlanTranslation(vlan.Vlan, vlanx) } } } for _, env := range config.Environments { // 2.e.) Once we have created all the vlans in environment manager, we store them as migration vlans // in the Translations struct, we need to write that structure to the DB so we can recall // translations if they already exist. if modify { if err := c.db.WriteTranslations(env.Number, env.Translations); err != nil { logger.Error("Update:WriteTranslations:AddVlans", env.Number, err) } } } return *logger } // PublicXlate will attempt to pull a translation across all same zoned environments to see if // we already have a public translation before we attempt to POST a new Network-Architect segment. // If we find a match, we'll return the translation back so that the xlates for this environment // can be updated. func (c *DCX) PublicXlate(vl netparse.Vlan) *common.VlanX { xlate, err := c.db.GetPublicTranslation(vl.MigrationZone) if err != nil { c.Error("PublicXlate:DB:GetPublicTranslation", vl.Name, fmt.Errorf("PublicXlate:%s", err)) return nil } if xlate == nil { return nil } return &common.VlanX{ Vlan: xlate.MigrationVlan.VlanNumber, ZoneName: xlate.MigrationVlan.ZoneName, NetworkSegments: xlate.MigrationVlan.NetworkSegments, } } // ENVNumber will poll all the devices in our mock environment to see if any of those devices are // part of an environment, if so, it will return that environment number, if there are more than // one environment numbers found, we will return an error. func (c *DCX) ENVNumber(env NetEnv) (string, error) { envs := make([]string, 0) for _, gw := range env.Gateways { devInfo, err := c.dcx.DeviceEnvironment(gw.ID) if err != nil { // If the error is a 404 not found, that's just indicating that the device doesn't belong // to an environment. if common.IContains(err.Error(), "404 not found") { continue } // Welp, we got an unknown error from DCX for the DeviceEnvironment call, so we // can't proceed with any other logic to add/update this device in environment manager. return "", fmt.Errorf("[%d] companies/environment:%s", gw.ID, err) } envs = append(envs, devInfo.Environment) } for _, dev := range env.Devices { devInfo, err := c.dcx.DeviceEnvironment(dev.ID) if err != nil { // If the error is a 404 not found, that's just indicating that the device doesn't belong // to an environment. if common.IContains(err.Error(), "404 not found") { continue } // Welp, we got an unknown error from DCX for the DeviceEnvironment call, so we // can't proceed with any other logic to add/update this device in environment manager. return "", fmt.Errorf("[%d] companies/environment:%s", dev.ID, err) } envs = append(envs, devInfo.Environment) } uniqueENVs := common.UniqueStrings(envs) if len(uniqueENVs) > 1 { return "", fmt.Errorf("multiple DCX ENVs for discovered NetEnv %v", uniqueENVs) } if len(uniqueENVs) == 1 { return uniqueENVs[0], nil } return "", nil }