From 32110839e68682d4f1d0c8478728126959f30fd9 Mon Sep 17 00:00:00 2001 From: ananta Date: Mon, 18 Aug 2014 15:44:15 +0530 Subject: [PATCH] Save files while updating a template. When updating a template object, any updates to the files must also be saved in the database. With this change, updates to files of a template are also saved in database. Change-Id: Ia5b84780158c47ffe807829ce2fc83f4ffbb6db3 --- heat/db/api.py | 4 +-- heat/db/sqlalchemy/api.py | 11 +++++--- heat/engine/template.py | 10 ++++---- heat/tests/test_sqlalchemy_api.py | 42 +++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/heat/db/api.py b/heat/db/api.py index 8372b4e2a1..ba39a96630 100644 --- a/heat/db/api.py +++ b/heat/db/api.py @@ -51,8 +51,8 @@ def raw_template_create(context, values): return IMPL.raw_template_create(context, values) -def raw_template_update_template(context, template_id, template): - return IMPL.raw_template_update_template(context, template_id, template) +def raw_template_update(context, template_id, values): + return IMPL.raw_template_update(context, template_id, values) def resource_data_get_all(resource, data=None): diff --git a/heat/db/sqlalchemy/api.py b/heat/db/sqlalchemy/api.py index 70e8515e9e..3c58cbe183 100644 --- a/heat/db/sqlalchemy/api.py +++ b/heat/db/sqlalchemy/api.py @@ -92,10 +92,15 @@ def raw_template_create(context, values): return raw_template_ref -def raw_template_update_template(context, template_id, template): +def raw_template_update(context, template_id, values): raw_template_ref = raw_template_get(context, template_id) - if raw_template_ref.template != template: - raw_template_ref.update_and_save({'template': template}) + # get only the changed values + values = dict((k, v) for k, v in values.items() + if getattr(raw_template_ref, k) != v) + + if values: + raw_template_ref.update_and_save(values) + return raw_template_ref diff --git a/heat/engine/template.py b/heat/engine/template.py index 818762a6df..b6146758ea 100644 --- a/heat/engine/template.py +++ b/heat/engine/template.py @@ -143,15 +143,15 @@ class Template(collections.Mapping): def store(self, context=None): '''Store the Template in the database and return its ID.''' + rt = { + 'template': self.t, + 'files': self.files + } if self.id is None: - rt = { - 'template': self.t, - 'files': self.files - } new_rt = db_api.raw_template_create(context, rt) self.id = new_rt.id else: - db_api.raw_template_update_template(context, self.id, self.t) + db_api.raw_template_update(context, self.id, rt) return self.id def __iter__(self): diff --git a/heat/tests/test_sqlalchemy_api.py b/heat/tests/test_sqlalchemy_api.py index f9d24036e0..20ce9916fd 100644 --- a/heat/tests/test_sqlalchemy_api.py +++ b/heat/tests/test_sqlalchemy_api.py @@ -1142,6 +1142,48 @@ class DBAPIRawTemplateTest(HeatTestCase): self.assertEqual(tp.id, template.id) self.assertEqual(tp.template, template.template) + def test_raw_template_update(self): + another_wp_template = ''' + { + "AWSTemplateFormatVersion" : "2010-09-09", + "Description" : "WordPress", + "Parameters" : { + "KeyName" : { + "Description" : "KeyName", + "Type" : "String", + "Default" : "test" + } + }, + "Resources" : { + "WebServer": { + "Type": "AWS::EC2::Instance", + "Properties": { + "ImageId" : "fedora-20.x86_64.qcow2", + "InstanceType" : "m1.xlarge", + "KeyName" : "test", + "UserData" : "wordpress" + } + } + } + } + ''' + new_t = template_format.parse(another_wp_template) + new_files = { + 'foo': 'bar', + 'myfile': 'file:///home/somefile' + } + new_values = { + 'template': new_t, + 'files': new_files + } + orig_tp = create_raw_template(self.ctx) + updated_tp = db_api.raw_template_update(self.ctx, + orig_tp.id, new_values) + + self.assertEqual(updated_tp.id, orig_tp.id) + self.assertEqual(updated_tp.template, new_t) + self.assertEqual(updated_tp.files, new_files) + class DBAPIUserCredsTest(HeatTestCase): def setUp(self):