コード例 #1
0
ファイル: slam_cli.py プロジェクト: LAL/SLAM
def modify(args):
    """Modify a given property of an object."""
    # argparse is made so we need lists for these arguments, but
    # interface.modify wants atom arguments, so in case of None arguments, we
    # insert an array containing None to avoid crashing on the call to
    # interface.modify
    if not args.host:
        args.host = [None]
    if not args.address:
        args.address = [None]
    if not args.extra:
        args.extra = [None]
    if not args.mac:
        args.mac = [None]

    try:
        if args.generator:
            interface.modify_generator(args.generator, args.default,
                args.output, args.header, args.footer, args.checkfile,
                args.timeout, args.domain, args.pool_name)
        else:
            clearalias = (args.alias and len(args.alias) == 1
                and len(args.alias[0]) == 0)
            interface.modify(args.pool_name, args.host[0], args.category,
                args.address[0], args.mac[0], args.extra[0], args.alias,
                args.serial, args.inventory, args.duration, args.lastuse,
                args.nodns, args.comment, clearalias=clearalias)
    except (interface.InexistantObjectError,
            interface.MissingParameterError) as exc:
        logging.error(str(exc))
        sys.exit(1)
コード例 #2
0
ファイル: views.py プロジェクト: aflp91/SLAM
def address_info(request, address):
    """Get information about an address or disallocate it."""
    addr = get_object_or_404(models.Address, addr=address)
    if request.method == "POST":
        try:
            interface.modify(address=address,
                comment=request.POST.get("comment"),
                duration=int(request.POST.get("duration")))
        except interface.InexistantObjectError:
            return error_view(request, 400, _("Missing information"),
                _("You must specify at least the comment or duration to "
                    "modify."))
        return msg_view(request, _("Address modified"),
            _("The address \"%(addr)s\" has been correctly modified.")
                % {"addr": address}, referer="/address/" + address)
    if request.method == "DELETE":
        data = request_data(request)
        if data.get("confirm"):
            return render_to_response("address.html",
                {"request": request, "addr": addr, "confirm_delete": 1})
        else:
            try:
                interface.delete(addresses=[address])
            except interface.InexistantObjectError:
                error_404(request)
            return msg_view(request, _("Address deleted"),
                _("The address \"%(addr)s\" has been correctly removed.")
                    % {"addr": address}, referer="/")
    else:
        if request.GET.get("format") == "json":
            return render_to_response("address.json",
                {"request": request, "addr": addr})
        else:
            return render_to_response("address.html",
                {"request": request, "addr": addr})
コード例 #3
0
ファイル: views.py プロジェクト: guillaume-philippon/SLAM
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)
コード例 #4
0
ファイル: views.py プロジェクト: aflp91/SLAM
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)
コード例 #5
0
ファイル: views.py プロジェクト: guillaume-philippon/SLAM
def address_info(request, address):
    """Get information about an address or disallocate it."""
    addr = get_object_or_404(models.Address, addr=address)
    if request.method == "POST":
        try:
            interface.modify(address=address,
                             comment=request.POST.get("comment"),
                             duration=int(request.POST.get("duration")))
        except interface.InexistantObjectError:
            return error_view(
                request, 400, _("Missing information"),
                _("You must specify at least the comment or duration to "
                  "modify."))
        return msg_view(
            request,
            _("Address modified"),
            _("The address \"%(addr)s\" has been correctly modified.") %
            {"addr": address},
            referer="/address/" + address)
    if request.method == "DELETE":
        data = request_data(request)
        if data.get("confirm"):
            return render_to_response("address.html", {
                "request": request,
                "addr": addr,
                "confirm_delete": 1
            })
        else:
            try:
                interface.delete(addresses=[address])
            except interface.InexistantObjectError:
                error_404(request)
            return msg_view(
                request,
                _("Address deleted"),
                _("The address \"%(addr)s\" has been correctly removed.") %
                {"addr": address},
                referer="/")
    else:
        if request.GET.get("format") == "json":
            return render_to_response("address.json", {
                "request": request,
                "addr": addr
            })
        else:
            return render_to_response("address.html", {
                "request": request,
                "addr": addr
            })
コード例 #6
0
ファイル: slam_cli.py プロジェクト: guillaume-philippon/SLAM
def modify(args):
    """Modify a given property of an object."""
    # argparse is made so we need lists for these arguments, but
    # interface.modify wants atom arguments, so in case of None arguments, we
    # insert an array containing None to avoid crashing on the call to
    # interface.modify
    if not args.host:
        args.host = [None]
    if not args.address:
        args.address = [None]
    if not args.extra:
        args.extra = [None]
    if not args.mac:
        args.mac = [None]

    try:
        if args.generator:
            interface.modify_generator(args.generator, args.default,
                                       args.output, args.header, args.footer,
                                       args.checkfile, args.timeout,
                                       args.domain, args.pool_name)
        else:
            clearalias = (args.alias and len(args.alias) == 1
                          and len(args.alias[0]) == 0)
            interface.modify(args.pool_name,
                             args.host[0],
                             args.category,
                             args.address[0],
                             args.mac[0],
                             args.extra[0],
                             args.alias,
                             args.serial,
                             args.inventory,
                             args.duration,
                             args.lastuse,
                             args.nodns,
                             args.comment,
                             clearalias=clearalias)
    except (interface.InexistantObjectError,
            interface.MissingParameterError) as exc:
        logging.error(str(exc))
        sys.exit(1)
