mirror of
https://github.com/ansible/awx.git
synced 2026-05-19 23:07:42 -02:30
defensive code for getting instance added, also simplified nested if
statements, rewrote some comments add a logger warning that the instance is being grabbed by the hostname and not the UUID
This commit is contained in:
@@ -129,24 +129,25 @@ class InstanceManager(models.Manager):
|
|||||||
other_inst.save(update_fields=['ip_address'])
|
other_inst.save(update_fields=['ip_address'])
|
||||||
logger.warning("IP address {0} conflict detected, ip address unset for host {1}.".format(ip_address, other_hostname))
|
logger.warning("IP address {0} conflict detected, ip address unset for host {1}.".format(ip_address, other_hostname))
|
||||||
|
|
||||||
# get the instance based on the hostname
|
# Return existing instance that matches hostname or UUID (default to UUID)
|
||||||
instance = self.filter(hostname=hostname)
|
if uuid is not None and uuid != UUID_DEFAULT and self.filter(uuid=uuid).exists():
|
||||||
|
instance = self.filter(uuid=uuid)
|
||||||
# Check or update the existing uuid
|
else:
|
||||||
if uuid is not None and uuid != UUID_DEFAULT:
|
# if instance was not retrieved by uuid and hostname was, use the hostname
|
||||||
if self.filter(uuid=uuid).exists():
|
instance = self.filter(hostname=hostname)
|
||||||
instance = self.filter(uuid=uuid)
|
|
||||||
|
|
||||||
# Return existing instance
|
# Return existing instance
|
||||||
if instance.exists():
|
if instance.exists():
|
||||||
instance = instance.get()
|
instance = instance.first() # in the unusual occasion that there is more than one, only get one
|
||||||
update_fields = []
|
update_fields = []
|
||||||
|
# if instance was retrieved by uuid and hostname has changed, update hostname
|
||||||
if instance.hostname != hostname:
|
if instance.hostname != hostname:
|
||||||
|
logger.warning("passed in hostname {0} is different from the original hostname {1}, updating to {0}".format(hostname, instance.hostname))
|
||||||
instance.hostname = hostname
|
instance.hostname = hostname
|
||||||
update_fields.append('hostname')
|
update_fields.append('hostname')
|
||||||
|
# if any other fields are to be updated
|
||||||
if instance.ip_address != ip_address:
|
if instance.ip_address != ip_address:
|
||||||
instance.ip_address = ip_address
|
instance.ip_address = ip_address
|
||||||
update_fields.append('ip_address')
|
|
||||||
if instance.node_type != node_type:
|
if instance.node_type != node_type:
|
||||||
instance.node_type = node_type
|
instance.node_type = node_type
|
||||||
update_fields.append('node_type')
|
update_fields.append('node_type')
|
||||||
|
|||||||
Reference in New Issue
Block a user