Merge "Add BasePhysnetHook"
This commit is contained in:
commit
fae5103660
106
ironic_inspector/plugins/base_physnet.py
Normal file
106
ironic_inspector/plugins/base_physnet.py
Normal file
@ -0,0 +1,106 @@
|
||||
# Copyright (c) 2017 StackHPC Ltd.
|
||||
#
|
||||
# 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.
|
||||
|
||||
import abc
|
||||
|
||||
from openstack import exceptions
|
||||
from oslo_config import cfg
|
||||
|
||||
from ironic_inspector.plugins import base
|
||||
from ironic_inspector import utils
|
||||
|
||||
LOG = utils.getProcessingLogger(__name__)
|
||||
|
||||
CONF = cfg.CONF
|
||||
|
||||
|
||||
class BasePhysnetHook(base.ProcessingHook):
|
||||
"""Base class for plugins that assign a physical network to ports.
|
||||
|
||||
The mechanism for mapping a port to a physical network should be provided
|
||||
by a subclass via the get_physnet() method.
|
||||
"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_physnet(self, port, iface_name, introspection_data):
|
||||
"""Return a physical network to apply to a port.
|
||||
|
||||
Subclasses should implement this method to determine how to map a port
|
||||
to a physical network.
|
||||
|
||||
:param port: The ironic port to patch.
|
||||
:param iface_name: Name of the interface.
|
||||
:param introspection_data: Introspection data.
|
||||
:returns: The physical network to set, or None.
|
||||
"""
|
||||
|
||||
def _get_physnet_patch(self, physnet, port):
|
||||
"""Return a patch to update the port's physical network.
|
||||
|
||||
:param physnet: The physical network to set.
|
||||
:param port: The ironic port to patch.
|
||||
:returns: A dict to be used as a patch for the port, or None.
|
||||
"""
|
||||
# Bug is here.
|
||||
if (not CONF.processing.overwrite_existing or
|
||||
port.physical_network == physnet):
|
||||
return
|
||||
return {'op': 'add', 'path': '/physical_network', 'value': physnet}
|
||||
|
||||
def before_update(self, introspection_data, node_info, **kwargs):
|
||||
"""Process introspection data and patch port physical network."""
|
||||
inventory = utils.get_inventory(introspection_data)
|
||||
|
||||
LOG.info("Plugin: %s", type(self))
|
||||
|
||||
ironic_ports = node_info.ports()
|
||||
|
||||
for iface in inventory['interfaces']:
|
||||
if iface['name'] not in introspection_data['all_interfaces']:
|
||||
continue
|
||||
|
||||
mac_address = iface['mac_address']
|
||||
port = ironic_ports.get(mac_address)
|
||||
if not port:
|
||||
LOG.debug("Skipping physical network processing for interface "
|
||||
"%s, matching port not found in Ironic.",
|
||||
mac_address,
|
||||
node_info=node_info, data=introspection_data)
|
||||
continue
|
||||
|
||||
# Determine the physical network for this port.
|
||||
# Port not touched in here.
|
||||
physnet = self.get_physnet(port, iface['name'], introspection_data)
|
||||
if physnet is None:
|
||||
LOG.debug("Skipping physical network processing for interface "
|
||||
"%s, no physical network mapping",
|
||||
mac_address,
|
||||
node_info=node_info, data=introspection_data)
|
||||
continue
|
||||
|
||||
patch = self._get_physnet_patch(physnet, port)
|
||||
if patch is None:
|
||||
LOG.debug("Skipping physical network processing for interface "
|
||||
"%s, no update required",
|
||||
mac_address,
|
||||
node_info=node_info, data=introspection_data)
|
||||
continue
|
||||
|
||||
try:
|
||||
node_info.patch_port(port, [patch])
|
||||
except exceptions.BadRequestException as e:
|
||||
LOG.warning("Failed to update port %(uuid)s: %(error)s",
|
||||
{'uuid': port.uuid, 'error': e},
|
||||
node_info=node_info)
|
104
ironic_inspector/test/unit/test_plugins_base_physnet.py
Normal file
104
ironic_inspector/test/unit/test_plugins_base_physnet.py
Normal file
@ -0,0 +1,104 @@
|
||||
# Copyright (c) 2017 StackHPC Ltd.
|
||||
#
|
||||
# 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.
|
||||
|
||||
import mock
|
||||
from oslo_config import cfg
|
||||
|
||||
from ironic_inspector import node_cache
|
||||
from ironic_inspector.plugins import base_physnet
|
||||
from ironic_inspector.test import base as test_base
|
||||
from ironic_inspector import utils
|
||||
|
||||
|
||||
class FakePortPhysnetHook(base_physnet.BasePhysnetHook):
|
||||
|
||||
def get_physnet(self, port, iface_name, introspection_data):
|
||||
return
|
||||
|
||||
|
||||
class TestBasePortPhysnetHook(test_base.NodeTest):
|
||||
hook = FakePortPhysnetHook()
|
||||
|
||||
def setUp(self):
|
||||
super(TestBasePortPhysnetHook, self).setUp()
|
||||
self.data = {
|
||||
'inventory': {
|
||||
'interfaces': [{
|
||||
'name': 'em1', 'mac_address': '11:11:11:11:11:11',
|
||||
'ipv4_address': '1.1.1.1',
|
||||
}],
|
||||
'cpu': 1,
|
||||
'disks': 1,
|
||||
'memory': 1
|
||||
},
|
||||
'all_interfaces': {
|
||||
'em1': {},
|
||||
}
|
||||
}
|
||||
|
||||
ports = [mock.Mock(spec=['address', 'uuid', 'physical_network'],
|
||||
address=a, physical_network='physnet1')
|
||||
for a in ('11:11:11:11:11:11',)]
|
||||
self.node_info = node_cache.NodeInfo(uuid=self.uuid, started_at=0,
|
||||
node=self.node, ports=ports)
|
||||
|
||||
@mock.patch.object(node_cache.NodeInfo, 'patch_port')
|
||||
@mock.patch.object(FakePortPhysnetHook, 'get_physnet')
|
||||
def test_expected_data(self, mock_get, mock_patch):
|
||||
patches = [
|
||||
{'path': '/physical_network',
|
||||
'value': 'physnet2', 'op': 'add'},
|
||||
]
|
||||
mock_get.return_value = 'physnet2'
|
||||
self.hook.before_update(self.data, self.node_info)
|
||||
port = list(self.node_info.ports().values())[0]
|
||||
mock_get.assert_called_once_with(port, 'em1', self.data)
|
||||
self.assertCalledWithPatch(patches, mock_patch)
|
||||
|
||||
@mock.patch.object(node_cache.NodeInfo, 'patch_port')
|
||||
@mock.patch.object(FakePortPhysnetHook, 'get_physnet')
|
||||
def test_noop(self, mock_get, mock_patch):
|
||||
mock_get.return_value = 'physnet1'
|
||||
self.hook.before_update(self.data, self.node_info)
|
||||
self.assertFalse(mock_patch.called)
|
||||
|
||||
@mock.patch.object(node_cache.NodeInfo, 'patch_port')
|
||||
def test_no_mapping(self, mock_patch):
|
||||
self.hook.physnet = None
|
||||
self.hook.before_update(self.data, self.node_info)
|
||||
self.assertFalse(mock_patch.called)
|
||||
|
||||
@mock.patch.object(node_cache.NodeInfo, 'patch_port')
|
||||
def test_interface_not_in_all_interfaces(self, mock_patch):
|
||||
self.data['all_interfaces'] = {}
|
||||
self.hook.before_update(self.data, self.node_info)
|
||||
self.assertFalse(mock_patch.called)
|
||||
|
||||
@mock.patch.object(node_cache.NodeInfo, 'patch_port')
|
||||
def test_interface_not_in_ironic(self, mock_patch):
|
||||
self.node_info._ports = {}
|
||||
self.hook.before_update(self.data, self.node_info)
|
||||
self.assertFalse(mock_patch.called)
|
||||
|
||||
def test_no_inventory(self):
|
||||
del self.data['inventory']
|
||||
self.assertRaises(utils.Error, self.hook.before_update,
|
||||
self.data, self.node_info)
|
||||
|
||||
@mock.patch.object(node_cache.NodeInfo, 'patch_port')
|
||||
def test_no_overwrite(self, mock_patch):
|
||||
cfg.CONF.set_override('overwrite_existing', False, group='processing')
|
||||
self.hook.before_update(self.data, self.node_info)
|
||||
self.assertFalse(mock_patch.called)
|
4
releasenotes/notes/physnet-base-4499ad3a7c08725f.yaml
Normal file
4
releasenotes/notes/physnet-base-4499ad3a7c08725f.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
---
|
||||
features:
|
||||
- Added base class (``BasePhysnetHook``) for plugins that assign a physical
|
||||
network to ports.
|
Loading…
Reference in New Issue
Block a user