domain state transition needs to change

This commit is contained in:
Alysia Broddrick 2023-08-16 08:34:56 -07:00
parent f38ed0d4df
commit 2963fd05d1
No known key found for this signature in database
GPG key ID: 03917052CD0F06B7
2 changed files with 50 additions and 24 deletions

View file

@ -223,6 +223,8 @@ class Domain(TimeStampedModel, DomainHelper):
while non-subordinate hosts MUST NOT. while non-subordinate hosts MUST NOT.
""" """
# TODO: call EPP to get this info instead of returning fake data. # TODO: call EPP to get this info instead of returning fake data.
#MISSING FROM DISPLAY
return [ return [
("ns1.example.com",), ("ns1.example.com",),
("ns2.example.com",), ("ns2.example.com",),
@ -232,6 +234,9 @@ class Domain(TimeStampedModel, DomainHelper):
@nameservers.setter # type: ignore @nameservers.setter # type: ignore
def nameservers(self, hosts: list[tuple[str]]): def nameservers(self, hosts: list[tuple[str]]):
# TODO: call EPP to set this info. # TODO: call EPP to set this info.
# if two nameservers change state to created, don't do it automatically
self.state=Domain.State.CREATED
pass pass
@Cache @Cache
@ -468,10 +473,12 @@ class Domain(TimeStampedModel, DomainHelper):
return registry.send(req, cleaned=True).res_data[0] return registry.send(req, cleaned=True).res_data[0]
except RegistryError as e: except RegistryError as e:
if already_tried_to_create: if already_tried_to_create:
logger.error("Already tried to create")
logger.error(e)
logger.error(e.code)
raise e raise e
if e.code == ErrorCode.OBJECT_DOES_NOT_EXIST: if e.code == ErrorCode.OBJECT_DOES_NOT_EXIST:
# avoid infinite loop # avoid infinite loop
already_tried_to_create = True already_tried_to_create = True
self._make_domain_in_registry() self._make_domain_in_registry()
else: else:
@ -479,33 +486,42 @@ class Domain(TimeStampedModel, DomainHelper):
logger.error(e.code) logger.error(e.code)
raise e raise e
def _make_domain_in_registry(self): def _make_domain_in_registry(self):
registrant = self._get_or_create_contact( logger.info("In make domain in registry ")
PublicContact.get_default_registrant() registrant = PublicContact.get_default_registrant()
) self._make_contact_in_registry(registrant)
logger.info("registrant is %s" % registrant)
#TODO-notes no chg item for registrant in the epplib should #TODO-notes no chg item for registrant in the epplib should
security_contact = self._get_or_create_contact( PublicContact.get_default_security())
security_contact = self._get_or_create_contact(self.get_default_security_contact())
req = commands.CreateDomain( req = commands.CreateDomain(
name=self.name, name=self.name,
registrant=registrant.id, registrant=registrant.registry_id,
auth_info=epp.DomainAuthInfo( auth_info=epp.DomainAuthInfo(
pw="2fooBAR123fooBaz" pw="2fooBAR123fooBaz"
), # not a password ), # not a password
) )
logger.info("_get_or_create_domain()-> about to send domain request") logger.info("_get_or_create_domain()-> about to send domain request")
logger.info(req)
try:
response=registry.send(req, cleaned=True) response=registry.send(req, cleaned=True)
except RegistryError as err:
if err.code!=ErrorCode.OBJECT_EXISTS:
raise err
logger.info("_get_or_create_domain()-> registry received create for "+self.name) logger.info("_get_or_create_domain()-> registry received create for "+self.name)
logger.info(response) logger.info(response)
# no error, so go ahead and update state # no error, so go ahead and update state
##
#make this a trainsition function
self.state = Domain.State.PENDING_CREATE self.state = Domain.State.PENDING_CREATE
self.save() self.save()
logger.info("update domain with secutity contact")
self._update_domain_with_contact(security_contact, rem=False) self._update_domain_with_contact(security_contact, rem=False)
def _make_contact_in_registry(self, contact: PublicContact): def _make_contact_in_registry(self, contact: PublicContact):
"""Create the contact in the registry, ignore duplicate contact errors""" """Create the contact in the registry, ignore duplicate contact errors"""
logger.info(contact)
logger.info(contact.registry_id)
create = commands.CreateContact( create = commands.CreateContact(
id=contact.registry_id, id=contact.registry_id,
postal_info=epp.PostalInfo( # type: ignore postal_info=epp.PostalInfo( # type: ignore
@ -541,25 +557,34 @@ class Domain(TimeStampedModel, DomainHelper):
types={DF.ADDR: "loc"}, types={DF.ADDR: "loc"},
) )
try: try:
logger.info("sending contact")
registry.send(create, cleaned=True) registry.send(create, cleaned=True)
return contact return contact
except RegistryError as err: except RegistryError as err:
#don't throw an error if it is just saying this is a duplicate contact #don't throw an error if it is just saying this is a duplicate contact
if err.code!=ErrorCode.OBJECT_EXISTS: if err.code!=ErrorCode.OBJECT_EXISTS:
raise err logger.error("Registry threw error for contact id %s contact type is %s, error code is\n %s full error is %s",contact.registry_id, contact.contact_type, err.code, err)
#TODO - Error handling here
else: else:
logger.warning("Registrar tried to create duplicate contact for id %s",contact.registry_id) logger.warning("Registrar tried to create duplicate contact for id %s",contact.registry_id)
def _get_or_create_contact(self, contact: PublicContact): def _get_or_create_contact(self, contact: PublicContact):
"""Try to fetch info about a contact. Create it if it does not exist.""" """Try to fetch info about a contact. Create it if it does not exist."""
while True:
try: try:
req = commands.InfoContact(id=contact.registry_id) req = commands.InfoContact(id=contact.registry_id)
return registry.send(req, cleaned=True).res_data[0] return registry.send(req, cleaned=True).res_data[0]
except RegistryError as e: except RegistryError as e:
if e.code == ErrorCode.OBJECT_DOES_NOT_EXIST: if e.code == ErrorCode.OBJECT_DOES_NOT_EXIST:
logger.info("_get_or_create_contact()-> contact doesn't exist so making it")
return self._make_contact_in_registry(contact=contact) return self._make_contact_in_registry(contact=contact)
else: else:
logger.error("Registry threw error for contact id %s contact type is %s, error code is\n %s full error is %s",contact.registry_id, contact.contact_type, err.code, err)
raise e raise e
def _update_or_create_host(self, host): def _update_or_create_host(self, host):
@ -595,8 +620,8 @@ class Domain(TimeStampedModel, DomainHelper):
# get contact info, if there are any # get contact info, if there are any
if ( if (
fetch_contacts # fetch_contacts and
and "_contacts" in cleaned "_contacts" in cleaned
and isinstance(cleaned["_contacts"], list) and isinstance(cleaned["_contacts"], list)
and len(cleaned["_contacts"]) and len(cleaned["_contacts"])
): ):
@ -633,8 +658,8 @@ class Domain(TimeStampedModel, DomainHelper):
# get nameserver info, if there are any # get nameserver info, if there are any
if ( if (
fetch_hosts # fetch_hosts and
and "_hosts" in cleaned "_hosts" in cleaned
and isinstance(cleaned["_hosts"], list) and isinstance(cleaned["_hosts"], list)
and len(cleaned["_hosts"]) and len(cleaned["_hosts"])
): ):
@ -661,6 +686,7 @@ class Domain(TimeStampedModel, DomainHelper):
) )
# replace the prior cache with new data # replace the prior cache with new data
logger.info("cache at the end of fetch is %s" % str(cache))
self._cache = cleaned self._cache = cleaned
except RegistryError as e: except RegistryError as e:

View file

@ -149,4 +149,4 @@ class PublicContact(TimeStampedModel):
) )
def __str__(self): def __str__(self):
return f"{self.name} <{self.email}>" return f"{self.name} <{self.email}> id: {self.registry_id}"