Move NetworkDetails to a separate module

Partially-Implements: blueprint json-network-config
Change-Id: I4d375cba9601e5c05ab9b9591f9e9bb6f3be3d86
This commit is contained in:
Alessandro Pilotti 2018-08-21 17:22:24 +03:00
parent 0d04300138
commit bde2be7e7d
10 changed files with 51 additions and 35 deletions

View File

@ -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

View File

@ -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,

View File

View File

@ -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",
]
)

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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)