File: scope.py

package info (click to toggle)
python-cloudkittyclient 5.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 748 kB
  • sloc: python: 3,512; makefile: 26; sh: 2
file content (143 lines) | stat: -rw-r--r-- 5,456 bytes parent folder | download | duplicates (2)
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
# Copyright 2019 Objectif Libre
#
#    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 oslo_utils import strutils

from cloudkittyclient.common import base
from cloudkittyclient import exc


class ScopeManager(base.BaseManager):
    """Class used to handle /v2/scope endpoint"""

    url = '/v2/scope'

    def get_scope_state(self, **kwargs):
        """Returns a paginated list of scopes along with their state.

        Some optional filters can be provided.

        :param offset: Index of the first scope that should be returned.
        :type offset: int
        :param limit: Maximal number of scopes to return.
        :type limit: int
        :param collector: Optional collector to filter on.
        :type collector: str or list of str
        :param fetcher: Optional fetcher to filter on.
        :type fetcher: str or list of str
        :param scope_id: Optional scope_id to filter on.
        :type scope_id: str or list of str
        :param scope_key: Optional scope_key to filter on.
        :type scope_key: str or list of str
        """

        for key in ('collector', 'fetcher', 'scope_id', 'scope_key'):
            if key in kwargs.keys():
                if isinstance(kwargs[key], list):
                    kwargs[key] = ','.join(kwargs[key])

        authorized_args = [
            'offset', 'limit', 'collector', 'fetcher', 'scope_id', 'scope_key']
        url = self.get_url(None, kwargs, authorized_args=authorized_args)
        return self.api_client.get(url).json()

    def reset_scope_state(self, **kwargs):
        """Returns nothing.

        Some optional filters can be provided.
        The all_scopes and the scope_id options are mutually exclusive and one
        must be provided.

        :param state: datetime object from which the state will be reset
        :type state: datetime.datetime
        :param all_scopes: Whether all scopes must be reset
        :type all_scopes: bool
        :param collector: Optional collector to filter on.
        :type collector: str or list of str
        :param fetcher: Optional fetcher to filter on.
        :type fetcher: str or list of str
        :param scope_id: Optional scope_id to filter on.
        :type scope_id: str or list of str
        :param scope_key: Optional scope_key to filter on.
        :type scope_key: str or list of str
        """

        if not kwargs.get('state'):
            raise exc.ArgumentRequired("'state' argument is required")

        if not kwargs.get('all_scopes') and not kwargs.get('scope_id'):
            raise exc.ArgumentRequired(
                "You must specify either 'scope_id' or 'all_scopes'")

        if kwargs.get('all_scopes') and kwargs.get('scope_id'):
            raise exc.InvalidArgumentError(
                "You can't specify both 'scope_id' and 'all_scopes'")

        for key in ('collector', 'fetcher', 'scope_id', 'scope_key'):
            if key in kwargs.keys():
                if isinstance(kwargs[key], list):
                    kwargs[key] = ','.join(kwargs[key])

        body = dict(
            state=kwargs.get('state'),
            scope_id=kwargs.get('scope_id'),
            scope_key=kwargs.get('scope_key'),
            collector=kwargs.get('collector'),
            fetcher=kwargs.get('fetcher'),
            all_scopes=kwargs.get('all_scopes'),
        )
        # Stripping None and False values
        body = dict(filter(lambda elem: bool(elem[1]), body.items()))

        url = self.get_url(None, kwargs)
        return self.api_client.put(url, json=body)

    def update_scope(self, **kwargs):
        """Update storage scope

        The `scope_id field` is mandatory, and all other are optional. Only the
        attributes sent will be updated. The attributes that are not sent will
        not be changed in the backend.

        :param collector: collector to be used by the scope.
        :type collector: str
        :param fetcher: fetcher to be used by the scope.
        :type fetcher: str
        :param scope_id: Mandatory scope_id to update.
        :type scope_id: str
        :param scope_key: scope_key to be used by the scope.
        :type scope_key: str
        :param active: Indicates if the scope is active or not
        :type active: str
        """

        if not kwargs.get('scope_id'):
            raise exc.ArgumentRequired("'scope_id' argument is required")

        body = dict(
            scope_id=kwargs.get('scope_id'),
            scope_key=kwargs.get('scope_key'),
            collector=kwargs.get('collector'),
            fetcher=kwargs.get('fetcher')
        )

        if kwargs.get('active'):
            body['active'] = strutils.bool_from_string(
                kwargs.get('active'), strict=True)

        # Stripping None
        body = dict(filter(lambda elem: elem[1] is not None, body.items()))

        url = self.get_url(None, kwargs)
        return self.api_client.patch(url, json=body).json()