This makes the keypair object load first from the API database, falling back to the main database as necessary. Creates happen in the API database only now. Since the Instance object uses this object to patch up its keypairs field on the fly, this adds a localonly parameter to the get method so that we don't end up with compute nodes calling back up to the API database in the lazy-load case. Only Instances that were created before the previous patch will be missing keypair data, and so we're guaranteed to have their keypairs in the main database (if at all). Related to blueprint cells-keypairs-api-db Change-Id: I700cf8633f694c933f17dd133fa6c84c2beac4c0
201 lines
6.8 KiB
Python
201 lines
6.8 KiB
Python
# Copyright 2013 IBM Corp.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
from oslo_db import exception as db_exc
|
|
from oslo_utils import versionutils
|
|
|
|
from nova import db
|
|
from nova.db.sqlalchemy import api as db_api
|
|
from nova.db.sqlalchemy import api_models
|
|
from nova import exception
|
|
from nova import objects
|
|
from nova.objects import base
|
|
from nova.objects import fields
|
|
|
|
KEYPAIR_TYPE_SSH = 'ssh'
|
|
KEYPAIR_TYPE_X509 = 'x509'
|
|
|
|
|
|
@db_api.api_context_manager.reader
|
|
def _get_from_db(context, user_id, name=None):
|
|
query = context.session.query(api_models.KeyPair).\
|
|
filter(api_models.KeyPair.user_id == user_id)
|
|
if name is not None:
|
|
db_keypair = query.filter(api_models.KeyPair.name == name).\
|
|
first()
|
|
if not db_keypair:
|
|
raise exception.KeypairNotFound(user_id=user_id, name=name)
|
|
return db_keypair
|
|
else:
|
|
return query.all()
|
|
|
|
|
|
@db_api.api_context_manager.reader
|
|
def _get_count_from_db(context, user_id):
|
|
return context.session.query(api_models.KeyPair).\
|
|
filter(api_models.KeyPair.user_id == user_id).\
|
|
count()
|
|
|
|
|
|
@db_api.api_context_manager.writer
|
|
def _create_in_db(context, values):
|
|
kp = api_models.KeyPair()
|
|
kp.update(values)
|
|
try:
|
|
kp.save(context.session)
|
|
except db_exc.DBDuplicateEntry:
|
|
raise exception.KeyPairExists(key_name=values['name'])
|
|
return kp
|
|
|
|
|
|
@db_api.api_context_manager.writer
|
|
def _destroy_in_db(context, user_id, name):
|
|
result = context.session.query(api_models.KeyPair).\
|
|
filter_by(user_id=user_id).\
|
|
filter_by(name=name).\
|
|
delete()
|
|
if not result:
|
|
raise exception.KeypairNotFound(user_id=user_id, name=name)
|
|
|
|
|
|
# TODO(berrange): Remove NovaObjectDictCompat
|
|
@base.NovaObjectRegistry.register
|
|
class KeyPair(base.NovaPersistentObject, base.NovaObject,
|
|
base.NovaObjectDictCompat):
|
|
# Version 1.0: Initial version
|
|
# Version 1.1: String attributes updated to support unicode
|
|
# Version 1.2: Added keypair type
|
|
# Version 1.3: Name field is non-null
|
|
# Version 1.4: Add localonly flag to get_by_name()
|
|
VERSION = '1.4'
|
|
|
|
fields = {
|
|
'id': fields.IntegerField(),
|
|
'name': fields.StringField(nullable=False),
|
|
'user_id': fields.StringField(nullable=True),
|
|
'fingerprint': fields.StringField(nullable=True),
|
|
'public_key': fields.StringField(nullable=True),
|
|
'type': fields.StringField(nullable=False),
|
|
}
|
|
|
|
def obj_make_compatible(self, primitive, target_version):
|
|
super(KeyPair, self).obj_make_compatible(primitive, target_version)
|
|
target_version = versionutils.convert_version_to_tuple(target_version)
|
|
if target_version < (1, 2) and 'type' in primitive:
|
|
del primitive['type']
|
|
|
|
@staticmethod
|
|
def _from_db_object(context, keypair, db_keypair):
|
|
ignore = {'deleted': False,
|
|
'deleted_at': None}
|
|
for key in keypair.fields:
|
|
if key in ignore and not hasattr(db_keypair, key):
|
|
keypair[key] = ignore[key]
|
|
else:
|
|
keypair[key] = db_keypair[key]
|
|
keypair._context = context
|
|
keypair.obj_reset_changes()
|
|
return keypair
|
|
|
|
@staticmethod
|
|
def _get_from_db(context, user_id, name):
|
|
return _get_from_db(context, user_id, name=name)
|
|
|
|
@staticmethod
|
|
def _destroy_in_db(context, user_id, name):
|
|
return _destroy_in_db(context, user_id, name)
|
|
|
|
@staticmethod
|
|
def _create_in_db(context, values):
|
|
return _create_in_db(context, values)
|
|
|
|
@base.remotable_classmethod
|
|
def get_by_name(cls, context, user_id, name,
|
|
localonly=False):
|
|
db_keypair = None
|
|
if not localonly:
|
|
try:
|
|
db_keypair = cls._get_from_db(context, user_id, name)
|
|
except exception.KeypairNotFound:
|
|
pass
|
|
if db_keypair is None:
|
|
db_keypair = db.key_pair_get(context, user_id, name)
|
|
return cls._from_db_object(context, cls(), db_keypair)
|
|
|
|
@base.remotable_classmethod
|
|
def destroy_by_name(cls, context, user_id, name):
|
|
try:
|
|
cls._destroy_in_db(context, user_id, name)
|
|
except exception.KeypairNotFound:
|
|
db.key_pair_destroy(context, user_id, name)
|
|
|
|
@base.remotable
|
|
def create(self):
|
|
if self.obj_attr_is_set('id'):
|
|
raise exception.ObjectActionError(action='create',
|
|
reason='already created')
|
|
|
|
# NOTE(danms): Check to see if it exists in the old DB before
|
|
# letting them create in the API DB, since we won't get protection
|
|
# from the UC.
|
|
try:
|
|
db.key_pair_get(self._context, self.user_id, self.name)
|
|
raise exception.KeyPairExists(key_name=self.name)
|
|
except exception.KeypairNotFound:
|
|
pass
|
|
|
|
updates = self.obj_get_changes()
|
|
db_keypair = self._create_in_db(self._context, updates)
|
|
self._from_db_object(self._context, self, db_keypair)
|
|
|
|
@base.remotable
|
|
def destroy(self):
|
|
try:
|
|
self._destroy_in_db(self._context, self.user_id, self.name)
|
|
except exception.KeypairNotFound:
|
|
db.key_pair_destroy(self._context, self.user_id, self.name)
|
|
|
|
|
|
@base.NovaObjectRegistry.register
|
|
class KeyPairList(base.ObjectListBase, base.NovaObject):
|
|
# Version 1.0: Initial version
|
|
# KeyPair <= version 1.1
|
|
# Version 1.1: KeyPair <= version 1.2
|
|
# Version 1.2: KeyPair <= version 1.3
|
|
VERSION = '1.2'
|
|
|
|
fields = {
|
|
'objects': fields.ListOfObjectsField('KeyPair'),
|
|
}
|
|
|
|
@staticmethod
|
|
def _get_from_db(context, user_id):
|
|
return _get_from_db(context, user_id)
|
|
|
|
@staticmethod
|
|
def _get_count_from_db(context, user_id):
|
|
return _get_count_from_db(context, user_id)
|
|
|
|
@base.remotable_classmethod
|
|
def get_by_user(cls, context, user_id):
|
|
api_db_keypairs = cls._get_from_db(context, user_id)
|
|
main_db_keypairs = db.key_pair_get_all_by_user(context, user_id)
|
|
return base.obj_make_list(context, cls(context), objects.KeyPair,
|
|
api_db_keypairs + main_db_keypairs)
|
|
|
|
@base.remotable_classmethod
|
|
def get_count_by_user(cls, context, user_id):
|
|
return (cls._get_count_from_db(context, user_id) +
|
|
db.key_pair_count_by_user(context, user_id))
|