From babf11d6dd310ab5e752655a9b59efd7bb611197 Mon Sep 17 00:00:00 2001 From: Andrey Kurilin Date: Tue, 8 Nov 2016 17:23:08 +0200 Subject: [PATCH] [Services] redesign metaclass to support subclasses Previous implementation restricts to create Mixin classes and it leds to duplicate a bunch of code. Restricted code: class SomeService(Service): def some_func(self): self._impl.some_func() def another_func(self): self._impl.another_func() class SomeMixin(object): def some_func(self): <- this method is common for several versions # do something class SomeV1(SomeService, SomeMixin): def another_func(self): # do something class SomeV2(SomeService, SomeMixin): def another_func(self): # do something class SomeV3(SomeService): def some_func(self): # do something def another_func(self): # do something Instead of requiring all methods to be overridden and hardcode several exceptions, it would be nice to provide more simplier way(similar to abstract classes) to do it - simple decorator to mark which methods should be implemented in subclasses Change-Id: Ib5e9dcecfc1497ceb746d96f0917abd6e2cc7c82 --- rally/plugins/openstack/service.py | 1 + .../openstack/services/identity/identity.py | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/rally/plugins/openstack/service.py b/rally/plugins/openstack/service.py index dd2ab5a9..6c7bcd26 100644 --- a/rally/plugins/openstack/service.py +++ b/rally/plugins/openstack/service.py @@ -18,6 +18,7 @@ from rally.task import service as base_service service = base_service.service compat_layer = base_service.compat_layer Service = base_service.Service +should_be_overridden = base_service.should_be_overridden class UnifiedOpenStackService(base_service.UnifiedService): diff --git a/rally/plugins/openstack/services/identity/identity.py b/rally/plugins/openstack/services/identity/identity.py index 99c3a2ab..820fe62e 100644 --- a/rally/plugins/openstack/services/identity/identity.py +++ b/rally/plugins/openstack/services/identity/identity.py @@ -30,6 +30,7 @@ class Identity(service.UnifiedOpenStackService): cloud_version = clients.keystone().version.split(".")[0][1:] return cloud_version == cls._meta_get("impl")._meta_get("version") + @service.should_be_overridden def create_project(self, project_name=None, domain_name="Default"): """Creates new project/tenant and return project object. @@ -41,16 +42,20 @@ class Identity(service.UnifiedOpenStackService): return self._impl.create_project(project_name, domain_name=domain_name) + @service.should_be_overridden def delete_project(self, project_id): """Deletes project.""" return self._impl.delete_project(project_id) + @service.should_be_overridden def list_projects(self): """List all projects.""" return self._impl.list_projects() - def create_user(self, username, password, email=None, project_id=None, - domain_name="Default", default_role="member"): + @service.should_be_overridden + def create_user(self, username=None, password=None, email=None, + project_id=None, domain_name="Default", + default_role="member"): """Create user. :param username: name of user @@ -71,22 +76,27 @@ class Identity(service.UnifiedOpenStackService): domain_name=domain_name, default_role=default_role) + @service.should_be_overridden def delete_user(self, user_id): """Deletes user by its id.""" self._impl.delete_user(user_id) + @service.should_be_overridden def list_users(self): """List all users.""" return self._impl.list_users() + @service.should_be_overridden def delete_service(self, service_id): """Deletes service.""" self._impl.delete_service(service_id) + @service.should_be_overridden def list_services(self): """List all services.""" return self._impl.list_services() + @service.should_be_overridden def create_role(self, name=None, domain_name="Default"): """Create role with specific name @@ -97,20 +107,24 @@ class Identity(service.UnifiedOpenStackService): """ return self._impl.create_role(name=name, domain_name=domain_name) + @service.should_be_overridden def add_role(self, role_id, user_id, project_id): """Add role to user.""" return self._impl.add_role(role_id=role_id, user_id=user_id, project_id=project_id) + @service.should_be_overridden def delete_role(self, role_id): """Deletes role.""" self._impl.delete_role(role_id) + @service.should_be_overridden def revoke_role(self, role_id, user_id, project_id): """Revokes a role from a user.""" return self._impl.revoke_role(role_id=role_id, user_id=user_id, project_id=project_id) + @service.should_be_overridden def list_roles(self, user_id=None, project_id=None, domain_name=None): """List all roles. @@ -124,6 +138,7 @@ class Identity(service.UnifiedOpenStackService): return self._impl.list_roles(user_id=user_id, project_id=project_id, domain_name=domain_name) + @service.should_be_overridden def get_role(self, role_id): """Get role.""" return self._impl.get_role(role_id)