// 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 shutdown import ( "errors" "fmt" "regexp" ) type adx struct { Opts Options RawConfig string StartupConfig string UpdatedConfig string } func (d *adx) modify() string { d.UpdatedConfig = d.RawConfig d.modifyMgmtInfo() d.modifyVlans() return d.UpdatedConfig } func (d *adx) modifyMgmtInfo() { if d.Opts.MgmtInfo == nil { return } re := regexp.MustCompile( fmt.Sprintf( `(?s)(interface management 1.*?ip address) %s %s(\b)`, d.Opts.MgmtInfo.IP.Old, d.Opts.MgmtInfo.Netmask.Old, )) d.UpdatedConfig = re.ReplaceAllString( d.UpdatedConfig, fmt.Sprintf(`${1} %s %s${2}`, d.Opts.MgmtInfo.IP.New, d.Opts.MgmtInfo.Netmask.New), ) re = regexp.MustCompile(fmt.Sprintf(`ip route (\S+ \S+) %s(\b)`, d.Opts.MgmtInfo.Gateway.Old)) d.UpdatedConfig = re.ReplaceAllString( d.UpdatedConfig, fmt.Sprintf(`ip route ${1} %s${2}`, d.Opts.MgmtInfo.Gateway.New), ) } func (d *adx) modifyVlans() { if d.Opts.VlanMap == nil { return } for o, n := range d.Opts.VlanMap { re := regexp.MustCompile(fmt.Sprintf(`vlan %s name (\S+) by port`, o)) d.UpdatedConfig = re.ReplaceAllString( d.UpdatedConfig, fmt.Sprintf(`vlan %s name ${1} by port`, n), ) } } func (d *adx) verify() error { if err := d.verifyMgmtInfo(); err != nil { return err } if err := d.verifyVlans(); err != nil { return err } return nil } func (d *adx) verifyMgmtInfo() error { if d.Opts.MgmtInfo == nil { return nil } re := regexp.MustCompile( fmt.Sprintf( `(?s)(interface management 1.*?ip address) %s %s(\b)`, d.Opts.MgmtInfo.IP.New, d.Opts.MgmtInfo.Netmask.New, ), ) if !re.MatchString(d.StartupConfig) { return errors.New("verify(): failure matching on updated mgmt IP") } return nil } func (d *adx) verifyVlans() error { if d.Opts.VlanMap == nil { return nil } vlans := make([]string, 0) for o := range d.Opts.VlanMap { re := regexp.MustCompile(fmt.Sprintf(`vlan %s name (\S+) by port`, o)) if re.MatchString(d.RawConfig) { vlans = append(vlans, o) } } for _, oldVlan := range vlans { re := regexp.MustCompile(fmt.Sprintf(`vlan %s name (\S+) by port`, d.Opts.VlanMap[oldVlan])) if !re.MatchString(d.StartupConfig) { return fmt.Errorf("verify(): failure matching on vlan %s", d.Opts.VlanMap[oldVlan]) } } return nil }