diff --git a/docs/deployment.md b/docs/deployment.md index 34122a9..6a8d800 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -2305,6 +2305,10 @@ Each request captures the reviewed SOA serial and records. You can edit the zone while it publishes. Publish the newer revision after the current request finishes. Use **Retry publishing** if the current revision failed. +In the DNS editor, **Use hosting IP** fills an A or AAAA record with a +matching address from the effective DNS zone template. Choose an address +when more than one is available. You can edit the value before saving. + To let the customer edit a zone's records, enable **Delegate record editing** on that zone. They can also publish the zone when Coulobre publishing is enabled. The operator controls the authoritative diff --git a/src/coulobre/hosting/dns_defaults.py b/src/coulobre/hosting/dns_defaults.py index af20e69..e336228 100644 --- a/src/coulobre/hosting/dns_defaults.py +++ b/src/coulobre/hosting/dns_defaults.py @@ -1,3 +1,5 @@ +from ipaddress import ip_address + from django.conf import settings from .models import DnsZoneDefaults @@ -8,3 +10,17 @@ def zone_defaults(): saved.nameservers = saved.nameservers or settings.HOSTING_NAMESERVERS saved.addresses = saved.addresses or settings.HOSTING_PUBLIC_ADDRESSES return saved + + +def hosting_address_choices(): + choices = {"A": [], "AAAA": []} + for configured in zone_defaults().addresses: + try: + address = ip_address(configured) + except ValueError: + continue + addresses = choices["A" if address.version == 4 else "AAAA"] + value = str(address) + if value not in addresses: + addresses.append(value) + return choices diff --git a/src/coulobre/hosting/static/hosting/dns-record-editor.js b/src/coulobre/hosting/static/hosting/dns-record-editor.js index 67c137d..edd8ce8 100644 --- a/src/coulobre/hosting/static/hosting/dns-record-editor.js +++ b/src/coulobre/hosting/static/hosting/dns-record-editor.js @@ -8,6 +8,7 @@ const valueLabels = { MX: "Mail server hostname", TXT: "Text", SRV: "Priority, weight, port and target", CAA: "Flags, tag and value", }; +const hostingAddresses = JSON.parse(document.getElementById("dns-hosting-addresses").textContent); function numberRows() { Array.from(rows.children).forEach((row, index) => { @@ -23,6 +24,11 @@ function appendRecord(record) { const priority = row.querySelector("[data-record-priority]"); const priorityLabel = row.querySelector("[data-priority-label]"); const valueLabel = row.querySelector("[data-record-value-label]"); + const hostingAddress = row.querySelector("[data-hosting-address]"); + const onlyAddress = row.querySelector("[data-only-hosting-address]"); + const addressSelect = row.querySelector("[data-hosting-address-select]"); + const useAddress = row.querySelector("[data-use-hosting-address]"); + const missingAddress = row.querySelector("[data-missing-hosting-address]"); name.value = record.name; type.value = record.type; value.value = record.value; @@ -42,8 +48,31 @@ function appendRecord(record) { value.rows = type.value === "TXT" ? 4 : 1; value.classList.toggle("dns-txt-value", type.value === "TXT"); valueLabel.textContent = valueLabels[type.value]; + + const addresses = hostingAddresses[type.value]; + hostingAddress.hidden = !addresses; + if (!addresses) return; + addressSelect.replaceChildren(...addresses.map((address) => new Option(address, address))); + if (addresses.length > 1) { + addressSelect.add(new Option("Choose a hosting IP", ""), 0); + addressSelect.selectedIndex = 0; + } + onlyAddress.hidden = addresses.length !== 1; + onlyAddress.textContent = addresses.length === 1 ? addresses[0] : ""; + addressSelect.hidden = addresses.length < 2; + useAddress.hidden = addresses.length === 0; + useAddress.disabled = !addressSelect.value; + missingAddress.hidden = addresses.length > 0; + missingAddress.textContent = `No hosting ${valueLabels[type.value]} is configured.`; }; type.addEventListener("change", updateFields); + addressSelect.addEventListener("change", () => { + useAddress.disabled = !addressSelect.value; + }); + useAddress.addEventListener("click", () => { + value.value = addressSelect.value; + value.focus(); + }); row.querySelector("[data-remove-record]").addEventListener("click", () => { const nextFocus = row.nextElementSibling?.querySelector("input") || add; row.remove(); diff --git a/src/coulobre/hosting/static/hosting/panel.css b/src/coulobre/hosting/static/hosting/panel.css index 4738634..6611eb6 100644 --- a/src/coulobre/hosting/static/hosting/panel.css +++ b/src/coulobre/hosting/static/hosting/panel.css @@ -1061,8 +1061,19 @@ dd { display: flex; gap: 0.5rem; } -.dns-edit-value > label { flex: 1; } +.dns-record-value-field { flex: 1; min-width: 0; } .dns-edit-value > [data-priority-label] { flex: 0 0 5rem; } +.dns-hosting-address { + display: flex; + align-items: center; + flex-wrap: wrap; + gap: 0.4rem; + margin-top: 0.4rem; +} +.dns-hosting-address[hidden], .dns-hosting-address > [hidden] { display: none; } +.dns-hosting-address > select { width: auto; max-width: 100%; } +.dns-hosting-address > code { overflow-wrap: anywhere; } +.dns-hosting-address > .heading-meta { margin: 0; } .dns-edit-row textarea { display: block; min-height: 2.45rem; diff --git a/src/coulobre/hosting/templates/hosting/dns_record_editor.html b/src/coulobre/hosting/templates/hosting/dns_record_editor.html index f134600..0d0b20e 100644 --- a/src/coulobre/hosting/templates/hosting/dns_record_editor.html +++ b/src/coulobre/hosting/templates/hosting/dns_record_editor.html @@ -1,4 +1,5 @@ {{ form.editor_rows|json_script:"dns-record-data" }} +{{ hosting_addresses|json_script:"dns-hosting-addresses" }}
+
+
+
+