mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-22 10:46:06 +02:00
remove nameservers model; update db with hosts and host_ips on _fetch_cache
This commit is contained in:
parent
aea90791a0
commit
7fdc61be46
5 changed files with 64 additions and 21 deletions
|
@ -10,6 +10,8 @@ from django_fsm import FSMField, transition, TransitionNotAllowed # type: ignor
|
|||
from django.db import models
|
||||
from django.utils import timezone
|
||||
from typing import Any
|
||||
from registrar.models.host import Host
|
||||
from registrar.models.host_ip import HostIP
|
||||
|
||||
|
||||
from registrar.utility.errors import (
|
||||
|
@ -1605,6 +1607,7 @@ class Domain(TimeStampedModel, DomainHelper):
|
|||
cache = self._extract_data_from_response(data_response)
|
||||
cleaned = self._clean_cache(cache, data_response)
|
||||
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._cache = cleaned
|
||||
|
@ -1651,7 +1654,7 @@ class Domain(TimeStampedModel, DomainHelper):
|
|||
return dnssec_data
|
||||
|
||||
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_contacts = self._cache.get("contacts")
|
||||
|
||||
|
@ -1666,6 +1669,51 @@ class Domain(TimeStampedModel, DomainHelper):
|
|||
if old_cache_contacts is not None:
|
||||
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):
|
||||
"""Update dates (expiration and creation) from cleaned"""
|
||||
requires_save = False
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue