Exemplo n.º 1
0
def view_edit_record(request,
                     dns_server,
                     zone_name,
                     record_name=None,
                     record_type=None,
                     record_data=None,
                     record_ttl=None):
    """View to edit an RR record to DNS zone."""
    this_server = get_object_or_404(models.BindServer, hostname=dns_server)
    if request.method == 'POST':
        if "in-addr.arpa" in zone_name or "ip6.arpa" in zone_name:
            form = forms.FormAddReverseRecord(request.POST)
        else:
            form = forms.FormAddForwardRecord(request.POST)
        if form.is_valid():
            form_cleaned = form.cleaned_data
            try:
                helpers.add_record(form_cleaned["dns_server"],
                                   str(form_cleaned["zone_name"]),
                                   str(form_cleaned["record_name"]),
                                   str(form_cleaned["record_type"]),
                                   str(form_cleaned["record_data"]),
                                   form_cleaned["ttl"],
                                   form_cleaned["key_name"],
                                   form_cleaned["create_reverse"])
            except (exceptions.KeyringException,
                    exceptions.RecordException) as exc:
                messages.error(
                    request, "Modifying %s.%s failed: %s" %
                    (form_cleaned["record_name"], zone_name, exc))
            else:
                messages.success(
                    request, "%s.%s was modified successfully." %
                    (form_cleaned["record_name"], zone_name))
                return redirect('zone_list',
                                dns_server=dns_server,
                                zone_name=zone_name)
        else:
            messages.error(request, "Form data was invalid. Check all inputs.")
    else:
        key_id = models.BindServer.objects.get(
            hostname=dns_server).default_transfer_key.id
        form = forms.FormAddForwardRecord(
            initial={
                'zone_name': zone_name,
                'record_name': record_name,
                'record_data': record_data,
                'ttl': record_ttl,
                'record_type': record_type,
                'key_name': key_id
            })

    return render(request, "bcommon/add_record_form.html", {
        "dns_server": this_server,
        "form": form
    })
Exemplo n.º 2
0
 def test_Valid_FormAddReverseRecord(self):
     """Test FormAddReverseRecord with valid data."""
     form_data = {"dns_server": "server1",
                  "record_name": 41,
                  "record_type": "PTR",
                  "zone_name": "1.254.10.in-addr.arpa",
                  "record_data": "reverse41.domain1.local",
                  "ttl": 3600,
                  "key_name": '1'}
     reverseform_1 = forms.FormAddReverseRecord(form_data)
     reverseform_1.is_valid()
     self.assertTrue(reverseform_1.is_valid())
Exemplo n.º 3
0
    def test_Valid_FormAddRecord(self):
        """Test FormAddRecord with valid data, with/without create_reverse."""
        form_data = {
            "dns_server": "server1",
            "record_name": "record1",
            "record_type": "A",
            "zone_name": "domain.local",
            "record_data": "10.254.254.254",
            "ttl": 3600,
            "key_name": None,
            "create_reverse": False
        }

        testform_1 = forms.FormAddForwardRecord(form_data)
        self.assertTrue(testform_1.is_valid())

        form_data = {
            "dns_server": "server1",
            "record_name": "record1",
            "record_type": "A",
            "zone_name": "domain.local",
            "record_data": "10.254.254.254",
            "ttl": 3600,
            "key_name": None,
            "create_reverse": True
        }

        testform_2 = forms.FormAddForwardRecord(form_data)
        self.assertTrue(testform_2.is_valid())

        form_data = {
            "dns_server": "server1",
            "record_name": 41,
            "record_type": "PTR",
            "zone_name": "1.254.10.in-addr.arpa",
            "record_data": "reverse41.domain1.local",
            "ttl": 3600,
            "key_name": None
        }
        reverseform_1 = forms.FormAddReverseRecord(form_data)
        reverseform_1.is_valid()
        self.assertTrue(reverseform_1.is_valid())
Exemplo n.º 4
0
def view_add_record(request, dns_server, zone_name):
    """View to allow to add A records."""
    if not request.user.is_staff and not request.user.is_superuser:
        return render(request, "403.html", status=403)
    this_server = get_object_or_404(models.BindServer, hostname=dns_server)

    if request.method == 'POST':
        if "in-addr.arpa" in zone_name or "ip6.arpa" in zone_name:
            form = forms.FormAddReverseRecord(request.POST)
        else:
            form = forms.FormAddForwardRecord(request.POST)
        if form.is_valid():
            form_cleaned = form.cleaned_data
            try:
                helpers.add_record(form_cleaned["dns_server"],
                                   str(form_cleaned["zone_name"]),
                                   str(form_cleaned["record_name"]),
                                   str(form_cleaned["record_type"]),
                                   str(form_cleaned["record_data"]),
                                   form_cleaned["ttl"],
                                   form_cleaned["key_name"],
                                   form_cleaned["create_reverse"])
            except (KeyringException, RecordException) as exc:
                messages.error(
                    request, "Adding %s.%s failed: %s" %
                    (form_cleaned["record_name"], zone_name, exc))
            else:
                messages.success(
                    request, "%s.%s was added successfully." %
                    (form_cleaned["record_name"], zone_name))
                return redirect('zone_list',
                                dns_server=dns_server,
                                zone_name=zone_name)
    else:
        form = forms.FormAddForwardRecord(initial={'zone_name': zone_name})

    return render(request, "bcommon/add_record_form.html", {
        "dns_server": this_server,
        "form": form
    })