From 5d8868a4d58122dce6fdc81fa0ed17ae7fc55672 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Mon, 4 Feb 2013 12:12:43 -0500 Subject: Refactor server password metadata to avoid direct db usage This refactors the set_password() method in the server_password API extension to merely format the metadata and return it, leaving the job of updating the database to the caller. In the API extension case, the update can be done immediately against the database as before. In the case of the metadata API handler and the xenapi virt driver making the call, conductor can be used to update the instance's system_metadata, thereby avoiding a direct database access. Related to blueprint no-db-compute Change-Id: I0563feaa97d768a96f950c148b1dbf51c356c4ac --- nova/api/metadata/password.py | 18 +++++++++++------- nova/api/openstack/compute/contrib/server_password.py | 5 ++++- 2 files changed, 15 insertions(+), 8 deletions(-) (limited to 'nova/api') diff --git a/nova/api/metadata/password.py b/nova/api/metadata/password.py index b2bb83b15..f3453e945 100644 --- a/nova/api/metadata/password.py +++ b/nova/api/metadata/password.py @@ -15,8 +15,9 @@ from webob import exc +from nova import conductor from nova import context -from nova import db +from nova import utils CHUNKS = 4 @@ -33,7 +34,7 @@ def extract_password(instance): return result or None -def set_password(context, instance_uuid, password): +def convert_password(context, password): """Stores password as system_metadata items. Password is stored with the keys 'password_0' -> 'password_3'. @@ -43,10 +44,7 @@ def set_password(context, instance_uuid, password): for i in xrange(CHUNKS): meta['password_%d' % i] = password[:CHUNK_LENGTH] password = password[CHUNK_LENGTH:] - db.instance_system_metadata_update(context, - instance_uuid, - meta, - False) + return meta def handle_password(req, meta_data): @@ -63,6 +61,12 @@ def handle_password(req, meta_data): if (req.content_length > MAX_SIZE or len(req.body) > MAX_SIZE): msg = _("Request is too large.") raise exc.HTTPBadRequest(explanation=msg) - set_password(ctxt, meta_data.uuid, req.body) + + conductor_api = conductor.API() + instance = conductor_api.instance_get_by_uuid(ctxt, meta_data.uuid) + sys_meta = utils.metadata_to_dict(instance['system_metadata']) + sys_meta.update(convert_password(ctxt, req.body)) + conductor_api.instance_update(ctxt, meta_data.uuid, + system_metadata=sys_meta) else: raise exc.HTTPBadRequest() diff --git a/nova/api/openstack/compute/contrib/server_password.py b/nova/api/openstack/compute/contrib/server_password.py index 0fd620fb8..9436d354f 100644 --- a/nova/api/openstack/compute/contrib/server_password.py +++ b/nova/api/openstack/compute/contrib/server_password.py @@ -24,6 +24,7 @@ from nova.api.openstack import extensions from nova.api.openstack import wsgi from nova.api.openstack import xmlutil from nova import compute +from nova import db from nova import exception @@ -62,7 +63,9 @@ class ServerPasswordController(object): context = req.environ['nova.context'] authorize(context) instance = self._get_instance(context, server_id) - password.set_password(context, instance['uuid'], None) + meta = password.convert_password(context, None) + db.instance_system_metadata_update(context, instance['uuid'], + meta, False) class Server_password(extensions.ExtensionDescriptor): -- cgit