File: utils.py

package info (click to toggle)
python-sushy 5.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,620 kB
  • sloc: python: 14,026; makefile: 24; sh: 2
file content (397 lines) | stat: -rw-r--r-- 13,470 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# Copyright 2017 Red Hat, Inc.
# All Rights Reserved.
#
#    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 collections
import functools
import logging
import threading

from sushy import exceptions
from sushy.resources import constants as res_cons

LOG = logging.getLogger(__name__)

CACHE_ATTR_NAMES_VAR_NAME = '_cache_attr_names'


def revert_dictionary(dictionary):
    """Given a dictionary revert it's mapping

    :param dictionary: A dictionary to be reverted
    :returns: A dictionary with the keys and values reverted

    """
    return {v: k for k, v in dictionary.items()}


def get_members_identities(members):
    """Extract and return a tuple of members identities

    :param members: A list of members in JSON format
    :returns: A tuple containing the members paths

    """
    members_list = []
    for member in members:
        path = member.get('@odata.id')
        if not path:
            LOG.warning('Could not find the \'@odata.id\' attribute for '
                        'member %s', member)
            continue
        members_list.append(path.rstrip('/'))

    return tuple(members_list)


def int_or_none(x):
    """Given a value x it cast as int or None

    :param x: The value to transform and return
    :returns: Either None or x cast to an int

    """
    if x is None:
        return None
    return int(x)


def bool_or_none(x):
    """Given a value x this method returns either a bool or None

    :param x: The value to transform and return
    :returns: Either None or x cast to a bool

    """
    if x is None:
        return None
    return bool(x)


def get_sub_resource_path_by(resource, subresource_name, is_collection=False):
    """Helper function to find the subresource path

    :param resource: ResourceBase instance on which the name
        gets queried upon.
    :param subresource_name: name of the resource field to
        fetch the '@odata.id' from.
    :param is_collection: if `True`, expect a list of resources to
        fetch the '@odata.id' from.
    :returns: Resource path (if `is_collection` is `False`) or
        a list of resource paths (if `is_collection` is `True`).
    """
    if not subresource_name:
        raise ValueError('"subresource_name" cannot be empty')

    if not isinstance(subresource_name, list):
        subresource_name = [subresource_name]

    body = resource.json
    for path_item in subresource_name:
        body = body.get(path_item, {})

    if not body:
        raise exceptions.MissingAttributeError(
            attribute='/'.join(subresource_name), resource=resource.path)

    elements = []

    try:
        if is_collection:
            for element in body:
                elements.append(element['@odata.id'])
            return elements

        return body['@odata.id']

    except (TypeError, KeyError):
        attribute = '/'.join(subresource_name)
        if is_collection:
            attribute += f'[{len(elements)}]'
        attribute += '/@odata.id'
        raise exceptions.MissingAttributeError(
            attribute=attribute, resource=resource.path)


def max_safe(iterable, default=0):
    """Helper wrapper over builtin max() function.

    This function is just a wrapper over builtin max() w/o ``key`` argument.
    The ``default`` argument specifies an object to return if the provided
    ``iterable`` is empty. Also it filters out the None type values.

    :param iterable: an iterable
    :param default: 0 by default
    """

    try:
        return max(x for x in iterable if x is not None)
    except ValueError:
        # TypeError is not caught here as that should be thrown.
        return default


def setdefaultattr(obj, name, default):
    """Python's ``dict.setdefault`` applied on Python objects.

    If name is an attribute with obj, return its value. If not, set name
    attribute with a value of default and return default.

    :param obj: a python object
    :param name: name of attribute
    :param default: default value to be set
    """

    try:
        return getattr(obj, name)
    except AttributeError:
        setattr(obj, name, default)
    return default


def cache_it(res_accessor_method):
    """Utility decorator to cache the return value of the decorated method.

    This decorator is to be used with any Sushy resource class method.
    This will internally create an attribute on the resource namely
    ``_cache_<decorated_method_name>``. This is referred to as the "caching
    attribute". This attribute will eventually hold the resultant value from
    the method invocation (when method gets first time called) and for every
    subsequent calls to that method this cached value will get returned. It
    expects the decorated method to contain its own logic of evaluation.

    This also assigns a variable named ``_cache_attr_names`` on the resource.
    This variable maintains a collection of all the existing
    "caching attribute" names.

    To invalidate or clear the cache use :py:func:`~cache_clear`.
    Usage:

    .. code-block:: python

      class SomeResource(base.ResourceBase):
        ...
        @cache_it
        def get_summary(self):
          # do some calculation and return the result
          # and this result will be cached.
          return result
        ...
        def _do_refresh(self, force):
          cache_clear(self, force)

    If the returned value is a Sushy resource instance or a sequence whose
    element is of type Sushy resource it handles the case of calling the
    ``refresh()`` method of that resource. This is done to avoid unnecessary
    recreation of a new resource instance which got already created at the
    first place in contrast to fresh retrieval of the resource json data.
    Again, the ``force`` argument is deliberately set to False to do only the
    "light refresh" of the resource (only the fresh retrieval of resource)
    instead of doing the complete exhaustive "cascading refresh" (resource
    with all its nested subresources recursively).

    .. code-block:: python

      class SomeResource(base.ResourceBase):
        ...
        @property
        @cache_it
        def nested_resource(self):
          return NestedResource(
            self._conn, "Path/to/NestedResource",
            redfish_version=self.redfish_version)
        ...
        def _do_refresh(self, force):
          # selective attribute clearing
          cache_clear(self, force, only_these=['nested_resource'])

    Do note that this is not thread safe. So guard your code to protect it
    from any kind of concurrency issues while using this decorator.

    :param res_accessor_method: the resource accessor decorated method.

    """
    cache_attr_name = '_cache_' + res_accessor_method.__name__

    @functools.wraps(res_accessor_method)
    def func_wrapper(res_selfie):

        cache_attr_val = getattr(res_selfie, cache_attr_name, None)
        if cache_attr_val is None:

            cache_attr_val = res_accessor_method(res_selfie)
            setattr(res_selfie, cache_attr_name, cache_attr_val)

            # Note(deray): Each resource instance maintains a collection of
            # all the cache attribute names in a private attribute.
            cache_attr_names = setdefaultattr(
                res_selfie, CACHE_ATTR_NAMES_VAR_NAME, set())
            cache_attr_names.add(cache_attr_name)

        from sushy.resources import base

        if isinstance(cache_attr_val, base.ResourceBase):
            cache_attr_val.refresh(force=False)
        elif isinstance(cache_attr_val, collections.abc.Sequence):
            for elem in cache_attr_val:
                if isinstance(elem, base.ResourceBase):
                    elem.refresh(force=False)

        return cache_attr_val

    return func_wrapper


