Feature 5650. Allow set VM interface ip-address
[osm/openvim.git] / osm_openvim / vim_db.py
index 5fc7c91..ec29943 100644 (file)
@@ -136,8 +136,10 @@ class vim_db():
                 
         self.logger.error("%s DB Exception %s. Command %s",func, str(e), cmd)
         if type(e[0]) is str:
-            if e[0][-5:] == "'con'": return -HTTP_Internal_Server_Error, "DB Exception, no connection."
-            else: raise
+            if e[0][-5:] == "'con'":
+                return -HTTP_Internal_Server_Error, "DB Exception, no connection."
+            else:
+                return -HTTP_Internal_Server_Error, e.args[1]
         if e.args[0]==2006 or e.args[0]==2013 : #MySQL server has gone away (((or)))    Exception 2013: Lost connection to MySQL server during query
             #reconnect
             self.connect()
@@ -1426,8 +1428,18 @@ class vim_db():
                             del iface["cidr"]
                             del iface["enable_dhcp"]
                             used_dhcp_ips = self._get_dhcp_ip_used_list(iface["net_id"])
-                            iface["ip_address"] = self.get_free_ip_from_range(dhcp_first_ip, dhcp_last_ip,
+                            if iface.get("ip_address"):
+                                if iface["ip_address"] in used_dhcp_ips:
+                                    iface["ip_address"] = None
+                            else:
+                                iface["ip_address"] = self.get_free_ip_from_range(dhcp_first_ip, dhcp_last_ip,
                                                                               dhcp_cidr, used_dhcp_ips)
+                            if 'links' in iface:
+                                del iface['links']
+                            if 'dns' in iface:
+                                del iface['dns']
+                            if 'routes' in iface:
+                                del iface['routes']
 
                         iface['uuid'] = str(myUuid.uuid1()) # create_uuid
                         cmd = "INSERT INTO uuids (uuid, root_uuid, used_at) VALUES ('%s','%s', 'ports')" % (iface['uuid'], uuid)
@@ -1443,6 +1455,7 @@ class vim_db():
                         else:
                             iface['mac'] = iface['mac_address']
                             del iface['mac_address']
+
                         #iface['mac']=iface.pop('mac_address', None)  #for leaving mac generation to libvirt
                         keys    = ",".join(iface.keys())
                         values  = ",".join( map(lambda x: "Null" if x is None else "'"+str(x)+"'", iface.values() ) )
@@ -1552,6 +1565,7 @@ class vim_db():
 
         ip_used_list.append(str(ips[1]))  # gw ip
         ip_used_list.append(str(ips[-1]))  # broadcast ip
+        ip_used_list.append(first_ip)
 
         for vm_ip in ips:
             if str(vm_ip) not in ip_used_list and IPAddress(first_ip) <= IPAddress(vm_ip) <= IPAddress(last_ip):