diff options
author | Lance Bragstad <ldbragst@us.ibm.com> | 2014-02-11 03:41:41 +0000 |
---|---|---|
committer | Lance Bragstad <ldbragst@us.ibm.com> | 2014-02-24 15:39:09 +0000 |
commit | dec2db651471fc05800fbca11d40742f50e5343e (patch) | |
tree | 06f49467b18845e42d407f973fd87432e2e44da1 | |
parent | 75d0bbd3886755ebeb331069e0dd74aa34906942 (diff) | |
download | keystone-dec2db651471fc05800fbca11d40742f50e5343e.tar.gz |
Move _BaseController to common/controllers.py
The _BaseController class from the Federation work offered
some nice functionality for ensuring the controller checks for required
arguments in resource CRUD. Moving this to controller.V3Controller so
that it can be used consistently on resources in the Identity V3 API.
Related-Bug: 1278739
Change-Id: Icb66e2e324820959fa9409a3b1388c89b82f8ec0
-rw-r--r-- | keystone/common/controller.py | 77 | ||||
-rw-r--r-- | keystone/contrib/federation/controllers.py | 53 |
2 files changed, 78 insertions, 52 deletions
diff --git a/keystone/common/controller.py b/keystone/common/controller.py index 5658a8bd7..04c1831fe 100644 --- a/keystone/common/controller.py +++ b/keystone/common/controller.py @@ -270,6 +270,13 @@ class V3Controller(wsgi.Application): the API. This is required for supporting self-referential links, pagination, etc. + Class parameters: + + * `_mutable_parameters` - set of parameters that can be changed by users. + Usually used by cls.check_immutable_params() + * `_public_parameters` - set of parameters that are exposed to the user. + Usually used by cls.filter_params() + """ collection_name = 'entities' @@ -571,3 +578,73 @@ class V3Controller(wsgi.Application): action, authorization.flatten(policy_dict)) LOG.debug(_('RBAC: Authorization granted')) + + @classmethod + def check_immutable_params(cls, ref): + """Raise exception when disallowed parameter is in ref. + + Check whether the ref dictionary representing a request has only + mutable parameters included. If not, raise an exception. This method + checks only root-level keys from a ref dictionary. + + :param ref: a dictionary representing deserialized request to be + stored + :raises: :class:`keystone.exception.ImmutableAttributeError` + + """ + ref_keys = set(ref.keys()) + blocked_keys = ref_keys.difference(cls._mutable_parameters) + + if not blocked_keys: + #No immutable parameters changed + return + + exception_args = {'target': cls.__name__, + 'attribute': blocked_keys.pop()} + raise exception.ImmutableAttributeError(**exception_args) + + @classmethod + def check_required_params(cls, ref): + """Raise exception when required parameter is not in ref. + + Check whether the ref dictionary representing a request has the + required parameters to fulfill the request. If not, raise an + exception. This method checks only root-level keys from a ref + dictionary. + + :param ref: a dictionary representing deserialized request to be + stored + :raises: :class:`keystone.exception.ValidationError` + + """ + ref_keys = set(ref.keys()) + missing_args = [] + + for required in cls._required_parameters: + if required not in ref_keys: + missing_args.append(required) + + if len(missing_args) > 0: + exception_args = {'target': cls.__name__, + 'attribute': missing_args.pop()} + raise exception.ValidationError(**exception_args) + else: + return + + @classmethod + def filter_params(cls, ref): + """Remove unspecified parameters from the dictionary. + + This function removes unspecified parameters from the dictionary. See + check_immutable_parameters for corresponding function that raises + exceptions. This method checks only root-level keys from a ref + dictionary. + + :param ref: a dictionary representing deserialized response to be + serialized + """ + ref_keys = set(ref.keys()) + blocked_keys = ref_keys - cls._public_parameters + for blocked_param in blocked_keys: + del ref[blocked_param] + return ref diff --git a/keystone/contrib/federation/controllers.py b/keystone/contrib/federation/controllers.py index 554618ed4..d3f914998 100644 --- a/keystone/contrib/federation/controllers.py +++ b/keystone/contrib/federation/controllers.py @@ -24,58 +24,7 @@ CONF = config.CONF class _ControllerBase(controller.V3Controller): - """Base behaviors for federation controllers. - - Two new class parameters: - - * `_mutable_parameters` - set of parameters that can be changed by users. - Usually used by cls.check_immutable_params() - * `_public_parameters` - set of parameters that are exposed to the user. - Usually used by cls.filter_params() - - """ - - @classmethod - def check_immutable_params(cls, ref): - """Raise exception when disallowed parameter is in ref. - - Check whether the ref dictionary representing a request has only - mutable parameters included. If not, raise an exception. This method - checks only root-level keys from a ref dictionary. - - :param ref: a dictionary representing deserialized request to be - stored - :raises: :class:`keystone.exception.ImmutableAttributeError` - - """ - ref_keys = set(ref.keys()) - blocked_keys = ref_keys.difference(cls._mutable_parameters) - - if not blocked_keys: - #No immutable parameters changed - return - - exception_args = {'target': cls.__name__, - 'attribute': blocked_keys.pop()} - raise exception.ImmutableAttributeError(**exception_args) - - @classmethod - def filter_params(cls, ref): - """Remove unspecified parameters from the dictionary. - - This function removes unspecified parameters from the dictionary. See - check_immutable_parameters for corresponding function that raises - exceptions. This method checks only root-level keys from a ref - dictionary. - - :param ref: a dictionary representing deserialized response to be - serialized - """ - ref_keys = set(ref.keys()) - blocked_keys = ref_keys - cls._public_parameters - for blocked_param in blocked_keys: - del ref[blocked_param] - return ref + """Base behaviors for federation controllers.""" @classmethod def base_url(cls, path=None): |