Skip to content

Commit be1e0b9

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "pull out put_allocation call from _heal_*"
2 parents 42df3ea + e286660 commit be1e0b9

File tree

3 files changed

+51
-57
lines changed

3 files changed

+51
-57
lines changed

nova/cmd/manage.py

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,59 +1658,29 @@ def _get_compute_node_uuid(ctxt, instance, node_cache):
16581658
node_cache[instance.node] = node_uuid
16591659
return node_uuid
16601660

1661-
def _heal_missing_alloc(
1662-
self, ctxt, instance, node_cache, dry_run, output, placement):
1663-
1661+
def _heal_missing_alloc(self, ctxt, instance, node_cache):
16641662
node_uuid = self._get_compute_node_uuid(
16651663
ctxt, instance, node_cache)
16661664

16671665
# Now get the resource allocations for the instance based
16681666
# on its embedded flavor.
16691667
resources = scheduler_utils.resources_from_flavor(
16701668
instance, instance.flavor)
1671-
if dry_run:
1672-
output(_('[dry-run] Create allocations for instance %(instance)s '
1673-
'on provider %(node_uuid)s: %(resources)s') %
1674-
{'instance': instance.uuid, 'node_uuid': node_uuid,
1675-
'resources': resources})
1676-
else:
1677-
payload = {
1678-
'allocations': {
1679-
node_uuid: {'resources': resources},
1680-
},
1681-
'project_id': instance.project_id,
1682-
'user_id': instance.user_id,
1683-
'consumer_generation': None
1684-
}
1685-
resp = placement.put_allocations(ctxt, instance.uuid, payload)
1686-
if resp:
1687-
output(_('Successfully created allocations for '
1688-
'instance %(instance)s against resource '
1689-
'provider %(provider)s.') %
1690-
{'instance': instance.uuid, 'provider': node_uuid})
1691-
return True
1692-
else:
1693-
raise exception.AllocationCreateFailed(
1694-
instance=instance.uuid, provider=node_uuid)
1695-
1696-
def _heal_missing_project_and_user_id(
1697-
self, ctxt, allocations, instance, dry_run, output, placement):
16981669

1670+
payload = {
1671+
'allocations': {
1672+
node_uuid: {'resources': resources},
1673+
},
1674+
'project_id': instance.project_id,
1675+
'user_id': instance.user_id,
1676+
'consumer_generation': None
1677+
}
1678+
return payload
1679+
1680+
def _heal_missing_project_and_user_id(self, allocations, instance):
16991681
allocations['project_id'] = instance.project_id
17001682
allocations['user_id'] = instance.user_id
1701-
if dry_run:
1702-
output(_('[dry-run] Update allocations for instance '
1703-
'%(instance)s: %(allocations)s') %
1704-
{'instance': instance.uuid, 'allocations': allocations})
1705-
else:
1706-
resp = placement.put_allocations(ctxt, instance.uuid, allocations)
1707-
if resp:
1708-
output(_('Successfully updated allocations for '
1709-
'instance %s.') % instance.uuid)
1710-
return True
1711-
else:
1712-
raise exception.AllocationUpdateFailed(
1713-
consumer_uuid=instance.uuid, error=resp.text)
1683+
return allocations
17141684

