Avoid lazy-load error when getting instance AZ

When [cinder]cross_az_attach=False (not the default) and doing
boot from volume, the API code validates the BDM by seeing if
the instance and the volume are in the same availability zone.
To get the AZ for the instance, the code is first trying to get
the instance.host value.

In Ocata we stopped creating the instance in the API and moved that
to conductor for cells v2. So the Instance object in this case now
is created in the _provision_instances method and stored in the
BuildRequest object. Since there is no host to set on the instance
yet and the Instance object wasn't populated from DB values, which
before would set the host field on the instance object to None by
default, trying to get instance.host will lazy-load the field and
it blows up with ObjectActionError.

The correct thing to do here is check if the host attribute is set
on the Instance object. There is clear intent to assume host is
not set in the instance since it was using instance.get('host'),
probably from way back in the days when the instance in this case
was a dict. So it's expecting to handle None, but we need to
modernize how that is checked.

Change-Id: I0dccb6a416dfe0eae4f7c52dfc28786a449b17bd
Closes-Bug: #1693600
This commit is contained in:
Matt Riedemann 2017-05-25 15:46:22 -04:00
parent 4b732b5b3e
commit 91bd79058a
2 changed files with 18 additions and 2 deletions

View File

@ -164,7 +164,7 @@ def get_availability_zones(context, get_only_available=False,
def get_instance_availability_zone(context, instance):
"""Return availability zone of specified instance."""
host = instance.get('host')
host = instance.host if 'host' in instance else None
if not host:
# Likely hasn't reached a viable compute node yet so give back the
# desired availability_zone in the instance record if the boot request

View File

@ -267,12 +267,28 @@ class AvailabilityZoneTestCases(test.TestCase):
az.get_instance_availability_zone(self.context, fake_inst))
def test_get_instance_availability_zone_no_host(self):
"""Test get availability zone from instance if host not set."""
"""Test get availability zone from instance if host is None."""
fake_inst = objects.Instance(host=None, availability_zone='inst-az')
result = az.get_instance_availability_zone(self.context, fake_inst)
self.assertEqual('inst-az', result)
def test_get_instance_availability_zone_no_host_set(self):
"""Test get availability zone from instance if host not set.
This is testing the case in the compute API where the Instance object
does not have the host attribute set because it's just the object that
goes into the BuildRequest, it wasn't actually pulled from the DB. The
instance in this case doesn't actually get inserted into the DB until
it reaches conductor. So the host attribute may not be set but we
expect availability_zone always will in the API because of
_validate_and_build_base_options setting a default value which goes
into the object.
"""
fake_inst = objects.Instance(availability_zone='inst-az')
result = az.get_instance_availability_zone(self.context, fake_inst)
self.assertEqual('inst-az', result)
def test_get_instance_availability_zone_no_host_no_az(self):
"""Test get availability zone if neither host nor az is set."""
fake_inst = objects.Instance(host=None, availability_zone=None)