File: simple_storage.py

package info (click to toggle)
python-sushy 5.10.0-4
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 3,224 kB
  • sloc: python: 17,702; makefile: 30; sh: 2
file content (107 lines) | stat: -rw-r--r-- 3,671 bytes parent folder | download
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
#    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.

# This is referred from Redfish standard schema.
# http://redfish.dmtf.org/schemas/v1/SimpleStorage.v1_2_0.json

import logging

from sushy.resources import base
from sushy.resources import common
from sushy.resources import constants as res_cons
from sushy import utils


LOG = logging.getLogger(__name__)


class DeviceListField(base.ListField):
    """The storage device/s associated with SimpleStorage."""

    name = base.Field('Name', required=True)
    """The name of the storage device"""

    model = base.Field('Model')
    """The model of the storage device"""

    capacity_bytes = base.Field('CapacityBytes', adapter=utils.int_or_none)
    """The size of the storage device."""

    status = common.StatusField('Status')
    """Describes the status and health of a storage device."""


class SimpleStorage(base.ResourceBase):
    """This class represents a simple storage.

    It represents the properties of a storage controller and its
    directly-attached devices. A storage device can be a disk drive or optical
    media device.
    """

    identity = base.Field('Id', required=True)
    """The SimpleStorage identity string"""

    name = base.Field('Name')
    """The name of the resource"""

    devices = DeviceListField('Devices', default=[])
    """The storage devices associated with this resource."""


class SimpleStorageCollection(base.ResourceCollectionBase):
    """Represents a collection of simple storage associated with system."""

    @property
    def _resource_type(self):
        return SimpleStorage

    @utils.cache_it
    def get_members(self):
        """Return SimpleStorage objects using expanded JSON when available."""
        members = []
        for member in self._json['Members']:
            # If data only contains a reference (@odata.id),
            # treat as unexpanded
            if (isinstance(member, dict)
                    and list(member.keys()) == ['@odata.id']):
                return super().get_members()

            simple_storage = SimpleStorage(
                self._conn, member['@odata.id'],
                json_doc=member, redfish_version=self.redfish_version,
                registries=self.registries, root=self.root)
            members.append(simple_storage)
        return members

    @property
    @utils.cache_it
    def disks_sizes_bytes(self):
        """Sizes of each Disk in bytes in SimpleStorage collection resource.

        Returns the list of cached values until it (or its parent resource)
        is refreshed.
        """
        return sorted(device.capacity_bytes
                      for simpl_stor in self.get_members()
                      for device in simpl_stor.devices
                      if (device.status.state == res_cons.State.ENABLED
                          and device.capacity_bytes is not None))

    @property
    def max_size_bytes(self):
        """Max size available (in bytes) among all enabled Disk resources.

        Returns the cached value until it (or its parent resource) is
        refreshed.
        """
        return utils.max_safe(self.disks_sizes_bytes)