]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Returns text error when instance validation fails
authorJianing YANG <jianingy@unitedstack.com>
Thu, 25 Jul 2013 12:47:31 +0000 (20:47 +0800)
committerJianing YANG <jianingy@unitedstack.com>
Tue, 6 Aug 2013 12:45:29 +0000 (20:45 +0800)
Since heat now is able to return parsable errors by middleware, we
don't need to return jsonfied error strings here.

Fixes Bug 1204582

Change-Id: Id3d68b9996ad67df0e25c6db5754f73bfa1e8830

heat/common/exception.py
heat/engine/resources/instance.py
heat/tests/test_validate.py

index eb236a5a38e462d28ca9edc5d5449bc3381e1b26..9d61ef45459a1796dba25ec0ba7fa0eb718a4bbe 100644 (file)
@@ -278,3 +278,11 @@ class ResourceFailure(OpenstackException):
 
 class NotSupported(OpenstackException):
     message = _("%(feature)s is not supported.")
+
+
+class ResourcePropertyConflict(OpenstackException):
+    message = _('Cannot define the following properties at the same time: %s.')
+
+    def __init__(self, *args):
+        self.message = self.message % ", ".join(args)
+        super(ResourcePropertyConflict, self).__init__()
index b8a2c980bd461fc77556448667f536640626713b..4eab8b7eb4490853fd78ae14d2c33135f744e776 100644 (file)
@@ -505,26 +505,18 @@ class Instance(resource.Resource):
         if key_name:
             keypairs = self.nova().keypairs.list()
             if not any(k.name == key_name for k in keypairs):
-                return {'Error':
-                        'Provided KeyName is not registered with nova'}
+                raise exception.UserKeyPairMissing(key_name=key_name)
 
         # check validity of security groups vs. network interfaces
         security_groups = self._get_security_groups()
         if security_groups and self.properties.get('NetworkInterfaces'):
-            return {'Error':
-                    'Cannot define both SecurityGroups/SecurityGroupIds and '
-                    'NetworkInterfaces properties.'}
+            raise exception.ResourcePropertyConflict(
+                'SecurityGroups/SecurityGroupIds',
+                'NetworkInterfaces')
 
         # make sure the image exists.
         image_identifier = self.properties['ImageId']
-        try:
-            self._get_image_id(image_identifier)
-        except exception.ImageNotFound:
-            return {'Error': 'Image %s was not found in glance' %
-                    image_identifier}
-        except exception.NoUniqueImageFound:
-            return {'Error': 'Multiple images were found with name %s' %
-                    image_identifier}
+        self._get_image_id(image_identifier)
 
         return
 
index c815d043d6fa284e54893ea94fccf3991e97ef68..69fb7561a497374805723642d6c7e3187d69f82b 100644 (file)
@@ -397,6 +397,34 @@ test_unregistered_key = '''
     }
     '''
 
+test_template_image = '''
+{
+  "AWSTemplateFormatVersion" : "2010-09-09",
+  "Description" : "test.",
+  "Parameters" : {
+
+    "KeyName" : {
+''' + \
+    '"Description" : "Name of an existing EC2' + \
+    'KeyPair to enable SSH access to the instances",' + \
+    '''
+          "Type" : "String"
+        }
+      },
+
+      "Resources" : {
+        "Instance": {
+          "Type": "AWS::EC2::Instance",
+          "Properties": {
+            "ImageId": "image_name",
+            "InstanceType": "m1.large",
+            "KeyName": { "Ref" : "KeyName" }
+          }
+        }
+      }
+    }
+    '''
+
 test_template_invalid_secgroups = '''
 {
   "AWSTemplateFormatVersion" : "2010-09-09",
@@ -700,7 +728,55 @@ class validateTest(HeatTestCase):
         self.m.ReplayAll()
 
         resource = stack.resources['Instance']
-        self.assertNotEqual(resource.validate(), None)
+        self.assertRaises(exception.UserKeyPairMissing, resource.validate)
+
+    def test_unregistered_image(self):
+        t = template_format.parse(test_template_image)
+        template = parser.Template(t)
+
+        stack = parser.Stack(self.ctx, 'test_stack', template,
+                             environment.Environment({'KeyName': 'test'}))
+
+        self.m.StubOutWithMock(instances.Instance, 'nova')
+        instances.Instance.nova().AndReturn(self.fc)
+        instances.Instance.nova().AndReturn(self.fc)
+        self.m.ReplayAll()
+
+        resource = stack.resources['Instance']
+        self.assertRaises(exception.ImageNotFound, resource.validate)
+
+        self.m.VerifyAll()
+
+    def test_duplicated_image(self):
+        t = template_format.parse(test_template_image)
+        template = parser.Template(t)
+
+        stack = parser.Stack(self.ctx, 'test_stack', template,
+                             environment.Environment({'KeyName': 'test'}))
+
+        class image_type(object):
+
+            def __init__(self, id, name):
+                self.id = id
+                self.name = name
+
+        image_list = [image_type(id='768b5464-3df5-4abf-be33-63b60f8b99d0',
+                                 name='image_name'),
+                      image_type(id='a57384f5-690f-48e1-bf46-c4291e6c887e',
+                                 name='image_name')]
+
+        self.m.StubOutWithMock(self.fc.images, 'list')
+        self.fc.images.list().AndReturn(image_list)
+
+        self.m.StubOutWithMock(instances.Instance, 'nova')
+        instances.Instance.nova().AndReturn(self.fc)
+        instances.Instance.nova().AndReturn(self.fc)
+        self.m.ReplayAll()
+
+        resource = stack.resources['Instance']
+        self.assertRaises(exception.NoUniqueImageFound, resource.validate)
+
+        self.m.VerifyAll()
 
     def test_invalid_security_groups_with_nics(self):
         t = template_format.parse(test_template_invalid_secgroups)
@@ -713,7 +789,8 @@ class validateTest(HeatTestCase):
         self.m.ReplayAll()
 
         resource = stack.resources['Instance']
-        self.assertNotEqual(resource.validate(), None)
+        self.assertRaises(exception.ResourcePropertyConflict,
+                          resource.validate)
 
     def test_invalid_security_group_ids_with_nics(self):
         t = template_format.parse(test_template_invalid_secgroupids)
@@ -726,7 +803,8 @@ class validateTest(HeatTestCase):
         self.m.ReplayAll()
 
         resource = stack.resources['Instance']
-        self.assertNotEqual(resource.validate(), None)
+        self.assertRaises(exception.ResourcePropertyConflict,
+                          resource.validate)
 
     def test_client_exception_from_nova_client(self):
         t = template_format.parse(test_template_nova_client_exception)