Ubuntu Pastebin

Paste from smoser at Thu, 7 Sep 2017 16:32:11 +0000

Download as text
 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
diff --git a/cloudinit/sources/DataSourceEc2.py b/cloudinit/sources/DataSourceEc2.py
index dfab55ce..be2991a2 100644
--- a/cloudinit/sources/DataSourceEc2.py
+++ b/cloudinit/sources/DataSourceEc2.py
@@ -27,6 +27,8 @@ SKIP_METADATA_URL_CODES = frozenset([uhelp.NOT_FOUND])
 STRICT_ID_PATH = ("datasource", "Ec2", "strict_id")
 STRICT_ID_DEFAULT = "warn"
 
+_unset = "_unset"
+
 
 class Platforms(object):
     ALIYUN = "AliYun"
@@ -284,10 +286,20 @@ class DataSourceEc2(sources.DataSource):
     @property
     def network_config(self):
         """Return a network config dict for rendering ENI or netplan files."""
-        if self._network_config is None:
+        if self._network_config == _unset;
+            result = None
             if self.metadata is not None:
-                self._network_config = convert_ec2_metadata_network_config(
-                    self.metadata)
+                # i changed the signature of convert_ec2_metadata_network_config
+                # to take just the network portion
+                net_md = self.metadata.get('network')
+                if isinstance(net_md, dict):
+                    result = convert_ec2_metadata_network_config(net_md)
+                else:
+                    LOG.warn("unexpected metadata 'network' key not valid: %s", net_md)
+            else:
+                LOG.warn("metadata was None?")
+            self._network_config = result
+
         return self._network_config
 
     def _crawl_metadata(self):
@@ -442,10 +454,13 @@ def _collect_platform_data():
     return data
 
 
-def convert_ec2_metadata_network_config(metadata=None, macs_to_nics=None):
+def convert_ec2_metadata_network_config(network_md=None, macs_to_nics=None):
     """Convert ec2 metadata to network config version 1 data dict.
 
-    @param: metadata: Dictionary of metadata crawled from EC2 metadata url.
+    @param: network_md: 'network' portion of EC2 metadata.
+       generally formed as {"interfaces": {"macs": {}} where
+       'macs' is a dictionary with mac address as key and contents like:
+       {"device-number": "0", "interface-id": "...", "local-ipv4s": ...}
     @param: macs_to_name: Optional dict mac addresses and the nic name. If
        not provided, get_interfaces_by_mac is called to get it from the OS.
 
@@ -454,7 +469,7 @@ def convert_ec2_metadata_network_config(metadata=None, macs_to_nics=None):
     netcfg = {'version': 1, 'config': []}
     if not macs_to_nics:
         macs_to_nics = net.get_interfaces_by_mac()
-    macs_metadata = metadata['network']['interfaces']['macs']
+    macs_metadata = network_md['interfaces']['macs']
     for mac, nic_name in macs_to_nics.items():
         nic_metadata = macs_metadata.get(mac)
         if not nic_metadata:
Download as text