def test_find_vlans_in_org_one_child(self): """Should return all vlans in this org and child org""" self.assertEqual( sorted(find_vlans_in_org(self.org2)), sorted( [FantasyVlan(self.vlan3.vlan), FantasyVlan(self.vlan2.vlan)]))
def test_find_vlans_in_org_filter_nonevalues(self): """Should filter vlans with no vlan value""" self.vlan1.vlan = None self.assertEqual( sorted(find_vlans_in_org(self.org1)), sorted( [FantasyVlan(self.vlan3.vlan), FantasyVlan(self.vlan2.vlan)]))
def test_find_vlans_in_org_two_children(self): """Should return all vlans in this org and all children orgs""" self.assertEqual( sorted(find_vlans_in_org(self.org1)), sorted([ FantasyVlan(self.vlan3.vlan), FantasyVlan(self.vlan2.vlan), FantasyVlan(self.vlan1.vlan) ]))
def test_find_vlans_in_org_no_duplicates(self): """Should filter duplicates""" self.org3.vlan_set.all.return_value = [self.vlan3, self.vlan2] self.assertEqual( sorted(find_vlans_in_org(self.org1)), sorted([ FantasyVlan(self.vlan3.vlan), FantasyVlan(self.vlan2.vlan), FantasyVlan(self.vlan1.vlan) ]))
def test_get_vlans(self): handler = ManagementFactory.get_instance(self.netboxHP) interface = Mock() swportvlan1 = Mock(vlan=Mock(vlan=1)) swportvlan2 = Mock(vlan=Mock(vlan=2)) interface.swportvlan_set.all.return_value = [swportvlan1, swportvlan2] self.assertEqual(sorted(handler._find_vlans_for_interface(interface)), [FantasyVlan(1), FantasyVlan(2)])
def _make_vlan(vlan): tag = int(vlan.tag) try: vlan_object = vlan_objects.get(vlan=tag) except (manage.Vlan.DoesNotExist, manage.Vlan.MultipleObjectsReturned): return FantasyVlan(tag, netident=vlan.name) else: return FantasyVlan(tag, netident=vlan_object.net_ident, descr=vlan_object.description)
def _find_vlans_for_interface(interface): """Find vland for the given interface.""" interface_vlans = interface.swportvlan_set.all() vlans = [] if interface_vlans: for swportvlan in interface_vlans: vlan = swportvlan.vlan if vlan.vlan: vlans.append(FantasyVlan(vlan.vlan, vlan.net_ident)) elif interface.vlan: vlans = [FantasyVlan(vlan=interface.vlan)] return vlans
def test_find_allowed_vlans_for_user(self): """Should return all vlans for this users org and child orgs""" account = Mock() account.organizations.all.return_value = [self.org1] self.assertEqual( sorted(find_allowed_vlans_for_user(account)), sorted([ FantasyVlan(self.vlan1.vlan), FantasyVlan(self.vlan2.vlan), FantasyVlan(self.vlan3.vlan) ]))
def find_default_vlan(self, include_netident=False): """Check config to see if a default vlan is set :rtype: FantasyVlan """ defaultvlan = self.getint("defaultvlan", "vlan", fallback=None) netident = self.get("defaultvlan", "netident", fallback="") if defaultvlan: if include_netident: return FantasyVlan(defaultvlan, netident) else: return FantasyVlan(defaultvlan)
def get_netbox_vlans(self): numerical_vlans = self.get_netbox_vlan_tags() vlan_objects = Vlan.objects.filter( swportvlan__interface__netbox=self.netbox).distinct() vlans = [] for numerical_vlan in numerical_vlans: try: vlan_object = vlan_objects.get(vlan=numerical_vlan) except (Vlan.DoesNotExist, Vlan.MultipleObjectsReturned): fantasy_vlan = FantasyVlan(numerical_vlan) else: fantasy_vlan = FantasyVlan(numerical_vlan, netident=vlan_object.net_ident, descr=vlan_object.description) vlans.append(fantasy_vlan) return sorted(list(set(vlans)), key=attrgetter('vlan'))
def find_vlans_in_org(org): """Find all vlans in an organization and child organizations :returns: list of FantasyVlans :rtype: list """ vlans = list(org.vlan_set.all()) for child_org in org.organization_set.all(): vlans.extend(find_vlans_in_org(child_org)) return [ FantasyVlan(x.vlan, x.net_ident) for x in list(set(vlans)) if x.vlan ]
def test_find_vlans_in_org_no_children(self): """Should return all vlans in this org""" self.assertEqual(find_vlans_in_org(self.org3), [FantasyVlan(self.vlan3.vlan)])