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 <[email protected]>
---
 lib/cmdlib/instance.py | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)

diff --git a/lib/cmdlib/instance.py b/lib/cmdlib/instance.py
index d5b2c6b..e545445 100644
--- a/lib/cmdlib/instance.py
+++ b/lib/cmdlib/instance.py
@@ -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 and nic[constants.INIC_NAME] == 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):
-- 
2.4.3.573.g4eafbef

Reply via email to