Prevent multiple communication nics for one instance
authorLisa Velden <velden@google.com>
Tue, 23 Jun 2015 15:16:42 +0000 (17:16 +0200)
committerHrvoje Ribicic <riba@google.com>
Wed, 24 Jun 2015 11:38:42 +0000 (11:38 +0000)
Check if a nic name is already in the list of all nics before adding it.
Expand the instance name before that check to ensure that we are always
checking for the correct name.

Signed-off-by: Lisa Velden <velden@google.com>
Reviewed-by: Hrvoje Ribicic <riba@google.com>

lib/cmdlib/instance.py

index d5b2c6b..200abca 100644 (file)
@@ -439,15 +439,28 @@ class LUInstanceCreate(LogicalUnit):
       raise errors.OpPrereqError("Cannot do IP address check without a name"
                                  " check", errors.ECODE_INVAL)
 
+    # instance name verification
+    if self.op.name_check:
+      self.hostname = _CheckHostnameSane(self, self.op.instance_name)
+      self.op.instance_name = self.hostname.name
+      # used in CheckPrereq for ip ping check
+      self.check_ip = self.hostname.ip
+    else:
+      self.check_ip = None
+
     # add NIC for instance communication
     if self.op.instance_communication:
       nic_name = _ComputeInstanceCommunicationNIC(self.op.instance_name)
 
-      self.op.nics.append({constants.INIC_NAME: nic_name,
-                           constants.INIC_MAC: constants.VALUE_GENERATE,
-                           constants.INIC_IP: constants.NIC_IP_POOL,
-                           constants.INIC_NETWORK:
-                             self.cfg.GetInstanceCommunicationNetwork()})
+      for nic in self.op.nics:
+        if nic.get(constants.INIC_NAME, None) == nic_name:
+          break
+      else:
+        self.op.nics.append({constants.INIC_NAME: nic_name,
+                             constants.INIC_MAC: constants.VALUE_GENERATE,
+                             constants.INIC_IP: constants.NIC_IP_POOL,
+                             constants.INIC_NETWORK:
+                               self.cfg.GetInstanceCommunicationNetwork()})
 
     # timeouts for unsafe OS installs
     if self.op.helper_startup_timeout is None:
@@ -467,15 +480,6 @@ class LUInstanceCreate(LogicalUnit):
     self._CheckDiskArguments()
     assert self.op.disk_template is not None
 
-    # instance name verification
-    if self.op.name_check:
-      self.hostname = _CheckHostnameSane(self, self.op.instance_name)
-      self.op.instance_name = self.hostname.name
-      # used in CheckPrereq for ip ping check
-      self.check_ip = self.hostname.ip
-    else:
-      self.check_ip = None
-
     # file storage checks
     if (self.op.file_driver and
         not self.op.file_driver in constants.FILE_DRIVER):