prototype page

This commit is contained in:
zandercymatics 2024-12-04 14:09:46 -07:00
parent bd64a04a91
commit 24feb00327
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
6 changed files with 98 additions and 24 deletions

View file

@ -298,6 +298,7 @@ urlpatterns = [
name="todo", name="todo",
), ),
path("domain/<int:pk>", views.DomainView.as_view(), name="domain"), path("domain/<int:pk>", views.DomainView.as_view(), name="domain"),
path("domain/<int:pk>/prototype-dns", views.PrototypeDomainDNSRecordView.as_view(), name="prototype-domain-dns"),
path("domain/<int:pk>/users", views.DomainUsersView.as_view(), name="domain-users"), path("domain/<int:pk>/users", views.DomainUsersView.as_view(), name="domain-users"),
path( path(
"domain/<int:pk>/dns", "domain/<int:pk>/dns",

View file

@ -355,20 +355,18 @@ class Domain(TimeStampedModel, DomainHelper):
def create_prototype_dns_record(self, dns_record_dict): def create_prototype_dns_record(self, dns_record_dict):
print(f"what is the key? {settings.SECRET_REGISTRY_TENANT_KEY}") print(f"what is the key? {settings.SECRET_REGISTRY_TENANT_KEY}")
# Don't execute this function on any other domain
if settings.IS_PRODUCTION and self.name != "igorville.gov":
logger.warning(f"create_dns_record was called for domain {self.name}")
return None
# Cloudflare API endpoints # Cloudflare API endpoints
base_url = "https://api.cloudflare.com/client/v4" base_url = "https://api.cloudflare.com/client/v4"
headers = { headers = {
"Authorization": f"Bearer {settings.SECRET_REGISTRY_TENANT_KEY}", "Authorization": f"Bearer {settings.SECRET_REGISTRY_TENANT_KEY}",
"Content-Type": "application/json" "Content-Type": "application/json"
} }
if settings.IS_PRODUCTION:
if self.name == "igorville.gov":
# do stuff
pass
else:
logger.warning(f"create_dns_record was called for domain {self.name}")
else:
pass
# TODO - check if these things exist before doing stuff # TODO - check if these things exist before doing stuff
# 1. Get tenant details # 1. Get tenant details

View file

@ -29,12 +29,16 @@
<p>You can enter your name servers, as well as other DNS-related information, in the following sections:</p> <p>You can enter your name servers, as well as other DNS-related information, in the following sections:</p>
{% url 'domain-dns-nameservers' pk=domain.id as url %} {% url 'domain-dns-nameservers' pk=domain.id as url %}
<ul class="usa-list"> <ul class="usa-list">
<li><a href="{{ url }}">Name servers</a></li> <li><a href="{{ url }}">Name servers</a></li>
{% url 'domain-dns-dnssec' pk=domain.id as url %} {% url 'domain-dns-dnssec' pk=domain.id as url %}
<li><a href="{{ url }}">DNSSEC</a></li> <li><a href="{{ url }}">DNSSEC</a></li>
{% if dns_prototype_flag %}
<li><a href="{% url 'prototype-domain-dns' pk=domain.id %}">Prototype DNS record creator</a></li>
{% endif %}
</ul> </ul>
{% endblock %} {# domain_content #} {% endblock %} {# domain_content #}

View file

@ -0,0 +1,29 @@
{% extends "domain_base.html" %}
{% load static field_helpers url_helpers %}
{% block title %}Prototype DNS | {{ domain.name }} | {% endblock %}
{% block domain_content %}
{% include "includes/form_errors.html" with form=form %}
<h1>Add a cloudflare DNS record</h1>
<p>
This is a prototype that demonstrates adding an 'A' record to igorville.gov.
Do note that this just adds records, but does not update or delete existing ones.
</p>
<form class="usa-form usa-form--large" method="post" novalidate id="form-container">
{% csrf_token %}
{% input_with_errors form.name %}
{% input_with_errors form.content %}
{% input_with_errors form.ttl %}
<button
type="submit"
class="usa-button"
>
Add record
</button>
</form>
{% endblock %} {# domain_content #}

View file

@ -13,6 +13,7 @@ from .domain import (
DomainAddUserView, DomainAddUserView,
DomainInvitationCancelView, DomainInvitationCancelView,
DomainDeleteUserView, DomainDeleteUserView,
PrototypeDomainDNSRecordView,
) )
from .user_profile import UserProfileView, FinishProfileSetupView from .user_profile import UserProfileView, FinishProfileSetupView
from .health import * from .health import *

View file

@ -466,28 +466,16 @@ class DomainDNSView(DomainBaseView):
class PrototypeDomainDNSRecordForm(forms.Form): class PrototypeDomainDNSRecordForm(forms.Form):
"""Form for adding DNS records in prototype.""" """Form for adding DNS records in prototype."""
record_type = forms.ChoiceField(
label="Record Type",
choices=[
("A", "A"),
("AAAA", "AAAA"),
("CNAME", "CNAME"),
("TXT", "TXT")
],
required=True
)
name = forms.CharField( name = forms.CharField(
label="Name", label="DNS record name (A record)",
required=True, required=True,
help_text="The DNS record name (e.g., www)" help_text="DNS record name"
) )
content = forms.GenericIPAddressField( content = forms.GenericIPAddressField(
label="IPv4 Address", label="IPv4 Address",
required=True, required=True,
protocol="IPv4", protocol="IPv4",
help_text="The IPv4 address this record points to"
) )
ttl = forms.ChoiceField( ttl = forms.ChoiceField(
@ -504,9 +492,62 @@ class PrototypeDomainDNSRecordForm(forms.Form):
(86400, "1 day") (86400, "1 day")
], ],
initial=1, initial=1,
help_text="Time to Live - how long DNS resolvers should cache this record"
) )
class PrototypeDomainDNSRecordView(DomainFormBaseView):
template_name = "prototype_domain_dns.html"
form_class = PrototypeDomainDNSRecordForm
def has_permission(self):
has_permission = super().has_permission()
if not has_permission:
return False
flag_enabled = flag_is_active_for_user(self.request.user, "dns_prototype_flag")
if not flag_enabled:
return False
return True
def get_success_url(self):
return reverse("prototype-domain-dns", kwargs={"pk": self.object.pk})
def post(self, request, *args, **kwargs):
"""Handle form submission."""
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
try:
# Format the DNS record according to Cloudflare's API requirements
dns_record = {
"type": "A",
"name": form.cleaned_data["name"],
"content": form.cleaned_data["content"],
"ttl": int(form.cleaned_data["ttl"]),
"comment": f"Test record (will eventually need to clean up)"
}
result = self.object.create_prototype_dns_record(dns_record)
if result: # Assuming create_prototype_dns_record returns the response data
messages.success(
request,
f"DNS A record '{form.cleaned_data['name']}' created successfully."
)
else:
messages.error(
request,
"Failed to create DNS A record. Please try again."
)
except Exception as err:
logger.error(f"Error creating DNS A record for {self.object.name}: {err}")
messages.error(
request,
f"An error occurred while creating the DNS A record: {err}"
)
return super().post(request)
class DomainNameserversView(DomainFormBaseView): class DomainNameserversView(DomainFormBaseView):
"""Domain nameserver editing view.""" """Domain nameserver editing view."""