def delete(args): """Delete an object from the database.""" if args.generator: try: interface.get_generator(args.generator) except interface.InexistantObjectError as exc: logging.error(str(exc)) sys.exit(1) print("Removed generator \"" + args.generator + "\".") models.Config.objects.filter(name=args.generator).delete() else: pool = None if args.pool_name: try: pool = interface.get_pool(args.pool_name[0]) except interface.InexistantObjectError as exc: logging.error(str(exc)) sys.exit(1) try: interface.delete(pool, args.address, args.host) except (interface.InexistantObjectError, models.AddressNotInPoolError, models.AddressNotAllocatedError) as exc: logging.error(str(exc)) sys.exit(1)
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})
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 })
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 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)
}) else: addrs = models.Address.objects.filter(host=host).order_by("addr") if request.method == "DELETE": if data.get("confirm"): return render_to_response( "host.html", { "request": request, "host": host, "addrs": addrs, "confirm_delete": 1 }) else: get_object_or_404(models.Host, name=host_name) try: interface.delete(hosts=[host_name]) except interface.InexistantObjectError: return error_404(request) return msg_view( request, host_name + _(" has been removed"), _("The host \"%(host)s\" has correctly been removed.") % {"host": host_name}, referer="/host") else: tmp_val = { "request": request, "host": host, "addrs": addrs, "pools": models.Pool.objects.all(), "mac": ", ".join([addr.macaddr for addr in addrs]),
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)
_("The address you provided is already allocated. Impossible " "to reallocate it to this host.")) return msg_view(request, _("Allocated %(addr)s") % {"addr": addr.addr}, _("The address %(addr)s has correctly been allocated to host " "%(host)s.") % {"addr": addr.addr, "host": host.name}) else: addrs = models.Address.objects.filter(host=host).order_by("addr") if request.method == "DELETE": if data.get("confirm"): return render_to_response("host.html", {"request": request, "host": host, "addrs": addrs, "confirm_delete": 1}) else: get_object_or_404(models.Host, name=host_name) try: interface.delete(hosts=[host_name]) except interface.InexistantObjectError: return error_404(request) return msg_view(request, host_name + _(" has been removed"), _("The host \"%(host)s\" has correctly been removed.") % {"host": host_name}, referer="/host") else: tmp_val = {"request": request, "host": host, "addrs": addrs, "pools": models.Pool.objects.all(), "mac": ", ".join([addr.macaddr for addr in addrs]), "props": models.Property.objects.filter(host=host)} if models.Property.objects.filter(name="owner", host=host): tmp_val["owner"] = models.Property.objects.get( name="owner", host=host).value if request.GET.get("format") == "json": return render_to_response("host.json", tmp_val)