← Back to team overview

cloud-init-dev team mailing list archive

[Merge] ~smoser/cloud-init:feature/digital-ocean-more-strict into cloud-init:master


Scott Moser has proposed merging ~smoser/cloud-init:feature/digital-ocean-more-strict into cloud-init:master.

Commit message:
DigitalOcean: be more strict in network configuration conversion.

a.) warn on a 'nic' type in metadata that is unknown.
b.) raise exception if metadata defines a nic that is not present.

Requested reviews:
  Ben Howard (utlemming)
  cloud init development team (cloud-init-dev)

For more details, see:
Your team cloud init development team is requested to review the proposed merge of ~smoser/cloud-init:feature/digital-ocean-more-strict into cloud-init:master.
diff --git a/cloudinit/sources/helpers/digitalocean.py b/cloudinit/sources/helpers/digitalocean.py
index 72f7bde..76ae410 100644
--- a/cloudinit/sources/helpers/digitalocean.py
+++ b/cloudinit/sources/helpers/digitalocean.py
@@ -136,11 +136,19 @@ def convert_network_configuration(config, dns_servers):
         mac_address = nic.get('mac')
         sysfs_name = macs_to_nics.get(mac_address)
+        if mac_address not in macs_to_nics:
+            raise RuntimeError(
+                "Did not find network interface on system with mac '%s'."
+                "Cannot apply configuration: %s" % (mac_address, nic))
         nic_type = nic.get('type', 'unknown')
         # Note: the entry 'public' above contains a list, but
         # the list will only ever have one nic inside it per digital ocean.
         # If it ever had more than one nic, then this code would
         # assign all 'public' the same name.
+        if nic_type not in NIC_MAP:
+            LOG.warn("Unknown / unexpected nic type '%s': %s" %
+                     (nic_type, nic))
         if_name = NIC_MAP.get(nic_type, sysfs_name)
         LOG.debug("mapped %s interface to %s, assigning name of %s",
diff --git a/tests/unittests/test_datasource/test_digitalocean.py b/tests/unittests/test_datasource/test_digitalocean.py
index 61d6e00..e9edbf1 100644
--- a/tests/unittests/test_datasource/test_digitalocean.py
+++ b/tests/unittests/test_datasource/test_digitalocean.py
@@ -330,4 +330,11 @@ class TestNetworkConvert(TestCase):
             sorted(['', '']),
             sorted([i['address'] for i in eth0['subnets']]))
+    @mock.patch('cloudinit.net.get_interfaces_by_mac')
+    def test_missing_nic_raises_runtime(self, m_get_interfaces_by_mac):
+        m_get_interfaces_by_mac.return_value = {'04:01:57:d1:9e:02': 'eth0'}
+        self.assertRaises(
+            RuntimeError, digitalocean.convert_network_configuration,
+            *[DO_META_2['interfaces'], DO_META_2['dns']['nameservers']])
 # vi: ts=4 expandtab