def add_pool(request): """Create a new pool.""" if request.method == "POST": pool_name = request.POST.get("name") try: category = request.POST.get("category") if category: poolobj = interface.create_pool(pool_name, request.POST.get("definition"), [category]) else: poolobj = interface.create_pool( pool_name, request.POST.get("definition")) except interface.DuplicateObjectError: return error_view(request, 409, _("Pool already exists"), _("A pool with the same name or the same category already" " exists in the database.")) except interface.MissingParameterError: return error_view(request, 400, _("Missing information"), _("You must specify at least a name and a definition to create" " a new pool.")) except addrrange.InvalidAddressError: return error_view(request, 412, _("Invalid address range"), _("The address range you provided is invalid.")) return msg_view(request, _("Created pool %(pool)s") % {"pool": pool_name}, _("The pool \"%(pool)s\" have been created: %(poolstr)s.") % {"pool": pool_name, "poolstr": str(poolobj)}, referer="/pool/" + str(pool_name)) else: return render_to_response("add_pool.html", {"request": request})
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
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_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_getcreate_pool(): interface.create_pool("pool20", "10.20.0.0/24", ["cat20"]) assert(str(interface.get_pool(pool_name="pool20")) == "pool20 (range: 10.20.0.0/24)") assert interface.get_pool(category="cat20").name == "pool20" interface.create_pool("pool20-2", "10.20.2.0/24") assert(str(interface.get_pool(pool_name="pool20-2")) == "pool20-2 (range: 10.20.2.0/24)") assert_raises(interface.InexistantObjectError, interface.get_pool, "inexistant") assert_raises(interface.InexistantObjectError, interface.get_pool, None, "inexistant") assert_raises(interface.DuplicateObjectError, interface.create_pool, "pool20-2", "10.20.3.0/24") assert_raises(interface.MissingParameterError, interface.create_pool, "pool20-3")
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 add_pool(request): """Create a new pool.""" if request.method == "POST": pool_name = request.POST.get("name") try: category = request.POST.get("category") if category: poolobj = interface.create_pool(pool_name, request.POST.get("definition"), [category]) else: poolobj = interface.create_pool(pool_name, request.POST.get("definition")) except interface.DuplicateObjectError: return error_view( request, 409, _("Pool already exists"), _("A pool with the same name or the same category already" " exists in the database.")) except interface.MissingParameterError: return error_view( request, 400, _("Missing information"), _("You must specify at least a name and a definition to create" " a new pool.")) except addrrange.InvalidAddressError: return error_view(request, 412, _("Invalid address range"), _("The address range you provided is invalid.")) return msg_view( request, _("Created pool %(pool)s") % {"pool": pool_name}, _("The pool \"%(pool)s\" have been created: %(poolstr)s.") % { "pool": pool_name, "poolstr": str(poolobj) }, referer="/pool/" + str(pool_name)) else: return render_to_response("add_pool.html", {"request": request})
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 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 create(args): """Create a new object.""" pool = [] if (args.host or args.address or args.generator) and args.pool_name: try: for pool_name in args.pool_name: pool.append(interface.get_pool(pool_name)) except interface.InexistantObjectError as exc: logging.error(str(exc)) sys.exit(1) if not args.generator and not args.extra and not pool: pool = [None] # generator creation if args.generator and args.extra: if not args.pool_name: args.pool_name = [] try: interface.create_generator(args.generator, args.extra[0], args.output, args.default, args.header, args.footer, args.checkfile, args.timeout, args.domain, args.pool_name) except (interface.DuplicateObjectError, interface.MissingParameterError) as exc: logging.error(str(exc)) sys.exit(1) else: if pool and len(pool) > 1: logging.error("Please specify only one pool name.") sys.exit(1) if args.pool_name and (args.host or args.address): pool = interface.get_pool(args.pool_name[0]) else: pool = None # host creation if args.host: _create_host(args, pool) # stand-alone addresses creation elif args.address: for addr in args.address: try: interface.allocate_address(pool, address=addr) print(addr + " has been allocated") except (models.AddressNotInPoolError, models.AddressNotAvailableError) as exc: logging.warn(str(exc)) # pool creation elif args.pool_name: if pool is None: try: pool = interface.create_pool(args.pool_name[0], args.pool, args.category) except (addrrange.InvalidAddressError, interface.MissingParameterError) as exc: logging.error(str(exc)) sys.exit(1) else: logging.error("Pool already exists :" + args.pool_name[0]) sys.exit(1) else: logging.error("Missing parameters for object creation.") sys.exit(1)