Get stacks from the DB by either name or id

This code was previously misleading, as it appeared to get stacks by id,
but actually got them by name. This patch adds a separate API, so
get_stack() gets the stack by id and get_stack_by_name() gets it by name.

Change-Id: I1cbbb3f9211661ad665e208d87298f177825f8ac
Signed-off-by: Zane Bitter <zbitter@redhat.com>
changes/81/181/1
Zane Bitter 10 years ago
parent 42ad6a7a65
commit 892b99bff4
  1. 8
      heat/db/api.py
  2. 21
      heat/db/sqlalchemy/api.py
  3. 20
      heat/engine/manager.py
  4. 6
      heat/engine/parser.py

@ -99,6 +99,10 @@ def stack_get(context, stack_id):
return IMPL.stack_get(context, stack_id)
def stack_get_by_name(context, stack_name):
return IMPL.stack_get_by_name(context, stack_name)
def stack_get_all(context):
return IMPL.stack_get_all(context)
@ -111,8 +115,8 @@ def stack_create(context, values):
return IMPL.stack_create(context, values)
def stack_delete(context, stack_name):
return IMPL.stack_delete(context, stack_name)
def stack_delete(context, stack_id):
return IMPL.stack_delete(context, stack_id)
def user_creds_create(context):

@ -124,11 +124,20 @@ def resource_get_all_by_stack(context, stack_id):
return results
def stack_get_by_name(context, stack_name):
result = model_query(context, models.Stack).\
filter_by(name=stack_name).first()
if (result is not None and context is not None and
result.username != context.username):
return None
return result
def stack_get(context, stack_id):
result = model_query(context, models.Stack).\
filter_by(name=stack_id).first()
if result is not None and context is not None and\
result.username != context.username:
filter_by(id=stack_id).first()
if (result is not None and context is not None and
result.username != context.username):
return None
return result
@ -140,7 +149,7 @@ def stack_get_all(context):
def stack_get_by_user(context):
results = model_query(context, models.Stack).\
filter_by(username=context.username).all()
filter_by(username=context.username).all()
return results
@ -151,8 +160,8 @@ def stack_create(context, values):
return stack_ref
def stack_delete(context, stack_name):
s = stack_get(context, stack_name)
def stack_delete(context, stack_id):
s = stack_get(context, stack_id)
if not s:
raise NotFound('Attempt to delete a stack with id: %s %s' %
(stack_name, 'that does not exist'))

@ -148,7 +148,7 @@ class EngineManager(manager.Manager):
self._authenticate(context)
res = {'stacks': []}
s = db_api.stack_get(context, stack_name)
s = db_api.stack_get_by_name(context, stack_name)
if s:
ps = parser.Stack(context, s.name,
s.raw_template.parsed_template.template,
@ -190,7 +190,7 @@ class EngineManager(manager.Manager):
self._authenticate(context)
if db_api.stack_get(None, stack_name):
if db_api.stack_get_by_name(None, stack_name):
return {'Error': 'Stack already exists with that name.'}
metadata_server = config.FLAGS.heat_metadata_server_url
@ -275,7 +275,7 @@ class EngineManager(manager.Manager):
arg3 -> Dict of http request parameters passed in from API side.
"""
self._authenticate(context)
s = db_api.stack_get(None, stack_name)
s = db_api.stack_get_by_name(None, stack_name)
if s:
return s.raw_template.template
return None
@ -290,7 +290,7 @@ class EngineManager(manager.Manager):
self._authenticate(context)
st = db_api.stack_get(context, stack_name)
st = db_api.stack_get_by_name(context, stack_name)
if not st:
return {'Error': 'No stack by that name'}
@ -327,7 +327,7 @@ class EngineManager(manager.Manager):
self._authenticate(context)
if stack_name is not None:
st = db_api.stack_get(context, stack_name)
st = db_api.stack_get_by_name(context, stack_name)
if not st:
return {'Error': 'No stack by that name'}
@ -343,7 +343,7 @@ class EngineManager(manager.Manager):
stack_name = event['stack']
resource_name = event['resource']
stack = db_api.stack_get(context, stack_name)
stack = db_api.stack_get_by_name(context, stack_name)
resource = db_api.resource_get_by_name_and_stack(context,
resource_name,
stack.id)
@ -381,7 +381,7 @@ class EngineManager(manager.Manager):
"""
Return the resource IDs of the given stack.
"""
stack = db_api.stack_get(context, stack_name)
stack = db_api.stack_get_by_name(context, stack_name)
if stack:
return [res.name for res in stack]
else:
@ -392,7 +392,7 @@ class EngineManager(manager.Manager):
Get the metadata for the given resource.
"""
s = db_api.stack_get(context, stack_name)
s = db_api.stack_get_by_name(context, stack_name)
if not s:
return ['stack', None]
@ -407,7 +407,7 @@ class EngineManager(manager.Manager):
"""
Update the metadata for the given resource.
"""
s = db_api.stack_get(context, stack_name)
s = db_api.stack_get_by_name(context, stack_name)
if not s:
return ['stack', None]
pt_id = s.raw_template.parsed_template.id
@ -460,7 +460,7 @@ class EngineManager(manager.Manager):
logger.info('no action for new state %s',
new_state)
else:
s = db_api.stack_get(None, wr.stack_name)
s = db_api.stack_get_by_name(None, wr.stack_name)
if s:
ps = parser.Stack(context, s.name,
s.raw_template.parsed_template.template,

@ -153,7 +153,7 @@ class Stack(object):
created, so commands like describe will work.
'''
if self.parsed_template_id == 0:
stack = db_api.stack_get(self.context, self.name)
stack = db_api.stack_get(self.context, self.id)
if stack:
self.parsed_template_id = stack.raw_template.parsed_template.id
else:
@ -242,7 +242,7 @@ class Stack(object):
'Failed to delete ' + ', '.join(failures))
else:
self.state_set(self.DELETE_COMPLETE, 'Deleted successfully')
db_api.stack_delete(self.context, self.name)
db_api.stack_delete(self.context, self.id)
def get_outputs(self):
outputs = self.resolve_runtime_data(self.outputs)
@ -262,7 +262,7 @@ class Stack(object):
'''
if self.parsed_template_id == 0:
stack = db_api.stack_get(self.context, self.name)
stack = db_api.stack_get(self.context, self.id)
if stack:
self.parsed_template_id = stack.raw_template.parsed_template.id

Loading…
Cancel
Save