17151685
def _heal_allocations_for_instance(self, ctxt, instance, node_cache,
17161686
output, placement, dry_run):
@@ -1756,14 +1726,15 @@ def _heal_allocations_for_instance(self, ctxt, instance, node_cache,
17561726
output(_("Allocation retrieval failed: %s") % e)
17571727
allocations = None
17581728

1729+
need_healing = False
17591730
# get_allocations_for_consumer uses safe_connect which will
17601731
# return None if we can't communicate with Placement, and the
17611732
# response can have an empty {'allocations': {}} response if
17621733
# there are no allocations for the instance so handle both
17631734
if not allocations or not allocations.get('allocations'):
17641735
# This instance doesn't have allocations
1765-
return self._heal_missing_alloc(
1766-
ctxt, instance, node_cache, dry_run, output, placement)
1736+
need_healing = 'Create'
1737+
allocations = self._heal_missing_alloc(ctxt, instance, node_cache)
17671738

17681739
if (allocations.get('project_id') != instance.project_id or
17691740
allocations.get('user_id') != instance.user_id):
@@ -1772,12 +1743,33 @@ def _heal_allocations_for_instance(self, ctxt, instance, node_cache,
17721743
# and re-put them. We don't use put_allocations here
17731744
# because we don't want to mess up shared or nested
17741745
# provider allocations.
1775-
return self._heal_missing_project_and_user_id(
1776-
ctxt, allocations, instance, dry_run, output, placement)
1777-
1778-
output(_('Instance %s already has allocations with '
1779-
'matching consumer project/user.') % instance.uuid)
1780-
return
1746+
need_healing = 'Update'
1747+
allocations = self._heal_missing_project_and_user_id(
1748+
allocations, instance)
1749+
1750+
if need_healing:
1751+
if dry_run:
1752+
output(_('[dry-run] %(operation)s allocations for instance '
1753+
'%(instance)s: %(allocations)s') %
1754+
{'operation': need_healing,
1755+
'instance': instance.uuid,
1756+
'allocations': allocations})
1757+
else:
1758+
resp = placement.put_allocations(ctxt, instance.uuid,
1759+
allocations)
1760+
if resp:
1761+
output(_('Successfully %(operation)sd allocations for '
1762+
'instance %(instance)s.') %
1763+
{'operation': need_healing.lower(),
1764+
'instance': instance.uuid})
1765+
return True
1766+
else:
1767+
raise exception.AllocationUpdateFailed(
1768+
consumer_uuid=instance.uuid, error='')
1769+
else:
1770+
output(_('Instance %s already has allocations with '
1771+
'matching consumer project/user.') % instance.uuid)
1772+
return
17811773

17821774
def _heal_instances_in_cell(self, ctxt, max_count, unlimited, output,
17831775
placement, dry_run, instance_uuid):

nova/tests/functional/test_nova_manage.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -652,13 +652,14 @@ def test_heal_allocations_update_sentinel_consumer(self):
652652
self.assertEqual(4, result, self.output.getvalue())
653653
output = self.output.getvalue()
654654
self.assertIn('Processed 0 instances.', output)
655-
self.assertIn('[dry-run] Update allocations for instance %s' %
656-
server['id'], output)
655+
self.assertIn('[dry-run] Update allocations for instance %s'
656+
% server['id'], output)
657657
# Now run heal_allocations which should update the consumer info.
658658
result = self.cli.heal_allocations(verbose=True)
659659
self.assertEqual(0, result, self.output.getvalue())
660660
output = self.output.getvalue()
661-
self.assertIn('Successfully updated allocations for instance', output)
661+
self.assertIn(
662+
'Successfully updated allocations for', output)
662663
self.assertIn('Processed 1 instances.', output)
663664
# Now assert that the consumer was actually updated.
664665
allocations = self.placement_api.get(
@@ -676,8 +677,9 @@ def test_heal_allocations_dry_run(self):
676677
self.assertEqual(4, result, self.output.getvalue())
677678
output = self.output.getvalue()
678679
self.assertIn('Processed 0 instances.', output)
679-
self.assertIn('[dry-run] Create allocations for instance %s on '
680-
'provider %s' % (server['id'], rp_uuid), output)
680+
self.assertIn('[dry-run] Create allocations for instance '
681+
'%s' % server['id'], output)
682+
self.assertIn(rp_uuid, output)
681683

682684
def test_heal_allocations_specific_instance(self):
683685
"""Tests the case that a specific instance is processed and only that

nova/tests/unit/test_nova_manage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2478,7 +2478,7 @@ def test_heal_allocations_put_allocations_fails(
24782478
mock_get_compute_node, mock_get_allocs, mock_get_instances,
24792479
mock_get_all_cells):
24802480
self.assertEqual(3, self.cli.heal_allocations())
2481-
self.assertIn('Failed to create allocations for instance',
2481+
self.assertIn('Failed to update allocations for consumer',
24822482
self.output.getvalue())
24832483
instance = mock_get_instances.return_value[0]
24842484
mock_res_from_flavor.assert_called_once_with(

0 commit comments

Comments
 (0)