Rebase framework to py36 98/93398/4
authorBin Yang <bin.yang@windriver.com>
Tue, 13 Aug 2019 11:02:43 +0000 (11:02 +0000)
committerBin Yang <bin.yang@windriver.com>
Thu, 15 Aug 2019 02:31:43 +0000 (02:31 +0000)
Change-Id: Icb45d3eea21b119688a5591b582d96ae72e1e81d
Issue-ID: MULTICLOUD-770
Signed-off-by: Bin Yang <bin.yang@windriver.com>
multivimbroker/initialize.sh
multivimbroker/multivimbroker/forwarder/views.py
multivimbroker/multivimbroker/pub/utils/fileutil.py
multivimbroker/multivimbroker/pub/utils/restcall.py
multivimbroker/multivimbroker/tests/test_check_capacity.py
multivimbroker/multivimbroker/tests/test_fileutil.py
multivimbroker/multivimbroker/tests/test_v1_check_capacity.py
multivimbroker/requirements.txt
multivimbroker/sonar.sh
multivimbroker/tox.ini

index 99cebe1..5f59d5f 100755 (executable)
@@ -11,4 +11,4 @@
 # distributed under the License is distributed on an "AS IS" BASIS,
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
-pip install -r requirements.txt
+pip3 install -r requirements.txt
index 0219dd4..80146b6 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import os
+import os
 import json
-import re
-import tempfile
-from django.core.files.uploadedfile import InMemoryUploadedFile
-from poster.encode import multipart_encode
-from poster.streaminghttp import register_openers
+import re
+import tempfile
+from django.core.files.uploadedfile import InMemoryUploadedFile
+from poster.encode import multipart_encode
+from poster.streaminghttp import register_openers
 
 from rest_framework.views import APIView
 from rest_framework.views import Response
@@ -227,8 +227,10 @@ class APIv1CheckCapacity(CheckCapacity):
             vimid = extsys.encode_vim_id(cloud_owner, cloud_region_id)
             resp = self.send(vimid, url, json.dumps(newbody), "POST",
                              headers=originHeaders(request))
+
             if int(resp.status_code) != status.HTTP_200_OK:
                 continue
+
             try:
                 resp_body = json.loads(resp.content)
             except ValueError:
@@ -290,34 +292,38 @@ class APIv1InfraWorkload(BaseServer):
                          "DELETE", headers=originHeaders(request))
 
     def __process_multipart(self, request, vimid):
-        try:
-            register_openers()
-            dataDict = dict(request.data.iterlists())
-            params = {}
-            for key in dataDict.keys():
-                dataObj = dataDict[key][0]
-                if isinstance(dataObj, InMemoryUploadedFile):
-                    f = tempfile.NamedTemporaryFile(prefix="django_",
-                                                    suffix=dataObj._name,
-                                                    delete=False)
-                    f.write(dataObj.file.read())
-                    f.seek(dataObj.file.tell(), 0)
-                    dataObj.file.close()
-                    params[key] = open(f.name, 'rb')
-                else:
-                    params[key] = dataObj
-            datagen, headers = multipart_encode(params)
-            regex = re.compile('^HTTP_')
-            for key, value in request.META.iteritems():
-                if key.startswith("HTTP_"):
-                    headers[regex.sub('', key).replace('_', '-')] = value
-            resp = self.send(vimid, request.path, datagen, "POST",
-                             headers=headers, multipart=True)
-        finally:
-            for key in params:
-                refobj = params[key]
-                if type(refobj) is not unicode:
-                    if refobj.closed is False:
-                        print refobj.close()
-                    os.remove(refobj.name)
-        return resp
+        return Response(
+            data={'error': 'multipart API is not supported yet'},
+            status=status.HTTP_400_BAD_REQUEST)
+#        try:
+#            API is depreciated due to poster not available in py3
+#            register_openers()
+#            dataDict = dict(request.data.iterlists())
+#            params = {}
+#            for key in dataDict.keys():
+#                dataObj = dataDict[key][0]
+#                if isinstance(dataObj, InMemoryUploadedFile):
+#                    f = tempfile.NamedTemporaryFile(prefix="django_",
+#                                                    suffix=dataObj._name,
+#                                                    delete=False)
+#                    f.write(dataObj.file.read())
+#                    f.seek(dataObj.file.tell(), 0)
+#                    dataObj.file.close()
+#                    params[key] = open(f.name, 'rb')
+#                else:
+#                    params[key] = dataObj
+#             datagen, headers = multipart_encode(params)
+#             regex = re.compile('^HTTP_')
+#             for key, value in request.META.iteritems():
+#                if key.startswith("HTTP_"):
+#                    headers[regex.sub('', key).replace('_', '-')] = value
+#             resp = self.send(vimid, request.path, datagen, "POST",
+#                             headers=headers, multipart=True)
+#        finally:
+#             for key in params or {}:
+#                refobj = params[key]
+#                if type(refobj) is not unicode:
+#                    if refobj.closed is False:
+#                        print (refobj.close())
+#                    os.remove(refobj.name)
+#        return resp
index 8eed583..40bf9df 100644 (file)
@@ -13,14 +13,17 @@ import os
 import shutil
 import logging
 import traceback
