def _create_host(args, pool): """Create hosts given as arguments.""" for host in args.host: if not args.address: args.address = [None] if not args.category: args.category = [None] if args.mac: macaddr = args.mac[0] del args.mac[0] else: macaddr = None try: hostres, addrres = interface.create_host( host, pool, args.address[0], macaddr, args.random, args.alias, args.category[0], args.serial, args.inventory, args.duration, args.nodns) if addrres is None: print("Host \"" + hostres + "\" have been created.") else: print("Assigned " + addrres + " to host " + hostres) except (models.AddressNotInPoolError, models.AddressNotAvailableError, models.FullPoolError, interface.DuplicateObjectError) as exc: logging.error(str(exc)) sys.exit(1) del args.address[0]
def _create_host(args, pool): """Create hosts given as arguments.""" for host in args.host: if not args.address: args.address = [None] if not args.category: args.category = [None] if args.mac: macaddr = args.mac[0] del args.mac[0] else: macaddr = None try: hostres, addrres = interface.create_host(host, pool, args.address[0], macaddr, args.random, args.alias, args.category[0], args.serial, args.inventory, args.duration, args.nodns) if addrres is None: print ("Host \"" + hostres + "\" have been created.") else: print("Assigned " + addrres + " to host " + hostres) except (models.AddressNotInPoolError, models.AddressNotAvailableError, models.FullPoolError, interface.DuplicateObjectError) as exc: logging.error(str(exc)) sys.exit(1) del args.address[0]
def add_host(request): """Add a new host to SLAM's database.""" if request.method == "POST": try: pool = interface.get_pool(pool_name=request.POST.get("pool_name"), category=request.POST.get("category")) except interface.InexistantObjectError: return error_404(request) alias = request.POST.get("alias") if alias: alias = alias.replace(", ", ",").split(",") else: alias = [] if (request.POST.get("mac") and models.Address.objects.filter( macaddr=request.POST.get("mac"))): return error_view(request, 412, _("MAC adress taken"), _("The MAC adresse of the host you are trying to create " "already belongs to host %(host)s.") % {"host": models.Address.objects.filter( macaddr=request.POST.get("mac"))[0].host.name}) mac = request.POST.get("mac") if mac: mac = mac.lower() if not re.match("^[a-f0-9]{2}(:[a-f0-9]{2}){5}$", mac): return error_view(request, 400, _("Invalid MAC"), _("The format of the given MAC address is invalid :" " %(mac)s.") % {"mac": mac}) try: hoststr, addrstr = interface.create_host( host=request.POST.get("name"), pool=pool, address=request.POST.get("address"), mac=mac, random=request.POST.get("random"), alias=alias, serial=request.POST.get("serial"), inventory=request.POST.get("inventory"), nodns=request.POST.get("nodns")) except interface.MissingParameterError: return error_view(request, 400, _("Missing information"), _("You must at least specify a name to create a new host.")) #annomalie espace dans le name host except interface.PropertyFormatError: return error_view(request, 400, _("Format invalid"), _("You must specify a valid host name or alias without space, special character etc.")) #anomalie9 except interface.DuplicateObjectError, e: return error_view(request, 409, _("Could not create host"), e.args[0]) #fin anomalie9 except models.AddressNotInPoolError: return error_view(request, 412, _("Address not in pool"), _("The address you provided (%(addr)s) is not in the pool " "%(pool)s.") % {"addr": str(request.POST.get("address")), "pool": str(pool.name)})
def test_prop(): interface.create_pool("pool60", "10.60.0.0/24") interface.create_host(host="host60-1", pool=interface.get_pool("pool60")) interface.quick_set_prop("prop=value", "pool60") assert(Property.objects.get( pool__name="pool60", name="prop").value == "value") interface.quick_set_prop("prop2=value2", host="host60-1") assert(Property.objects.get( host__name="host60-1", name="prop2").value == "value2") interface.quick_set_prop("prop", "pool60", del_=True) assert not Property.objects.filter(pool__name="pool60") interface.quick_set_prop("prop2", host="host60-1", del_=True) assert not Property.objects.filter(host__name="host60-1") assert_raises(interface.PropertyFormatError, interface.quick_set_prop, "foobar", "pool60") assert_raises(interface.InexistantObjectError, interface.quick_set_prop, "inexistant", "pool60", del_=True)
def test_macaddress(): interface.create_pool("pool15", "10.15.0.0/24") interface.create_host(host="host15-1") assert not Address.objects.filter(host__name="host15-1") interface.create_host("host15-2", pool=interface.get_pool("pool15"), mac="mac-2") assert Address.objects.get(host__name="host15-2").macaddr == "mac-2" interface.create_host("host15-3", mac="mac-3") assert Address.objects.get(host__name="host15-3").macaddr == "mac-3" interface.create_host("host15-4", pool=interface.get_pool("pool15"), mac="mac-4", address="10.15.0.142") assert Address.objects.get(host__name="host15-4").macaddr == "mac-4"
def test_generate(): # most of the test are in test_generator.py interface.create_pool("pool40", "10.40.0.0/24") interface.create_host(host="host40-1", pool=interface.get_pool("pool40")) interface.create_host(host="host40-2", pool=interface.get_pool("pool40")) interface.generate(None, ["pool40"], "bind", "-", update=False) assert_raises(interface.ConfigurationFormatError, interface.generate, None, ["pool40"], "unknown", "-", None, None, False) handle, path = tempfile.mkstemp() interface.create_generator("dnsgen2", "bind", path, timeout="6H") interface.generate("dnsgen2", ["pool40"], output="-", update=False) f = open(path, "r+") res = f.read() assert(res == "\n; This section will be automatically generated by SLAM" + " any manual change will\n; be overwritten on the next generation of" + " this file.\n; Pool pool40 (range: 10.40.0.0/24)\n" + "host40-1\t6H\tIN\tA\t10.40.0.0\n" + "host40-2\t6H\tIN\tA\t10.40.0.1\n" + "; END of section automatically generated by SLAM\n") f.close() os.unlink(path)
def test_modify(): interface.create_pool("pool30-1", "10.30.1.0/24") assert interface.get_pool("pool30-1").category == "" interface.modify(["pool30-1"], category=["cat30-1"]) assert interface.get_pool("pool30-1").category == "cat30-1" interface.modify(["pool30-1"], newname="pool30-2") assert(Pool.objects.filter(name="pool30-2") and interface.get_pool("pool30-2").category == "cat30-1") interface.create_host(host="host30-1", pool=interface.get_pool("pool30-2"), mac="foobar") assert Address.objects.get(host__name="host30-1").macaddr == "foobar" interface.modify(host="host30-1", mac="mac30-1") assert Address.objects.get(host__name="host30-1").macaddr == "mac30-1" interface.modify(host="host30-1", newname="host30-2") assert(Host.objects.filter(name="host30-2") and Address.objects.get(host__name="host30-2").macaddr == "mac30-1") hostobj = interface.get_host("host30-2") assert len(hostobj.alias_set.all()) == 0 assert not hostobj.nodns interface.modify(host="host30-2", alias=["alias30-1", "alias30-2"], nodns=True) hostobj = interface.get_host("host30-2") assert(len(hostobj.alias_set.all()) == 2 and hostobj.alias_set.all()[1].name == "alias30-2" and hostobj.nodns) assert_raises(interface.MissingParameterError, interface.modify, ["pool30-2"]) assert_raises(interface.MissingParameterError, interface.modify, None, "host30-2") assert_raises(interface.InexistantObjectError, interface.modify, None) interface.create_host(host="host30-3", pool=interface.get_pool("pool30-2"), address="10.30.1.142", mac="foobar") interface.modify(host="host30-3", address="10.30.1.142", mac="barfoo") assert Address.objects.get(host__name="host30-3").macaddr == "barfoo" interface.create_host(host="host30-4") interface.modify(host="host30-4", mac="imac") assert Address.objects.get(host__name="host30-4").macaddr == "imac" hostobj = interface.get_host("host30-4") assert(not (hostobj.serial or hostobj.inventory or Address.objects.get(host__name=hostobj.name).duration)) interface.modify(host=hostobj.name, serial="srlnm", inventory="invtnm") hostobj = interface.get_host("host30-4") assert hostobj.serial == "srlnm" and hostobj.inventory == "invtnm" addrobj = Address.objects.get(addr="10.30.1.0") assert not addrobj.comment interface.modify(address="10.30.1.0", comment="The quick brown fox...") addrobj = Address.objects.get(addr="10.30.1.0") assert addrobj.comment == "The quick brown fox..."
def test_delete(): interface.create_pool("pool50", "10.50.0.0/24") interface.create_host(host="host50-1", pool=interface.get_pool("pool50")) interface.create_host(host="host50-2", pool=interface.get_pool("pool50")) interface.create_host(host="host50-3", pool=interface.get_pool("pool50")) Pool.objects.get(name="pool50").allocate("10.50.0.42", Host.objects.get(name="host50-3")) assert Host.objects.filter(name="host50-1") interface.delete(hosts=["host50-1"]) assert not Host.objects.filter(name="host50-1") assert not Address.objects.filter(addr="10.50.0.0") assert Address.objects.filter(host__name="host50-3").count() == 2 interface.delete(addresses=["10.50.0.42"]) assert Address.objects.filter(host__name="host50-3").count() == 1 assert Pool.objects.filter(name="pool50") interface.delete(pool=Pool.objects.get(name="pool50")) assert not Pool.objects.filter(name="pool50") assert not Address.objects.filter(host__name="host50-2") assert_raises(interface.InexistantObjectError, interface.delete, None, ["inexistant"])
def add_host(request): """Add a new host to SLAM's database.""" if request.method == "POST": try: pool = interface.get_pool(pool_name=request.POST.get("pool_name"), category=request.POST.get("category")) except interface.InexistantObjectError: return error_404(request) alias = request.POST.get("alias") if alias: alias = alias.replace(", ", ",").split(",") else: alias = [] if (request.POST.get("mac") and models.Address.objects.filter( macaddr=request.POST.get("mac"))): return error_view( request, 412, _("MAC adress taken"), _("The MAC adresse of the host you are trying to create " "already belongs to host %(host)s.") % { "host": models.Address.objects.filter( macaddr=request.POST.get("mac"))[0].host.name }) mac = request.POST.get("mac") if mac: mac = mac.lower() if not re.match("^[a-f0-9]{2}(:[a-f0-9]{2}){5}$", mac): return error_view( request, 400, _("Invalid MAC"), _("The format of the given MAC address is invalid :" " %(mac)s.") % {"mac": mac}) try: hoststr, addrstr = interface.create_host( host=request.POST.get("name"), pool=pool, address=request.POST.get("address"), mac=mac, random=request.POST.get("random"), alias=alias, serial=request.POST.get("serial"), inventory=request.POST.get("inventory"), nodns=request.POST.get("nodns")) except interface.MissingParameterError: return error_view( request, 400, _("Missing information"), _("You must at least specify a name to create a new host.")) #annomalie espace dans le name host except interface.PropertyFormatError: return error_view( request, 400, _("Format invalid"), _("You must specify a valid host name or alias without space, special character etc." )) #anomalie9 except interface.DuplicateObjectError, e: return error_view(request, 409, _("Could not create host"), e.args[0]) #fin anomalie9 except models.AddressNotInPoolError: return error_view( request, 412, _("Address not in pool"), _("The address you provided (%(addr)s) is not in the pool " "%(pool)s.") % { "addr": str(request.POST.get("address")), "pool": str(pool.name) })
def test_getcreate_host(): interface.create_host(host="host10") hostobj = interface.get_host("host10") assert str(hostobj) == "host10" assert interface.get_host(None) is None assert_raises(interface.InexistantObjectError, interface.get_host, "inexistant") interface.create_pool("pool10", "10.10.0.0/24", ["cat10"]) interface.create_host(host="host10-2", pool=interface.get_pool("pool10")) assert(interface.get_host("host10-2").address_set.all()[0].pool.name == "pool10") interface.create_host(host="host10-3", pool=interface.get_pool(category="cat10")) assert(interface.get_host("host10-3").address_set.all()[0].pool.name == "pool10") assert_raises(interface.DuplicateObjectError, interface.create_host, "host10-3") interface.create_host(host="host10-4", mac="mac10-4", category="cat10") assert(interface.get_host("host10-4").category == "cat10") assert(Address.objects.get(host__name="host10-4").pool.name == "pool10") assert(interface.get_host("host10-4").address_set.all()[0].macaddr == "mac10-4") interface.create_host(host="host10-6", pool=Pool.objects.get(name="pool10"), address="10.10.0.142") assert Address.objects.get(host__name="host10-6").addr == "10.10.0.142" interface.create_host(host="host10-5", pool=interface.get_pool("pool10"), mac="mac10-5") addrobj = interface.get_host("host10-5").address_set.all()[0] assert addrobj.addr == "10.10.0.3" and addrobj.macaddr == "mac10-5" interface.create_host(host="host10-15", pool=interface.get_pool("pool10"), alias=["alias10-1", "alias10-2"]) hostobj = interface.get_host("host10-15") assert(len(hostobj.alias_set.all()) == 2 and hostobj.alias_set.all()[1].name == "alias10-2") interface.create_pool("pool10-2", "172.16.102.0/24", ["cat10"]) interface.create_host(host="host10-25", address="172.16.102.234") assert(Address.objects.get(host__name="host10-25").pool.name == "pool10-2") interface.create_host(host="host10-30", pool=interface.get_pool("pool10-2"), serial="serialnum", inventory="inventorynum", duration=42) hostobj = interface.get_host("host10-30") assert(hostobj.serial == "serialnum" and hostobj.inventory == "inventorynum" and Address.objects.get(host__name="host10-30").duration > Address.objects.get(host__name="host10-30").date + datetime.timedelta(days=41)) interface.create_host(host="host10-40", pool=interface.get_pool("pool10"), nodns=True) hostobj = interface.get_host("host10-40") assert hostobj.nodns