Ejemplo n.º 1
0
 def _parse_expiration_date(self, expiration_date):
     if expiration_date is None:
         return None
     if not expiration_date.endswith('Z'):
         expiration_date += 'Z'
     try:
         expiration_time = timeutils.parse_isotime(expiration_date)
     except ValueError:
         raise exception.ValidationTimeStampError()
     if timeutils.is_older_than(expiration_time, 0):
         raise exception.ValidationExpirationError()
     return expiration_time
Ejemplo n.º 2
0
    def create_trust(self, context, trust=None):
        """Create a new trust.

        The user creating the trust must be the trustor.

        """

        # TODO(ayoung): instead of raising ValidationError on the first
        # problem, return a collection of all the problems.
        if not trust:
            raise exception.ValidationError(attribute='trust',
                                            target='request')
        if trust.get('project_id') and not trust.get('roles'):
            raise exception.Forbidden(
                _('At least one role should be specified.'))
        try:
            user_id = self._get_user_id(context)
            _trustor_only(context, trust, user_id)
            #confirm that the trustee exists
            self.identity_api.get_user(trust['trustee_user_id'])
            all_roles = self.assignment_api.list_roles()
            clean_roles = self._clean_role_list(context, trust, all_roles)
            if trust.get('project_id'):
                user_role = self.assignment_api.get_roles_for_user_and_project(
                    user_id, trust['project_id'])
            else:
                user_role = []
            for trust_role in clean_roles:
                matching_roles = [
                    x for x in user_role if x == trust_role['id']
                ]
                if not matching_roles:
                    raise exception.RoleNotFound(role_id=trust_role['id'])
            if trust.get('expires_at') is not None:
                if not trust['expires_at'].endswith('Z'):
                    trust['expires_at'] += 'Z'
                try:
                    trust['expires_at'] = (timeutils.parse_isotime(
                        trust['expires_at']))
                except ValueError:
                    raise exception.ValidationTimeStampError()
            trust_id = uuid.uuid4().hex
            new_trust = self.trust_api.create_trust(trust_id, trust,
                                                    clean_roles)
            self._fill_in_roles(context, new_trust, all_roles)
            return TrustV3.wrap_member(context, new_trust)
        except KeyError as e:
            raise exception.ValidationError(attribute=e.args[0],
                                            target='trust')