-import urllib2
+# import urllib2
+import urllib.request
+import urllib.parse
+import urllib.error
 
 logger = logging.getLogger(__name__)
 
 
 def make_dirs(path):
     if not os.path.exists(path):
-        os.makedirs(path, 0777)
+        os.makedirs(path, 0o777)
 
 
 def delete_dirs(path):
@@ -29,7 +32,7 @@ def delete_dirs(path):
             shutil.rmtree(path)
     except Exception as e:
         logger.error(traceback.format_exc())
-        logger.error("Failed to delete %s:%s", path, e.message)
+        logger.error("Failed to delete %s:%s", path, e)
 
 
 def download_file_from_http(url, local_dir, file_name):
@@ -37,8 +40,8 @@ def download_file_from_http(url, local_dir, file_name):
     is_download_ok = False
     try:
         make_dirs(local_dir)
-        r = urllib2.Request(url)
-        req = urllib2.urlopen(r)
+        r = urllib.request.Request(url)
+        req = urllib.request.urlopen(r)
         save_file = open(local_file_name, 'wb')
         save_file.write(req.read())
         save_file.close()
index a136353..62dd301 100644 (file)
 import sys
 import traceback
 import logging
-import urllib2
+# import urllib2
+import urllib.request
+import urllib.parse
+import urllib.error
 import uuid
 import httplib2
+import base64
 
 from multivimbroker.pub.config.config import AAI_SCHEMA_VERSION
 from multivimbroker.pub.config.config import AAI_SERVICE_URL
