Browse Source

pull out functions from _heal_allocations_for_instance

There are two separate healing is going on in
_heal_allocations_for_instance one that heals a missing allocation and
the other that heals the project_id and the user_id in the existing
allocation. This patch pull the separate steps out to separate
functions.

Related-Bug: #1819923

Change-Id: I4b85fc22d2e8f57f718cde90bf556384b169d635
tags/20.0.0.0rc1
Balazs Gibizer 5 months ago
parent
commit
e6fc316261
1 changed files with 57 additions and 45 deletions
  1. 57
    45
      nova/cmd/manage.py

+ 57
- 45
nova/cmd/manage.py View File

@@ -1658,6 +1658,59 @@ class PlacementCommands(object):
1658 1658
         node_cache[instance.node] = node_uuid
1659 1659
         return node_uuid
1660 1660
 
1661
+    def _heal_missing_alloc(
1662
+            self, ctxt, instance, node_cache, dry_run, output, placement):
1663
+
1664
+        node_uuid = self._get_compute_node_uuid(
1665
+            ctxt, instance, node_cache)
1666
+
1667
+        # Now get the resource allocations for the instance based
1668
+        # on its embedded flavor.
1669
+        resources = scheduler_utils.resources_from_flavor(
1670
+            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
+            if placement.put_allocations(
1678
+                    ctxt, node_uuid, instance.uuid, resources,
1679
+                    instance.project_id, instance.user_id,
1680
+                    consumer_generation=None):
1681
+                output(_('Successfully created allocations for '
1682
+                         'instance %(instance)s against resource '
1683
+                         'provider %(provider)s.') %
1684
+                       {'instance': instance.uuid, 'provider': node_uuid})
1685
+                return True
1686
+            else:
1687
+                raise exception.AllocationCreateFailed(
1688
+                    instance=instance.uuid, provider=node_uuid)
1689
+
1690
+    def _heal_missing_project_and_user_id(
1691
+            self, allocations, instance, dry_run, output, placement):
1692
+
1693
+        allocations['project_id'] = instance.project_id
1694
+        allocations['user_id'] = instance.user_id
1695
+        # We use CONSUMER_GENERATION_VERSION for PUT
1696
+        # /allocations/{consumer_id} to mirror the body structure from
1697
+        # get_allocs_for_consumer.
1698
+        if dry_run:
1699
+            output(_('[dry-run] Update allocations for instance '
1700
+                     '%(instance)s: %(allocations)s') %
1701
+                   {'instance': instance.uuid, 'allocations': allocations})
1702
+        else:
1703
+            resp = placement.put(
1704
+                '/allocations/%s' % instance.uuid,
1705
+                allocations, version=report.CONSUMER_GENERATION_VERSION)
1706
+            if resp:
1707
+                output(_('Successfully updated allocations for '
1708
+                         'instance %s.') % instance.uuid)
1709
+                return True
1710
+            else:
1711
+                raise exception.AllocationUpdateFailed(
1712
+                    consumer_uuid=instance.uuid, error=resp.text)
1713
+
1661 1714
     def _heal_allocations_for_instance(self, ctxt, instance, node_cache,
1662 1715
                                        output, placement, dry_run):
1663 1716
         """Checks the given instance to see if it needs allocation healing
@@ -1725,54 +1778,13 @@ class PlacementCommands(object):
1725 1778
             # and re-put them. We don't use put_allocations here
1726 1779
             # because we don't want to mess up shared or nested
1727 1780
             # provider allocations.
1728
-            allocations['project_id'] = instance.project_id
1729
-            allocations['user_id'] = instance.user_id
1730
-            # We use CONSUMER_GENERATION_VERSION for PUT
1731
-            # /allocations/{consumer_id} to mirror the body structure from
1732
-            # get_allocs_for_consumer.
1733
-            if dry_run:
1734
-                output(_('[dry-run] Update allocations for instance '
1735
-                         '%(instance)s: %(allocations)s') %
1736
-                       {'instance': instance.uuid, 'allocations': allocations})
1737
-            else:
1738
-                resp = placement.put(
1739
-                    '/allocations/%s' % instance.uuid,
1740
-                    allocations, version=report.CONSUMER_GENERATION_VERSION)
1741
-                if resp:
1742
-                    output(_('Successfully updated allocations for '
1743
-                             'instance %s.') % instance.uuid)
1744
-                    return True
1745
-                else:
1746
-                    raise exception.AllocationUpdateFailed(
1747
-                        consumer_uuid=instance.uuid, error=resp.text)
1781
+            return self._heal_missing_project_and_user_id(
1782
+                allocations, instance, dry_run, output, placement)
1748 1783
 
1749 1784
         # This instance doesn't have allocations so we need to find
1750 1785
         # its compute node resource provider.
1751
-        node_uuid = self._get_compute_node_uuid(
1752
-            ctxt, instance, node_cache)
1753
-
1754
-        # Now get the resource allocations for the instance based
1755
-        # on its embedded flavor.
1756
-        resources = scheduler_utils.resources_from_flavor(
1757
-            instance, instance.flavor)
1758
-        if dry_run:
1759
-            output(_('[dry-run] Create allocations for instance %(instance)s '
1760
-                     'on provider %(node_uuid)s: %(resources)s') %
1761
-                   {'instance': instance.uuid, 'node_uuid': node_uuid,
1762
-                    'resources': resources})
1763
-        else:
1764
-            if placement.put_allocations(
1765
-                    ctxt, node_uuid, instance.uuid, resources,
1766
-                    instance.project_id, instance.user_id,
1767
-                    consumer_generation=None):
1768
-                output(_('Successfully created allocations for '
1769
-                         'instance %(instance)s against resource '
1770
-                         'provider %(provider)s.') %
1771
-                       {'instance': instance.uuid, 'provider': node_uuid})
1772
-                return True
1773
-            else:
1774
-                raise exception.AllocationCreateFailed(
1775
-                    instance=instance.uuid, provider=node_uuid)
1786
+        return self._heal_missing_alloc(
1787
+            ctxt, instance, node_cache, dry_run, output, placement)
1776 1788
 
1777 1789
     def _heal_instances_in_cell(self, ctxt, max_count, unlimited, output,
1778 1790
                                 placement, dry_run, instance_uuid):

Loading…
Cancel
Save