1# Copyright 2015 Cloudbase Solutions Srl
2# All Rights Reserved.
3#
4#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5#    not use this file except in compliance with the License. You may obtain
6#    a copy of the License at
7#
8#         http://www.apache.org/licenses/LICENSE-2.0
9#
10#    Unless required by applicable law or agreed to in writing, software
11#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13#    License for the specific language governing permissions and limitations
14#    under the License.
15
16"""
17Utility class for VM related operations on Hyper-V.
18"""
19
20import sys
21
22from os_win._i18n import _
23
24# Define WMI specific exceptions, so WMI won't have to be imported in any
25# module that expects those exceptions.
26if sys.platform == 'win32':
27    from six.moves.builtins import WindowsError
28    import wmi
29
30    x_wmi = wmi.x_wmi
31    x_wmi_timed_out = wmi.x_wmi_timed_out
32else:
33    class WindowsError(Exception):
34        def __init__(self, winerror=None):
35            self.winerror = winerror
36
37    class x_wmi(Exception):
38        def __init__(self, info='', com_error=None):
39            super(x_wmi, self).__init__(info)
40            self.info = info
41            self.com_error = com_error
42
43    class x_wmi_timed_out(x_wmi):
44        pass
45
46
47class OSWinException(Exception):
48    msg_fmt = 'An exception has been encountered.'
49
50    def __init__(self, message=None, **kwargs):
51        self.kwargs = kwargs
52        self.error_code = kwargs.get('error_code')
53
54        if not message:
55            message = self.msg_fmt % kwargs
56
57        self.message = message
58        super(OSWinException, self).__init__(message)
59
60
61class NotFound(OSWinException):
62    msg_fmt = _("Resource could not be found: %(resource)s")
63
64
65class PciDeviceNotFound(NotFound):
66    msg_fmt = _("No assignable PCI device with vendor id: %(vendor_id)s and "
67                "product id: %(product_id)s was found.")
68
69
70class HyperVException(OSWinException):
71    pass
72
73
74# TODO(alexpilotti): Add a storage exception base class
75class VHDResizeException(HyperVException):
76    msg_fmt = _("Exception encountered while resizing the VHD %(vhd_path)s."
77                "Reason: %(reason)s")
78
79
80class HyperVAuthorizationException(HyperVException):
81    msg_fmt = _("The Windows account running nova-compute on this Hyper-V "
82                "host doesn't have the required permissions to perform "
83                "Hyper-V related operations.")
84
85
86class HyperVVMNotFoundException(NotFound, HyperVException):
87    msg_fmt = _("VM not found: %(vm_name)s")
88
89
90class HyperVPortNotFoundException(NotFound, HyperVException):
91    msg_fmt = _("Switch port not found: %(port_name)s")
92
93
94class HyperVvNicNotFound(NotFound, HyperVException):
95    msg_fmt = _("vNic not found: %(vnic_name)s")
96
97
98class HyperVvSwitchNotFound(NotFound, HyperVException):
99    msg_fmt = _("vSwitch not found: %(vswitch_name)s.")
100
101
102class Invalid(OSWinException):
103    pass
104
105
106class UnsupportedOperation(Invalid):
107    msg_fmt = _("The operation failed due to the reason: %(reason)s")
108
109
110class InvalidParameterValue(Invalid):
111    msg_fmt = _("Invalid parameter value for: "
112                "%(param_name)s=%(param_value)s")
113
114
115class InvalidVMVersion(Invalid):
116    msg_fmt = _("VM '%(vm_name)s' has an invalid version for this operation: "
117                "%(version)s. Version is expected to be between: "
118                "%(min_version)s and %(max_version)s.")
119
120
121class SMBException(OSWinException):
122    pass
123
124
125class Win32Exception(OSWinException):
126    msg_fmt = _("Executing Win32 API function %(func_name)s failed. "
127                "Error code: %(error_code)s. "
128                "Error message: %(error_message)s")
129
130
131class VHDException(OSWinException):
132    pass
133
134
135class VHDWin32APIException(VHDException, Win32Exception):
136    pass
137
138
139class FCException(OSWinException):
140    pass
141
142
143class FCWin32Exception(FCException, Win32Exception):
144    pass
145
146
147class WMIException(OSWinException):
148    def __init__(self, message=None, wmi_exc=None):
149        if wmi_exc:
150            try:
151                wmi_exc_message = wmi_exc.com_error.excepinfo[2].strip()
152                message = "%s WMI exception message: %s" % (message,
153                                                            wmi_exc_message)
154            except AttributeError:
155                pass
156            except IndexError:
157                pass
158        super(WMIException, self).__init__(message)
159
160
161class WqlException(OSWinException):
162    pass
163
164
165class ISCSITargetException(OSWinException):
166    pass
167
168
169class ISCSITargetWMIException(ISCSITargetException, WMIException):
170    pass
171
172
173class ISCSIInitiatorAPIException(Win32Exception):
174    pass
175
176
177class ISCSILunNotAvailable(ISCSITargetException):
178    msg_fmt = _("Could not find lun %(target_lun)s "
179                "for iSCSI target %(target_iqn)s.")
180
181
182class Win32IOException(Win32Exception):
183    pass
184
185
186class DiskNotFound(NotFound):
187    pass
188
189
190class HyperVRemoteFXException(HyperVException):
191    pass
192
193
194class HyperVClusterException(HyperVException):
195    pass
196
197
198class DNSException(OSWinException):
199    pass
200
201
202class Timeout(OSWinException):
203    msg_fmt = _("Timed out waiting for the specified resource.")
204
205
206class DNSZoneNotFound(NotFound, DNSException):
207    msg_fmt = _("DNS Zone not found: %(zone_name)s")
208
209
210class DNSZoneAlreadyExists(DNSException):
211    msg_fmt = _("DNS Zone already exists: %(zone_name)s")
212
213
214class WMIJobFailed(HyperVException):
215    msg_fmt = _("WMI job failed with status %(job_state)s. "
216                "Error summary description: %(error_summ_desc)s. "
217                "Error description: %(error_desc)s "
218                "Error code: %(error_code)s.")
219
220    def __init__(self, message=None, **kwargs):
221        self.error_code = kwargs.get('error_code', None)
222        self.job_state = kwargs.get('job_state', None)
223
224        super(WMIJobFailed, self).__init__(message, **kwargs)
225
226
227class JobTerminateFailed(HyperVException):
228    msg_fmt = _("Could not terminate the requested job(s).")
229
230
231class ClusterException(OSWinException):
232    pass
233
234
235class ClusterObjectNotFound(NotFound, ClusterException):
236    pass
237
238
239class ClusterWin32Exception(ClusterException, Win32Exception):
240    pass
241
242
243class ClusterGroupMigrationFailed(ClusterException):
244    msg_fmt = _("Failed to migrate cluster group %(group_name)s. "
245                "Expected state %(expected_state)s. "
246                "Expected owner node: %(expected_node)s. "
247                "Current group state: %(group_state)s. "
248                "Current owner node: %(owner_node)s.")
249
250
251class ClusterGroupMigrationTimeOut(ClusterGroupMigrationFailed):
252    msg_fmt = _("Cluster group '%(group_name)s' migration "
253                "timed out after %(time_elapsed)0.3fs. ")
254
255
256class ClusterPropertyRetrieveFailed(ClusterException):
257    msg_fmt = _("Failed to retrieve a cluster property.")
258
259
260class ClusterPropertyListEntryNotFound(ClusterPropertyRetrieveFailed):
261    msg_fmt = _("The specified cluster property list does not contain "
262                "an entry named '%(property_name)s'")
263
264
265class ClusterPropertyListParsingError(ClusterPropertyRetrieveFailed):
266    msg_fmt = _("Parsing a cluster property list failed.")
267
268
269class SCSIPageParsingError(Invalid):
270    msg_fmt = _("Parsing SCSI Page %(page)s failed. "
271                "Reason: %(reason)s.")
272
273
274class SCSIIdDescriptorParsingError(Invalid):
275    msg_fmt = _("Parsing SCSI identification descriptor failed. "
276                "Reason: %(reason)s.")
277
278
279class ResourceUpdateError(OSWinException):
280    msg_fmt = _("Failed to update the specified resource.")
281
282
283class DiskUpdateError(OSWinException):
284    msg_fmt = _("Failed to update the specified disk.")
285