changed host object set to contain the list off add hosts instead of using multiple objects

This commit is contained in:
Alysia Broddrick 2023-10-11 08:12:53 -07:00
parent 36c9cbdaa3
commit c66813100e
No known key found for this signature in database
GPG key ID: 03917052CD0F06B7
2 changed files with 48 additions and 25 deletions

View file

@ -252,8 +252,7 @@ class Domain(TimeStampedModel, DomainHelper):
return hostList return hostList
def _create_host(self, host, addrs): def _create_host(self, host, addrs):
"""Call _check_host first before using this function, """Creates the host object in the registry
This creates the host object in the registry
doesn't add the created host to the domain doesn't add the created host to the domain
returns ErrorCode (int)""" returns ErrorCode (int)"""
logger.info("Creating host") logger.info("Creating host")
@ -380,7 +379,7 @@ class Domain(TimeStampedModel, DomainHelper):
new_values = { new_values = {
key: newHostDict.get(key) key: newHostDict.get(key)
for key in newHostDict for key in newHostDict
if key not in previousHostDict if key not in previousHostDict and key.strip() != ""
} }
for nameserver, ip in new_values.items(): for nameserver, ip in new_values.items():
@ -402,15 +401,19 @@ class Domain(TimeStampedModel, DomainHelper):
% (hostTuple[0], updated_response_code) % (hostTuple[0], updated_response_code)
) )
def createNewHostList(self, new_values: dict) -> list: def createNewHostList(self, new_values: dict):
"""convert the dictionary of new values to a list of HostObjSet """convert the dictionary of new values to a list of HostObjSet
for use in the UpdateDomain epp message for use in the UpdateDomain epp message
Args: Args:
new_values: dict(str,list)- dict of {nameserver:ips} to add to domain new_values: dict(str,list)- dict of {nameserver:ips} to add to domain
Returns: Returns:
list[epp.HostObjSet]-epp object list for use in the UpdateDomain epp message tuple [list[epp.HostObjSet], int]
list[epp.HostObjSet]-epp object for use in the UpdateDomain epp message
defaults to empty list
int-number of items being created default 0
""" """
addToDomainList = []
hostStringList = []
for key, value in new_values.items(): for key, value in new_values.items():
createdCode = self._create_host( createdCode = self._create_host(
host=key, addrs=value host=key, addrs=value
@ -419,21 +422,31 @@ class Domain(TimeStampedModel, DomainHelper):
createdCode == ErrorCode.COMMAND_COMPLETED_SUCCESSFULLY createdCode == ErrorCode.COMMAND_COMPLETED_SUCCESSFULLY
or createdCode == ErrorCode.OBJECT_EXISTS or createdCode == ErrorCode.OBJECT_EXISTS
): ):
addToDomainList.append(epp.HostObjSet([key])) hostStringList.append(key)
if hostStringList == []:
return [], 0
return addToDomainList addToDomainObject = epp.HostObjSet(hosts=hostStringList)
return [addToDomainObject], len(hostStringList)
def createDeleteHostList(self, hostsToDelete: list[str]): def createDeleteHostList(self, hostsToDelete: list[str]):
""" """
Args: Args:
hostsToDelete (list[str])- list of nameserver/host names to remove hostsToDelete (list[str])- list of nameserver/host names to remove
Returns: Returns:
list[epp.HostObjSet]-epp object list for use in the UpdateDomain epp message tuple [list[epp.HostObjSet], int]
list[epp.HostObjSet]-epp object for use in the UpdateDomain epp message
defaults to empty list
int-number of items being created default 0
""" """
deleteList = [] deleteStrList = []
for nameserver in hostsToDelete: for nameserver in hostsToDelete:
deleteList.append(epp.HostObjSet([nameserver])) deleteStrList.append(nameserver)
return deleteList if deleteStrList == []:
return [], 0
deleteObj = epp.HostObjSet(hosts=hostsToDelete)
return [deleteObj], len(deleteStrList)
@Cache @Cache
def dnssecdata(self) -> extensions.DNSSECExtension: def dnssecdata(self) -> extensions.DNSSECExtension:
@ -482,8 +495,8 @@ class Domain(TimeStampedModel, DomainHelper):
_ = self._update_host_values( _ = self._update_host_values(
updated_values, oldNameservers updated_values, oldNameservers
) # returns nothing, just need to be run and errors ) # returns nothing, just need to be run and errors
addToDomainList = self.createNewHostList(new_values) addToDomainList, addToDomainCount = self.createNewHostList(new_values)
deleteHostList = self.createDeleteHostList(deleted_values) deleteHostList, deleteCount = self.createDeleteHostList(deleted_values)
responseCode = self.addAndRemoveHostsFromDomain( responseCode = self.addAndRemoveHostsFromDomain(
hostsToAdd=addToDomainList, hostsToDelete=deleteHostList hostsToAdd=addToDomainList, hostsToDelete=deleteHostList
) )
@ -492,9 +505,8 @@ class Domain(TimeStampedModel, DomainHelper):
if responseCode != ErrorCode.COMMAND_COMPLETED_SUCCESSFULLY: if responseCode != ErrorCode.COMMAND_COMPLETED_SUCCESSFULLY:
raise NameserverError(code=nsErrorCodes.UNABLE_TO_UPDATE_DOMAIN) raise NameserverError(code=nsErrorCodes.UNABLE_TO_UPDATE_DOMAIN)
successTotalNameservers = ( successTotalNameservers = len(oldNameservers) - deleteCount + addToDomainCount
len(oldNameservers) - len(deleteHostList) + len(addToDomainList)
)
self._delete_hosts_if_not_used(hostsToDelete=deleted_values) self._delete_hosts_if_not_used(hostsToDelete=deleted_values)
if successTotalNameservers < 2: if successTotalNameservers < 2:
try: try:

View file

@ -993,8 +993,7 @@ class TestRegistrantNameservers(MockEppLib):
update_domain_with_created = commands.UpdateDomain( update_domain_with_created = commands.UpdateDomain(
name=self.domain.name, name=self.domain.name,
add=[ add=[
common.HostObjSet([created_host1.name]), common.HostObjSet([created_host1.name, created_host2.name]),
common.HostObjSet([created_host2.name]),
], ],
rem=[], rem=[],
) )
@ -1125,8 +1124,12 @@ class TestRegistrantNameservers(MockEppLib):
name=self.domainWithThreeNS.name, name=self.domainWithThreeNS.name,
add=[], add=[],
rem=[ rem=[
common.HostObjSet(hosts=["ns1.my-nameserver-2.com"]), common.HostObjSet(
common.HostObjSet(hosts=["ns1.cats-are-superior3.com"]), hosts=[
"ns1.my-nameserver-2.com",
"ns1.cats-are-superior3.com",
]
),
], ],
nsset=None, nsset=None,
keyset=None, keyset=None,
@ -1178,12 +1181,20 @@ class TestRegistrantNameservers(MockEppLib):
commands.UpdateDomain( commands.UpdateDomain(
name=self.domainWithThreeNS.name, name=self.domainWithThreeNS.name,
add=[ add=[
common.HostObjSet(hosts=["ns1.cats-are-superior1.com"]), common.HostObjSet(
common.HostObjSet(hosts=["ns1.cats-are-superior2.com"]), hosts=[
"ns1.cats-are-superior1.com",
"ns1.cats-are-superior2.com",
]
),
], ],
rem=[ rem=[
common.HostObjSet(hosts=["ns1.my-nameserver-2.com"]), common.HostObjSet(
common.HostObjSet(hosts=["ns1.cats-are-superior3.com"]), hosts=[
"ns1.my-nameserver-2.com",
"ns1.cats-are-superior3.com",
]
),
], ],
nsset=None, nsset=None,
keyset=None, keyset=None,