Moved root history out of instance details and into standalone mgmt call. PEP8 fixes.

This commit is contained in:
Ed Cranford 2012-05-10 11:42:24 -05:00
parent c4e617f6e7
commit b8ea7d1a93
12 changed files with 99 additions and 51 deletions

View File

@ -32,7 +32,6 @@ LOG = logging.getLogger('reddwarf.db.sqlalchemy.session')
def configure_db(options, models_mapper=None):
from reddwarf.instance import models
configure_sqlalchemy_log(options)
global _ENGINE
if not _ENGINE:
@ -40,7 +39,18 @@ def configure_db(options, models_mapper=None):
if models_mapper:
models_mapper.map(_ENGINE)
else:
mappers.map(_ENGINE, models.persisted_models())
from reddwarf.instance import models as base_models
from reddwarf.extensions.mysql import models as mysql_models
model_modules = [
base_models,
mysql_models,
]
models = {}
for module in model_modules:
models.update(module.persisted_models())
mappers.map(_ENGINE, models)
def configure_sqlalchemy_log(options):

View File

@ -50,6 +50,9 @@ class Mgmt(extensions.ExtensionsDescriptor):
resource = extensions.ResourceExtension('{tenant_id}/mgmt/instances',
service.MgmtInstanceController(),
deserializer=wsgi.RequestDeserializer(),
serializer=serializer)
serializer=serializer,
member_actions={'root': 'GET'},
)
resources.append(resource)
return resources

View File

@ -22,6 +22,7 @@ from reddwarf.common import exception
from reddwarf.common import wsgi
from reddwarf.instance import models as instance_models
from reddwarf.extensions.mgmt import views
from reddwarf.extensions.mysql import models as mysql_models
from reddwarf.instance.service import InstanceController
LOG = logging.getLogger(__name__)
@ -56,3 +57,19 @@ class MgmtInstanceController(InstanceController):
return wsgi.Result(str(e), 404)
return wsgi.Result(views.InstanceView(server,
add_addresses=self.add_addresses).data(), 200)
def root(self, req, tenant_id, id):
"""Return the date and time root was enabled on an instance,
if ever."""
LOG.info(_("req : '%s'\n\n") % req)
LOG.info(_("Showing root history for tenant '%s'") % tenant_id)
LOG.info(_("id : '%s'\n\n") % id)
context = req.environ[wsgi.CONTEXT_KEY]
try:
server = instance_models.Instance.load(context=context, id=id)
except exception.ReddwarfError, e:
LOG.error(e)
return wsgi.Result(str(e), 404)
reh = mysql_models.RootHistory.load(context=context, instance_id=id)
return wsgi.Result(views.RootHistoryView(reh.id, enabled=reh.created,
user_id=reh.user).data(), 200)

View File

@ -15,6 +15,12 @@
# License for the specific language governing permissions and limitations
# under the License.
from collections import defaultdict
def tree():
return defaultdict(tree)
def get_ip_address(addresses):
if addresses is not None and \
@ -62,3 +68,18 @@ class InstancesView(InstanceView):
def data_for_instance(self, instance):
return InstanceView(instance,
self.add_addresses).data()['instance']
class RootHistoryView(object):
def __init__(self, instance_id, enabled='Never', user_id='Nobody'):
self.instance_id = instance_id
self.enabled = enabled
self.user = user_id
def data(self):
res = tree()
res['root_history']['id'] = self.instance_id
res['root_history']['enabled'] = self.enabled
res['root_history']['user'] = self.user
return res

View File

@ -25,6 +25,7 @@ from reddwarf import db
from reddwarf.common import config
from reddwarf.common import exception
from reddwarf.common import utils
from reddwarf.instance import models as base_models
from reddwarf.guestagent.db import models as guest_models
from reddwarf.common.remote import create_guest_client
@ -33,6 +34,12 @@ CONFIG = config.Config
LOG = logging.getLogger(__name__)
def persisted_models():
return {
'root_enabled_history': RootHistory,
}
def load_and_verify(context, instance_id):
# Load InstanceServiceStatus to verify if its running
instance = base_models.Instance.load(context, instance_id)
@ -113,12 +120,41 @@ class Root(object):
root = create_guest_client(context, instance_id).enable_root()
root_user = guest_models.MySQLUser()
root_user.deserialize(root)
root_history = base_models.RootHistory.create(context,
instance_id,
user)
root_history = RootHistory.create(context, instance_id, user)
return root_user
class RootHistory(object):
_auto_generated_attrs = ['id']
_data_fields = ['instance_id', 'user', 'created']
_table_name = 'root_enabled_history'
def __init__(self, instance_id, user):
self.id = instance_id
self.user = user
self.created = utils.utcnow()
def save(self):
LOG.debug(_("Saving %s: %s") % (self.__class__.__name__,
self.__dict__))
return db.db_api.save(self)
@classmethod
def load(cls, context, instance_id):
history = db.db_api.find_by(cls, id=instance_id)
return history
@classmethod
def create(cls, context, instance_id, user):
history = cls.load(context, instance_id)
if history is not None:
return history
history = RootHistory(instance_id, user)
history.save()
return history
class Users(object):
@classmethod

