diff --git a/HACKING.rst b/HACKING.rst index 937ad54bb5..cf6abb39d8 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -44,7 +44,7 @@ Use the built-in logging module, and ensure you ``getLogger``:: Imports ------- -- Do not import objects, only modules +- Import modules, not module attributes - Do not import more than one module per line - Do not make relative imports - Order your imports by the full module path diff --git a/keystone/catalog/__init__.py b/keystone/catalog/__init__.py index 47f60b38ae..c029a486f8 100644 --- a/keystone/catalog/__init__.py +++ b/keystone/catalog/__init__.py @@ -15,3 +15,4 @@ # under the License. from keystone.catalog.core import * +from keystone.catalog import controllers diff --git a/keystone/catalog/controllers.py b/keystone/catalog/controllers.py new file mode 100644 index 0000000000..b58f3d07b3 --- /dev/null +++ b/keystone/catalog/controllers.py @@ -0,0 +1,154 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2012 OpenStack LLC +# Copyright 2012 Canonical Ltd. +# +# 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. + +import uuid + +from keystone.catalog import core +from keystone.common import controller +from keystone.common import wsgi +from keystone import identity +from keystone import policy +from keystone import token + + +class Service(wsgi.Application): + def __init__(self): + self.catalog_api = core.Manager() + self.identity_api = identity.Manager() + self.policy_api = policy.Manager() + self.token_api = token.Manager() + super(Service, self).__init__() + + def get_services(self, context): + self.assert_admin(context) + service_list = self.catalog_api.list_services(context) + return {'OS-KSADM:services': service_list} + + def get_service(self, context, service_id): + self.assert_admin(context) + service_ref = self.catalog_api.get_service(context, service_id) + return {'OS-KSADM:service': service_ref} + + def delete_service(self, context, service_id): + self.assert_admin(context) + self.catalog_api.delete_service(context, service_id) + + def create_service(self, context, OS_KSADM_service): + self.assert_admin(context) + service_id = uuid.uuid4().hex + service_ref = OS_KSADM_service.copy() + service_ref['id'] = service_id + new_service_ref = self.catalog_api.create_service( + context, service_id, service_ref) + return {'OS-KSADM:service': new_service_ref} + + +class Endpoint(wsgi.Application): + def __init__(self): + self.catalog_api = core.Manager() + self.identity_api = identity.Manager() + self.policy_api = policy.Manager() + self.token_api = token.Manager() + super(Endpoint, self).__init__() + + def get_endpoints(self, context): + self.assert_admin(context) + endpoint_list = self.catalog_api.list_endpoints(context) + return {'endpoints': endpoint_list} + + def create_endpoint(self, context, endpoint): + self.assert_admin(context) + endpoint_id = uuid.uuid4().hex + endpoint_ref = endpoint.copy() + endpoint_ref['id'] = endpoint_id + new_endpoint_ref = self.catalog_api.create_endpoint( + context, endpoint_id, endpoint_ref) + return {'endpoint': new_endpoint_ref} + + def delete_endpoint(self, context, endpoint_id): + self.assert_admin(context) + self.catalog_api.delete_endpoint(context, endpoint_id) + + +class ServiceV3(controller.V3Controller): + @controller.protected + def create_service(self, context, service): + ref = self._assign_unique_id(self._normalize_dict(service)) + self._require_attribute(ref, 'type') + + ref = self.catalog_api.create_service(context, ref['id'], ref) + return {'service': ref} + + @controller.protected + def list_services(self, context): + refs = self.catalog_api.list_services(context) + refs = self._filter_by_attribute(context, refs, 'type') + return {'services': self._paginate(context, refs)} + + @controller.protected + def get_service(self, context, service_id): + ref = self.catalog_api.get_service(context, service_id) + return {'service': ref} + + @controller.protected + def update_service(self, context, service_id, service): + self._require_matching_id(service_id, service) + + ref = self.catalog_api.update_service(context, service_id, service) + return {'service': ref} + + @controller.protected + def delete_service(self, context, service_id): + return self.catalog_api.delete_service(context, service_id) + + +class EndpointV3(controller.V3Controller): + @controller.protected + def create_endpoint(self, context, endpoint): + ref = self._assign_unique_id(self._normalize_dict(endpoint)) + self._require_attribute(ref, 'service_id') + self._require_attribute(ref, 'interface') + self.catalog_api.get_service(context, ref['service_id']) + + ref = self.catalog_api.create_endpoint(context, ref['id'], ref) + return {'endpoint': ref} + + @controller.protected + def list_endpoints(self, context): + refs = self.catalog_api.list_endpoints(context) + refs = self._filter_by_attribute(context, refs, 'service_id') + refs = self._filter_by_attribute(context, refs, 'interface') + return {'endpoints': self._paginate(context, refs)} + + @controller.protected + def get_endpoint(self, context, endpoint_id): + ref = self.catalog_api.get_endpoint(context, endpoint_id) + return {'endpoint': ref} + + @controller.protected + def update_endpoint(self, context, endpoint_id, endpoint): + self._require_matching_id(endpoint_id, endpoint) + + if 'service_id' in endpoint: + self.catalog_api.get_service(context, endpoint['service_id']) + + ref = self.catalog_api.update_endpoint(context, endpoint_id, endpoint) + return {'endpoint': ref} + + @controller.protected + def delete_endpoint(self, context, endpoint_id): + return self.catalog_api.delete_endpoint(context, endpoint_id) diff --git a/keystone/catalog/core.py b/keystone/catalog/core.py index 79b3df2ad2..df100d7404 100644 --- a/keystone/catalog/core.py +++ b/keystone/catalog/core.py @@ -17,17 +17,10 @@ """Main entry point into the Catalog service.""" -import uuid - -from keystone.common import controller from keystone.common import logging from keystone.common import manager -from keystone.common import wsgi from keystone import config from keystone import exception -from keystone import identity -from keystone import policy -from keystone import token CONF = config.CONF @@ -216,132 +209,3 @@ class Driver(object): """ raise exception.NotImplemented() - - -class ServiceController(wsgi.Application): - def __init__(self): - self.catalog_api = Manager() - self.identity_api = identity.Manager() - self.policy_api = policy.Manager() - self.token_api = token.Manager() - super(ServiceController, self).__init__() - - def get_services(self, context): - self.assert_admin(context) - service_list = self.catalog_api.list_services(context) - return {'OS-KSADM:services': service_list} - - def get_service(self, context, service_id): - self.assert_admin(context) - service_ref = self.catalog_api.get_service(context, service_id) - return {'OS-KSADM:service': service_ref} - - def delete_service(self, context, service_id): - self.assert_admin(context) - self.catalog_api.delete_service(context, service_id) - - def create_service(self, context, OS_KSADM_service): - self.assert_admin(context) - service_id = uuid.uuid4().hex - service_ref = OS_KSADM_service.copy() - service_ref['id'] = service_id - new_service_ref = self.catalog_api.create_service( - context, service_id, service_ref) - return {'OS-KSADM:service': new_service_ref} - - -class EndpointController(wsgi.Application): - def __init__(self): - self.catalog_api = Manager() - self.identity_api = identity.Manager() - self.policy_api = policy.Manager() - self.token_api = token.Manager() - super(EndpointController, self).__init__() - - def get_endpoints(self, context): - self.assert_admin(context) - endpoint_list = self.catalog_api.list_endpoints(context) - return {'endpoints': endpoint_list} - - def create_endpoint(self, context, endpoint): - self.assert_admin(context) - endpoint_id = uuid.uuid4().hex - endpoint_ref = endpoint.copy() - endpoint_ref['id'] = endpoint_id - new_endpoint_ref = self.catalog_api.create_endpoint( - context, endpoint_id, endpoint_ref) - return {'endpoint': new_endpoint_ref} - - def delete_endpoint(self, context, endpoint_id): - self.assert_admin(context) - self.catalog_api.delete_endpoint(context, endpoint_id) - - -class ServiceControllerV3(controller.V3Controller): - @controller.protected - def create_service(self, context, service): - ref = self._assign_unique_id(self._normalize_dict(service)) - self._require_attribute(ref, 'type') - - ref = self.catalog_api.create_service(context, ref['id'], ref) - return {'service': ref} - - @controller.protected - def list_services(self, context): - refs = self.catalog_api.list_services(context) - refs = self._filter_by_attribute(context, refs, 'type') - return {'services': self._paginate(context, refs)} - - @controller.protected - def get_service(self, context, service_id): - ref = self.catalog_api.get_service(context, service_id) - return {'service': ref} - - @controller.protected - def update_service(self, context, service_id, service): - self._require_matching_id(service_id, service) - - ref = self.catalog_api.update_service(context, service_id, service) - return {'service': ref} - - @controller.protected - def delete_service(self, context, service_id): - return self.catalog_api.delete_service(context, service_id) - - -class EndpointControllerV3(controller.V3Controller): - @controller.protected - def create_endpoint(self, context, endpoint): - ref = self._assign_unique_id(self._normalize_dict(endpoint)) - self._require_attribute(ref, 'service_id') - self._require_attribute(ref, 'interface') - self.catalog_api.get_service(context, ref['service_id']) - - ref = self.catalog_api.create_endpoint(context, ref['id'], ref) - return {'endpoint': ref} - - @controller.protected - def list_endpoints(self, context): - refs = self.catalog_api.list_endpoints(context) - refs = self._filter_by_attribute(context, refs, 'service_id') - refs = self._filter_by_attribute(context, refs, 'interface') - return {'endpoints': self._paginate(context, refs)} - - @controller.protected - def get_endpoint(self, context, endpoint_id): - ref = self.catalog_api.get_endpoint(context, endpoint_id) - return {'endpoint': ref} - - @controller.protected - def update_endpoint(self, context, endpoint_id, endpoint): - self._require_matching_id(endpoint_id, endpoint) - - if 'service_id' in endpoint: - self.catalog_api.get_service(context, endpoint['service_id']) - - ref = self.catalog_api.update_endpoint(context, endpoint_id, endpoint) - return {'endpoint': ref} - - @controller.protected - def delete_endpoint(self, context, endpoint_id): - return self.catalog_api.delete_endpoint(context, endpoint_id) diff --git a/keystone/contrib/admin_crud/core.py b/keystone/contrib/admin_crud/core.py index d3c1d16857..7526c3eb70 100644 --- a/keystone/contrib/admin_crud/core.py +++ b/keystone/contrib/admin_crud/core.py @@ -27,11 +27,11 @@ class CrudExtension(wsgi.ExtensionRouter): """ def add_routes(self, mapper): - tenant_controller = identity.TenantController() - user_controller = identity.UserController() - role_controller = identity.RoleController() - service_controller = catalog.ServiceController() - endpoint_controller = catalog.EndpointController() + tenant_controller = identity.controllers.Tenant() + user_controller = identity.controllers.User() + role_controller = identity.controllers.Role() + service_controller = catalog.controllers.Service() + endpoint_controller = catalog.controllers.Endpoint() # Tenant Operations mapper.connect( diff --git a/keystone/contrib/user_crud/core.py b/keystone/contrib/user_crud/core.py index 7d4c1ea63a..03f7bf2fd6 100644 --- a/keystone/contrib/user_crud/core.py +++ b/keystone/contrib/user_crud/core.py @@ -20,9 +20,8 @@ import uuid from keystone import exception from keystone.common import logging from keystone.common import wsgi -from keystone.identity import Manager as IdentityManager -from keystone.identity import UserController as UserManager -from keystone.token import Manager as TokenManager +from keystone import identity +from keystone import token LOG = logging.getLogger(__name__) @@ -30,9 +29,9 @@ LOG = logging.getLogger(__name__) class UserController(wsgi.Application): def __init__(self): - self.identity_api = IdentityManager() - self.token_api = TokenManager() - self.user_controller = UserManager() + self.identity_api = identity.Manager() + self.token_api = token.Manager() + self.user_controller = identity.controllers.User() def set_user_password(self, context, user_id, user): token_id = context.get('token_id') diff --git a/keystone/identity/__init__.py b/keystone/identity/__init__.py index 07a1a650af..f6fe44188c 100644 --- a/keystone/identity/__init__.py +++ b/keystone/identity/__init__.py @@ -15,5 +15,5 @@ # under the License. from keystone.identity.core import * -from keystone.identity.controllers import * -from keystone.identity.routers import * +from keystone.identity import controllers +from keystone.identity import routers diff --git a/keystone/identity/controllers.py b/keystone/identity/controllers.py index e1def2b97a..77bcda2cf6 100644 --- a/keystone/identity/controllers.py +++ b/keystone/identity/controllers.py @@ -22,26 +22,22 @@ import uuid from keystone.common import controller from keystone.common import logging -from keystone.common import manager from keystone.common import wsgi -from keystone import config from keystone import exception +from keystone.identity import core from keystone import policy from keystone import token -from keystone.identity import core -CONF = config.CONF - LOG = logging.getLogger(__name__) -class TenantController(wsgi.Application): +class Tenant(wsgi.Application): def __init__(self): self.identity_api = core.Manager() self.policy_api = policy.Manager() self.token_api = token.Manager() - super(TenantController, self).__init__() + super(Tenant, self).__init__() def get_all_tenants(self, context, **kw): """Gets a list of all tenants for an admin user.""" @@ -161,12 +157,12 @@ class TenantController(wsgi.Application): return o -class UserController(wsgi.Application): +class User(wsgi.Application): def __init__(self): self.identity_api = core.Manager() self.policy_api = policy.Manager() self.token_api = token.Manager() - super(UserController, self).__init__() + super(User, self).__init__() def get_user(self, context, user_id): self.assert_admin(context) @@ -244,12 +240,12 @@ class UserController(wsgi.Application): return self.update_user(context, user_id, user) -class RoleController(wsgi.Application): +class Role(wsgi.Application): def __init__(self): self.identity_api = core.Manager() self.token_api = token.Manager() self.policy_api = policy.Manager() - super(RoleController, self).__init__() + super(Role, self).__init__() # COMPAT(essex-3) def get_user_roles(self, context, user_id, tenant_id=None): @@ -413,7 +409,7 @@ class RoleController(wsgi.Application): self.token_api.revoke_tokens(context, user_id, tenant_id) -class DomainControllerV3(controller.V3Controller): +class DomainV3(controller.V3Controller): @controller.protected def create_domain(self, context, domain): ref = self._assign_unique_id(self._normalize_dict(domain)) @@ -442,7 +438,7 @@ class DomainControllerV3(controller.V3Controller): return self.identity_api.delete_domain(context, domain_id) -class ProjectControllerV3(controller.V3Controller): +class ProjectV3(controller.V3Controller): @controller.protected def create_project(self, context, project): ref = self._assign_unique_id(self._normalize_dict(project)) @@ -476,7 +472,7 @@ class ProjectControllerV3(controller.V3Controller): return self.identity_api.delete_project(context, project_id) -class UserControllerV3(controller.V3Controller): +class UserV3(controller.V3Controller): @controller.protected def create_user(self, context, user): ref = self._assign_unique_id(self._normalize_dict(user)) @@ -505,7 +501,7 @@ class UserControllerV3(controller.V3Controller): return self.identity_api.delete_user(context, user_id) -class CredentialControllerV3(controller.V3Controller): +class CredentialV3(controller.V3Controller): @controller.protected def create_credential(self, context, credential): ref = self._assign_unique_id(self._normalize_dict(credential)) @@ -537,7 +533,7 @@ class CredentialControllerV3(controller.V3Controller): return self.identity_api.delete_credential(context, credential_id) -class RoleControllerV3(controller.V3Controller): +class RoleV3(controller.V3Controller): @controller.protected def create_role(self, context, role): ref = self._assign_unique_id(self._normalize_dict(role)) diff --git a/keystone/identity/routers.py b/keystone/identity/routers.py index 49c0785381..2acd27553c 100644 --- a/keystone/identity/routers.py +++ b/keystone/identity/routers.py @@ -16,34 +16,23 @@ """WSGI Routers for the Identity service.""" -import urllib -import urlparse -import uuid - -from keystone.common import controller -from keystone.common import logging -from keystone.common import manager from keystone.common import wsgi -from keystone import config -from keystone import exception -from keystone import policy -from keystone import token -from keystone.identity import core, controllers +from keystone.identity import controllers -class PublicRouter(wsgi.ComposableRouter): +class Public(wsgi.ComposableRouter): def add_routes(self, mapper): - tenant_controller = controllers.TenantController() + tenant_controller = controllers.Tenant() mapper.connect('/tenants', controller=tenant_controller, action='get_tenants_for_token', conditions=dict(method=['GET'])) -class AdminRouter(wsgi.ComposableRouter): +class Admin(wsgi.ComposableRouter): def add_routes(self, mapper): # Tenant Operations - tenant_controller = controllers.TenantController() + tenant_controller = controllers.Tenant() mapper.connect('/tenants', controller=tenant_controller, action='get_all_tenants', @@ -54,14 +43,14 @@ class AdminRouter(wsgi.ComposableRouter): conditions=dict(method=['GET'])) # User Operations - user_controller = controllers.UserController() + user_controller = controllers.User() mapper.connect('/users/{user_id}', controller=user_controller, action='get_user', conditions=dict(method=['GET'])) # Role Operations - roles_controller = controllers.RoleController() + roles_controller = controllers.Role() mapper.connect('/tenants/{tenant_id}/users/{user_id}/roles', controller=roles_controller, action='get_user_roles', diff --git a/keystone/policy/__init__.py b/keystone/policy/__init__.py index 8a355c1184..3379c617e6 100644 --- a/keystone/policy/__init__.py +++ b/keystone/policy/__init__.py @@ -15,3 +15,4 @@ # under the License. from keystone.policy.core import * +from keystone.policy import controllers diff --git a/keystone/policy/controllers.py b/keystone/policy/controllers.py new file mode 100644 index 0000000000..e3b9625283 --- /dev/null +++ b/keystone/policy/controllers.py @@ -0,0 +1,48 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2012 OpenStack LLC +# +# 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 keystone.common import controller + + +class PolicyV3(controller.V3Controller): + @controller.protected + def create_policy(self, context, policy): + ref = self._assign_unique_id(self._normalize_dict(policy)) + self._require_attribute(ref, 'blob') + self._require_attribute(ref, 'type') + + ref = self.policy_api.create_policy(context, ref['id'], ref) + return {'policy': ref} + + @controller.protected + def list_policies(self, context): + refs = self.policy_api.list_policies(context) + refs = self._filter_by_attribute(context, refs, 'type') + return {'policies': self._paginate(context, refs)} + + @controller.protected + def get_policy(self, context, policy_id): + ref = self.policy_api.get_policy(context, policy_id) + return {'policy': ref} + + @controller.protected + def update_policy(self, context, policy_id, policy): + ref = self.policy_api.update_policy(context, policy_id, policy) + return {'policy': ref} + + @controller.protected + def delete_policy(self, context, policy_id): + return self.policy_api.delete_policy(context, policy_id) diff --git a/keystone/policy/core.py b/keystone/policy/core.py index 2e5676fc2b..447e11e374 100644 --- a/keystone/policy/core.py +++ b/keystone/policy/core.py @@ -18,7 +18,6 @@ from keystone.common import manager -from keystone.common import controller from keystone import config from keystone import exception @@ -102,34 +101,3 @@ class Driver(object): """ raise exception.NotImplemented() - - -class PolicyControllerV3(controller.V3Controller): - @controller.protected - def create_policy(self, context, policy): - ref = self._assign_unique_id(self._normalize_dict(policy)) - self._require_attribute(ref, 'blob') - self._require_attribute(ref, 'type') - - ref = self.policy_api.create_policy(context, ref['id'], ref) - return {'policy': ref} - - @controller.protected - def list_policies(self, context): - refs = self.policy_api.list_policies(context) - refs = self._filter_by_attribute(context, refs, 'type') - return {'policies': self._paginate(context, refs)} - - @controller.protected - def get_policy(self, context, policy_id): - ref = self.policy_api.get_policy(context, policy_id) - return {'policy': ref} - - @controller.protected - def update_policy(self, context, policy_id, policy): - ref = self.policy_api.update_policy(context, policy_id, policy) - return {'policy': ref} - - @controller.protected - def delete_policy(self, context, policy_id): - return self.policy_api.delete_policy(context, policy_id) diff --git a/keystone/service.py b/keystone/service.py index 808fb91f1c..11003b119b 100644 --- a/keystone/service.py +++ b/keystone/service.py @@ -80,13 +80,13 @@ class V3Router(wsgi.ComposingRouter): self.crud_routes( mapper, - catalog.ServiceControllerV3(**apis), + catalog.controllers.ServiceV3(**apis), 'services', 'service') self.crud_routes( mapper, - catalog.EndpointControllerV3(**apis), + catalog.controllers.EndpointV3(**apis), 'endpoints', 'endpoint') @@ -94,11 +94,11 @@ class V3Router(wsgi.ComposingRouter): self.crud_routes( mapper, - identity.DomainControllerV3(**apis), + identity.controllers.DomainV3(**apis), 'domains', 'domain') - project_controller = identity.ProjectControllerV3(**apis) + project_controller = identity.controllers.ProjectV3(**apis) self.crud_routes( mapper, project_controller, @@ -112,17 +112,17 @@ class V3Router(wsgi.ComposingRouter): self.crud_routes( mapper, - identity.UserControllerV3(**apis), + identity.controllers.UserV3(**apis), 'users', 'user') self.crud_routes( mapper, - identity.CredentialControllerV3(**apis), + identity.controllers.CredentialV3(**apis), 'credentials', 'credential') - role_controller = identity.RoleControllerV3(**apis) + role_controller = identity.controllers.RoleV3(**apis) self.crud_routes( mapper, role_controller, @@ -171,7 +171,7 @@ class V3Router(wsgi.ComposingRouter): # Policy - policy_controller = policy.PolicyControllerV3(**apis) + policy_controller = policy.controllers.PolicyV3(**apis) self.crud_routes( mapper, policy_controller, @@ -260,7 +260,7 @@ class AdminRouter(wsgi.ComposingRouter): controller=extensions_controller, action='get_extension_info', conditions=dict(method=['GET'])) - identity_router = identity.AdminRouter() + identity_router = identity.routers.Admin() routers = [identity_router] super(AdminRouter, self).__init__(mapper, routers) @@ -302,7 +302,7 @@ class PublicRouter(wsgi.ComposingRouter): action='get_extension_info', conditions=dict(method=['GET'])) - identity_router = identity.PublicRouter() + identity_router = identity.routers.Public() routers = [identity_router] super(PublicRouter, self).__init__(mapper, routers)