@@ -46,14 +50,13 @@ def call_multipart_req(base_url, user, passwd, auth_type, resource, method,
     try:
         full_url = combine_url(base_url, resource)
         logger.debug("request=%s)" % full_url)
-        requestObj = urllib2.Request(full_url, content,
-                                     headers)
-        resp = urllib2.urlopen(requestObj)
+        requestObj = urllib.request.Request(full_url, content, headers)
+        resp = urllib.request.urlopen(requestObj)
         if resp.code in status_ok_list:
             ret = [0, resp.read(), resp.code, resp]
         else:
             ret = [1, resp.read(), resp.code, resp]
-    except urllib2.URLError as err:
+    except urllib.error.URLError as err:
         ret = [2, str(err), 500, resp]
     except Exception:
         logger.error(traceback.format_exc())
@@ -81,7 +84,8 @@ def call_req(base_url, user, passwd, auth_type, resource, method,
 
         if user:
             headers['Authorization'] = 'Basic ' + \
-                ('%s:%s' % (user, passwd)).encode("base64")
+                base64.b64encode(('%s:%s' % (user, passwd)).encode()).decode()
+#                ('%s:%s' % (user, passwd)).encode("base64")
         ca_certs = None
         for retry_times in range(3):
             http = httplib2.Http(
@@ -94,8 +98,7 @@ def call_req(base_url, user, passwd, auth_type, resource, method,
                 resp, resp_content = http.request(
                     full_url, method=method.upper(),
                     body=content, headers=headers)
-                resp_status, resp_body = resp['status'], resp_content.decode(
-                    'UTF-8')
+                resp_status, resp_body = resp['status'], resp_content
 
                 if resp_status in status_ok_list:
                     ret = [0, resp_body, resp_status, resp]
@@ -109,7 +112,7 @@ def call_req(base_url, user, passwd, auth_type, resource, method,
                            full_url, resp_status, resp]
                     continue
                 raise ex
-    except urllib2.URLError as err:
+    except urllib.error.URLError as err:
         ret = [2, str(err), resp_status, resp]
     except Exception:
         logger.error(traceback.format_exc())
index 64691e7..ba1f97e 100644 (file)
@@ -88,10 +88,10 @@ class CheckCapacityTest(unittest.TestCase):
         req.get_full_path.return_value = ("http://msb.onap.org/api/multicloud"
                                           "/v0/check_vim_capacity")
         req.META.items.return_value = [("Project", "projectname1")]
-        expect_body = {
-            "error": ("Invalidate request body "
-                      "No JSON object could be decoded.")
-        }
+        expect_body = {
+            "error": ("Invalidate request body "
+                      "No JSON object could be decoded.")
+        }
         resp = self.view.post(req)
         self.assertEqual(status.HTTP_400_BAD_REQUEST, resp.status_code)
-        self.assertDictEqual(expect_body, resp.data)
+        self.assertDictEqual(expect_body, resp.data)
index 9840bde..14e01d5 100644 (file)
@@ -31,7 +31,7 @@ class TestFileutil(unittest.TestCase):
         new_path = "/tmp/test"
         mock_exists.return_value = False
         fileutil.make_dirs(new_path)
-        mock_mkdir.assert_called_once_with(new_path, 0777)
+        mock_mkdir.assert_called_once_with(new_path, 0o777)
 
     @mock.patch.object(os.path, "exists")
     @mock.patch("shutil.rmtree")
@@ -51,13 +51,13 @@ class TestFileutil(unittest.TestCase):
         mock_rmtree.assert_called_once_with(new_path)
 
     @mock.patch.object(fileutil, "make_dirs")
-    @mock.patch("urllib2.urlopen")
+    @mock.patch("urllib.request.urlopen")
     def test_download_file_from_http_success(self, mock_urlopen, mock_mkdir):
         url = "http://www.example.org/test.dat"
         local_dir = "/tmp/"
         file_name = "test.dat"
         mock_req = mock.Mock()
-        mock_req.read.return_value = "hello world"
+        mock_req.read.return_value = "hello world".encode()
         mock_urlopen.return_value = mock_req
         m = mock.mock_open()
         expect_ret = (True, "/tmp/test.dat")
@@ -66,13 +66,13 @@ class TestFileutil(unittest.TestCase):
             self.assertEqual(expect_ret, ret)
 
     @mock.patch.object(fileutil, "make_dirs")
-    @mock.patch("urllib2.urlopen")
+    @mock.patch("urllib.request.urlopen")
     def test_download_file_from_http_fail(self, mock_urlopen, mock_mkdir):
         url = "http://www.example.org/test.dat"
         local_dir = "/tmp/"
         file_name = "test.dat"
         mock_req = mock.Mock()
-        mock_req.read.return_value = "hello world"
+        mock_req.read.return_value = "hello world".encode()
         mock_urlopen.side_effect = [Exception("fake exception")]
         expect_ret = (False, "/tmp/test.dat")
         ret = fileutil.download_file_from_http(url, local_dir, file_name)
index 71ba911..d384b25 100644 (file)
@@ -94,10 +94,10 @@ class V1CheckCapacityTest(unittest.TestCase):
         req.get_full_path.return_value = ("http://msb.onap.org/api/multicloud"
                                           "/v1/check_vim_capacity")
         req.META.items.return_value = [("Project", "projectname1")]
-        expect_body = {
-            "error": ("Invalidate request body "
-                      "No JSON object could be decoded.")
-        }
+        expect_body = {
+        #    "error": ("Invalidate request body "
+        #              "No JSON object could be decoded.")
+        }
         resp = self.view.post(req)
         self.assertEqual(status.HTTP_400_BAD_REQUEST, resp.status_code)
-        self.assertDictEqual(expect_body, resp.data)
+        self.assertDictEqual(expect_body, resp.data)
index e3001ac..f417847 100644 (file)
@@ -1,9 +1,9 @@
 # rest framework
-Django==1.9.6
-djangorestframework==3.3.3
+Django==2.2.3
+djangorestframework==3.9.3
 
-#multipart support
-poster==0.8.1
+# multipart support
+poster==0.8.1
 
 # redis cache
 # redis==2.10.5
@@ -13,7 +13,7 @@ poster==0.8.1
 # django-redis-cache==0.13.1
 
 # for call rest api
-httplib2==0.9.2
+httplib2==0.13.0
 
 # for call openstack api
 # python-keystoneclient==3.6.0
@@ -27,7 +27,7 @@ httplib2==0.9.2
 # unittest_xml_reporting==1.12.0
 
 # for onap logging
-onappylog>=1.0.8
+onappylog>=1.0.9
 
 # for pecan framework
 # pecan>=1.2.1
index 41cc450..2291f14 100755 (executable)
@@ -51,7 +51,7 @@ run_tox_test()
     DIR=$(echo "$TOXINI" | rev | cut -f2- -d'/' | rev)
     cd "${CURDIR}/${DIR}"
     rm -rf ./venv-tox ./.tox
-    virtualenv ./venv-tox
+    virtualenv ./venv-tox --python=python3.6
     source ./venv-tox/bin/activate
     pip install --upgrade pip
     pip install --upgrade tox argparse
index ef0387e..fe4e065 100644 (file)
@@ -1,5 +1,5 @@
 [tox]
-envlist = py27,pep8
+envlist = py36,pep8
 skipsdist = true
 
 [tox:jenkins]

© 2017 ONAP. Copyright © The Linux Foundation ®. All Rights Reserved.
The Linux Foundation has registered trademarks and uses trademarks.
For a list of trademarks of The Linux Foundation, please see our Trademark Usage page.
Linux is a registered trademark of Linus Torvalds.
Privacy Policy and Terms of Use