def new_vc(self, vc_domain, l1, name): """ Create new VC :param vc_domain: :param l1: :param name: :return: """ if not name: name = "VLAN_%d" % l1 # Check constraints while vc_domain.vc_set.filter(name=name).count() > 0: name += "_" # self.new_vcs += [{"vc_domain": vc_domain, "l1": l1, "name": name}] if self.to_save: vc = VC(vc_domain=vc_domain, name=name, l1=l1, l2=0) vc.save()
def get_vc_interfaces_count(self, vc_id): vc = VC.get_by_id(vc_id) if not vc: return 0 objects = vc.vc_domain.managedobject_set.values_list("id", flat=True) l1 = vc.l1 n = SubInterface.objects.filter( Q(managed_object__in=objects) & (Q(untagged_vlan=l1, enabled_afi=["BRIDGE"]) | Q(tagged_vlans=l1, enabled_afi=["BRIDGE"]) | Q(vlan_ids=l1))).count() return n
def api_bulk_import(self, request, vc_domain, items): n = 0 for i in items: if not VC.objects.filter( vc_domain=vc_domain, l1=i["l1"], l2=i["l2"]).exists(): # Add only not-existing VC(vc_domain=vc_domain, l1=i["l1"], l2=i["l2"], name=i["name"], description=i["description"]).save() n += 1 return {"status": True, "imported": n}
def test_vc_labels(data): # Get type vc_type = VCType.objects.get(name=data["vc_type"]) # Create temporary domain vc_domain = VCDomain(name="TEST DOMAIN", type=vc_type) vc_domain.save() vc = VC(vc_domain=vc_domain, name="TEST VC", l1=data.get("l1", 0), l2=data.get("l2", 0)) if "$except" in data: with pytest.raises(data["$except"]): vc.save() else: vc.save() assert smart_text(vc) vc.delete() # Cleanup vc_domain.delete()
def get_vc_prefixes(self, vc_id): vc = VC.get_by_id(vc_id) if not vc: return [] objects = vc.vc_domain.managedobject_set.values_list("id", flat=True) ipv4 = set() ipv6 = set() # @todo: Exact match on vlan_ids for si in SubInterface.objects.filter( Q(managed_object__in=objects) & Q(vlan_ids=vc.l1) & (Q(enabled_afi=["IPv4"]) | Q(enabled_afi=["IPv6"]))).only( "enabled_afi", "ipv4_addresses", "ipv6_addresses"): if "IPv4" in si.enabled_afi: ipv4.update([IP.prefix(ip).first for ip in si.ipv4_addresses]) if "IPv6" in si.enabled_afi: ipv6.update([IP.prefix(ip).first for ip in si.ipv6_addresses]) p = [str(x.first) for x in sorted(ipv4)] p += [str(x.first) for x in sorted(ipv6)] return p