コード例 #7
0
ファイル: views.py プロジェクト: guillaume-philippon/SLAM
def pool_info(request, pool_name):
    """Manipulate a specified pool."""
    data = request_data(request)
    poolobj = get_object_or_404(models.Pool, name=pool_name)
    if request.method == "PUT":
        try:
            category = data.get("category")
            if category:
                interface.modify(pools=[pool_name],
                                 category=[category],
                                 newname=data.get("newname"))
            else:
                interface.modify(pools=[pool_name],
                                 newname=data.get("newname"))
        except interface.MissingParameterError as exc:
            return error_view(
                request, 400, _("Bad Request"),
                _("Your request is invalid, please verify it is correct. "
                  "Internal reason: %(exc)s") % {"exc": str(exc)})
        except (interface.InexistantObjectError,
                interface.DuplicateObjectError):
            return error_404(request)
        if data.get("newname"):
            referer = "/pool/" + str(data.get("newname"))
        else:
            referer = "/pool/" + pool_name
        return msg_view(
            request,
            _("Pool \"%(pool)s\" has been modified") % {"pool": pool_name},
            _("The pool \"%(pool)s\" has been correctly modified.") %
            {"pool": pool_name},
            referer=referer)
    elif request.method == "DELETE":
        name = str(poolobj.name)
        def_ = str(poolobj)
        try:
            interface.delete(pool=poolobj)
        except interface.InexistantObjectError:
            return error_404(request)
        except models.AddressNotAllocatedError:
            return error_view(
                request, 412, _("Address not allocated"),
                _("The address you provided is already unallocated."))
        return msg_view(
            request,
            _("%(name)s has been removed") % {"name": name},
            _("The pool \"%(name)s\" (%(def)s) has been removed.") % {
                "name": name,
                "def": def_
            },
            referer="/pool")
    else:
        addr_used = models.Address.objects.filter(pool=poolobj).count()
        addr_avail = poolobj.len()
        if addr_avail == 0:
            addr_avail = 1
        addrs = models.Address.objects.filter(pool=poolobj)
        if request.GET.get("sort") == "name":
            addrs = addrs.order_by("host__name")
        elif request.GET.get("sort") == "alias":
            addrs = addrs.order_by("host__alias__name")
        elif request.GET.get("sort") == "mac":
            addrs = addrs.order_by("macaddr")
        else:
            addrs = list(addrs)
            addrs = interface.sort_addresses(addrs)
        templ_values = {
            "request": request,
            "pool": poolobj,
            "addr_used": addr_used,
            "addr_avail": addr_avail,
            "addr_perc": addr_used * 100 / addr_avail,
            "addrs": addrs,
            "props": models.Property.objects.filter(pool=poolobj)
        }
        if request.GET.get("format") == "json":
            return render_to_response("pool.json", templ_values)
        else:
            return render_to_response("pool.html", templ_values)
コード例 #8
0
ファイル: test_interface.py プロジェクト: jouvin/SLAM
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..."
コード例 #9
0
ファイル: views.py プロジェクト: aflp91/SLAM
def pool_info(request, pool_name):
    """Manipulate a specified pool."""
    data = request_data(request)
    poolobj = get_object_or_404(models.Pool, name=pool_name)
    if request.method == "PUT":
        try:
            category = data.get("category")
            if category:
                interface.modify(pools=[pool_name], category=[category],
                    newname=data.get("newname"))
            else:
                interface.modify(pools=[pool_name],
                    newname=data.get("newname"))
        except interface.MissingParameterError as exc:
            return error_view(request, 400, _("Bad Request"),
                _("Your request is invalid, please verify it is correct. "
                    "Internal reason: %(exc)s") % {"exc": str(exc)})
        except (interface.InexistantObjectError,
                interface.DuplicateObjectError):
            return error_404(request)
        if data.get("newname"):
            referer = "/pool/" + str(data.get("newname"))
        else:
            referer = "/pool/" + pool_name
        return msg_view(request,
            _("Pool \"%(pool)s\" has been modified") % {"pool": pool_name},
            _("The pool \"%(pool)s\" has been correctly modified.")
                % {"pool": pool_name}, referer=referer)
    elif request.method == "DELETE":
        name = str(poolobj.name)
        def_ = str(poolobj)
        try:
            interface.delete(pool=poolobj)
        except interface.InexistantObjectError:
            return error_404(request)
        except models.AddressNotAllocatedError:
            return error_view(request, 412, _("Address not allocated"),
                _("The address you provided is already unallocated."))
        return msg_view(request,
            _("%(name)s has been removed") % {"name": name},
            _("The pool \"%(name)s\" (%(def)s) has been removed.")
                % {"name": name, "def": def_}, referer="/pool")
    else:
        addr_used = models.Address.objects.filter(pool=poolobj).count()
        addr_avail = poolobj.len()
        addrs = models.Address.objects.filter(pool=poolobj)
        if request.GET.get("sort") == "name":
            addrs = addrs.order_by("host__name")
        elif request.GET.get("sort") == "alias":
            addrs = addrs.order_by("host__alias__name")
        elif request.GET.get("sort") == "mac":
            addrs = addrs.order_by("macaddr")
        else:
            addrs = list(addrs)
            addrs = interface.sort_addresses(addrs)
        templ_values = {"request": request,
            "pool": poolobj,
            "addr_used": addr_used,
            "addr_avail": addr_avail,
            "addr_perc": addr_used * 100 / addr_avail,
            "addrs": addrs,
            "props": models.Property.objects.filter(pool=poolobj)}
        if request.GET.get("format") == "json":
            return render_to_response("pool.json", templ_values)
        else:
            return render_to_response("pool.html", templ_values)