Beispiel #1
0
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
Beispiel #2
0
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..."
Beispiel #3
0
def host_info(request, host_name):
    """Manipulate or show a host in the database."""
    data = request_data(request)
    try:
        host = interface.get_host(host_name)
    except interface.InexistantObjectError:
        return error_404(request)

    if request.method == "PUT":
        category = data.get("category")
        if category:
            category = [category]
        alias = data.get("alias")
        if not alias:
            alias = ""
        alias = alias.replace(", ", ",")
        if alias:
            alias = alias.split(",")
        mac = data.get("macaddr")
        try:
            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})

            interface.modify(host=host_name,
                             mac=data.get("macaddr"),
                             newname=data.get("newname"),
                             category=category,
                             alias=alias,
                             serial=data.get("serial"),
                             inventory=data.get("inventory"),
                             nodns=((data.get("nodns") == "on") != host.nodns),
                             clearalias=data.get("clearalias") == "on")
            if data.get("owner"):
                interface.set_prop("owner", data.get("owner"), host=host)
        except interface.MissingParameterError:
            return error_view(
                request, 400, _("Missing information"),
                _("You must specify the new values to modify an object."))
        #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, _("Host or alias already exists"),
                              e.args[0])
        #fin anomalie9
        except interface.InexistantObjectError:
            return error_404(request)
Beispiel #4
0
def _list_hosts(args):
    """List all the hosts in the database."""
    for host in args.host:
        if host:
            try:
                hostobj = interface.get_host(host)
            except interface.InexistantObjectError:
                logging.error("Could not find a host name: " + host)
                sys.exit(1)

            # mac address(es)
            hoststr = str(hostobj)
            if models.Address.objects.filter(host=hostobj).exclude(macaddr=""):
                hoststr = hoststr + ", mac: "
                for addrobj in models.Address.objects.filter(
                        host=hostobj).exclude(macaddr=""):
                    hoststr = hoststr + addrobj.macaddr + ", "
                hoststr = hoststr[:-2]  # strip the last comma

            print "Host " + hoststr
            if hostobj.serial:
                print("Serial number: " + hostobj.serial)
            if hostobj.inventory:
                print("Inventory number: " + hostobj.inventory)
            if hostobj.category:
                print("Category: " + hostobj.category)
            if hostobj.alias_set.count() > 0:
                print(
                    "Alias: " + ", ".join([
                        alias.name for alias in list(hostobj.alias_set.all())
                    ]))
            if hostobj.nodns:
                print("NODNS")
            for addr in models.Address.objects.filter(host=hostobj).exclude(
                    addr="").order_by("addr"):
                if addr.pool:
                    print("Address " + str(addr) + " (pool: " +
                          addr.pool.name + ")")
                else:
                    print "Address " + str(addr)
            for prop in models.Property.objects.filter(host=hostobj):
                print str(prop)
        else:
            for hostobj in models.Host.objects.all().order_by("name"):
                msg = str(hostobj)
                if hostobj.alias_set.all():
                    msg += " (" + ", ".join(
                        [str(alias)
                         for alias in hostobj.alias_set.all()]) + ")"
                if hostobj.category:
                    msg += ", category: " + hostobj.category
                for addr in hostobj.address_set.all():
                    msg += ", " + str(addr)
                print(msg)
Beispiel #5
0
def _list_hosts(args):
    """List all the hosts in the database."""
    for host in args.host:
        if host:
            try:
                hostobj = interface.get_host(host)
            except interface.InexistantObjectError:
                logging.error("Could not find a host name: " + host)
                sys.exit(1)

            # mac address(es)
            hoststr = str(hostobj)
            if models.Address.objects.filter(host=hostobj).exclude(macaddr=""):
                hoststr = hoststr + ", mac: "
                for addrobj in models.Address.objects.filter(
                        host=hostobj).exclude(macaddr=""):
                    hoststr = hoststr + addrobj.macaddr + ", "
                hoststr = hoststr[:-2] # strip the last comma

            print "Host " + hoststr
            if hostobj.serial:
                print("Serial number: " + hostobj.serial)
            if hostobj.inventory:
                print("Inventory number: " + hostobj.inventory)
            if hostobj.category:
                print("Category: " + hostobj.category)
            if hostobj.alias_set.count() > 0:
                print("Alias: " + ", ".join(
                    [alias.name for alias in list(hostobj.alias_set.all())]))
            if hostobj.nodns:
                print("NODNS")
            for addr in models.Address.objects.filter(host=hostobj).exclude(
                    addr="").order_by("addr"):
                if addr.pool:
                    print ("Address " + str(addr) + " (pool: " +
                        addr.pool.name + ")")
                else:
                    print "Address " + str(addr)
            for prop in models.Property.objects.filter(host=hostobj):
                print str(prop)
        else:
            for hostobj in models.Host.objects.all().order_by("name"):
                msg = str(hostobj)
                if hostobj.alias_set.all():
                    msg += " (" + ", ".join([str(alias) for alias
                        in hostobj.alias_set.all()]) + ")"
                if hostobj.category:
                    msg += ", category: " + hostobj.category
                for addr in hostobj.address_set.all():
                    msg += ", " + str(addr)
                print(msg)
