Add traceback to all APIs
authorBin Yang <bin.yang@windriver.com>
Mon, 27 Mar 2017 05:53:37 +0000 (13:53 +0800)
committerBin Yang <bin.yang@windriver.com>
Mon, 27 Mar 2017 05:53:37 +0000 (13:53 +0800)
fix a bug in vport API as well.

Change-Id: I2e4db0b1576a3208285df59db8a238315dd48f91
Issue-Id: MULTIVIM-55
Signed-off-by: Bin Yang <bin.yang@windriver.com>
20 files changed:
kilo/kilo/requests/views/flavor.py
kilo/kilo/requests/views/hosts.py
kilo/kilo/requests/views/image.py
kilo/kilo/requests/views/limits.py
kilo/kilo/requests/views/network.py
kilo/kilo/requests/views/server.py
kilo/kilo/requests/views/subnet.py
kilo/kilo/requests/views/tenants.py
kilo/kilo/requests/views/volume.py
kilo/kilo/requests/views/vport.py
newton/newton/requests/views/flavor.py
newton/newton/requests/views/hosts.py
newton/newton/requests/views/image.py
newton/newton/requests/views/limits.py
newton/newton/requests/views/network.py
newton/newton/requests/views/server.py
newton/newton/requests/views/subnet.py
newton/newton/requests/views/tenants.py
newton/newton/requests/views/volume.py
newton/newton/requests/views/vport.py

