def create_key_pair(self, context, user_id, key_name, key_type=keypair_obj.KEYPAIR_TYPE_SSH): """Create a new key pair.""" self._validate_new_key_pair(context, user_id, key_name, key_type) private_key, public_key, fingerprint = self._generate_key_pair( user_id, key_type) # Create the keypair reservations num_keypairs = self._check_num_keypairs_quota(context, 1) reserve_opts = {'keypairs': num_keypairs} reservations = self.quota.reserve(context, **reserve_opts) keypair = objects.KeyPair(context) keypair.user_id = user_id keypair.name = key_name keypair.type = key_type keypair.fingerprint = fingerprint keypair.public_key = public_key keypair.project_id = context.tenant keypair.create() # Commit keypairs reservations if reservations: self.quota.commit(context, reservations) return keypair, private_key
def create_key_pair(self, context, user_id, key_name, key_type=keypair_obj.KEYPAIR_TYPE_SSH): """Create a new key pair.""" LOG.debug('Going to create key pair') self._validate_new_key_pair(context, user_id, key_name, key_type) private_key, public_key, fingerprint = self._generate_key_pair( user_id, key_type) # Create the keypair reservations num_keypairs = self._check_num_keypairs_quota(context, 1) reserve_opts = {'keypairs': num_keypairs} reservations = self.quota.reserve(context, **reserve_opts) keypair = objects.KeyPair(context) keypair.user_id = user_id keypair.name = key_name keypair.type = key_type keypair.fingerprint = fingerprint keypair.public_key = public_key keypair.project_id = context.tenant try: keypair.create() except Exception: with excutils.save_and_reraise_exception(): self.quota.rollback(context, reservations) # Commit keypairs reservations if reservations: self.quota.commit(context, reservations) return keypair, private_key
def test_destroy(self): with mock.patch.object(self.dbapi, 'key_pair_destroy', autospec=True) as mock_keypair_destroy: mock_keypair_destroy.return_value = self.fake_keypair keypair = objects.KeyPair(self.context, **self.fake_keypair) keypair.destroy() mock_keypair_destroy.assert_called_once_with( self.context, self.fake_keypair['user_id'], self.fake_keypair['name'])
def create_key_pair(self, context, user_id, key_name, key_type=keypair_obj.KEYPAIR_TYPE_SSH): """Create a new key pair.""" self._validate_new_key_pair(context, user_id, key_name, key_type) private_key, public_key, fingerprint = self._generate_key_pair( user_id, key_type) keypair = objects.KeyPair(context) keypair.user_id = user_id keypair.name = key_name keypair.type = key_type keypair.fingerprint = fingerprint keypair.public_key = public_key keypair.create() return keypair, private_key
def import_key_pair(self, context, user_id, key_name, public_key, key_type=keypair_obj.KEYPAIR_TYPE_SSH): """Import a key pair using an existing public key.""" self._validate_new_key_pair(context, user_id, key_name, key_type) fingerprint = self._generate_fingerprint(public_key, key_type) keypair = objects.KeyPair(context) keypair.user_id = user_id keypair.name = key_name keypair.type = key_type keypair.fingerprint = fingerprint keypair.public_key = public_key keypair.create() return keypair
def test_create(self): with mock.patch.object(self.dbapi, 'key_pair_get', autospec=True) as mock_keypair_get: with mock.patch.object(self.dbapi, 'key_pair_create', autospec=True) as mock_keypair_create: mock_keypair_get.side_effect = exception.KeypairNotFound( user_id=self.fake_keypair['user_id'], name=self.fake_keypair['name']) mock_keypair_create.return_value = self.fake_keypair create_params = copy.copy(self.fake_keypair) create_params.pop('id') keypair = objects.KeyPair(self.context, **create_params) values = keypair.obj_get_changes() keypair.create() mock_keypair_create.assert_called_once_with( self.context, values) self.assertEqual(self.fake_keypair['name'], keypair.name) self.assertEqual(self.fake_keypair['user_id'], keypair.user_id)