Skip to content

Commit

Permalink
Update approval step to archive IPs and ASNs
Browse files Browse the repository at this point in the history
As opposed to deleting them like we did before
  • Loading branch information
mrchrisadams committed Oct 25, 2023
1 parent 25bca70 commit db655d9
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 67 deletions.
21 changes: 21 additions & 0 deletions apps/accounts/models/hosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
)
from ..permissions import manage_provider, manage_datacenter
from apps.greencheck.choices import StatusApproval, GreenlistChoice
# import apps.greencheck.models as gc_models

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -468,6 +469,26 @@ def mark_as_pending_review(self, approval_request):

return False

# Queries
# set the return type to be a Queryset of the relevant model
# import queryset from django


def active_ip_ranges(self) -> models.QuerySet["GreencheckIP"]:
"""
Return the active IP ranges for this provider
"""
return self.greencheckip_set.filter(active=True)

def active_asns(self) -> models.QuerySet["GreencheckASN"]:
"""
Return the active ASNs for this provider
"""
return self.greencheckasn_set.filter(active=True)


# Properties

def counts_as_green(self):
"""
A convenience check, provide a simple to let us avoid
Expand Down
22 changes: 19 additions & 3 deletions apps/accounts/models/provider_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,14 @@ def approve(self) -> Hostingprovider:
hp.services.clear()

for asn in hp.greencheckasn_set.all():
asn.delete()
# TODO: decide about logging this change if it changes
# the state the ASN
asn.archive()

for ip_range in hp.greencheckip_set.all():
ip_range.delete()
# TODO: decide about logging this change if it changes
# the state the ASN
ip_range.archive()

for doc in hp.supporting_documents.all():
doc.delete()
Expand Down Expand Up @@ -237,6 +241,9 @@ def approve(self) -> Hostingprovider:

# create related objects: ASNs
for asn in self.providerrequestasn_set.all():
if matching_inactive_asn := GreencheckASN.objects.filter(active=False, asn=asn.asn, hostingprovider=hp):
matching_inactive_asn.update(active=True)
continue
try:
GreencheckASN.objects.create(
active=True, asn=asn.asn, hostingprovider=hp
Expand All @@ -246,8 +253,17 @@ def approve(self) -> Hostingprovider:
f"Failed to approve the request `{self}` because the ASN '{asn}' already exists in the database"
) from e

# create related objects: IP ranges
# create related objects: new IP ranges, or activate existing ones
# if inactive matching ones exist in the database
for ip_range in self.providerrequestiprange_set.all():
if matching_inactive_ip := GreencheckIp.objects.filter(active=False,
ip_start=ip_range.start,
ip_end=ip_range.end,
hostingprovider=hp
):
matching_inactive_ip.update(active=True)
continue

GreencheckIp.objects.create(
active=True,
ip_start=ip_range.start,
Expand Down
Loading

0 comments on commit db655d9

Please sign in to comment.