index 2e2194b..fb8fb69 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 import logging
 import json
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -118,6 +119,7 @@ class Flavors(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
@@ -240,6 +242,8 @@ class Flavors(APIView):
 
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
+
             if sess and resp and resp.status_code == 200:
                 self.delete_flavor(sess, flavorid)
 
@@ -306,6 +310,7 @@ class Flavors(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
index b9d0ed1..dfd3451 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 import logging
 import json
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -76,6 +77,7 @@ class Hosts(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
index 339c73e..c8f66b2 100644 (file)
@@ -15,6 +15,7 @@ import logging
 import json
 import urllib2
 import threading
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -65,6 +66,7 @@ class imageThread (threading.Thread):
             logger.debug("response status code of transfer_image %s" % resp.status_code)
             return None
         except Exception as e:
+            logger.error(traceback.format_exc())
             logger.debug("Failed to transfer_image:%s" % str(e))
             return None
         pass
@@ -89,6 +91,7 @@ class Images(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
@@ -201,6 +204,7 @@ class Images(APIView):
             return Response(data={'error': 'image is not accessible:%s' % str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
@@ -220,6 +224,7 @@ class Images(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
index fce584f..ee44124 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 import logging
 import json
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -75,6 +76,7 @@ class Limits(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
index c013348..eb45189 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 import logging
 import json
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -48,6 +49,7 @@ class Networks(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
@@ -131,6 +133,7 @@ class Networks(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
@@ -153,6 +156,7 @@ class Networks(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
index 69216bf..75b0a87 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 import logging
 import json
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -79,6 +80,7 @@ class Servers(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
@@ -237,6 +239,7 @@ class Servers(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
@@ -256,6 +259,7 @@ class Servers(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
index ef90c53..3512a2f 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 import logging
 import json
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -50,6 +51,7 @@ class Subnets(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
@@ -133,6 +135,7 @@ class Subnets(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
@@ -155,6 +158,7 @@ class Subnets(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
index cefbc8a..c8a7fff 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 import logging
 import json
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -70,6 +71,7 @@ class Tenants(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
index eea3537..6682a4c 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 import logging
 import json
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -50,6 +51,7 @@ class Volumes(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
@@ -135,6 +137,7 @@ class Volumes(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
@@ -154,6 +157,7 @@ class Volumes(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
index 6738cab..987524d 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 import logging
 import json
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -43,30 +44,33 @@ class Vports(APIView):
         logger.debug("Ports--get::> %s" % request.data)
         try:
             # prepare request resource to vim instance
-            vim = VimDriverUtils.get_vim_info(vimid)
-            sess = VimDriverUtils.get_session(vim, tenantid)
-
-            content, status_code = self.get_ports(sess, request, vim, tenantid, portid)
+            query = VimDriverUtils.get_query_part(request)
+            content, status_code = self.get_ports(query, vimid, tenantid, portid)
 
             return Response(data=content, status=status_code)
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
-
-    def get_ports(self, sess, request, vim, tenantid, portid=""):
+    def get_ports(self, query="", vimid="", tenantid="", portid=""):
         logger.debug("Ports--get_ports::> %s" % portid)
+        vim = VimDriverUtils.get_vim_info(vimid)
+        sess = VimDriverUtils.get_session(vim, tenantid)
+
         if sess:
             # prepare request resource to vim instance
             req_resouce = "v2.0/ports"
             if portid:
                 req_resouce += "/%s" % portid
 
-            query = VimDriverUtils.get_query_part(request)
             if query:
                 req_resouce += "?%s" % query
+
+            vim = VimDriverUtils.get_vim_info(vimid)
+            sess = VimDriverUtils.get_session(vim, tenantid)
             resp = sess.get(req_resouce, endpoint_filter=self.service)
             content = resp.json()
             vim_dict = {
@@ -80,18 +84,16 @@ class Vports(APIView):
                 # convert the key naming in ports
                 for port in content["ports"]:
                     # use only 1st entry of fixed_ips
-                    if port:
-                        tmpips = port.pop("fixed_ips", None)
-                        port.update(tmpips[0])
+                    tmpips = port.pop("fixed_ips", None) if port else None
+                    port.update(tmpips[0]) if tmpips and len(tmpips) > 0 else None
                     VimDriverUtils.replace_key_by_mapping(port,
                                                           self.keys_mapping)
             else:
                 # convert the key naming in the port specified by id
                 port = content.pop("port", None)
                 #use only 1st entry of fixed_ips
-                if port:
-                    tmpips = port.pop("fixed_ips", None)
-                    port.update(tmpips[0])
+                tmpips = port.pop("fixed_ips", None) if port else None
+                port.update(tmpips[0]) if tmpips and len(tmpips) > 0 else None
 
                 VimDriverUtils.replace_key_by_mapping(port,
                                                       self.keys_mapping)
@@ -102,12 +104,9 @@ class Vports(APIView):
     def post(self, request, vimid="", tenantid="", portid=""):
         logger.debug("Ports--post::> %s" % request.data)
         try:
-            # prepare request resource to vim instance
-            vim = VimDriverUtils.get_vim_info(vimid)
-            sess = VimDriverUtils.get_session(vim, tenantid)
-
             #check if already created: name
-            content, status_code = self.get_ports(sess, request, vim, tenantid)
+            query = "name=%s" % request.data["name"]
+            content, status_code = self.get_ports(query, vimid, tenantid, portid)
             existed = False
             if status_code == 200:
                 for port in content["ports"]:
@@ -123,15 +122,18 @@ class Vports(APIView):
                     return Response(data=port, status=status_code)
 
             #otherwise create a new one
-            return self.create_port(sess, request, vim, tenantid)
+            return self.create_port(request, vimid, tenantid)
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
-    def create_port(self, sess, request, vim, tenantid):
+    def create_port(self, request, vimid, tenantid):
         logger.debug("Ports--create::> %s" % request.data)
+        vim = VimDriverUtils.get_vim_info(vimid)
+        sess = VimDriverUtils.get_session(vim, tenantid)
         if sess:
             # prepare request resource to vim instance
             req_resouce = "v2.0/ports"
@@ -156,7 +158,7 @@ class Vports(APIView):
             resp_body = resp.json()["port"]
             #use only 1 fixed_ip
             tmpips = resp_body.pop("fixed_ips", None)
-            if tmpips:
+            if tmpips and len(tmpips) > 0:
                 resp_body.update(tmpips[0])
 
             VimDriverUtils.replace_key_by_mapping(resp_body, self.keys_mapping)
@@ -188,6 +190,7 @@ class Vports(APIView):
         except VimDriverKiloException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
index 197ecd8..c59c42c 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 import logging
 import json
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -118,6 +119,7 @@ class Flavors(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
@@ -240,6 +242,8 @@ class Flavors(APIView):
 
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
+
             if sess and resp and resp.status_code == 200:
                 self.delete_flavor(sess, flavorid)
 
@@ -303,6 +307,7 @@ class Flavors(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
index f0ea686..7801c55 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 import logging
 import json
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -76,6 +77,7 @@ class Hosts(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
index 24f4cb4..bf24771 100644 (file)
@@ -15,6 +15,7 @@ import logging
 import json
 import urllib2
 import threading
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -65,6 +66,7 @@ class imageThread (threading.Thread):
             logger.debug("response status code of transfer_image %s" % resp.status_code)
             return None
         except Exception as e:
+            logger.error(traceback.format_exc())
             logger.debug("Failed to transfer_image:%s" % str(e))
             return None
         pass
@@ -89,6 +91,7 @@ class Images(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
@@ -201,6 +204,7 @@ class Images(APIView):
             return Response(data={'error': 'image is not accessible:%s' % str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
@@ -220,6 +224,7 @@ class Images(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
index 33e4af3..83d0e50 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 import logging
 import json
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -75,6 +76,7 @@ class Limits(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
index 0f591be..a1b440d 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 import logging
 import json
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -48,6 +49,7 @@ class Networks(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
@@ -131,6 +133,7 @@ class Networks(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
@@ -153,6 +156,7 @@ class Networks(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
index 26f30de..51a0efa 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 import logging
 import json
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -79,6 +80,7 @@ class Servers(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
@@ -236,6 +238,7 @@ class Servers(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
@@ -255,6 +258,7 @@ class Servers(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
index 2430463..b37d4d3 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 import logging
 import json
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -50,6 +51,7 @@ class Subnets(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
@@ -133,6 +135,7 @@ class Subnets(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
@@ -155,6 +158,7 @@ class Subnets(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
index f94140c..7f82bb6 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 import logging
 import json
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -75,6 +76,7 @@ class Tenants(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
index f9df757..a126aad 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 import logging
 import json
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -50,6 +51,7 @@ class Volumes(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
@@ -135,6 +137,7 @@ class Volumes(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
@@ -154,6 +157,7 @@ class Volumes(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass
index 750726e..a5b8db9 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 import logging
 import json
+import traceback
 
 from rest_framework import status
 from rest_framework.response import Response
@@ -43,30 +44,33 @@ class Vports(APIView):
         logger.debug("Ports--get::> %s" % request.data)
         try:
             # prepare request resource to vim instance
-            vim = VimDriverUtils.get_vim_info(vimid)
-            sess = VimDriverUtils.get_session(vim, tenantid)
-
-            content, status_code = self.get_ports(sess, request, vim, tenantid, portid)
+            query = VimDriverUtils.get_query_part(request)
+            content, status_code = self.get_ports(query, vimid, tenantid, portid)
 
             return Response(data=content, status=status_code)
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
-
-    def get_ports(self, sess, request, vim, tenantid, portid=""):
+    def get_ports(self, query="", vimid="", tenantid="", portid=""):
         logger.debug("Ports--get_ports::> %s" % portid)
+        vim = VimDriverUtils.get_vim_info(vimid)
+        sess = VimDriverUtils.get_session(vim, tenantid)
+
         if sess:
             # prepare request resource to vim instance
             req_resouce = "v2.0/ports"
             if portid:
                 req_resouce += "/%s" % portid
 
-            query = VimDriverUtils.get_query_part(request)
             if query:
                 req_resouce += "?%s" % query
+
+            vim = VimDriverUtils.get_vim_info(vimid)
+            sess = VimDriverUtils.get_session(vim, tenantid)
             resp = sess.get(req_resouce, endpoint_filter=self.service)
             content = resp.json()
             vim_dict = {
@@ -80,18 +84,16 @@ class Vports(APIView):
                 # convert the key naming in ports
                 for port in content["ports"]:
                     # use only 1st entry of fixed_ips
-                    if port:
-                        tmpips = port.pop("fixed_ips", None)
-                        port.update(tmpips[0])
+                    tmpips = port.pop("fixed_ips", None) if port else None
+                    port.update(tmpips[0]) if tmpips and len(tmpips) > 0 else None
                     VimDriverUtils.replace_key_by_mapping(port,
                                                           self.keys_mapping)
             else:
                 # convert the key naming in the port specified by id
                 port = content.pop("port", None)
                 #use only 1st entry of fixed_ips
-                if port:
-                    tmpips = port.pop("fixed_ips", None)
-                    port.update(tmpips[0])
+                tmpips = port.pop("fixed_ips", None) if port else None
+                port.update(tmpips[0]) if tmpips and len(tmpips) > 0 else None
 
                 VimDriverUtils.replace_key_by_mapping(port,
                                                       self.keys_mapping)
@@ -102,12 +104,9 @@ class Vports(APIView):
     def post(self, request, vimid="", tenantid="", portid=""):
         logger.debug("Ports--post::> %s" % request.data)
         try:
-            # prepare request resource to vim instance
-            vim = VimDriverUtils.get_vim_info(vimid)
-            sess = VimDriverUtils.get_session(vim, tenantid)
-
             #check if already created: name
-            content, status_code = self.get_ports(sess, request, vim, tenantid)
+            query = "name=%s" % request.data["name"]
+            content, status_code = self.get_ports(query, vimid, tenantid, portid)
             existed = False
             if status_code == 200:
                 for port in content["ports"]:
@@ -123,15 +122,18 @@ class Vports(APIView):
                     return Response(data=port, status=status_code)
 
             #otherwise create a new one
-            return self.create_port(sess, request, vim, tenantid)
+            return self.create_port(request, vimid, tenantid)
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
-    def create_port(self, sess, request, vim, tenantid):
+    def create_port(self, request, vimid, tenantid):
         logger.debug("Ports--create::> %s" % request.data)
+        vim = VimDriverUtils.get_vim_info(vimid)
+        sess = VimDriverUtils.get_session(vim, tenantid)
         if sess:
             # prepare request resource to vim instance
             req_resouce = "v2.0/ports"
@@ -156,7 +158,7 @@ class Vports(APIView):
             resp_body = resp.json()["port"]
             #use only 1 fixed_ip
             tmpips = resp_body.pop("fixed_ips", None)
-            if tmpips:
+            if tmpips and len(tmpips) > 0:
                 resp_body.update(tmpips[0])
 
             VimDriverUtils.replace_key_by_mapping(resp_body, self.keys_mapping)
@@ -188,6 +190,7 @@ class Vports(APIView):
         except VimDriverNewtonException as e:
             return Response(data={'error': e.content}, status=e.status_code)
         except Exception as e:
+            logger.error(traceback.format_exc())
             return Response(data={'error': str(e)},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         pass