From feb567f647c3ac270164fd60792a154be8d5353e Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Mon, 2 Apr 2012 23:43:39 -0500 Subject: [PATCH] Fixing the mysql list calls * Became compliant with the new guest api code * Fixed a small guest api bug * Fixed the views to return the proper data * Added databases in list_users * Fixed the issue with bad nova data * Misc pep8 fixes --- reddwarf/common/remote.py | 1 - reddwarf/extensions/mysql/models.py | 20 ++++++++++---------- reddwarf/extensions/mysql/views.py | 13 ++++--------- reddwarf/flavor/__init__.py | 1 - reddwarf/flavor/models.py | 1 - reddwarf/flavor/service.py | 1 + reddwarf/flavor/views.py | 7 ++++--- reddwarf/guestagent/api.py | 2 +- reddwarf/guestagent/dbaas.py | 12 ++++++++++++ reddwarf/instance/models.py | 13 ++++++++++++- reddwarf/instance/service.py | 6 ++++-- reddwarf/tests/fakes/guestagent.py | 1 + reddwarf/tests/fakes/nova.py | 6 ++++-- 13 files changed, 53 insertions(+), 31 deletions(-) diff --git a/reddwarf/common/remote.py b/reddwarf/common/remote.py index 793711a81b..5dc5aafccb 100644 --- a/reddwarf/common/remote.py +++ b/reddwarf/common/remote.py @@ -65,4 +65,3 @@ if CONFIG.get("remote_implementation", "real") == "fake": def create_nova_client(context): return fake_create_nova_client(context) - diff --git a/reddwarf/extensions/mysql/models.py b/reddwarf/extensions/mysql/models.py index bfdf397614..66d710b803 100644 --- a/reddwarf/extensions/mysql/models.py +++ b/reddwarf/extensions/mysql/models.py @@ -25,7 +25,7 @@ from reddwarf.common import config from reddwarf.common import exception from reddwarf.instance import models as base_models from reddwarf.guestagent.db import models as guest_models -from reddwarf.guestagent import api as guest_api +from reddwarf.common.remote import create_guest_client CONFIG = config.Config LOG = logging.getLogger(__name__) @@ -90,12 +90,12 @@ class User(object): def create(cls, context, instance_id, users): # Load InstanceServiceStatus to verify if its running load_and_verify(context, instance_id) - guest_api.API().create_user(context, instance_id, users) + create_guest_client(context, instance_id).create_user(users) @classmethod def delete(cls, context, instance_id, username): load_and_verify(context, instance_id) - guest_api.API().delete_user(context, instance_id, username) + create_guest_client(context, instance_id).delete_user(username) class Root(object): @@ -103,12 +103,12 @@ class Root(object): @classmethod def load(cls, context, instance_id): load_and_verify(context, instance_id) - return guest_api.API().is_root_enabled(context, instance_id) + return create_guest_client(context, instance_id).is_root_enabled() @classmethod def create(cls, context, instance_id): load_and_verify(context, instance_id) - root = guest_api.API().enable_root(context, instance_id) + root = create_guest_client(context, instance_id).enable_root() root_user = guest_models.MySQLUser() root_user.deserialize(root) return root_user @@ -119,7 +119,7 @@ class Users(object): @classmethod def load(cls, context, instance_id): load_and_verify(context, instance_id) - user_list = guest_api.API().list_users(context, instance_id) + user_list = create_guest_client(context, instance_id).list_users() model_users = [] for user in user_list: mysql_user = guest_models.MySQLUser() @@ -147,12 +147,12 @@ class Schema(object): @classmethod def create(cls, context, instance_id, schemas): load_and_verify(context, instance_id) - guest_api.API().create_database(context, instance_id, schemas) + create_guest_client(context, instance_id).create_database(schemas) @classmethod def delete(cls, context, instance_id, schema): load_and_verify(context, instance_id) - guest_api.API().delete_database(context, instance_id, schema) + create_guest_client(context, instance_id).delete_database(schema) class Schemas(object): @@ -160,9 +160,9 @@ class Schemas(object): @classmethod def load(cls, context, instance_id): load_and_verify(context, instance_id) - schema_list = guest_api.API().list_databases(context, instance_id) + schemas = create_guest_client(context, instance_id).list_databases() model_schemas = [] - for schema in schema_list: + for schema in schemas: mysql_schema = guest_models.MySQLDatabase() mysql_schema.deserialize(schema) model_schemas.append(Schema(mysql_schema.name, diff --git a/reddwarf/extensions/mysql/views.py b/reddwarf/extensions/mysql/views.py index a76b5aaa25..9cdd27b0fe 100644 --- a/reddwarf/extensions/mysql/views.py +++ b/reddwarf/extensions/mysql/views.py @@ -26,7 +26,7 @@ class UserView(object): "name": self.user.name, "databases": self.user.databases } - return {"users": user_dict} + return user_dict class UsersView(object): @@ -40,7 +40,7 @@ class UsersView(object): for user in self.users: data.append(UserView(user).data()) - return data + return {"users": data} class RootCreatedView(UserView): @@ -68,12 +68,7 @@ class SchemaView(object): self.schema = schema def data(self): - schema_dict = { - "name": self.schema.name, - "collate": self.schema.collate, - "character_set": self.schema.character_set - } - return {"databases": schema_dict} + return {"name": self.schema.name} class SchemasView(object): @@ -87,4 +82,4 @@ class SchemasView(object): for schema in self.schemas: data.append(SchemaView(schema).data()) - return data + return {"databases": data} diff --git a/reddwarf/flavor/__init__.py b/reddwarf/flavor/__init__.py index 46c04f2bfb..cbf4a45060 100644 --- a/reddwarf/flavor/__init__.py +++ b/reddwarf/flavor/__init__.py @@ -14,4 +14,3 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. - diff --git a/reddwarf/flavor/models.py b/reddwarf/flavor/models.py index 8e477e3b4c..43d88ae746 100644 --- a/reddwarf/flavor/models.py +++ b/reddwarf/flavor/models.py @@ -77,4 +77,3 @@ class Flavors(NovaRemoteModelBase): def __iter__(self): for item in self.flavors: yield item - diff --git a/reddwarf/flavor/service.py b/reddwarf/flavor/service.py index 4a65d182a1..db9fe5957b 100644 --- a/reddwarf/flavor/service.py +++ b/reddwarf/flavor/service.py @@ -73,6 +73,7 @@ class FlavorController(BaseController): flavors = models.Flavors(context=context) return wsgi.Result(views.FlavorsView(flavors, req).data(), 200) + class API(wsgi.Router): """API""" def __init__(self): diff --git a/reddwarf/flavor/views.py b/reddwarf/flavor/views.py index 7516475802..82508e0ed9 100644 --- a/reddwarf/flavor/views.py +++ b/reddwarf/flavor/views.py @@ -15,6 +15,7 @@ # License for the specific language governing permissions and limitations # under the License. + class FlavorView(object): def __init__(self, flavor, req=None): @@ -31,7 +32,7 @@ class FlavorView(object): def _build_links(self): result = [] #scheme = self.req.scheme - scheme = 'https' # Forcing https + scheme = 'https' # Forcing https endpoint = self.req.host splitpath = self.req.path.split('/') detailed = '' @@ -49,8 +50,8 @@ class FlavorView(object): path = '/'.join(splitpath) href = href_template % locals() elif rlink['rel'] == 'bookmark': - splitpath.pop(2) # Remove the version. - splitpath.pop(1) # Remove the tenant id. + splitpath.pop(2) # Remove the version. + splitpath.pop(1) # Remove the tenant id. path = '/'.join(splitpath) href = href_template % locals() diff --git a/reddwarf/guestagent/api.py b/reddwarf/guestagent/api.py index 31e46a20df..b7babf906b 100644 --- a/reddwarf/guestagent/api.py +++ b/reddwarf/guestagent/api.py @@ -53,7 +53,7 @@ class API(object): def list_users(self): """Make an asynchronous call to list database users""" LOG.debug("Listing Users for Instance %s", self.id) - return rpc.call(context, self._get_routing_key(), + return rpc.call(self.context, self._get_routing_key(), {"method": "list_users"}) def delete_user(self, user): diff --git a/reddwarf/guestagent/dbaas.py b/reddwarf/guestagent/dbaas.py index 18bb741caa..eb08893c26 100644 --- a/reddwarf/guestagent/dbaas.py +++ b/reddwarf/guestagent/dbaas.py @@ -137,6 +137,18 @@ class DBaaSAgent(object): LOG.debug("user = " + str(row)) mysql_user = models.MySQLUser() mysql_user.name = row['User'] + # Now get the databases + t = text("""SELECT grantee, table_schema + from information_schema.SCHEMA_PRIVILEGES + group by grantee, table_schema;""") + db_result = client.execute(t) + for db in db_result: + matches = re.match("^'(.+)'@", db['grantee']) + if matches is not None and \ + matches.group(1) == mysql_user.name: + mysql_db = models.MySQLDatabase() + mysql_db.name = db['table_schema'] + mysql_user.databases.append(mysql_db.serialize()) users.append(mysql_user.serialize()) LOG.debug("users = " + str(users)) return users diff --git a/reddwarf/instance/models.py b/reddwarf/instance/models.py index 70934d8ac8..d2955c1733 100644 --- a/reddwarf/instance/models.py +++ b/reddwarf/instance/models.py @@ -250,7 +250,18 @@ class Instances(object): find_server = create_server_list_matcher(servers) for db in db_infos: status = InstanceServiceStatus.find_by(instance_id=db.id) - server = find_server(db.id, db.compute_instance_id) + try: + # TODO(hub-cap): Figure out if this is actually correct. + # We are not sure if we should be doing some validation. + # Basically if the server find returns nothing, but we + # have something, there is a mismatch between what the + # nova db has compared to what we have. We should have + # a way to handle this. + server = find_server(db.id, db.compute_instance_id) + except rd_exceptions.ComputeInstanceNotFound: + LOG.info(_("Could not find server %s") % + db.compute_instance_id) + continue ret.append(Instance(context, db, server, status)) return ret diff --git a/reddwarf/instance/service.py b/reddwarf/instance/service.py index 575605ab32..50ea8fa34d 100644 --- a/reddwarf/instance/service.py +++ b/reddwarf/instance/service.py @@ -266,7 +266,8 @@ class API(wsgi.Router): mapper = routes.Mapper() super(API, self).__init__(mapper) self._instance_router(mapper) - self._flavor_router(mapper) #TODO(ed-): Remove after restructure + # TODO(ed-): Remove after restructure + self._flavor_router(mapper) def _instance_router(self, mapper): instance_resource = InstanceController().create_resource() @@ -274,7 +275,8 @@ class API(wsgi.Router): mapper.resource("instance", path, controller=instance_resource, collection={'detail': 'GET'}) - #TODO(ed-): remove this when all mention of flavorservice et cetera are moved away + # TODO(ed-): remove this when all mention of flavorservice + # et cetera are moved away def _flavor_router(self, mapper): flavor_resource = flavorservice.FlavorController().create_resource() path = "/{tenant_id}/flavors" diff --git a/reddwarf/tests/fakes/guestagent.py b/reddwarf/tests/fakes/guestagent.py index ccfb536666..b2c3f3fdce 100644 --- a/reddwarf/tests/fakes/guestagent.py +++ b/reddwarf/tests/fakes/guestagent.py @@ -33,6 +33,7 @@ class FakeGuest(object): def prepare(self, memory_mb, databases): from reddwarf.instance.models import InstanceServiceStatus from reddwarf.instance.models import ServiceStatuses + def update_db(): status = InstanceServiceStatus.find_by(instance_id=self.id) status.status = ServiceStatuses.RUNNING diff --git a/reddwarf/tests/fakes/nova.py b/reddwarf/tests/fakes/nova.py index eac1085143..9edaab1274 100644 --- a/reddwarf/tests/fakes/nova.py +++ b/reddwarf/tests/fakes/nova.py @@ -46,7 +46,8 @@ class FakeFlavor(object): return "flavors/%s" % self.id def to_dict(self): - return {"id":self.id, "links":self.links} + return {"id": self.id, "links": self.links} + class FakeFlavors(object): @@ -76,6 +77,7 @@ class FakeFlavors(object): return value raise nova_exceptions.NotFound(404, "Flavor href not found %s" % href) + class FakeServer(object): def __init__(self, parent, id, name, image_id, flavor_ref): @@ -131,7 +133,7 @@ class FakeServers(object): def __init__(self, flavors): self.db = {} self.flavors = flavors - self.next_id = 10; + self.next_id = 10 self.events = EventSimulator() def create(self, name, image_id, flavor_ref, files):