From bde2be7e7d82802fc24680d59a88343edbdd4752 Mon Sep 17 00:00:00 2001 From: Alessandro Pilotti Date: Tue, 21 Aug 2018 17:22:24 +0300 Subject: [PATCH] Move NetworkDetails to a separate module Partially-Implements: blueprint json-network-config Change-Id: I4d375cba9601e5c05ab9b9591f9e9bb6f3be3d86 --- cloudbaseinit/metadata/services/base.py | 19 ----------- .../metadata/services/opennebulaservice.py | 3 +- cloudbaseinit/models/__init__.py | 0 cloudbaseinit/models/network.py | 32 +++++++++++++++++++ cloudbaseinit/plugins/common/networkconfig.py | 6 ++-- .../services/test_baseopenstackservice.py | 5 +-- .../services/test_opennebulaservice.py | 3 +- .../plugins/common/test_networkconfig.py | 8 ++--- cloudbaseinit/tests/utils/test_debiface.py | 6 ++-- cloudbaseinit/utils/debiface.py | 4 +-- 10 files changed, 51 insertions(+), 35 deletions(-) create mode 100644 cloudbaseinit/models/__init__.py create mode 100644 cloudbaseinit/models/network.py diff --git a/cloudbaseinit/metadata/services/base.py b/cloudbaseinit/metadata/services/base.py index 9cb3c1a4..6267cc40 100644 --- a/cloudbaseinit/metadata/services/base.py +++ b/cloudbaseinit/metadata/services/base.py @@ -14,7 +14,6 @@ import abc -import collections import gzip import io import time @@ -30,24 +29,6 @@ from cloudbaseinit.utils import encoding CONF = cloudbaseinit_conf.CONF LOG = oslo_logging.getLogger(__name__) -# Both the custom service(s) and the networking plugin -# should know about the entries of these kind of objects. -NetworkDetails = collections.namedtuple( - "NetworkDetails", - [ - "name", - "mac", - "address", - "address6", - "netmask", - "netmask6", - "broadcast", - "gateway", - "gateway6", - "dnsnameservers", - ] -) - class NotExistingMetadataException(Exception): pass diff --git a/cloudbaseinit/metadata/services/opennebulaservice.py b/cloudbaseinit/metadata/services/opennebulaservice.py index abc5940b..9c8b8e78 100644 --- a/cloudbaseinit/metadata/services/opennebulaservice.py +++ b/cloudbaseinit/metadata/services/opennebulaservice.py @@ -24,6 +24,7 @@ from oslo_log import log as oslo_logging import six from cloudbaseinit.metadata.services import base +from cloudbaseinit.models import network as network_model from cloudbaseinit.osutils import factory as osutils_factory from cloudbaseinit.utils import encoding @@ -232,7 +233,7 @@ class OpenNebulaService(base.BaseMetadataService): netmask = self._calculate_netmask(address, gateway) broadcast = self._compute_broadcast(address, netmask) # gather them as namedtuple objects - details = base.NetworkDetails( + details = network_model.NetworkDetails( name=IF_FORMAT.format(iid=iid), mac=mac, address=address, diff --git a/cloudbaseinit/models/__init__.py b/cloudbaseinit/models/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/cloudbaseinit/models/network.py b/cloudbaseinit/models/network.py new file mode 100644 index 00000000..441c72c7 --- /dev/null +++ b/cloudbaseinit/models/network.py @@ -0,0 +1,32 @@ +# Copyright 2012 Cloudbase Solutions Srl +# +# 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 collections + + +NetworkDetails = collections.namedtuple( + "NetworkDetails", + [ + "name", + "mac", + "address", + "address6", + "netmask", + "netmask6", + "broadcast", + "gateway", + "gateway6", + "dnsnameservers", + ] +) diff --git a/cloudbaseinit/plugins/common/networkconfig.py b/cloudbaseinit/plugins/common/networkconfig.py index a8f4d9a9..e620ce57 100644 --- a/cloudbaseinit/plugins/common/networkconfig.py +++ b/cloudbaseinit/plugins/common/networkconfig.py @@ -18,7 +18,7 @@ import re from oslo_log import log as oslo_logging from cloudbaseinit import exception -from cloudbaseinit.metadata.services import base as service_base +from cloudbaseinit.models import network as network_model from cloudbaseinit.osutils import factory as osutils_factory from cloudbaseinit.plugins.common import base as plugin_base from cloudbaseinit.utils import network @@ -64,7 +64,7 @@ def _preprocess_nics(network_details, network_adapters): # Check and update every NetworkDetails object. total = len(network_adapters) for nic in network_details: - if not isinstance(nic, service_base.NetworkDetails): + if not isinstance(nic, network_model.NetworkDetails): raise exception.CloudbaseInitException( "invalid NetworkDetails object {!r}" .format(type(nic)) @@ -103,7 +103,7 @@ def _preprocess_nics(network_details, network_adapters): idx = _name2idx(nic.name) if not mac and idx < total: mac = network_adapters[idx][1] - nic = service_base.NetworkDetails( + nic = network_model.NetworkDetails( nic.name, mac, address, diff --git a/cloudbaseinit/tests/metadata/services/test_baseopenstackservice.py b/cloudbaseinit/tests/metadata/services/test_baseopenstackservice.py index 7b62ec66..548d6fd7 100644 --- a/cloudbaseinit/tests/metadata/services/test_baseopenstackservice.py +++ b/cloudbaseinit/tests/metadata/services/test_baseopenstackservice.py @@ -25,6 +25,7 @@ except ImportError: from cloudbaseinit import conf as cloudbaseinit_conf from cloudbaseinit.metadata.services import base from cloudbaseinit.metadata.services import baseopenstackservice +from cloudbaseinit.models import network as network_model from cloudbaseinit.tests.metadata import fake_json_response from cloudbaseinit.utils import x509constants @@ -217,7 +218,7 @@ class TestBaseOpenStackService(unittest.TestCase): self.assertIsNone(ret) return # check returned NICs details - nic0 = base.NetworkDetails( + nic0 = network_model.NetworkDetails( fake_json_response.NAME0, fake_json_response.MAC0.upper(), fake_json_response.ADDRESS0, @@ -229,7 +230,7 @@ class TestBaseOpenStackService(unittest.TestCase): fake_json_response.GATEWAY60, fake_json_response.DNSNS0.split() ) - nic1 = base.NetworkDetails( + nic1 = network_model.NetworkDetails( fake_json_response.NAME1, None, fake_json_response.ADDRESS1, diff --git a/cloudbaseinit/tests/metadata/services/test_opennebulaservice.py b/cloudbaseinit/tests/metadata/services/test_opennebulaservice.py index f321521c..5d3e96ba 100644 --- a/cloudbaseinit/tests/metadata/services/test_opennebulaservice.py +++ b/cloudbaseinit/tests/metadata/services/test_opennebulaservice.py @@ -24,6 +24,7 @@ except ImportError: from cloudbaseinit.metadata.services import base from cloudbaseinit.metadata.services import opennebulaservice +from cloudbaseinit.models import network as network_model from cloudbaseinit.tests import testutils @@ -104,7 +105,7 @@ OPEN = mock.mock_open(read_data=CONTEXT.encode()) def _get_nic_details(iid=0): - details = base.NetworkDetails( + details = network_model.NetworkDetails( opennebulaservice.IF_FORMAT.format(iid=iid), MAC, ADDRESS, diff --git a/cloudbaseinit/tests/plugins/common/test_networkconfig.py b/cloudbaseinit/tests/plugins/common/test_networkconfig.py index 44c6f02b..0d4ba1d2 100644 --- a/cloudbaseinit/tests/plugins/common/test_networkconfig.py +++ b/cloudbaseinit/tests/plugins/common/test_networkconfig.py @@ -22,7 +22,7 @@ except ImportError: import mock from cloudbaseinit import exception -from cloudbaseinit.metadata.services import base as service_base +from cloudbaseinit.models import network as network_model from cloudbaseinit.plugins.common import base as plugin_base from cloudbaseinit.plugins.common import networkconfig from cloudbaseinit.tests import testutils @@ -163,7 +163,7 @@ class TestNetworkConfigPlugin(unittest.TestCase): self._network_details = [] for ind in range(self._count): adapter = (adapters_names[ind], macs[ind]) - nic = service_base.NetworkDetails( + nic = network_model.NetworkDetails( details_names[ind], None if no_macs else macs[ind], addresses[ind], @@ -219,7 +219,7 @@ class TestNetworkConfigPlugin(unittest.TestCase): def test_execute_missing_all(self): nic = self._network_details[0] - nic = service_base.NetworkDetails( + nic = network_model.NetworkDetails( nic.name, "00" + nic.mac[2:], nic.address, @@ -240,7 +240,7 @@ class TestNetworkConfigPlugin(unittest.TestCase): gateway=False, fail=False): ind = self._count - 1 nic = self._network_details[ind] - nic2 = service_base.NetworkDetails( + nic2 = network_model.NetworkDetails( None if name else nic.name, None if mac else nic.mac, None if address else nic.address, diff --git a/cloudbaseinit/tests/utils/test_debiface.py b/cloudbaseinit/tests/utils/test_debiface.py index f3cd1ac7..db2b3d1f 100644 --- a/cloudbaseinit/tests/utils/test_debiface.py +++ b/cloudbaseinit/tests/utils/test_debiface.py @@ -15,7 +15,7 @@ import unittest -from cloudbaseinit.metadata.services import base as service_base +from cloudbaseinit.models import network as network_model from cloudbaseinit.tests.metadata import fake_json_response from cloudbaseinit.tests import testutils from cloudbaseinit.utils import debiface @@ -39,7 +39,7 @@ class TestInterfacesParser(unittest.TestCase): self.assertFalse(nics) return # check what we've got - nic0 = service_base.NetworkDetails( + nic0 = network_model.NetworkDetails( fake_json_response.NAME0, fake_json_response.MAC0.upper(), fake_json_response.ADDRESS0, @@ -51,7 +51,7 @@ class TestInterfacesParser(unittest.TestCase): fake_json_response.GATEWAY60, fake_json_response.DNSNS0.split() ) - nic1 = service_base.NetworkDetails( + nic1 = network_model.NetworkDetails( fake_json_response.NAME1, None, fake_json_response.ADDRESS1, diff --git a/cloudbaseinit/utils/debiface.py b/cloudbaseinit/utils/debiface.py index eee9f469..bf779f5a 100644 --- a/cloudbaseinit/utils/debiface.py +++ b/cloudbaseinit/utils/debiface.py @@ -18,7 +18,7 @@ import re from oslo_log import log as oslo_logging import six -from cloudbaseinit.metadata.services import base as service_base +from cloudbaseinit.models import network as network_model LOG = oslo_logging.getLogger(__name__) @@ -102,7 +102,7 @@ def _add_nic(iface, nics): return # no information gathered LOG.debug("Found new interface: %s", iface) # Each missing detail is marked as None. - nic = service_base.NetworkDetails(**iface) + nic = network_model.NetworkDetails(**iface) nics.append(nic)