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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
|
# 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.
"""Client for V1 API."""
import collections
import logging
import time
import warnings
from ironic_inspector_client.common import http
from ironic_inspector_client.common.i18n import _
DEFAULT_API_VERSION = (1, 0)
"""Server API version used by default."""
MAX_API_VERSION = (1, 13)
"""Maximum API version this client was designed to work with.
This does not mean that other versions won't work at all - the server might
still support them.
"""
# using huge timeout by default, as precise timeout should be set in
# ironic-inspector settings
DEFAULT_RETRY_INTERVAL = 10
"""Default interval (in seconds) between retries when waiting for introspection
to finish."""
DEFAULT_MAX_RETRIES = 3600
"""Default number of retries when waiting for introspection to finish."""
LOG = logging.getLogger(__name__)
class WaitTimeoutError(Exception):
"""Timeout while waiting for nodes to finish introspection."""
class ClientV1(http.BaseClient):
"""Client for API v1.
Create this object to use Python API, for example::
import ironic_inspector_client
client = ironic_inspector_client.ClientV1(session=keystone_session)
This code creates a client with API version *1.0* and a given Keystone
`session
<https://docs.openstack.org/keystoneauth/latest/using-sessions.html>`_.
The service URL is fetched from the service catalog in this case. Optional
arguments ``service_type``, ``interface`` and ``region_name`` can be
provided to modify how the URL is looked up.
If the catalog lookup fails, the local host with port 5050 is tried.
However, this behaviour is deprecated and should not be relied on.
Also an explicit ``inspector_url`` can be passed to bypass service catalog.
Optional ``api_version`` argument is a minimum API version that a server
must support. It can be a tuple (MAJ, MIN), string "MAJ.MIN" or integer
(only major, minimum supported minor version is assumed).
:ivar rules: Reference to the introspection rules API.
Instance of :py:class:`ironic_inspector_client.v1.RulesAPI`.
"""
def __init__(self, **kwargs):
"""Create a client.
See :py:class:`ironic_inspector_client.common.http.HttpClient` for the
list of acceptable arguments.
:param kwargs: arguments to pass to the BaseClient constructor.
api_version is set to DEFAULT_API_VERSION by default.
"""
kwargs.setdefault('api_version', DEFAULT_API_VERSION)
super(ClientV1, self).__init__(**kwargs)
self.rules = RulesAPI(self.request)
def _check_parameters(self, node_id, uuid):
"""Deprecate uuid parameters.
Check the parameters and return a deprecation warning
if the uuid parameter is present.
"""
node_id = node_id or uuid
if not isinstance(node_id, str):
raise TypeError(
_("Expected string for node_id argument, got %r") % node_id)
if uuid:
warnings.warn("Parameter uuid is deprecated and will be "
"removed in future releases, please use "
"node_id instead.", DeprecationWarning)
return node_id
def introspect(self, node_id=None, manage_boot=None, uuid=None):
"""Start introspection for a node.
:param uuid: node UUID or name, deprecated
:param node_id: node node_id or name
:param manage_boot: whether to manage boot during introspection of
this node. If it is None (the default), then this argument is not
passed to API and the server default is used instead.
:raises: :py:class:`ironic_inspector_client.ClientError` on error
reported from a server
:raises: :py:class:`ironic_inspector_client.VersionNotSupported` if
requested api_version is not supported
:raises: *requests* library exception on connection problems.
"""
node_id = self._check_parameters(node_id, uuid)
params = {}
if manage_boot is not None:
params['manage_boot'] = str(int(manage_boot))
self.request('post', '/introspection/%s' % node_id, params=params)
def reprocess(self, node_id=None, uuid=None):
"""Reprocess stored introspection data.
If swift support is disabled, introspection data won't be stored,
this request will return error response with 404 code.
:param uuid: node UUID or name, deprecated
:param node_id: node node_id or name
:raises: :py:class:`ironic_inspector_client.ClientError` on error
reported from a server
:raises: :py:class:`ironic_inspector_client.VersionNotSupported` if
requested api_version is not supported
:raises: *requests* library exception on connection problems.
:raises: TypeError if uuid is not a string.
"""
node_id = self._check_parameters(node_id, uuid)
return self.request('post',
'/introspection/%s/data/unprocessed' %
node_id)
def list_statuses(self, marker=None, limit=None):
"""List introspection statuses.
Supports pagination via the marker and limit params. The items are
sorted by the server according to the `started_at` attribute, newer
items first.
:param marker: pagination maker, UUID or None
:param limit: pagination limit, int or None
:raises: :py:class:`ironic_inspector_client.ClientError` on error
reported from a server
:raises: :py:class:`ironic_inspector_client.VersionNotSupported` if
requested api_version is not supported
:raises: *requests* library exception on connection problems.
:return: a list of status dictionaries with the keys:
* `error` an error string or None,
* `finished` whether introspection was finished,
* `finished_at` an ISO8601 timestamp or None,
* `links` with a self-link URL,
* `started_at` an ISO8601 timestamp,
* `uuid` the node UUID
"""
if not (marker is None or isinstance(marker, str)):
raise TypeError(_('Expected a string value of the marker, got '
'%s instead') % marker)
if not (limit is None or isinstance(limit, int)):
raise TypeError(_('Expected an integer value of the limit, got '
'%s instead') % limit)
params = {
'marker': marker,
'limit': limit,
}
response = self.request('get', '/introspection', params=params)
return response.json()['introspection']
def get_status(self, node_id=None, uuid=None):
"""Get introspection status for a node.
:param uuid: node UUID or name, deprecated
:param node_id: node node_id or name
:raises: :py:class:`ironic_inspector_client.ClientError` on error
reported from a server
:raises: :py:class:`ironic_inspector_client.VersionNotSupported`
if requested api_version is not supported
:raises: *requests* library exception on connection problems.
:return: dictionary with the keys:
* `error` an error string or None,
* `finished` whether introspection was finished,
* `finished_at` an ISO8601 timestamp or None,
* `links` with a self-link URL,
* `started_at` an ISO8601 timestamp,
* `uuid` the node UUID
"""
node_id = self._check_parameters(node_id, uuid)
return self.request('get', '/introspection/%s' % node_id).json()
def wait_for_finish(self, node_ids=None,
retry_interval=DEFAULT_RETRY_INTERVAL,
max_retries=DEFAULT_MAX_RETRIES,
sleep_function=time.sleep, uuids=None):
"""Wait for introspection finishing for given nodes.
:param uuids: collection of node UUIDs or names, deprecated
:param node_ids: collection of node node_ids or names
:param retry_interval: sleep interval between retries.
:param max_retries: maximum number of retries.
:param sleep_function: function used for sleeping between retries.
:raises: :py:class:`ironic_inspector_client.WaitTimeoutError` on
timeout
:raises: :py:class:`ironic_inspector_client.ClientError` on error
reported from a server
:raises: :py:class:`ironic_inspector_client.VersionNotSupported` if
requested api_version is not supported
:raises: *requests* library exception on connection problems.
:return: dictionary UUID -> status (the same as in get_status).
"""
result = {}
node_ids = node_ids or uuids
if uuids:
warnings.warn("Parameter uuid is deprecated and will be "
"removed in future releases, please use "
"node_id instead.", DeprecationWarning)
elif not node_ids:
raise TypeError("The node_ids argument is required")
# Number of attempts = number of retries + first attempt
for attempt in range(max_retries + 1):
new_active_node_ids = []
for node_id in node_ids:
status = self.get_status(node_id)
if status.get('finished'):
result[node_id] = status
else:
new_active_node_ids.append(node_id)
if new_active_node_ids:
if attempt != max_retries:
node_ids = new_active_node_ids
LOG.debug('Still waiting for introspection results for '
'%(count)d nodes, attempt %(attempt)d of '
'%(total)d',
{'count': len(new_active_node_ids),
'attempt': attempt + 1,
'total': max_retries + 1})
sleep_function(retry_interval)
else:
return result
raise WaitTimeoutError(_("Timeout while waiting for introspection "
"of nodes %s") % new_active_node_ids)
def get_data(self, node_id=None, raw=False, uuid=None, processed=True):
"""Get introspection data from the last introspection of a node.
If swift support is disabled, introspection data won't be stored,
this request will return error response with 404 code.
:param uuid: node UUID or name, deprecated
:param node_id: node node_id or name
:param raw: whether to return raw binary data or parsed JSON data
:param processed: whether to return the final processed data or the
raw unprocessed data received from the ramdisk.
:returns: bytes or a dict depending on the 'raw' argument
:raises: :py:class:`ironic_inspector_client.ClientError` on error
reported from a server
:raises: :py:class:`ironic_inspector_client.VersionNotSupported` if
requested api_version is not supported
:raises: *requests* library exception on connection problems.
:raises: TypeError if uuid is not a string
"""
node_id = self._check_parameters(node_id, uuid)
url = ('/introspection/%s/data' if processed
else '/introspection/%s/data/unprocessed')
resp = self.request('get', url % node_id)
if raw:
return resp.content
else:
return resp.json()
def abort(self, node_id=None, uuid=None):
"""Abort running introspection for a node.
:param uuid: node UUID or name, deprecated
:param node_id: node node_id or name
:raises: :py:class:`ironic_inspector_client.ClientError` on error
reported from a server
:raises: :py:class:`ironic_inspector_client.VersionNotSupported` if
requested api_version is not supported
:raises: *requests* library exception on connection problems.
:raises: TypeError if uuid is not a string.
"""
node_id = self._check_parameters(node_id, uuid)
return self.request('post', '/introspection/%s/abort' % node_id)
def get_interface_data(self, node_ident, interface, field_sel):
"""Get interface data for the input node and interface
To get LLDP data, collection must be enabled by the kernel parameter
``ipa-collect-lldp=1``, and a relevant inspector plugin must be
enabled, e.g., ``lldp_basic``, ``local_link_connection``.
:param node_ident: node UUID or name
:param interface: interface name
:param field_sel: list of all fields for which to get data
:returns: interface data in OrderedDict
:raises: ValueError if interface is not found.
"""
# Use OrderedDict to maintain order of user-entered fields
iface_data = collections.OrderedDict()
data = self.get_data(node_ident)
all_interfaces = data.get('all_interfaces', [])
# Make sure interface name is valid
if interface not in all_interfaces:
raise ValueError(
_("Interface %s was not found on this node")
% interface)
# If lldp data not available this will still return interface,
# mac, node_ident etc.
lldp_proc = all_interfaces[interface].get('lldp_processed', {})
for f in field_sel:
if f == 'node_ident':
iface_data[f] = node_ident
elif f == 'interface':
iface_data[f] = interface
elif f == 'mac':
iface_data[f] = all_interfaces[interface].get(f)
elif f == 'switch_port_vlan_ids':
iface_data[f] = [item['id'] for item in
lldp_proc.get('switch_port_vlans', [])]
else:
iface_data[f] = lldp_proc.get(f)
return iface_data
def get_all_interface_data(self, node_ident,
field_sel, vlan=None):
"""Get interface data for all of the interfaces on this node
:param node_ident: node UUID or name
:param field_sel: list of all fields for which to get data
:param vlan: list of vlans used to filter the lists returned
:returns: list of interface data, each interface in a list
"""
# Get inventory data for this node
data = self.get_data(node_ident)
all_interfaces = data.get('all_interfaces', [])
rows = []
if vlan:
vlan = set(vlan)
# walk all interfaces, appending data to row if not filtered
for interface in all_interfaces:
iface_dict = self.get_interface_data(node_ident,
interface,
field_sel)
values = list(iface_dict.values())
# Use (optional) vlans to filter row
if not vlan:
rows.append(values)
continue
# curr_vlans may be None
curr_vlans = iface_dict.get('switch_port_vlan_ids', [])
if curr_vlans and (vlan & set(curr_vlans)):
rows.append(values) # vlan matches, display this row
return rows
class RulesAPI(object):
"""Introspection rules API.
Do not create instances of this class directly, use
:py:attr:`ironic_inspector_client.v1.ClientV1.rules` instead.
"""
def __init__(self, requester):
self._request = requester
def create(self, conditions, actions, uuid=None, description=None):
"""Create a new introspection rule.
:param conditions: list of rule conditions
:param actions: list of rule actions
:param uuid: rule UUID, will be generated if not specified
:param description: optional rule description
:returns: rule representation
:raises: :py:class:`ironic_inspector_client.ClientError` on error
reported from a server
:raises: :py:class:`ironic_inspector_client.VersionNotSupported` if
requested api_version is not supported
"""
if uuid is not None and not isinstance(uuid, str):
raise TypeError(
_("Expected string for uuid argument, got %r") % uuid)
for name, arg in [('conditions', conditions), ('actions', actions)]:
if not isinstance(arg, list) or not all(isinstance(x, dict)
for x in arg):
raise TypeError(_("Expected list of dicts for %(arg)s "
"argument, got %(real)r"),
{'arg': name, 'real': arg})
body = {'uuid': uuid, 'conditions': conditions, 'actions': actions,
'description': description}
return self.from_json(body)
def from_json(self, json_rule):
"""Import an introspection rule from JSON data.
:param json_rule: rule information as a dict with keys matching
arguments of :py:meth:`RulesAPI.create`.
:returns: rule representation
:raises: :py:class:`ironic_inspector_client.ClientError` on error
reported from a server
:raises: :py:class:`ironic_inspector_client.VersionNotSupported` if
requested api_version is not supported
"""
return self._request('post', '/rules', json=json_rule).json()
def get_all(self):
"""List all introspection rules.
:returns: list of short rule representations (uuid, description
and links)
:raises: :py:class:`ironic_inspector_client.ClientError` on error
reported from a server
:raises: :py:class:`ironic_inspector_client.VersionNotSupported` if
requested api_version is not supported
"""
return self._request('get', '/rules').json()['rules']
def get(self, uuid):
"""Get detailed information about an introspection rule.
:param uuid: rule UUID
:returns: rule representation
:raises: :py:class:`ironic_inspector_client.ClientError` on error
reported from a server
:raises: :py:class:`ironic_inspector_client.VersionNotSupported` if
requested api_version is not supported
"""
if not isinstance(uuid, str):
raise TypeError(
_("Expected string for uuid argument, got %r") % uuid)
return self._request('get', '/rules/%s' % uuid).json()
def delete(self, uuid):
"""Delete an introspection rule.
:param uuid: rule UUID
:raises: :py:class:`ironic_inspector_client.ClientError` on error
reported from a server
:raises: :py:class:`ironic_inspector_client.VersionNotSupported` if
requested api_version is not supported
"""
if not isinstance(uuid, str):
raise TypeError(
_("Expected string for uuid argument, got %r") % uuid)
self._request('delete', '/rules/%s' % uuid)
def delete_all(self):
"""Delete all introspection rules.
:raises: :py:class:`ironic_inspector_client.ClientError` on error
reported from a server
:raises: :py:class:`ironic_inspector_client.VersionNotSupported` if
requested api_version is not supported
"""
self._request('delete', '/rules')
|