| @@ -183,13 +183,18 @@ def checkchanges(module): | |||
| ''' | |||
| mod = importlib.import_module(module) | |||
| mods = [ i for i in mod.__dict__.itervalues() if isinstance(i, vlanmang.SwitchConfig) ] | |||
| mods = [ (k, v) for k, v in mod.__dict__.iteritems() if isinstance(v, vlanmang.SwitchConfig) ] | |||
| res = [] | |||
| for i in mods: | |||
| for name, i in mods: | |||
| print 'probing %s' % `name` | |||
| vlans = i.vlanconf.keys() | |||
| switch = SNMPSwitch(i.host, i.community) | |||
| #print `switch` | |||
| switchpvid = switch.getpvid() | |||
| #print `switchpvid` | |||
| #import pdb; pdb.set_trace() | |||
| portmapping = switch.getportmapping() | |||
| invportmap = { y: x for x, y in portmapping.iteritems() } | |||
| lufun = invportmap.__getitem__ | |||
| @@ -208,7 +213,7 @@ def checkchanges(module): | |||
| pvidmap = getpvidmapping(i.vlanconf, lufun) | |||
| switchpvid = switch.getpvid() | |||
| res.extend(('setpvid', idx, vlan, switchpvid[idx]) for idx, vlan in | |||
| res.extend((switch, name, 'setpvid', idx, vlan, switchpvid[idx]) for idx, vlan in | |||
| pvidmap.iteritems() if switchpvid[idx] != vlan) | |||
| # compare egress & untagged | |||
| @@ -218,11 +223,11 @@ def checkchanges(module): | |||
| untagged = getuntagged(i.vlanconf, lufun) | |||
| for i in vlans: | |||
| if not _cmpbits(switchegress[i], egress[i]): | |||
| res.append(('setegress', i, egress[i], switchegress[i])) | |||
| res.append((switch, name, 'setegress', i, egress[i], switchegress[i])) | |||
| if not _cmpbits(switchuntagged[i], untagged[i]): | |||
| res.append(('setuntagged', i, untagged[i], switchuntagged[i])) | |||
| res.append((switch, name, 'setuntagged', i, untagged[i], switchuntagged[i])) | |||
| return res, switch | |||
| return res | |||
| def getidxs(lst, lookupfun): | |||
| '''Take a list of ports, and if any are a string, replace them w/ | |||
| @@ -282,6 +287,9 @@ class SNMPSwitch(object): | |||
| self._cd = auth | |||
| self._targ = UdpTransportTarget((host, 161)) | |||
| def __repr__(self): | |||
| return '<SNMPSwitch: auth=%s, targ=%s>' % (`self._cd`, `self._targ`) | |||
| def _getmany(self, *oids): | |||
| woids = [ ObjectIdentity(*oid) for oid in oids ] | |||
| [ oid.resolveWithMib(_mvc) for oid in woids ] | |||
| @@ -291,7 +299,7 @@ class SNMPSwitch(object): | |||
| ContextData(), *(ObjectType(oid) for oid in woids))) | |||
| if errorInd: # pragma: no cover | |||
| raise ValueError(errorIndication) | |||
| raise ValueError(errorInd) | |||
| elif errorStatus: | |||
| if str(errorStatus) == 'tooBig' and len(oids) > 1: | |||
| # split the request in two | |||
| @@ -329,7 +337,7 @@ class SNMPSwitch(object): | |||
| ContextData(), ObjectType(oid, value))) | |||
| if errorInd: # pragma: no cover | |||
| raise ValueError(errorIndication) | |||
| raise ValueError(errorInd) | |||
| elif errorStatus: # pragma: no cover | |||
| raise ValueError('%s at %s' % | |||
| (errorStatus.prettyPrint(), errorIndex and | |||
| @@ -464,17 +472,17 @@ class SNMPSwitch(object): | |||
| self._set(('Q-BRIDGE-MIB', 'dot1qVlanStaticUntaggedPorts', | |||
| int(vlan)), value) | |||
| if __name__ == '__main__': # pragma: no cover | |||
| def main(): | |||
| import pprint | |||
| import sys | |||
| changes, switch = checkchanges('data') | |||
| changes = checkchanges('data') | |||
| if not changes: | |||
| print 'No changes to apply.' | |||
| sys.exit(0) | |||
| pprint.pprint(changes) | |||
| pprint.pprint([ x[1:] for x in changes ]) | |||
| res = raw_input('Apply the changes? (type yes to apply): ') | |||
| if res != 'yes': | |||
| @@ -483,7 +491,12 @@ if __name__ == '__main__': # pragma: no cover | |||
| print 'applying...' | |||
| failed = [] | |||
| for verb, arg1, arg2, oldarg in changes: | |||
| prevname = None | |||
| for switch, name, verb, arg1, arg2, oldarg in changes: | |||
| if prevname != name: | |||
| print 'Configuring switch %s...' % `name` | |||
| prevname = name | |||
| print '%s: %s %s' % (verb, arg1, `arg2`) | |||
| try: | |||
| fun = getattr(switch, verb) | |||
| @@ -498,6 +511,9 @@ if __name__ == '__main__': # pragma: no cover | |||
| for verb, arg1, arg2, e in failed: | |||
| print '%s: %s %s: %s' % (verb, arg1, arg2, `e`) | |||
| if __name__ == '__main__': # pragma: no cover | |||
| main() | |||
| class _TestMisc(unittest.TestCase): | |||
| def setUp(self): | |||
| import test_data | |||
| @@ -630,10 +646,16 @@ class _TestMisc(unittest.TestCase): | |||
| 283: '00000000111111111110011', | |||
| } ] | |||
| res, switch = checkchanges('data') | |||
| res = checkchanges('data') | |||
| # Make sure that the first one are all instances of SNMPSwitch | |||
| # XXX make sure args for them are correct. | |||
| self.assertTrue(all(isinstance(x[0], SNMPSwitch) for x in res)) | |||
| self.assertIsInstance(switch, SNMPSwitch) | |||
| # Make sure that the name provided is correct | |||
| self.assertTrue(all(x[1] == 'distswitch' for x in res)) | |||
| res = [ x[2:] for x in res ] | |||
| validres = [ ('setpvid', x, 5, 283) for x in xrange(1, 9) ] + \ | |||
| [ ('setpvid', 20, 1, 283), | |||
| ('setpvid', 21, 1, 283), | |||