View File

@ -710,7 +710,6 @@ def persisted_models():
'instance': DBInstance,
'service_image': ServiceImage,
'service_statuses': InstanceServiceStatus,
'root_enabled_history': RootHistory,
}
@ -788,37 +787,6 @@ class ServiceStatus(object):
return self._description
class RootHistory(ModelBase):
_auto_generated_attrs = ['id']
_data_fields = ['instance_id', 'user', 'created']
_table_name = 'root_enabled_history'
def __init__(self, instance_id, user):
self.id = instance_id
self.user = user
self.created = utils.utcnow()
def save(self):
LOG.debug(_("Saving %s: %s") % (self.__class__.__name__,
self.__dict__))
return db.db_api.save(self)
@classmethod
def load(cls, context, instance_id):
history = db.db_api.find_by(cls, id=instance_id)
return history
@classmethod
def create(cls, context, instance_id, user):
history = cls.load(context, instance_id)
if history is not None:
return history
history = RootHistory(instance_id, user)
history.save()
return history
class ServiceStatuses(object):
RUNNING = ServiceStatus(0x01, 'running', 'ACTIVE')
BLOCKED = ServiceStatus(0x02, 'blocked', 'BLOCKED')

View File

@ -206,10 +206,7 @@ class InstanceController(BaseController):
LOG.error(e)
return wsgi.Result(str(e), 404)
# TODO(cp16net): need to set the return code correctly
# Adding the root history, if it exists.
history = models.RootHistory.load(context=context, instance_id=id)
return wsgi.Result(views.InstanceDetailView(server,
roothistory=history,
add_addresses=self.add_addresses,
add_volumes=self.add_volumes).data(), 200)

View File

@ -58,21 +58,17 @@ class InstanceView(object):
class InstanceDetailView(InstanceView):
def __init__(self, instance, roothistory=None, add_addresses=False,
def __init__(self, instance, add_addresses=False,
add_volumes=False):
super(InstanceDetailView, self).__init__(instance,
add_addresses=add_addresses,
add_volumes=add_volumes)
self.roothistory = roothistory
def data(self):
result = super(InstanceDetailView, self).data()
result['instance']['created'] = self.instance.created
result['instance']['flavor'] = self.instance.flavor
result['instance']['updated'] = self.instance.updated
if self.roothistory:
result['instance']['root_enabled_at'] = self.roothistory.created
result['instance']['root_enabled_by'] = self.roothistory.user
return result

View File

@ -260,7 +260,7 @@ class TopicPublisher(Publisher):
options = {'durable': config.Config.get('rabbit_durable_queues',
False),
'auto_delete': False,
'exclusive': False }
'exclusive': False}
options.update(kwargs)
super(TopicPublisher, self).__init__(channel,
config.Config.get('control_exchange', 'reddwarf'),

View File

@ -42,7 +42,7 @@ class ContextTest(unittest.TestCase):
def test_creating_context(self):
tmp_ctx_dict = {'user': USER, 'tenant': TENANT, 'is_admin': True,
'show_deleted': True, 'read_only': True,
'auth_tok': AUTH_TOK }
'auth_tok': AUTH_TOK}
tmp_ctx = context.ReddwarfContext.from_dict(tmp_ctx_dict)
self.assertEqual(tmp_ctx.user, USER)
self.assertEqual(tmp_ctx.tenant, TENANT)

View File

@ -31,6 +31,6 @@ class ExceptionTest(unittest.TestCase):
def test_exception_with_message_args(self):
test_message = "test message %(one)s %(two)s"
test_args = {'one': 1, 'two': 2 }
test_args = {'one': 1, 'two': 2}
exc = exception.ReddwarfError(test_message, one=1, two=2)
self.assertEqual(str(exc), test_message % test_args)

View File

@ -93,7 +93,7 @@ class StringifyExcludeTest(unittest.TestCase):
def test_exclude_keys(self):
exclude_keys = ['one']
key_values = {'one': 1, 'two': 2 }
key_values = {'one': 1, 'two': 2}
new_keys = utils.exclude(key_values, *exclude_keys)
self.assertEqual(len(new_keys), 1)
self.assertEqual(new_keys, {'two': 2 })
self.assertEqual(new_keys, {'two': 2})