Beispiel #6
0
def host_info(request, host_name):
    """Manipulate or show a host in the database."""
    data = request_data(request)
    try:
        host = interface.get_host(host_name)
    except interface.InexistantObjectError:
        return error_404(request)

    if request.method == "PUT":
        category = data.get("category")
        if category:
            category = [category]
        alias = data.get("alias")
        if not alias:
            alias = ""
        alias = alias.replace(", ", ",")
        if alias:
            alias = alias.split(",")
        mac = data.get("macaddr")
        try:
            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})

            interface.modify(host=host_name,
                mac=data.get("macaddr"),
                newname=data.get("newname"),
                category=category,
                alias=alias,
                serial=data.get("serial"),
                inventory=data.get("inventory"),
                nodns=((data.get("nodns") == "on") != host.nodns),
                clearalias=data.get("clearalias") == "on")
            if data.get("owner"):
                interface.set_prop("owner", data.get("owner"), host=host)
        except interface.MissingParameterError:
            return error_view(request, 400, _("Missing information"),
                _("You must specify the new values to modify an object."))
        #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, _("Host or alias already exists"), e.args[0])
        #fin anomalie9
        except interface.InexistantObjectError:
            return error_404(request)
Beispiel #7
0
def get(args):
    """Get new address from pool."""
    pool = None
    if args.pool_name:
        try:
            pool = interface.get_pool(args.pool_name[0])
        except (interface.InexistantObjectError,
                interface.DuplicateObjectError) as exc:
            logging.error(str(exc))
            sys.exit(1)

    if args.host:
        for host in args.host:
            try:
                hostobj = interface.get_host(host)
            except interface.InexistantObjectError as exc:
                logging.error(str(exc))
                sys.exit(1)

            addr = None
            if not args.address:
                args.address = [None]
            try:
                if args.category:
                    args.category = args.category[0]
                addr = interface.allocate_address(pool, hostobj,
                                                  args.address[0], args.random,
                                                  args.category, args.duration)
                del args.address[0]
            except (interface.MissingParameterError,
                    models.FullPoolError) as exc:
                logging.error(str(exc))
            except (models.AddressNotInPoolError,
                    models.AddressNotAvailableError) as exc:
                logging.warn(str(exc))

            if addr:
                print("Assigned " + str(addr) + " to host " + host)
    else:
        logging.error("Missing host to allocate address to.")
        sys.exit(1)
Beispiel #8
0
def get(args):
    """Get new address from pool."""
    pool = None
    if args.pool_name:
        try:
            pool = interface.get_pool(args.pool_name[0])
        except (interface.InexistantObjectError,
                interface.DuplicateObjectError) as exc:
            logging.error(str(exc))
            sys.exit(1)

    if args.host:
        for host in args.host:
            try:
                hostobj = interface.get_host(host)
            except interface.InexistantObjectError as exc:
                logging.error(str(exc))
                sys.exit(1)

            addr = None
            if not args.address:
                args.address = [None]
            try:
                if args.category:
                    args.category = args.category[0]
                addr = interface.allocate_address(pool, hostobj,
                    args.address[0], args.random, args.category, args.duration)
                del args.address[0]
            except (interface.MissingParameterError,
                    models.FullPoolError) as exc:
                logging.error(str(exc))
            except (models.AddressNotInPoolError,
                    models.AddressNotAvailableError) as exc:
                logging.warn(str(exc))

            if addr:
                print("Assigned " + str(addr) + " to host " + host)
    else:
        logging.error("Missing host to allocate address to.")
        sys.exit(1)