File: manager.py

package info (click to toggle)
python-sushy 5.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 2,620 kB
  • sloc: python: 14,026; makefile: 24; sh: 2
file content (277 lines) | stat: -rw-r--r-- 10,598 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
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
# 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.
# https://redfish.dmtf.org/schemas/Manager.v1_4_0.json

import logging

from sushy import exceptions
from sushy.resources import base
from sushy.resources import common
from sushy.resources import constants as res_cons
from sushy.resources.manager import constants as mgr_cons
from sushy.resources.manager import virtual_media
from sushy.resources.system import ethernet_interface
from sushy import utils


LOG = logging.getLogger(__name__)


class ActionsField(base.CompositeField):
    reset = common.ResetActionField('#Manager.Reset')


class RemoteAccessField(base.CompositeField):
    service_enabled = base.Field('ServiceEnabled')

    max_concurrent_sessions = base.Field('MaxConcurrentSessions')

    connect_types_supported = base.Field('ConnectTypesSupported',
                                         adapter=list)


class Manager(base.ResourceBase):

    auto_dst_enabled = base.Field('AutoDSTEnabled')
    """Indicates whether the manager is configured for automatic DST
        adjustment"""

    firmware_version = base.Field('FirmwareVersion')
    """The manager firmware version"""

    graphical_console = RemoteAccessField('GraphicalConsole')
    """A dictionary containing the remote access support service via
       graphical console (e.g. KVMIP) and max concurrent sessions
    """

    serial_console = RemoteAccessField('SerialConsole')
    """A dictionary containing the remote access support service via
       serial console (e.g. Telnet, SSH, IPMI) and max concurrent sessions
    """

    command_shell = RemoteAccessField('CommandShell')
    """A dictionary containing the remote access support service via
       command shell (e.g. Telnet, SSH) and max concurrent sessions
    """

    description = base.Field('Description')
    """The manager description"""

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

    name = base.Field('Name')
    """The manager name"""

    model = base.Field('Model')
    """The manager model"""

    manager_type = base.MappedField('ManagerType', mgr_cons.ManagerType)
    """The manager type"""

    uuid = base.Field('UUID')
    """The manager UUID"""

    _actions = ActionsField('Actions')

    def __init__(self, connector, identity, redfish_version=None,
                 registries=None, root=None):
        """A class representing a Manager

        :param connector: A Connector instance
        :param identity: The identity of the Manager resource
        :param redfish_version: The version of RedFish. Used to construct
            the object according to schema of the given version.
        :param registries: Dict of Redfish Message Registry objects to be
            used in any resource that needs registries to parse messages
        :param root: Sushy root object. Empty for Sushy root itself.
        """
        super().__init__(
            connector, identity, redfish_version=redfish_version,
            registries=registries, root=root)

    def get_supported_graphical_console_types(self):
        """Get the supported values for Graphical Console connection types.

        :returns: A set of supported values.
        """
        if (not self.graphical_console
                or not self.graphical_console.connect_types_supported):
            LOG.warning('Could not figure out the supported values for '
                        'remote access via graphical console for Manager %s',
                        self.identity)
            return set(mgr_cons.GraphicalConnectType)

        return {v for v in mgr_cons.GraphicalConnectType
                if v.value in self.graphical_console.connect_types_supported}

    def get_supported_serial_console_types(self):
        """Get the supported values for Serial Console connection types.

        :returns: A set of supported values.
        """
        if (not self.serial_console
                or not self.serial_console.connect_types_supported):
            LOG.warning('Could not figure out the supported values for '
                        'remote access via serial console for Manager %s',
                        self.identity)
            return set(mgr_cons.SerialConnectType)

        return {v for v in mgr_cons.SerialConnectType
                if v.value in self.serial_console.connect_types_supported}

    def get_supported_command_shell_types(self):
        """Get the supported values for Command Shell connection types.

        :returns: A set of supported values.
        """
        if (not self.command_shell
                or not self.command_shell.connect_types_supported):
            LOG.warning('Could not figure out the supported values for '
                        'remote access via command shell for Manager %s',
                        self.identity)
            return set(mgr_cons.CommandConnectType)

        return {v for v in mgr_cons.CommandConnectType
                if v.value in self.command_shell.connect_types_supported}

    def _get_reset_action_element(self):
        reset_action = self._actions.reset

        if not reset_action:
            raise exceptions.MissingActionError(action='#Manager.Reset',
                                                resource=self._path)
        return reset_action

    def get_allowed_reset_manager_values(self):
        """Get the allowed values for resetting the manager.

        :returns: A set of allowed values.
        :raises: MissingAttributeError, if Actions/#Manager.Reset attribute
            not present.
        """
        reset_action = self._get_reset_action_element()

        if not reset_action.allowed_values:
            LOG.warning('Could not figure out the allowed values for the '
                        'reset manager action for Manager %s', self.identity)
            return set(res_cons.ResetType)

        return {v for v in res_cons.ResetType
                if v.value in reset_action.allowed_values}

    def reset_manager(self, value):
        """Reset the manager.

        :param value: The target value.
        :raises: InvalidParameterValueError, if the target value is not
            allowed.
        """
        valid_resets = self.get_allowed_reset_manager_values()
        if value not in valid_resets:
            raise exceptions.InvalidParameterValueError(
                parameter='value', value=value, valid_values=valid_resets)

        value = res_cons.ResetType(value).value
        target_uri = self._get_reset_action_element().target_uri

        LOG.debug('Resetting the Manager %s ...', self.identity)
        self._conn.post(target_uri, data={'ResetType': value})
        LOG.info('The Manager %s is being reset', self.identity)

    @property
    @utils.cache_it
    def virtual_media(self):
        return virtual_media.VirtualMediaCollection(
            self._conn, utils.get_sub_resource_path_by(self, 'VirtualMedia'),
            redfish_version=self.redfish_version, registries=self.registries,
            root=self.root)

    @property
    @utils.cache_it
    def systems(self):
        """A list of systems managed by this manager.

        Returns a list of `System` objects representing systems being
        managed by this manager.

        :raises: MissingAttributeError if '@odata.id' field is missing.
        :returns: A list of `System` instances
        """
        paths = utils.get_sub_resource_path_by(
            self, ["Links", "ManagerForServers"], is_collection=True)

        from sushy.resources.system import system
        return [system.System(self._conn, path,
                              redfish_version=self.redfish_version,
                              registries=self.registries, root=self.root)
                for path in paths]

    @property
    @utils.cache_it
    def chassis(self):
        """A list of chassis managed by this manager.

        Returns a list of `Chassis` objects representing the chassis
        or cabinets managed by this manager.

        :raises: MissingAttributeError if '@odata.id' field is missing.
        :returns: A list of `Chassis` instances
        """
        paths = utils.get_sub_resource_path_by(
            self, ["Links", "ManagerForChassis"], is_collection=True)

        from sushy.resources.chassis import chassis
        return [chassis.Chassis(self._conn, path,
                                redfish_version=self.redfish_version,
                                registries=self.registries, root=self.root)
                for path in paths]

    @property
    @utils.cache_it
    def ethernet_interfaces(self):
        """Property to reference `EthernetInterfaceCollection` instance

        It is set once when the first time it is queried. On refresh,
        this property is marked as stale (greedy-refresh not done).
        Here the actual refresh of the sub-resource happens, if stale.
        """
        return ethernet_interface.EthernetInterfaceCollection(
            self._conn,
            utils.get_sub_resource_path_by(self, "EthernetInterfaces"),
            redfish_version=self.redfish_version,
            registries=self.registries, root=self.root)


class ManagerCollection(base.ResourceCollectionBase):

    @property
    def _resource_type(self):
        return Manager

    def __init__(self, connector, path, redfish_version=None, registries=None,
                 root=None):
        """A class representing a ManagerCollection

        :param connector: A Connector instance
        :param path: The canonical path to the Manager collection resource
        :param redfish_version: The version of RedFish. Used to construct
            the object according to schema of the given version.
        :param registries: Dict of Redfish Message Registry objects to be
            used in any resource that needs registries to parse messages
        :param root: Sushy root object. Empty for Sushy root itself.
        """
        super().__init__(
            connector, path, redfish_version=redfish_version,
            registries=registries, root=root)