def cache_clear(res_selfie, force_refresh, only_these=None):
    """Clear some or all cached values of the resource.

    If the cache variable refers to a resource instance then the
    ``invalidate()`` method is called on that. Otherwise it is set to None.
    Should there be a need to force refresh the resource and its sub-resources,
    "cascading refresh", ``force_refresh`` is to be set to True.

    This is the complimentary method of ``cache_it`` decorator.

    :param res_selfie: the resource instance.
    :param force_refresh: force_refresh argument of ``invalidate()`` method.
    :param only_these: expects a sequence of specific method names
        for which the cached value/s need to be cleared only. When None, all
        the cached values are cleared.
    """
    cache_attr_names = setdefaultattr(
        res_selfie, CACHE_ATTR_NAMES_VAR_NAME, set())
    if only_these is not None:
        if not isinstance(only_these, collections.abc.Sequence):
            raise TypeError("'only_these' must be a sequence.")

        cache_attr_names = cache_attr_names.intersection(
            '_cache_' + attr for attr in only_these)

    for cache_attr_name in cache_attr_names:
        cache_attr_val = getattr(res_selfie, cache_attr_name)

        from sushy.resources import base

        if isinstance(cache_attr_val, base.ResourceBase):
            cache_attr_val.invalidate(force_refresh)
        elif isinstance(cache_attr_val, collections.abc.Sequence):
            for elem in cache_attr_val:
                if isinstance(elem, base.ResourceBase):
                    elem.invalidate(force_refresh)
                else:
                    setattr(res_selfie, cache_attr_name, None)
                    break
        else:
            setattr(res_selfie, cache_attr_name, None)


def camelcase_to_underscore_joined(camelcase_str):
    """Convert camelCase string to underscore_joined string

    :param camelcase_str: The camelCase string
    :returns: the equivalent underscore_joined string
    """
    if not camelcase_str:
        raise ValueError('"camelcase_str" cannot be empty')

    r = camelcase_str[0].lower()
    for i, letter in enumerate(camelcase_str[1:], 1):
        if letter.isupper():
            try:
                if (camelcase_str[i - 1].islower()
                        or camelcase_str[i + 1].islower()):
                    r += '_'
            except IndexError:
                pass

        r += letter.lower()

    return r


def synchronized(wrapped):
    """Simple synchronization decorator.

    Decorating a method like so:

    .. code-block:: python

      @synchronized
      def foo(self, *args):
        ...

    ensures that only one thread will execute the foo method at a time.
    """
    lock = threading.RLock()

    @functools.wraps(wrapped)
    def wrapper(*args, **kwargs):
        with lock:
            return wrapped(*args, **kwargs)

    return wrapper


_REMOVE = frozenset(['password', 'x-auth-token'])


def sanitize(item):
    """Remove passwords from the item."""
    if isinstance(item, dict):
        return {key: ('***' if key.lower() in _REMOVE else sanitize(value))
                for key, value in item.items()}
    else:
        return item


def process_apply_time_input(
        payload, apply_time, maint_window_start_time, maint_window_duration):
    """Validates apply time input for asynchronous operations

    :param payload: Payload for which to process apply time settings
    :param apply_time: When to update the attribute. Optional.
            An :py:class:`sushy.ApplyTime` value.
    :param maint_window_start_time: The start time of a maintenance window,
        datetime. Required when updating during maintenance window and
        default maintenance window not set by the system.
    :param maint_window_duration: Duration of maintenance time since
        maintenance window start time in seconds. Required when updating
        during maintenance window and default maintenance window not
        set by the system.

    :raises ValueError: When input apply time settings incorrect
    :returns: Payload with adjusted apply time settings if valid
    """

    if (not apply_time
            and (maint_window_start_time or maint_window_duration)):
        raise ValueError('"apply_time" missing when passing maintenance '
                         'window settings')
    if apply_time:
        prop = '@Redfish.SettingsApplyTime'
        payload[prop] = {
            '@odata.type': '#Settings.v1_0_0.PreferredApplyTime',
            'ApplyTime': res_cons.ApplyTime(apply_time).value,
        }
        if maint_window_start_time and not maint_window_duration:
            raise ValueError('"maint_window_duration" missing')
        if not maint_window_start_time and maint_window_duration:
            raise ValueError('"maint_window_start_time" missing')
        if maint_window_start_time and maint_window_duration:
            payload[prop]['MaintenanceWindowStartTime'] =\
                maint_window_start_time.isoformat()
            payload[prop]['MaintenanceWindowDurationInSeconds'] =\
                maint_window_duration

    return payload