File: settings.py

package info (click to toggle)
python-sushy 5.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,036 kB
  • sloc: python: 16,382; makefile: 24; sh: 2
file content (203 lines) | stat: -rw-r--r-- 6,525 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
# 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/Settings.v1_2_0.json

import logging

from dateutil import parser

from sushy.resources import base
from sushy.resources import common
from sushy.resources import constants as res_cons
from sushy.resources.registry import message_registry

# Settings update statuses

UPDATE_UNKNOWN = 0
"""Update status unknown"""

UPDATE_SUCCESS = 1
"""Update was successful"""

UPDATE_FAILURE = 2
"""Update encountered errors"""

UPDATE_PENDING = 3
"""Update waiting for being applied"""

NO_UPDATES = 4
"""No updates made"""


class SettingsUpdate:
    """Contains Settings update status and details of the update"""

    def __init__(self, status, messages):
        self._status = status
        self._messages = messages

    @property
    def status(self):
        """The status of the update"""
        return self._status

    @property
    def messages(self):
        """List of :class:`.MessageListField` with messages from the update"""
        return self._messages


LOG = logging.getLogger(__name__)


class MaintenanceWindowField(base.CompositeField):

    maintenance_window_duration_in_seconds = base.Field(
        'MaintenanceWindowDurationInSeconds',
        required=True)
    """The expiry time of maintenance window in seconds"""

    maintenance_window_start_time = base.Field(
        'MaintenanceWindowStartTime',
        required=True,
        adapter=parser.parse)
    """The start time of a maintenance window"""


class SettingsApplyTimeField(base.CompositeField):
    def __init__(self):
        super().__init__(
            path="@Redfish.SettingsApplyTime")

    apply_time = base.Field('ApplyTime', adapter=str)
    """When the future configuration should be applied"""

    apply_time_allowable_values = base.Field(
        'ApplyTime@Redfish.AllowableValues', adapter=list)
    """The list of allowable ApplyTime values"""

    maintenance_window_start_time = base.Field('MaintenanceWindowStartTime',
                                               adapter=parser.parse)
    """The start time of a maintenance window"""

    maintenance_window_duration_in_seconds = base.Field(
        'MaintenanceWindowDurationInSeconds', adapter=int)
    """The expiry time of maintenance window in seconds"""


class SettingsField(base.CompositeField):
    """The settings of a resource

    Represents the future state and configuration of the resource. The
    field is added to resources that support future state and
    configuration.

    This field includes several properties to help clients monitor when
    the resource is consumed by the service and determine the results of
    applying the values, which may or may not have been successful.
    """

    def __init__(self):
        super().__init__(path="@Redfish.Settings")

    time = base.Field('Time')
    """Indicates the time the settings were applied to the server"""

    _etag = base.Field('ETag')
    """The ETag of the resource to which the settings were applied,
    after the application
    """

    _settings_object_idref = common.IdRefField("SettingsObject")
    """Reference to the resource the client may PUT/PATCH in order
    to change this resource
    """

    _supported_apply_times = base.MappedListField(
        'SupportedApplyTimes',
        res_cons.ApplyTime)
    """List of supported apply times"""

    @property
    def maintenance_window(self):
        """MaintenanceWindow field

        Indicates if a given resource has a maintenance window assignment
        for applying settings or operations
        """
        LOG.warning('The @Redfish.MaintenanceWindow annotation does not '
                    'appear within @Redfish.Settings. Instead use the '
                    'maintenance_window property in the target resource '
                    '(e.g. System resource)')
        return None

    messages = base.MessageListField("Messages")
    """Represents the results of the last time the values of the Settings
    resource were applied to the server"""

    @property
    def operation_apply_time_support(self):
        """OperationApplyTimeSupport field

        Indicates if a client is allowed to request for a specific apply
        time of a create, delete, or action operation of a given resource
        """
        LOG.warning('Redfish ApplyTime annotations do not appear within '
                    '@Redfish.Settings. Instead use the apply_time_settings '
                    'property in the target resource (e.g. Bios resource)')
        return None

    def commit(self, connector, value):
        """Commits new settings values

        The new values will be applied when the system or a service
        restarts.

        :param connector: A Connector instance
        :param value: Value representing JSON whose structure is specific
            to each resource and the caller must format it correctly
        :returns: Response object
        """

        return connector.patch(self.resource_uri, data=value, etag=self._etag)

    @property
    def resource_uri(self):
        return self._settings_object_idref.resource_uri

    def get_status(self, registries):
        """Determines the status of last update based

        Uses message id-s and severity to determine the status.

        :param registries: registries to use to parse message
        :returns: :class:`.SettingsUpdate` object containing status
            and any messages
        """

        if not self.time:
            return SettingsUpdate(NO_UPDATES, None)

        parsed_msgs = []
        for m in self.messages:
            parsed_msgs.append(
                message_registry.parse_message(registries, m))
        any_errors = any(m for m in parsed_msgs
                         if m.severity != res_cons.Severity.OK)

        if any_errors:
            status = UPDATE_FAILURE
        else:
            status = UPDATE_SUCCESS
        return SettingsUpdate(status, parsed_msgs)