summaryrefslogtreecommitdiff
path: root/keystoneclient/v3/application_credentials.py
blob: 694ee8cd1e8ddb0a47d14193d5fdb691c927871d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Copyright 2018 SUSE Linux GmbH
#
# 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 six

from keystoneclient import base
from keystoneclient import exceptions
from keystoneclient.i18n import _
from keystoneclient import utils


class ApplicationCredential(base.Resource):
    """Represents an Identity application credential.

    Attributes:
        * id: a uuid that identifies the application credential
        * user: the user who owns the application credential
        * name: application credential name
        * secret: application credential secret
        * description: application credential description
        * expires_at: expiry time
        * roles: role assignments on the project
        * unrestricted: whether the application credential has restrictions
            applied
        * access_rules: a list of access rules defining what API requests the
            application credential may be used for

    """

    pass


class ApplicationCredentialManager(base.CrudManager):
    """Manager class for manipulating Identity application credentials."""

    resource_class = ApplicationCredential
    collection_key = 'application_credentials'
    key = 'application_credential'

    def create(self, name, user=None, secret=None, description=None,
               expires_at=None, roles=None,
               unrestricted=False, access_rules=None, **kwargs):
        """Create a credential.

        :param string name: application credential name
        :param string user: User ID
        :param secret: application credential secret
        :param description: application credential description
        :param datetime.datetime expires_at: expiry time
        :param List roles: list of roles on the project. Maybe a list of IDs
            or a list of dicts specifying role name and domain
        :param bool unrestricted: whether the application credential has
            restrictions applied
        :param List access_rules: a list of dicts representing access rules

        :returns: the created application credential
        :rtype:
            :class:`keystoneclient.v3.application_credentials.ApplicationCredential`

        """
        user = user or self.client.user_id
        self.base_url = '/users/%(user)s' % {'user': user}

        # Convert roles list into list-of-dict API format
        role_list = []
        if roles:
            if not isinstance(roles, list):
                roles = [roles]
            for role in roles:
                if isinstance(role, six.string_types):
                    role_list.extend([{'id': role}])
                elif isinstance(role, dict):
                    role_list.extend([role])
                else:
                    msg = (_("Roles must be a list of IDs or role dicts."))
                    raise exceptions.CommandError(msg)

        if not role_list:
            role_list = None

        # Convert datetime.datetime expires_at to iso format string
        if expires_at:
            expires_str = utils.isotime(at=expires_at, subsecond=True)
        else:
            expires_str = None

        return super(ApplicationCredentialManager, self).create(
            name=name,
            secret=secret,
            description=description,
            expires_at=expires_str,
            roles=role_list,
            unrestricted=unrestricted,
            access_rules=access_rules,
            **kwargs)

    def get(self, application_credential, user=None):
        """Retrieve an application credential.

        :param application_credential: the credential to be retrieved from the
            server
        :type applicationcredential: str or
            :class:`keystoneclient.v3.application_credentials.ApplicationCredential`

        :returns: the specified application credential
        :rtype:
            :class:`keystoneclient.v3.application_credentials.ApplicationCredential`

        """
        user = user or self.client.user_id
        self.base_url = '/users/%(user)s' % {'user': user}

        return super(ApplicationCredentialManager, self).get(
            application_credential_id=base.getid(application_credential))

    def list(self, user=None, **kwargs):
        """List application credentials.

        :param string user: User ID

        :returns: a list of application credentials
        :rtype: list of
            :class:`keystoneclient.v3.application_credentials.ApplicationCredential`
        """
        user = user or self.client.user_id
        self.base_url = '/users/%(user)s' % {'user': user}

        return super(ApplicationCredentialManager, self).list(**kwargs)

    def find(self, user=None, **kwargs):
        """Find an application credential with attributes matching ``**kwargs``.

        :param string user: User ID

        :returns: a list of matching application credentials
        :rtype: list of
            :class:`keystoneclient.v3.application_credentials.ApplicationCredential`
        """
        user = user or self.client.user_id
        self.base_url = '/users/%(user)s' % {'user': user}

        return super(ApplicationCredentialManager, self).find(**kwargs)

    def delete(self, application_credential, user=None):
        """Delete an application credential.

        :param application_credential: the application credential to be deleted
        :type credential: str or
            :class:`keystoneclient.v3.application_credentials.ApplicationCredential`

        :returns: response object with 204 status
        :rtype: :class:`requests.models.Response`

        """
        user = user or self.client.user_id
        self.base_url = '/users/%(user)s' % {'user': user}

        return super(ApplicationCredentialManager, self).delete(
            application_credential_id=base.getid(application_credential))

    def update(self):
        raise exceptions.MethodNotImplemented(
            _('Application credentials are immutable, updating is not'
              ' supported.'))