Add validation to KeyPair resource
Make sure a keypair with the name passed to the KeyPair resource doesn't already exist. Closes-Bug: #1278532 Change-Id: I466e035e207def85ded5b2978a7da8cfe5ffb6a8
This commit is contained in:
parent
8c4adee13f
commit
6124fdea65
@ -124,6 +124,18 @@ class KeyPair(resource.Resource):
|
||||
def FnGetRefId(self):
|
||||
return self.resource_id
|
||||
|
||||
def validate(self):
|
||||
super(KeyPair, self).validate()
|
||||
name = self.properties[self.NAME]
|
||||
try:
|
||||
nova_utils.get_keypair(self.nova(), name)
|
||||
except exception.UserKeyPairMissing:
|
||||
pass
|
||||
else:
|
||||
msg = _('Cannot create KeyPair resource with a name of "%s" (a '
|
||||
'keypair with that name already exists)') % name
|
||||
raise exception.StackValidationFailed(message=msg)
|
||||
|
||||
|
||||
class KeypairConstraint(object):
|
||||
|
||||
|
@ -14,12 +14,15 @@
|
||||
|
||||
import collections
|
||||
import copy
|
||||
import mox
|
||||
|
||||
from novaclient import exceptions as nova_exceptions
|
||||
|
||||
from heat.engine import clients
|
||||
from heat.common import exception
|
||||
from heat.engine import scheduler
|
||||
from heat.engine.resources import nova_keypair
|
||||
from heat.engine.resources import nova_utils
|
||||
from heat.tests.common import HeatTestCase
|
||||
from heat.tests.v1_1 import fakes
|
||||
from heat.tests import utils
|
||||
@ -149,6 +152,24 @@ class NovaKeyPairTest(HeatTestCase):
|
||||
self.assertEqual(tp_test.resource_id, created_key.name)
|
||||
self.m.VerifyAll()
|
||||
|
||||
def test_validate_okay(self):
|
||||
test_res = self._get_test_resource(self.kp_template)
|
||||
self.m.StubOutWithMock(nova_utils, 'get_keypair')
|
||||
nova_utils.get_keypair(mox.IgnoreArg(), 'key_pair').AndRaise(
|
||||
exception.UserKeyPairMissing(key_name='foo'))
|
||||
self.m.ReplayAll()
|
||||
self.assertIsNone(test_res.validate())
|
||||
|
||||
def test_validate_failure_key_exists(self):
|
||||
test_res = self._get_test_resource(self.kp_template)
|
||||
self.m.StubOutWithMock(nova_utils, 'get_keypair')
|
||||
nova_utils.get_keypair(mox.IgnoreArg(), 'key_pair').AndReturn('foo')
|
||||
self.m.ReplayAll()
|
||||
exc = self.assertRaises(exception.StackValidationFailed,
|
||||
test_res.validate)
|
||||
self.assertIn('Cannot create KeyPair resource with a name of '
|
||||
'"key_pair"', str(exc))
|
||||
|
||||
|
||||
class KeypairConstraintTest(HeatTestCase):
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user