]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Allow instance NetworkInterfaces to be list of str or dict
authorSteve Baker <sbaker@redhat.com>
Wed, 6 Mar 2013 02:29:12 +0000 (15:29 +1300)
committerSteve Baker <sbaker@redhat.com>
Wed, 6 Mar 2013 02:29:12 +0000 (15:29 +1300)
It should be possible to specify NetworkInterfaces of an instance either
as an array of strings or as an array of dicts.

The following should now be possible:
This will define nics in the order specified by DeviceIndex
"NetworkInterfaces" : [
  { "NetworkInterfaceId" : {"Ref" : "controlXface"}, "DeviceIndex" : "0" },
  { "NetworkInterfaceId" : {"Ref" : "controlXface2"}, "DeviceIndex" : "1" },
],

This will define nics in the order specified by list position
"NetworkInterfaces" : [{"Ref" : "controlXface"}, {"Ref" : "controlXface2"}],

Mixing the 2 forms is supported but the resulting order is not defined.

Amazon documentation is ambigious about which form is actually supported
http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html#cfn-ec2-instance-networkinterfaces
http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-ec2.html#cfn-template-snippets-eni
Fixes: Bug #1096013
Change-Id: Id387d2e750cbe533a9b8a1fedc49d5bcf690fbcf

heat/engine/resources/instance.py
heat/tests/test_instance.py

index eb2bf053ecec979ccba359c423460fd751645ae6..5e068cbdff4818c7d6378cc744c06edc3a79d5f5 100644 (file)
@@ -216,6 +216,23 @@ class Instance(resource.Resource):
 
         return self.mime_string
 
+    @staticmethod
+    def _build_nics(network_interfaces):
+        if not network_interfaces:
+            return None
+
+        nics = []
+        for nic in network_interfaces:
+            if isinstance(nic, basestring):
+                nics.append({
+                    'NetworkInterfaceId': nic,
+                    'DeviceIndex': len(nics)})
+            else:
+                nics.append(nic)
+        sorted_nics = sorted(nics, key=lambda nic: int(nic['DeviceIndex']))
+
+        return [{'port-id': nic['NetworkInterfaceId']} for nic in sorted_nics]
+
     def handle_create(self):
         if self.properties.get('SecurityGroups') is None:
             security_groups = None
@@ -267,12 +284,7 @@ class Instance(resource.Resource):
         else:
             scheduler_hints = None
 
-        nics = []
-        if self.properties['NetworkInterfaces']:
-            for nic in self.properties['NetworkInterfaces']:
-                nics.append({'port-id': nic})
-        else:
-            nics = None
+        nics = self._build_nics(self.properties['NetworkInterfaces'])
 
         server_userdata = self._build_userdata(userdata)
         server = None
index 1884a019b80e5a61b6d2a34aaf2fbce40b645f45..fcc22c863f33a7b066b5a38d5c13ef2d55d64054 100644 (file)
@@ -181,3 +181,32 @@ class instancesTest(unittest.TestCase):
         self.assertEqual(instance.update(update_template),
                          instance.UPDATE_COMPLETE)
         self.assertEqual(instance.metadata, {'test': 123})
+
+    def test_build_nics(self):
+        self.assertEqual(None, instances.Instance._build_nics([]))
+        self.assertEqual(None, instances.Instance._build_nics(None))
+        self.assertEqual([
+            {'port-id': 'id3'}, {'port-id': 'id1'}, {'port-id': 'id2'}],
+            instances.Instance._build_nics([
+                'id3', 'id1', 'id2']))
+        self.assertEqual([
+            {'port-id': 'id1'},
+            {'port-id': 'id2'},
+            {'port-id': 'id3'}], instances.Instance._build_nics([
+                {'NetworkInterfaceId': 'id3', 'DeviceIndex': '3'},
+                {'NetworkInterfaceId': 'id1', 'DeviceIndex': '1'},
+                {'NetworkInterfaceId': 'id2', 'DeviceIndex': 2},
+            ]))
+        self.assertEqual([
+            {'port-id': 'id1'},
+            {'port-id': 'id2'},
+            {'port-id': 'id3'},
+            {'port-id': 'id4'},
+            {'port-id': 'id5'}
+        ], instances.Instance._build_nics([
+            {'NetworkInterfaceId': 'id3', 'DeviceIndex': '3'},
+            {'NetworkInterfaceId': 'id1', 'DeviceIndex': '1'},
+            {'NetworkInterfaceId': 'id2', 'DeviceIndex': 2},
+            'id4',
+            'id5'
+        ]))