remove nameservers model; update db with hosts and host_ips on _fetch_cache

This commit is contained in:
David Kennedy 2023-12-21 07:45:05 -05:00
parent aea90791a0
commit 7fdc61be46
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B
5 changed files with 64 additions and 21 deletions

View file

@ -1052,7 +1052,6 @@ admin.site.register(models.DomainInformation, DomainInformationAdmin)
admin.site.register(models.Domain, DomainAdmin) admin.site.register(models.Domain, DomainAdmin)
admin.site.register(models.DraftDomain, DraftDomainAdmin) admin.site.register(models.DraftDomain, DraftDomainAdmin)
admin.site.register(models.Host, MyHostAdmin) admin.site.register(models.Host, MyHostAdmin)
admin.site.register(models.Nameserver, MyHostAdmin)
admin.site.register(models.Website, WebsiteAdmin) admin.site.register(models.Website, WebsiteAdmin)
admin.site.register(models.PublicContact, AuditedAdmin) admin.site.register(models.PublicContact, AuditedAdmin)
admin.site.register(models.DomainApplication, DomainApplicationAdmin) admin.site.register(models.DomainApplication, DomainApplicationAdmin)

View file

@ -0,0 +1,15 @@
# Generated by Django 4.2.7 on 2023-12-21 11:07
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("registrar", "0057_domainapplication_submission_date"),
]
operations = [
migrations.DeleteModel(
name="Nameserver",
),
]

View file

@ -7,7 +7,6 @@ from .draft_domain import DraftDomain
from .host_ip import HostIP from .host_ip import HostIP
from .host import Host from .host import Host
from .domain_invitation import DomainInvitation from .domain_invitation import DomainInvitation
from .nameserver import Nameserver
from .user_domain_role import UserDomainRole from .user_domain_role import UserDomainRole
from .public_contact import PublicContact from .public_contact import PublicContact
from .user import User from .user import User
@ -24,7 +23,6 @@ __all__ = [
"DomainInvitation", "DomainInvitation",
"HostIP", "HostIP",
"Host", "Host",
"Nameserver",
"UserDomainRole", "UserDomainRole",
"PublicContact", "PublicContact",
"User", "User",
@ -41,7 +39,6 @@ auditlog.register(DomainInvitation)
auditlog.register(DomainInformation) auditlog.register(DomainInformation)
auditlog.register(HostIP) auditlog.register(HostIP)
auditlog.register(Host) auditlog.register(Host)
auditlog.register(Nameserver)
auditlog.register(UserDomainRole) auditlog.register(UserDomainRole)
auditlog.register(PublicContact) auditlog.register(PublicContact)
auditlog.register(User, m2m_fields=["user_permissions", "groups"]) auditlog.register(User, m2m_fields=["user_permissions", "groups"])

View file

@ -10,6 +10,8 @@ from django_fsm import FSMField, transition, TransitionNotAllowed # type: ignor
from django.db import models from django.db import models
from django.utils import timezone from django.utils import timezone
from typing import Any from typing import Any
from registrar.models.host import Host
from registrar.models.host_ip import HostIP
from registrar.utility.errors import ( from registrar.utility.errors import (
@ -1605,6 +1607,7 @@ class Domain(TimeStampedModel, DomainHelper):
cache = self._extract_data_from_response(data_response) cache = self._extract_data_from_response(data_response)
cleaned = self._clean_cache(cache, data_response) cleaned = self._clean_cache(cache, data_response)
self._update_hosts_and_contacts(cleaned, fetch_hosts, fetch_contacts) self._update_hosts_and_contacts(cleaned, fetch_hosts, fetch_contacts)
self._update_hosts_and_ips_in_db(cleaned, fetch_hosts)
self._update_dates(cleaned) self._update_dates(cleaned)
self._cache = cleaned self._cache = cleaned
@ -1651,7 +1654,7 @@ class Domain(TimeStampedModel, DomainHelper):
return dnssec_data return dnssec_data
def _update_hosts_and_contacts(self, cleaned, fetch_hosts, fetch_contacts): def _update_hosts_and_contacts(self, cleaned, fetch_hosts, fetch_contacts):
"""Capture and store old hosts and contacts from cache if the don't exist""" """Capture and cache old hosts and contacts from cache if they don't exist in cleaned"""
old_cache_hosts = self._cache.get("hosts") old_cache_hosts = self._cache.get("hosts")
old_cache_contacts = self._cache.get("contacts") old_cache_contacts = self._cache.get("contacts")
@ -1666,6 +1669,51 @@ class Domain(TimeStampedModel, DomainHelper):
if old_cache_contacts is not None: if old_cache_contacts is not None:
cleaned["contacts"] = old_cache_contacts cleaned["contacts"] = old_cache_contacts
def _update_hosts_and_ips_in_db(self, cleaned, fetch_hosts):
"""Update hosts and host_ips in database if retrieved from registry.
Parameters:
self: the domain to be updated with hosts and ips from cleaned
cleaned: dict containing hosts. Hosts are provided as a list of dicts, e.g.
[{"name": "ns1.example.com",}, {"name": "ns1.example.gov"}, "addrs": ["0.0.0.0"])]
fetch_hosts: boolean indicating whether or not fetch_hosts was called
"""
if fetch_hosts:
cleaned_hosts = cleaned["hosts"]
# Get all existing hosts from the database for this domain
existing_hosts_in_db = Host.objects.filter(domain=self)
# Identify hosts to delete
cleaned_host_names = set(cleaned_host["name"] for cleaned_host in cleaned_hosts)
hosts_to_delete_from_db = [
existing_host for existing_host in existing_hosts_in_db if existing_host.name not in cleaned_host_names
]
# Delete hosts and their associated HostIP instances
for host_to_delete in hosts_to_delete_from_db:
# Delete associated HostIP instances
HostIP.objects.filter(host=host_to_delete).delete()
# Delete the host itself
host_to_delete.delete()
# Update or create Hosts and HostIPs
for cleaned_host in cleaned_hosts:
# Check if the cleaned_host already exists
host_in_db, host_created = Host.objects.get_or_create(domain=self, name=cleaned_host["name"])
# Get cleaned list of ips for update
cleaned_ips = cleaned_host["addrs"]
if not host_created:
# Get all existing ips from the database for this host
existing_ips_in_db = HostIP.objects.filter(host=host_in_db)
# Identify IPs to delete
ips_to_delete_from_db = [
existing_ip for existing_ip in existing_ips_in_db if existing_ip.address not in cleaned_ips
]
# Delete IPs
for ip_to_delete in ips_to_delete_from_db:
# Delete the ip
ip_to_delete.delete()
# Update or create HostIP instances
for ip_address in cleaned_ips:
HostIP.objects.get_or_create(address=ip_address, host=host_in_db)
def _update_dates(self, cleaned): def _update_dates(self, cleaned):
"""Update dates (expiration and creation) from cleaned""" """Update dates (expiration and creation) from cleaned"""
requires_save = False requires_save = False

View file

@ -1,16 +0,0 @@
from .host import Host
class Nameserver(Host):
"""
A nameserver is a host which has been delegated to respond to DNS queries.
The registry is the source of truth for this data.
This model exists ONLY to allow a new registrant to draft DNS entries
before their application is approved.
"""
# there is nothing here because all of the fields are
# defined over there on the Host class
pass