mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-22 18:56:15 +02:00
removed checkhost, added strip on field, added getNameserverChanges
This commit is contained in:
parent
c747be074c
commit
1c6b1b0e2a
4 changed files with 93 additions and 65 deletions
|
@ -20,8 +20,8 @@ class DomainNameserverForm(forms.Form):
|
||||||
|
|
||||||
"""Form for changing nameservers."""
|
"""Form for changing nameservers."""
|
||||||
|
|
||||||
server = forms.CharField(label="Name server")
|
server = forms.CharField(label="Name server", strip=True)
|
||||||
|
#when adding IPs to this form ensure they are stripped as well
|
||||||
|
|
||||||
NameserverFormset = formset_factory(
|
NameserverFormset = formset_factory(
|
||||||
DomainNameserverForm,
|
DomainNameserverForm,
|
||||||
|
|
|
@ -241,27 +241,28 @@ class Domain(TimeStampedModel, DomainHelper):
|
||||||
# TODO-848: This should actually have a second tuple value with the ip address
|
# TODO-848: This should actually have a second tuple value with the ip address
|
||||||
# ignored because uncertain if we will even have a way to display mult.
|
# ignored because uncertain if we will even have a way to display mult.
|
||||||
# and adresses can be a list of mult address
|
# and adresses can be a list of mult address
|
||||||
|
|
||||||
hostList.append((host["name"],))
|
hostList.append((host["name"],))
|
||||||
|
|
||||||
return hostList
|
return hostList
|
||||||
|
|
||||||
def _check_host(self, hostnames: list[str]):
|
# def _check_host(self, hostnames: list[str]):
|
||||||
"""check if host is available, True if available
|
# """check if host is available, True if available
|
||||||
returns boolean"""
|
# returns boolean"""
|
||||||
# TODO-848: Double check this implementation is needed bc it's untested code
|
# # TODO-848: Double check this implementation is needed bc it's untested code
|
||||||
# Check if the IP address is available/real
|
# # Check if the IP address is available/real
|
||||||
checkCommand = commands.CheckHost(hostnames)
|
# checkCommand = commands.CheckHost(hostnames)
|
||||||
try:
|
# try:
|
||||||
response = registry.send(checkCommand, cleaned=True)
|
# response = registry.send(checkCommand, cleaned=True)
|
||||||
return response.res_data[0].avail
|
# return response.res_data[0].avail
|
||||||
except RegistryError as err:
|
# except RegistryError as err:
|
||||||
logger.warning(
|
# logger.warning(
|
||||||
"Couldn't check hosts %s. Errorcode was %s, error was %s",
|
# "Couldn't check hosts %s. Errorcode was %s, error was %s",
|
||||||
hostnames,
|
# hostnames,
|
||||||
err.code,
|
# err.code,
|
||||||
err,
|
# err,
|
||||||
)
|
# )
|
||||||
return False
|
# return False
|
||||||
|
|
||||||
def _create_host(self, host, addrs):
|
def _create_host(self, host, addrs):
|
||||||
"""Call _check_host first before using this function,
|
"""Call _check_host first before using this function,
|
||||||
|
@ -285,6 +286,35 @@ class Domain(TimeStampedModel, DomainHelper):
|
||||||
except RegistryError as e:
|
except RegistryError as e:
|
||||||
logger.error("Error _create_host, code was %s error was %s" % (e.code, e))
|
logger.error("Error _create_host, code was %s error was %s" % (e.code, e))
|
||||||
return e.code
|
return e.code
|
||||||
|
def getNameserverChanges(self, hosts:list[tuple[str]]):
|
||||||
|
"""
|
||||||
|
calls self.nameserver, it should pull from cache but may result
|
||||||
|
in an epp call
|
||||||
|
returns tuple of four values as follows:
|
||||||
|
deleted_values:
|
||||||
|
updated_values:
|
||||||
|
new_values:
|
||||||
|
oldNameservers:"""
|
||||||
|
oldNameservers=self.nameservers
|
||||||
|
|
||||||
|
previousHostDict = {tup[0]: tup[1:] for tup in oldNameservers}
|
||||||
|
newHostDict = {tup[0]: tup[1:] for tup in hosts}
|
||||||
|
|
||||||
|
deleted_values = []
|
||||||
|
updated_values = []
|
||||||
|
new_values = []
|
||||||
|
|
||||||
|
for key in previousHostDict:
|
||||||
|
if key not in newHostDict:
|
||||||
|
deleted_values.append(previousHostDict[key])
|
||||||
|
elif newHostDict[key] != previousHostDict[key]:
|
||||||
|
updated_values.append(newHostDict[key])
|
||||||
|
|
||||||
|
for key in newHostDict:
|
||||||
|
if key not in previousHostDict:
|
||||||
|
new_values.append(newHostDict[key])
|
||||||
|
|
||||||
|
return (deleted_values,updated_values,new_values, oldNameservers)
|
||||||
|
|
||||||
@nameservers.setter # type: ignore
|
@nameservers.setter # type: ignore
|
||||||
def nameservers(self, hosts: list[tuple[str]]):
|
def nameservers(self, hosts: list[tuple[str]]):
|
||||||
|
@ -306,46 +336,44 @@ class Domain(TimeStampedModel, DomainHelper):
|
||||||
logger.info("Setting nameservers")
|
logger.info("Setting nameservers")
|
||||||
logger.info(hosts)
|
logger.info(hosts)
|
||||||
|
|
||||||
# currenthosts = self.nameservers
|
#get the changes made by user and old nameserver values
|
||||||
# that way you have current hosts
|
deleted_values,updated_values,new_values, oldNameservers=self.getNameserverChanges(hosts=hosts)
|
||||||
|
|
||||||
count = 0
|
count = 0
|
||||||
for hostTuple in hosts:
|
for hostTuple in hosts:
|
||||||
host = hostTuple[0].strip() # for removing empty string -- do we need strip?
|
|
||||||
addrs = None
|
addrs = None
|
||||||
|
host=hostTuple[0]
|
||||||
if len(hostTuple) > 1:
|
if len(hostTuple) > 1:
|
||||||
addrs = hostTuple[1:] # list of all the ip address
|
addrs = hostTuple[1:] # list of all the ip address
|
||||||
# TODO-848: Do we want to clean the addresses (strip it if not null?)
|
|
||||||
# TODO-848: Check if the host a .gov (do .split on the last item), isdotgov can be a boolean function
|
# TODO-848: Check if the host a .gov (do .split on the last item), isdotgov can be a boolean function
|
||||||
# TODO-848: if you are dotgov and don't have an IP address then raise error
|
# TODO-848: if you are dotgov and don't have an IP address then raise error
|
||||||
# NOTE-848: TRY logger.info() or print()
|
# NOTE-848: TRY logger.info() or print()
|
||||||
avail = self._check_host([host])
|
|
||||||
|
|
||||||
if avail:
|
|
||||||
# TODO-848: Go through code flow to figure out why count is not incrementing
|
|
||||||
|
|
||||||
createdCode = self._create_host(host=host, addrs=addrs) # creates in registry
|
createdCode = self._create_host(host=host, addrs=addrs) # creates in registry
|
||||||
# TODO-848: Double check if _create_host should handle duplicates + update domain obj?
|
# TODO-848: Double check if _create_host should handle duplicates + update domain obj?
|
||||||
# NOTE-848: if createdCode == ErrorCode.OBJECT_EXISTS: --> self.nameservers
|
# NOTE-848: if createdCode == ErrorCode.OBJECT_EXISTS: --> self.nameservers
|
||||||
|
|
||||||
count += 1
|
count += 1
|
||||||
# NOTE-848: Host can be used by multiple domains
|
# NOTE-848: Host can be used by multiple domains
|
||||||
if createdCode == ErrorCode.COMMAND_COMPLETED_SUCCESSFULLY:
|
if createdCode == ErrorCode.COMMAND_COMPLETED_SUCCESSFULLY:
|
||||||
# NOTE-848: Add host to domain (domain already created, just adding to it)
|
# NOTE-848: Add host to domain (domain already created, just adding to it)
|
||||||
request = commands.UpdateDomain(
|
request = commands.UpdateDomain(
|
||||||
name=self.name, add=[epp.HostObjSet([host])]
|
name=self.name, add=[epp.HostObjSet([host])]
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
registry.send(request, cleaned=True)
|
||||||
|
# count += 1
|
||||||
|
except RegistryError as e:
|
||||||
|
logger.error(
|
||||||
|
"Error adding nameserver, code was %s error was %s"
|
||||||
|
% (e.code, e)
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
|
||||||
registry.send(request, cleaned=True)
|
|
||||||
# count += 1
|
|
||||||
except RegistryError as e:
|
|
||||||
logger.error(
|
|
||||||
"Error adding nameserver, code was %s error was %s"
|
|
||||||
% (e.code, e)
|
|
||||||
)
|
|
||||||
# elif createdCode == ErrorCode.OBJECT_EXISTS:
|
# elif createdCode == ErrorCode.OBJECT_EXISTS:
|
||||||
# count += 1
|
# count += 1
|
||||||
|
# unchangedValuesCount=len(oldNameservers)-len(deleted_values)+addedNameservers
|
||||||
try:
|
try:
|
||||||
print("COUNT IS ")
|
print("COUNT IS ")
|
||||||
print(count)
|
print(count)
|
||||||
|
|
|
@ -561,7 +561,7 @@ class MockEppLib(TestCase):
|
||||||
self.contacts = contacts
|
self.contacts = contacts
|
||||||
self.hosts = hosts
|
self.hosts = hosts
|
||||||
self.statuses = statuses
|
self.statuses = statuses
|
||||||
self.avail = avail
|
self.avail = avail #use for CheckDomain
|
||||||
|
|
||||||
mockDataInfoDomain = fakedEppObject(
|
mockDataInfoDomain = fakedEppObject(
|
||||||
"fakepw",
|
"fakepw",
|
||||||
|
@ -585,8 +585,8 @@ class MockEppLib(TestCase):
|
||||||
mockDataInfoHosts = fakedEppObject(
|
mockDataInfoHosts = fakedEppObject(
|
||||||
"lastPw", cr_date=datetime.datetime(2023, 8, 25, 19, 45, 35)
|
"lastPw", cr_date=datetime.datetime(2023, 8, 25, 19, 45, 35)
|
||||||
)
|
)
|
||||||
mockDataCheckHosts = fakedEppObject(
|
mockDataCreateHost =fakedEppObject(
|
||||||
"lastPw", cr_date=datetime.datetime(2023, 8, 25, 19, 45, 35), avail=True,
|
"lastPw", cr_date=datetime.datetime(2023, 8, 25, 19, 45, 35)
|
||||||
)
|
)
|
||||||
|
|
||||||
def mockSend(self, _request, cleaned):
|
def mockSend(self, _request, cleaned):
|
||||||
|
@ -608,10 +608,8 @@ class MockEppLib(TestCase):
|
||||||
# use this for when a contact is being updated
|
# use this for when a contact is being updated
|
||||||
# sets the second send() to fail
|
# sets the second send() to fail
|
||||||
raise RegistryError(code=ErrorCode.OBJECT_EXISTS)
|
raise RegistryError(code=ErrorCode.OBJECT_EXISTS)
|
||||||
elif (isinstance(_request, commands.CheckHost)):
|
|
||||||
return MagicMock(res_data=[self.mockDataCheckHosts])
|
|
||||||
elif (isinstance(_request, commands.CreateHost)):
|
elif (isinstance(_request, commands.CreateHost)):
|
||||||
return MagicMock(res_data=[self.mockDataCheckHosts], code=ErrorCode.COMMAND_COMPLETED_SUCCESSFULLY)
|
return MagicMock(res_data=[self.mockDataCreateHost], code=ErrorCode.COMMAND_COMPLETED_SUCCESSFULLY)
|
||||||
return MagicMock(res_data=[self.mockDataInfoHosts])
|
return MagicMock(res_data=[self.mockDataInfoHosts])
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
|
@ -534,6 +534,21 @@ class TestRegistrantNameservers(MockEppLib):
|
||||||
"""
|
"""
|
||||||
super().setUp()
|
super().setUp()
|
||||||
self.domain, _ = Domain.objects.get_or_create(name="my-nameserver.gov", state=Domain.State.DNS_NEEDED)
|
self.domain, _ = Domain.objects.get_or_create(name="my-nameserver.gov", state=Domain.State.DNS_NEEDED)
|
||||||
|
def test_get_nameserver_changes(self):
|
||||||
|
self.domain._cache["hosts"]=[{
|
||||||
|
"name": "ns1.example.com",
|
||||||
|
"addrs": None
|
||||||
|
},
|
||||||
|
{"name": "ns2.example.com",
|
||||||
|
"addrs": ["1.2.3"]
|
||||||
|
},
|
||||||
|
{"name": "ns3.example.com",
|
||||||
|
"addrs": None
|
||||||
|
}
|
||||||
|
]
|
||||||
|
newChanges=[("ns1.example.com",),("ns3.example.com",["1.2.4"]),("ns4.example.com",)]
|
||||||
|
retTuple=self.domain.getNameserverChanges(newChanges)
|
||||||
|
print(retTuple)
|
||||||
|
|
||||||
def test_user_adds_one_nameserver(self):
|
def test_user_adds_one_nameserver(self):
|
||||||
"""
|
"""
|
||||||
|
@ -555,9 +570,6 @@ class TestRegistrantNameservers(MockEppLib):
|
||||||
|
|
||||||
# checking if commands were sent (commands have to be sent in order)
|
# checking if commands were sent (commands have to be sent in order)
|
||||||
expectedCalls = [
|
expectedCalls = [
|
||||||
call(
|
|
||||||
commands.CheckHost([created_host.name]), cleaned=True
|
|
||||||
),
|
|
||||||
call(created_host, cleaned=True),
|
call(created_host, cleaned=True),
|
||||||
call(update_domain_with_created, cleaned=True),
|
call(update_domain_with_created, cleaned=True),
|
||||||
]
|
]
|
||||||
|
@ -591,14 +603,8 @@ class TestRegistrantNameservers(MockEppLib):
|
||||||
|
|
||||||
# checking if commands were sent (commands have to be sent in order)
|
# checking if commands were sent (commands have to be sent in order)
|
||||||
expectedCalls = [
|
expectedCalls = [
|
||||||
call(
|
|
||||||
commands.CheckHost([created_host1.name]), cleaned=True
|
|
||||||
),
|
|
||||||
call(created_host1, cleaned=True),
|
call(created_host1, cleaned=True),
|
||||||
call(update_domain_with_created1, cleaned=True),
|
call(update_domain_with_created1, cleaned=True),
|
||||||
call(
|
|
||||||
commands.CheckHost([created_host2.name]), cleaned=True
|
|
||||||
),
|
|
||||||
call(created_host2, cleaned=True),
|
call(created_host2, cleaned=True),
|
||||||
call(update_domain_with_created2, cleaned=True),
|
call(update_domain_with_created2, cleaned=True),
|
||||||
]
|
]
|
||||||
|
@ -639,12 +645,8 @@ class TestRegistrantNameservers(MockEppLib):
|
||||||
self.domain.nameservers = [(nameserver1,), (nameserver2,), (nameserver3,), (nameserver4,),
|
self.domain.nameservers = [(nameserver1,), (nameserver2,), (nameserver3,), (nameserver4,),
|
||||||
(nameserver5,), (nameserver6,), (nameserver7,), (nameserver8,), (nameserver9), (nameserver10,),
|
(nameserver5,), (nameserver6,), (nameserver7,), (nameserver8,), (nameserver9), (nameserver10,),
|
||||||
(nameserver11,), (nameserver12,), (nameserver13,), (nameserver14,)]
|
(nameserver11,), (nameserver12,), (nameserver13,), (nameserver14,)]
|
||||||
print("!! Hello I am in _get_14_nameservers!")
|
|
||||||
|
|
||||||
# TO-FIX: This is borked because it hits the error as soon as we set up 14
|
|
||||||
self.assertRaises(ValueError, _get_14_nameservers)
|
self.assertRaises(ValueError, _get_14_nameservers)
|
||||||
print("self.mockedSendFunction.call_args_list is ")
|
|
||||||
print(self.mockedSendFunction.call_args_list)
|
|
||||||
self.assertEqual(self.mockedSendFunction.call_count, 0)
|
self.assertEqual(self.mockedSendFunction.call_count, 0)
|
||||||
|
|
||||||
@skip("not implemented yet")
|
@skip("not implemented yet")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue