From 6ceec4e3a48059ea8b281c1e125811a7803fa2fa Mon Sep 17 00:00:00 2001 From: Andrew Lazarev Date: Tue, 2 Jun 2015 12:13:54 -0700 Subject: [PATCH] Switched from all stacks polling to filtered list There is no need to get all stacks to get a specific stack. Change-Id: I67142204308ec61af4bcc9bee8f607e1fcd419c5 Related-Bug: #1414472 Closes-Bug: #1461212 --- sahara/service/validations/base.py | 9 ++++----- sahara/tests/unit/service/validation/utils.py | 7 +++++-- sahara/utils/openstack/heat.py | 12 +++++++----- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/sahara/service/validations/base.py b/sahara/service/validations/base.py index 6efdbd0f..a006eb38 100644 --- a/sahara/service/validations/base.py +++ b/sahara/service/validations/base.py @@ -255,11 +255,10 @@ def check_cluster_unique_name(name): def check_heat_stack_name(cluster_name): if CONF.infrastructure_engine == 'heat': - for stack in heat.client().stacks.list(): - if stack.stack_name == cluster_name: - raise ex.NameAlreadyExistsException( - _("Cluster name '%s' is already used as Heat stack name") - % cluster_name) + if heat.get_stack(cluster_name, raise_on_missing=False): + raise ex.NameAlreadyExistsException( + _("Cluster name '%s' is already used as Heat stack name") + % cluster_name) def check_cluster_hostnames_lengths(cluster_name, node_groups): diff --git a/sahara/tests/unit/service/validation/utils.py b/sahara/tests/unit/service/validation/utils.py index b733d486..c594b7e5 100644 --- a/sahara/tests/unit/service/validation/utils.py +++ b/sahara/tests/unit/service/validation/utils.py @@ -76,8 +76,11 @@ def _get_availability_zone_list(detailed=True): return [FakeAvailabilityZone('nova')] -def _get_heat_stack_list(): - return [FakeStack('test-heat')] +def _get_heat_stack_list(**kwargs): + if (kwargs.get('filters') and + kwargs.get('filters').get('name') == 'test-heat'): + return [FakeStack('test-heat')] + return [] class FakeStack(object): diff --git a/sahara/utils/openstack/heat.py b/sahara/utils/openstack/heat.py index f67b7cc9..c190cc64 100644 --- a/sahara/utils/openstack/heat.py +++ b/sahara/utils/openstack/heat.py @@ -47,11 +47,13 @@ def client(): insecure=CONF.heat.api_insecure) -def get_stack(stack_name): - heat = client() - for stack in base.execute_with_retries(heat.stacks.list): - if stack.stack_name == stack_name: - return stack +def get_stack(stack_name, raise_on_missing=True): + for stack in base.execute_with_retries( + client().stacks.list, filters={'name': stack_name}): + return stack + + if not raise_on_missing: + return None raise ex.NotFoundException(_('Failed to find stack %(stack)s') % {'stack': stack_name})