// 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 discover contains the business logic to pull switchport data for a device and // interface with Rackspace systems to add to an environment. package ecm import ( "fmt" "strings" "github.rackspace.com/DCM/discover/cmdb" "github.rackspace.com/DCM/netparse" "github.rackspace.com/gorax/clients/dcxAutohost" "github.rackspace.com/gorax/tools/util" ) type ( // Environment type represents the different outputs from the DCX API // that describes an environment. DCXEnvironment struct { dcxAutohost.DeviceEnvInfo Vlans []dcxAutohost.EnvironmentVlan `json:"vlans"` IPBlockCIDR *dcxAutohost.IPBlockCIDR `json:"ip_blocks"` Devices []DCXDevice `json:"devices"` } // DCXDevice represents the details about a device we would receive from // the DCX APIs DCXDevice struct { ID int IPAddress *dcxAutohost.EnvironmentIPs `json:"ip_addresses"` NATs []*dcxAutohost.EnvironmentNat `json:"nats"` } ) // GetDCXEnv method will pull all the data we need from DCXs API for a // given environment number. func (c *DCX) GetDCXEnv(env string) DCXEnvironment { e := DCXEnvironment{} info, err := c.dcx.EnvironmentData(env) if err != nil { return e } c.Debug("GetDCXEnv:EnvironmentData", env) e.ID = info.ID e.Name = info.Name e.Environment = info.Environment e.Gateways = info.Gateways // Gather the VLANs associated to an environment // See: https://api.dcx.rackspace.com/#environments__environment_number__vlans_get if e.Vlans, err = c.dcx.EnvironmentVlansWithSecondary(env); err != nil { c.Error("GetDCXEnv:VlansWithSecondaryVlans", env, err) return e } c.Debug("GetDCXEnv:VlansWithSecondaryVlans", env) // Gather the public/private IP blocks associated to the environment // See: https://api.dcx.rackspace.com/#environments__environment_number__ip_block_cidrs_get if e.IPBlockCIDR, err = c.dcx.IPBlockCIDR(env); err != nil { c.Error("GetDCXEnv:IPBlocksInEnvironment", env, err) return e } c.Debug("GetDCXEnv:IPBlocksInEnvironment", env) // get all the devices associated to an environment // See: https://api.dcx.rackspace.com/#environments__environment_number__devices_get devices, err := c.dcx.EnvironmentDevices(env) if err != nil { c.Error("GetDCXEnv:DevicesInEnvironment", env, err) return e } c.Debug("GetDCXEnv:DevicesInEnvironment", env) // Add the gateway devices to the device list since the initial call // will omit the gateway devices. devices = append(devices, e.Gateways...) // Filter out any devices that may appear twice in the list (for instance, if // gateway devices are somehow returned in both calls) //nolint devices = util.UniqueInts(devices) // Gather all the IP information public/private/NATs for all devices in the environment // See: https://api.dcx.rackspace.com/#environments__environment_number__devices_ip_addresses_get ipData, err := c.dcx.EnvironmentIpAddress(env) if err != nil { c.Error("GetDCXEnv:DeviceIPAssignments", env, err) return e } c.Debug("GetDCXEnv:DeviceIPAssignments", env) // Range through the response and append on the IPAddress info and NAT info for a given device // to the deviceMap data we created above for _, data := range ipData.Devices { e.Devices = append(e.Devices, DCXDevice{ ID: data.DeviceNumber, IPAddress: data.IpAddress, NATs: data.Nats, }) } return e } // VlanExist will check if the vlan passed in is already in the DCXEnvironment // structure. func (e *DCXEnvironment) VlanExist(v netparse.Vlan) bool { for _, vl := range e.Vlans { if strings.Contains(vl.SegmentName, fmt.Sprintf("VL%d_", v.Vlan)) { return true } } return false } // HasDevice will check if the passed in device exist in the DCX environment. func (e *DCXEnvironment) HasDevice(d cmdb.Model) bool { for _, dv := range e.Devices { if dv.ID == d.ID { return true } } for _, dv := range e.Gateways { if dv == d.ID { return true } } return false } // HasIPBlock will check if the passed in ip block exist in the DCX environment. func (e *DCXEnvironment) HasIPBlock(block netparse.DestinationNetwork) bool { if e.IPBlockCIDR == nil { e.IPBlockCIDR = &dcxAutohost.IPBlockCIDR{ Public: make([]string, 0), Private: make([]string, 0), } return false } for _, dcxBlock := range e.IPBlockCIDR.Public { if dcxBlock == block.CIDR() { return true } } return false }