update keypair's context

if keypair with generated name exist, it will re-generate name
instead of deleting existing one.

Change-Id: Iba092b246a850fdd2c5a29ab522a0968705fac7b
This commit is contained in:
zhangzhang
2016-10-31 04:45:36 -04:00
parent 2ea2b60413
commit 7cdc5e2cb8
2 changed files with 9 additions and 13 deletions

View File

@@ -13,8 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import novaclient.exceptions
from rally.common.i18n import _
from rally.common import logging
from rally import osclients
@@ -29,16 +27,15 @@ LOG = logging.getLogger(__name__)
class Keypair(context.Context):
def _generate_keypair(self, credential):
keypair_name = self.generate_random_name()
nova_client = osclients.Clients(credential).nova()
# NOTE(hughsaunders): If keypair exists, it should re-generate name.
# NOTE(hughsaunders): If keypair exists, it must be deleted as we can't
# retrieve the private key
try:
nova_client.keypairs.delete(keypair_name)
except novaclient.exceptions.NotFound:
pass
keypairs = nova_client.keypairs.list()
keypair_names = [keypair.name for keypair in keypairs]
while True:
keypair_name = self.generate_random_name()
if keypair_name not in keypair_names:
break
keypair = nova_client.keypairs.create(keypair_name)
return {"private": keypair.private_key,

View File

@@ -84,6 +84,7 @@ class KeyPairContextTestCase(test.TestCase):
mock_keypair.id = "key_id"
keypair_ctx = keypairs.Keypair(self.ctx_without_keys)
keypair_ctx.generate_random_name = mock.Mock()
key = keypair_ctx._generate_keypair("credential")
self.assertEqual({
@@ -94,8 +95,6 @@ class KeyPairContextTestCase(test.TestCase):
}, key)
mock_clients.assert_has_calls([
mock.call().nova().keypairs.delete(
keypair_ctx.generate_random_name.return_value),
mock.call().nova().keypairs.create(
keypair_ctx.generate_random_name.return_value)
keypair_ctx.generate_random_name.return_value),
])