summaryrefslogtreecommitdiff
path: root/keystoneclient/v3/limits.py
blob: 5d298a4a3e0a81d997df8d13c4be6506a3b5a425 (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
#    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 keystoneclient import base


class Limit(base.Resource):
    """Represents a project limit.

    Attributes:
      * id: a UUID that identifies the project limit
      * service_id: a UUID that identifies the service for the limit
      * region_id: a UUID that identifies the region for the limit
      * project_id: a UUID that identifies the project for the limit
      * resource_name: the name of the resource to limit
      * resource_limit: the limit to apply to the project
      * description: a description for the project limit

    """

    pass


class LimitManager(base.CrudManager):
    """Manager class for project limits."""

    resource_class = Limit
    collection_key = 'limits'
    key = 'limit'

    def create(self, project, service, resource_name, resource_limit,
               description=None, region=None, **kwargs):
        """Create a project-specific limit.

        :param project: the project to create a limit for.
        :type project: str or :class:`keystoneclient.v3.projects.Project`
        :param service: the service that owns the resource to limit.
        :type service: str or :class:`keystoneclient.v3.services.Service`
        :param resource_name: the name of the resource to limit
        :type resource_name: str
        :param resource_limit: the quantity of the limit
        :type resource_limit: int
        :param description: a description of the limit
        :type description: str
        :param region: region the limit applies to
        :type region: str or :class:`keystoneclient.v3.regions.Region`

        :returns: a reference of the created limit
        :rtype: :class:`keystoneclient.v3.limits.Limit`

        """
        limit_data = base.filter_none(
            project_id=base.getid(project),
            service_id=base.getid(service),
            resource_name=resource_name,
            resource_limit=resource_limit,
            description=description,
            region_id=base.getid(region),
            **kwargs
        )
        body = {self.collection_key: [limit_data]}
        resp, body = self.client.post('/limits', body=body)
        limit = body[self.collection_key].pop()
        return self.resource_class(self, limit)

    def update(self, limit, project=None, service=None, resource_name=None,
               resource_limit=None, description=None, **kwargs):
        """Update a project-specific limit.

        :param limit: a limit to update
        :param project: the project ID of the limit to update
        :type project: str or :class:`keystoneclient.v3.projects.Project`
        :param resource_limit: the limit of the limit's resource to update
        :type: resource_limit: int
        :param description: a description of the limit
        :type description: str

        :returns: a reference of the updated limit.
        :rtype: :class:`keystoneclient.v3.limits.Limit`

        """
        return super(LimitManager, self).update(
            limit_id=base.getid(limit),
            project_id=base.getid(project),
            service_id=base.getid(service),
            resource_name=resource_name,
            resource_limit=resource_limit,
            description=description,
            **kwargs
        )

    def get(self, limit):
        """Retrieve a project limit.

        :param limit:
            the project-specific limit to be retrieved.
        :type limit:
            str or :class:`keystoneclient.v3.limit.Limit`

        :returns: a project-specific limit
        :rtype: :class:`keystoneclient.v3.limit.Limit`

        """
        return super(LimitManager, self).get(limit_id=base.getid(limit))

    def list(self, service=None, region=None, resource_name=None, **kwargs):
        """List project-specific limits.

        Any parameter provided will be passed to the server as a filter

        :param service: service to filter limits by
        :type service: UUID or :class:`keystoneclient.v3.services.Service`
        :param region: region to filter limits by
        :type region: UUID or :class:`keystoneclient.v3.regions.Region`
        :param resource_name: the name of the resource to filter limits by
        :type resource_name: str

        :returns: a list of project-specific limits.
        :rtype: list of :class:`keystoneclient.v3.limits.Limit`

        """
        return super(LimitManager, self).list(
            service_id=base.getid(service),
            region_id=base.getid(region),
            resource_name=resource_name,
            **kwargs
        )

    def delete(self, limit):
        """Delete a project-specific limit.

        :param limit: the project-specific limit to be deleted.
        :type limit: str or :class:`keystoneclient.v3.limit.Limit`

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

        """
        return super(LimitManager, self).delete(limit_id=base.getid(limit))