summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBin Yang <bin.yang@windriver.com>2017-09-28 02:10:13 +0000
committerGerrit Code Review <gerrit@onap.org>2017-09-28 02:10:13 +0000
commit0aa903c0dc422a8660cfe64616a4d191318816e6 (patch)
tree1b44bd0679d3ff5926f1eb6c37a0d626a73f065b
parentc9fab88d3f71735439b1f7c534eff77158b40958 (diff)
parent003e56fd3f537e37a791ad8f6e74005f84e352d6 (diff)
Merge "Refactor flavor UTs"
-rw-r--r--newton/newton/requests/tests/test_flavor.py413
-rw-r--r--newton/newton/requests/views/flavor.py90
2 files changed, 228 insertions, 275 deletions
diff --git a/newton/newton/requests/tests/test_flavor.py b/newton/newton/requests/tests/test_flavor.py
index cb7a4ffe..7ddb1122 100644
--- a/newton/newton/requests/tests/test_flavor.py
+++ b/newton/newton/requests/tests/test_flavor.py
@@ -13,14 +13,13 @@
# limitations under the License.
import mock
-import unittest
-from django.test import Client
from rest_framework import status
+from newton.requests.tests import mock_info
+from newton.requests.tests import test_base
from newton.requests.views.flavor import Flavors
from newton.requests.views.util import VimDriverUtils
-from newton.requests.tests import mock_info
MOCK_GET_FLAVORS_RESPONSE = {
@@ -37,6 +36,12 @@ MOCK_GET_FLAVOR_RESPONSE = {
}
}
+MOCK_GET_EXTRA_SPECS = {
+ "extra_specs": {
+ "key": "test"
+ }
+}
+
MOCK_POST_FLAVOR_REQUEST = {
"id": "uuid_3",
"name": "flavor_3"
@@ -55,234 +60,198 @@ MOCK_POST_FLAVOR_RESPONSE = {
}
-class MockResponse(object):
- status_code = 200
- content = ''
-
- def json(self):
- pass
-
-
-class TestFlavors(unittest.TestCase):
- def setUp(self):
- self.client = Client()
-
- def tearDown(self):
- pass
-
- @mock.patch.object(Flavors, 'get_flavor_extra_specs')
- @mock.patch.object(VimDriverUtils, 'get_session')
- @mock.patch.object(VimDriverUtils, 'get_vim_info')
- def test_get_flavors(self, mock_get_vim_info, mock_get_session, mock_get_flavor_extra_specs):
-
- mock_session_specs = ["get"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
-
- mock_get_flavors_response_obj = mock.Mock(spec=MockResponse)
- mock_get_flavors_response_obj.status_code = 200
- mock_get_flavors_response_obj.content = MOCK_GET_FLAVORS_RESPONSE
- mock_get_flavors_response_obj.json.return_value = MOCK_GET_FLAVORS_RESPONSE
- mock_session.get.return_value = mock_get_flavors_response_obj
-
- mock_extra_specs = mock.Mock(spec=MockResponse)
+class TestFlavors(test_base.TestRequest):
+
+ @mock.patch.object(Flavors, '_get_flavor_extra_specs')
+ @mock.patch.object(VimDriverUtils, 'get_session')
+ @mock.patch.object(VimDriverUtils, 'get_vim_info')
+ def test_get_flavors(self, mock_get_vim_info, mock_get_session,
+ mock_get_flavor_extra_specs):
+ mock_get_session.side_effect = [
+ test_base.get_mock_session(
+ ["get"], {"get": {"content": MOCK_GET_FLAVORS_RESPONSE}}),
+ ]
+
+ mock_extra_specs = mock.Mock(spec=test_base.MockResponse)
+ mock_extra_specs.json.return_value = {"extra_specs": {}}
+
+ mock_get_flavor_extra_specs.return_value = mock_extra_specs
+ mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
+
+ response = self.client.get(
+ ("/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne"
+ "/fcca3cc49d5e42caae15459e27103efc/flavors"),
+ {}, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
+ context = response.json()
+
+ self.assertEquals(status.HTTP_200_OK, response.status_code)
+ self.assertIsNotNone(context['flavors'])
+ self.assertEqual(MOCK_GET_FLAVORS_RESPONSE["flavors"],
+ context['flavors'])
+
+ @mock.patch.object(Flavors, '_get_flavor_extra_specs')
+ @mock.patch.object(VimDriverUtils, 'get_session')
+ @mock.patch.object(VimDriverUtils, 'get_vim_info')
+ def test_get_flavor(self, mock_get_vim_info, mock_get_session,
+ mock_get_flavor_extra_specs):
+ mock_get_session.side_effect = [
+ test_base.get_mock_session(
+ ["get"],
+ {"get": {"content": MOCK_GET_FLAVOR_RESPONSE}}),
+ ]
+
+ mock_extra_specs = mock.Mock(spec=test_base.MockResponse)
+ mock_extra_specs.json.return_value = {"extra_specs": {}}
+
+ mock_get_flavor_extra_specs.return_value = mock_extra_specs
+ mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
+
+ response = self.client.get(
+ ("/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne"
+ "/fcca3cc49d5e42caae15459e27103efc/flavors/uuid_1"),
+ {}, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
+ context = response.json()
+
+ self.assertEquals(status.HTTP_200_OK, response.status_code)
+ self.assertEqual(MOCK_GET_FLAVOR_RESPONSE["id"], context["id"])
+
+ @mock.patch.object(Flavors, '_get_flavor_extra_specs')
+ @mock.patch.object(VimDriverUtils, 'get_session')
+ @mock.patch.object(VimDriverUtils, 'get_vim_info')
+ def test_get_flavor_not_found(
+ self, mock_get_vim_info, mock_get_session,
+ mock_get_flavor_extra_specs):
+ mock_get_session.side_effect = [
+ test_base.get_mock_session(
+ ["get"],
+ {"get": {"status_code":status.HTTP_404_NOT_FOUND}}),
+ ]
+
+ mock_extra_specs = mock.Mock(spec=test_base.MockResponse)
+ mock_extra_specs.json.return_value = {"extra_specs": {}}
+
+ mock_get_flavor_extra_specs.return_value = mock_extra_specs
+ mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
+
+ response = self.client.get(
+ ("/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne"
+ "/fcca3cc49d5e42caae15459e27103efc/flavors/uuid_1"),
+ {}, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
+
+ # TODO(sshank): 404 status is not possible.
+ self.assertEquals(status.HTTP_500_INTERNAL_SERVER_ERROR,
+ response.status_code)
+ self.assertIn('error', response.data)
+
+ @mock.patch.object(VimDriverUtils, 'get_session')
+ @mock.patch.object(VimDriverUtils, 'get_vim_info')
+ def test_create_flavor(self, mock_get_vim_info, mock_get_session):
+ mock_get_session.side_effect = [
+ test_base.get_mock_session(
+ ["get", "post"], {
+ "get": {"content": MOCK_GET_FLAVORS_RESPONSE},
+ "post": {
+ "content": MOCK_POST_FLAVOR_RESPONSE,
+ "status_code": status.HTTP_202_ACCEPTED,
+ }
+ }
+ ),
+ ]
+ mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
+
+ response = self.client.post(
+ ("/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne"
+ "/fcca3cc49d5e42caae15459e27103efc/flavors"),
+ MOCK_POST_FLAVOR_REQUEST,
+ HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
+ context = response.json()
+
+ self.assertEquals(status.HTTP_202_ACCEPTED,
+ response.status_code)
+ self.assertIsNotNone(context['id'])
+ self.assertEqual(1, context['returnCode'])
+
+
+ @mock.patch.object(Flavors, '_get_flavor_extra_specs')
+ @mock.patch.object(VimDriverUtils, 'get_session')
+ @mock.patch.object(VimDriverUtils, 'get_vim_info')
+ def test_create_existing_flavor(
+ self, mock_get_vim_info, mock_get_session,
+ mock_get_flavor_extra_specs):
+ mock_get_session.side_effect = [
+ test_base.get_mock_session(
+ ["get", "post"], {
+ "get": {"content": MOCK_GET_FLAVORS_RESPONSE},
+ "post": {
+ "content": MOCK_POST_FLAVOR_RESPONSE,
+ "status_code": status.HTTP_202_ACCEPTED,
+ }
+ }),
+ ]
+ mock_extra_specs = mock.Mock(spec=test_base.MockResponse)
mock_extra_specs.json.return_value = {"extra_specs": {}}
mock_get_flavor_extra_specs.return_value = mock_extra_specs
mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
- mock_get_session.return_value = mock_session
-
- response = self.client.get(
- "/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc/flavors",
- {}, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
- context = response.json()
-
- self.assertEquals(status.HTTP_200_OK, response.status_code)
- self.assertIsNotNone(context['flavors'])
- self.assertEqual(MOCK_GET_FLAVORS_RESPONSE["flavors"], context['flavors'])
-
- @mock.patch.object(Flavors, 'get_flavor_extra_specs')
- @mock.patch.object(VimDriverUtils, 'get_session')
- @mock.patch.object(VimDriverUtils, 'get_vim_info')
- def test_get_flavor(self, mock_get_vim_info, mock_get_session, mock_get_flavor_extra_specs):
-
- mock_session_specs = ["get"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
-
- mock_get_flavor_response_obj = mock.Mock(spec=MockResponse)
- mock_get_flavor_response_obj.status_code = 200
- mock_get_flavor_response_obj.content = MOCK_GET_FLAVOR_RESPONSE
- mock_get_flavor_response_obj.json.return_value = MOCK_GET_FLAVOR_RESPONSE
- mock_session.get.return_value = mock_get_flavor_response_obj
-
- mock_extra_specs = mock.Mock(spec=MockResponse)
- mock_extra_specs.json.return_value = {"extra_specs": {}}
-
- mock_get_flavor_extra_specs.return_value = mock_extra_specs
- mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
- mock_get_session.return_value = mock_session
-
- response = self.client.get(
- "/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc"
- "/flavors/uuid_1",
- {}, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
- context = response.json()
-
- self.assertEquals(status.HTTP_200_OK, response.status_code)
- self.assertEqual(MOCK_GET_FLAVOR_RESPONSE["id"], context["id"])
-
- @mock.patch.object(Flavors, 'get_flavor_extra_specs')
- @mock.patch.object(VimDriverUtils, 'get_session')
- @mock.patch.object(VimDriverUtils, 'get_vim_info')
- def test_get_flavor_not_found(self, mock_get_vim_info, mock_get_session, mock_get_flavor_extra_specs):
- mock_session_specs = ["get"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
-
- mock_get_flavor_response_obj = mock.Mock(spec=MockResponse)
- mock_get_flavor_response_obj.status_code = 404
- mock_get_flavor_response_obj.content = {}
- mock_get_flavor_response_obj.json.return_value = {}
- mock_session.get.return_value = mock_get_flavor_response_obj
-
- mock_extra_specs = mock.Mock(spec=MockResponse)
- mock_extra_specs.json.return_value = {"extra_specs": {}}
-
- mock_get_flavor_extra_specs.return_value = mock_extra_specs
- mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
- mock_get_session.return_value = mock_session
-
- response = self.client.get(
- "/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc"
- "/flavors/uuid_1",
- {}, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
-
- # TODO(sshank): 404 status is not possible.
- self.assertEquals(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
- self.assertIn('error', response.data)
-
- @mock.patch.object(VimDriverUtils, 'get_session')
- @mock.patch.object(VimDriverUtils, 'get_vim_info')
- def test_post_flavor(self, mock_get_vim_info, mock_get_session):
-
- mock_session_specs = ["get", "post"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
-
- mock_get_flavors_response_obj = mock.Mock(spec=MockResponse)
- mock_get_flavors_response_obj.status_code = 200
- mock_get_flavors_response_obj.content = MOCK_GET_FLAVORS_RESPONSE
- mock_get_flavors_response_obj.json.return_value = MOCK_GET_FLAVORS_RESPONSE
- mock_session.get.return_value = mock_get_flavors_response_obj
-
- mock_post_flavor_response_obj = mock.Mock(spec=MockResponse)
- mock_post_flavor_response_obj.status_code = 202
- mock_post_flavor_response_obj.content = MOCK_POST_FLAVOR_RESPONSE
- mock_post_flavor_response_obj.json.return_value = MOCK_POST_FLAVOR_RESPONSE
- mock_session.post.return_value = mock_post_flavor_response_obj
-
- mock_extra_specs = mock.Mock(spec=MockResponse)
- mock_extra_specs.json.return_value = {"extra_specs": {}}
-
- #mock_get_flavor_extra_specs.return_value = mock_extra_specs
- mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
- mock_get_session.return_value = mock_session
response = self.client.post(
- "/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc/flavors",
- MOCK_POST_FLAVOR_REQUEST, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
- context = response.json()
-
- self.assertEquals(status.HTTP_202_ACCEPTED, response.status_code)
- self.assertIsNotNone(context['id'])
- self.assertEqual(1, context['returnCode'])
-
- @mock.patch.object(Flavors, 'get_flavor_extra_specs')
- @mock.patch.object(VimDriverUtils, 'get_session')
- @mock.patch.object(VimDriverUtils, 'get_vim_info')
- def test_post_flavor_existing(self, mock_get_vim_info, mock_get_session, mock_get_flavor_extra_specs):
- mock_session_specs = ["get", "post"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
-
- mock_get_flavors_response_obj = mock.Mock(spec=MockResponse)
- mock_get_flavors_response_obj.status_code = 200
- mock_get_flavors_response_obj.content = MOCK_GET_FLAVORS_RESPONSE
- mock_get_flavors_response_obj.json.return_value = MOCK_GET_FLAVORS_RESPONSE
- mock_session.get.return_value = mock_get_flavors_response_obj
-
- mock_post_flavor_response_obj = mock.Mock(spec=MockResponse)
- mock_post_flavor_response_obj.status_code = 202
- mock_post_flavor_response_obj.content = MOCK_POST_FLAVOR_RESPONSE
- mock_post_flavor_response_obj.json.return_value = MOCK_POST_FLAVOR_RESPONSE
- mock_session.post.return_value = mock_post_flavor_response_obj
-
- mock_extra_specs = mock.Mock(spec=MockResponse)
- mock_extra_specs.json.return_value = {"extra_specs": {}}
-
- mock_get_flavor_extra_specs.return_value = mock_extra_specs
- mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
- mock_get_session.return_value = mock_session
-
- response = self.client.post(
- "/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc/flavors",
- MOCK_POST_FLAVOR_REQUEST_EXISTING, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
+ ("/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/"
+ "fcca3cc49d5e42caae15459e27103efc/flavors"),
+ MOCK_POST_FLAVOR_REQUEST_EXISTING,
+ HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
context = response.json()
self.assertEquals(status.HTTP_200_OK, response.status_code)
self.assertIsNotNone(context['returnCode'])
self.assertEqual(0, context['returnCode'])
- @mock.patch.object(VimDriverUtils, 'get_session')
- @mock.patch.object(VimDriverUtils, 'get_vim_info')
- def test_post_flavor_empty(self, mock_get_vim_info, mock_get_session):
- mock_session_specs = ["get", "post"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
-
- mock_get_flavors_response_obj = mock.Mock(spec=MockResponse)
- mock_get_flavors_response_obj.status_code = 200
- mock_get_flavors_response_obj.content = MOCK_GET_FLAVORS_RESPONSE
- mock_get_flavors_response_obj.json.return_value = MOCK_GET_FLAVORS_RESPONSE
- mock_session.get.return_value = mock_get_flavors_response_obj
-
- mock_post_flavor_response_obj = mock.Mock(spec=MockResponse)
- mock_post_flavor_response_obj.status_code = 202
- mock_post_flavor_response_obj.content = MOCK_POST_FLAVOR_RESPONSE
- mock_post_flavor_response_obj.json.return_value = MOCK_POST_FLAVOR_RESPONSE
- mock_session.post.return_value = mock_post_flavor_response_obj
-
- mock_extra_specs = mock.Mock(spec=MockResponse)
- mock_extra_specs.json.return_value = {"extra_specs": {}}
-
- mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
- mock_get_session.return_value = mock_session
-
- response = self.client.post(
- "/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc/flavors",
- {}, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
-
- context = response.json()
- self.assertIn('error', context)
- self.assertEquals(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
-
- @mock.patch.object(Flavors, 'get_flavor_extra_specs')
- @mock.patch.object(VimDriverUtils, 'get_session')
- @mock.patch.object(VimDriverUtils, 'get_vim_info')
- def test_delete_flavor(self, mock_get_vim_info, mock_get_session, mock_get_flavor_extra_specs):
- mock_session_specs = ["delete"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
-
- mock_delete_flavor_response_obj = mock.Mock(spec=MockResponse)
- mock_delete_flavor_response_obj.status_code = 204
-
- mock_session.delete.return_value = mock_delete_flavor_response_obj
-
- mock_extra_specs = mock.Mock(spec=MockResponse)
- mock_extra_specs.json.return_value = {"extra_specs": {}}
-
- mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
- mock_get_session.return_value = mock_session
-
- response = self.client.delete(
- "/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc"
- "/flavors/uuid_1",
- HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
-
- self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
- self.assertIsNone(response.data)
+ @mock.patch.object(VimDriverUtils, 'get_session')
+ @mock.patch.object(VimDriverUtils, 'get_vim_info')
+ def test_create_empty_flavor(
+ self, mock_get_vim_info,mock_get_session):
+ mock_get_session.side_effect = [
+ test_base.get_mock_session(
+ ["get", "post"],
+ {
+ "get": {"content": MOCK_GET_FLAVORS_RESPONSE},
+ "post": {
+ "content": MOCK_POST_FLAVOR_RESPONSE,
+ "status_code": status.HTTP_202_ACCEPTED
+ }
+ })
+ ]
+ mock_extra_specs = mock.Mock(spec=test_base.MockResponse)
+ mock_extra_specs.json.return_value = {"extra_specs": {}}
+
+ mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
+ response = self.client.post(
+ ("/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/"
+ "fcca3cc49d5e42caae15459e27103efc/flavors"),
+ {}, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
+
+ context = response.json()
+ self.assertIn('error', context)
+ self.assertEquals(status.HTTP_500_INTERNAL_SERVER_ERROR,
+ response.status_code)
+
+ @mock.patch.object(VimDriverUtils, 'get_session')
+ @mock.patch.object(VimDriverUtils, 'get_vim_info')
+ def test_delete_flavor(self, mock_get_vim_info, mock_get_session):
+ mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
+ mock_get_session.side_effect = [
+ test_base.get_mock_session(
+ ["get", "delete"],
+ {
+ "get": { "content": MOCK_GET_EXTRA_SPECS },
+ "delete": {"status_code": status.HTTP_204_NO_CONTENT }
+ }),
+ ]
+
+ response = self.client.delete(
+ ("/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/"
+ "fcca3cc49d5e42caae15459e27103efc/flavors/uuid_1"),
+ HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
+
+ self.assertEqual(status.HTTP_204_NO_CONTENT,
+ response.status_code)
+ self.assertIsNone(response.data)
diff --git a/newton/newton/requests/views/flavor.py b/newton/newton/requests/views/flavor.py
index 87d5481e..2797461d 100644
--- a/newton/newton/requests/views/flavor.py
+++ b/newton/newton/requests/views/flavor.py
@@ -14,13 +14,13 @@
import logging
import json
import traceback
+
from keystoneauth1.exceptions import HttpError
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from newton.pub.exceptions import VimDriverNewtonException
-
from newton.requests.views.util import VimDriverUtils
logger = logging.getLogger(__name__)
@@ -38,7 +38,7 @@ class Flavors(APIView):
("extra_specs", "extraSpecs"),
]
- def convertExtraSpecs(self, extraSpecs, extra_specs, reverse=False):
+ def _convert_extra_specs(self, extraSpecs, extra_specs, reverse=False):
if reverse == False:
#from extraSpecs to extra_specs
for spec in extraSpecs:
@@ -58,16 +58,16 @@ class Flavors(APIView):
vim = VimDriverUtils.get_vim_info(vimid)
sess = VimDriverUtils.get_session(vim, tenantid)
- resp = self.get_flavor(sess, request, flavorid)
+ resp = self._get_flavor(sess, request, flavorid)
content = resp.json()
if flavorid:
flavor = content.pop("flavor", None)
- extraResp = self.get_flavor_extra_specs(sess, flavor["id"])
+ extraResp = self._get_flavor_extra_specs(sess, flavor["id"])
extraContent = extraResp.json()
if extraContent["extra_specs"]:
extraSpecs = []
- self.convertExtraSpecs(extraSpecs, extraContent["extra_specs"], True)
+ self._convert_extra_specs(extraSpecs, extraContent["extra_specs"], True)
flavor["extraSpecs"] = extraSpecs
VimDriverUtils.replace_key_by_mapping(flavor,
self.keys_mapping)
@@ -92,11 +92,11 @@ class Flavors(APIView):
#iterate each flavor to get extra_specs
for flavor in content["flavors"]:
- extraResp = self.get_flavor_extra_specs(sess, flavor["id"])
+ extraResp = self._get_flavor_extra_specs(sess, flavor["id"])
extraContent = extraResp.json()
if extraContent["extra_specs"]:
extraSpecs = []
- self.convertExtraSpecs(extraSpecs, extraContent["extra_specs"], True)
+ self._convert_extra_specs(extraSpecs, extraContent["extra_specs"], True)
flavor["extraSpecs"] = extraSpecs
VimDriverUtils.replace_key_by_mapping(flavor,
self.keys_mapping)
@@ -121,22 +121,19 @@ class Flavors(APIView):
return Response(data={'error': str(e)},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
- def get_flavor_extra_specs(self, sess, flavorid):
- if not flavorid:
- return {}
- else:
+ def _get_flavor_extra_specs(self, sess, flavorid):
+ if flavorid:
logger.debug("Flavors--get_extra_specs::> %s" % flavorid)
# prepare request resource to vim instance
req_resouce = "/flavors/%s/os-extra_specs" % flavorid
resp = sess.get(req_resouce, endpoint_filter=self.service)
return resp
+ return {}
- def get_flavor(self, sess, request, flavorid=""):
+ def _get_flavor(self, sess, request, flavorid=""):
logger.debug("Flavors--get basic")
- if not sess:
- return {}
- else:
+ if sess:
# prepare request resource to vim instance
req_resouce = "/flavors"
if flavorid:
@@ -148,9 +145,8 @@ class Flavors(APIView):
if query:
req_resouce += "?%s" % query
- resp = sess.get(req_resouce, endpoint_filter=self.service)
- return resp
-
+ return sess.get(req_resouce, endpoint_filter=self.service)
+ return {}
def post(self, request, vimid="", tenantid="", flavorid=""):
logger.debug("Flavors--post::> %s" % request.data)
@@ -163,7 +159,7 @@ class Flavors(APIView):
sess = VimDriverUtils.get_session(vim, tenantid)
#check if the flavor is already created: name or id
- tmpresp = self.get_flavor(sess, request)
+ tmpresp = self._get_flavor(sess, request)
content = tmpresp.json()
#iterate each flavor to get extra_specs
existed = False
@@ -175,12 +171,12 @@ class Flavors(APIView):
existed = True
break
- if existed == True:
- extraResp = self.get_flavor_extra_specs(sess, flavor["id"])
+ if existed:
+ extraResp = self._get_flavor_extra_specs(sess, flavor["id"])
extraContent = extraResp.json()
if extraContent["extra_specs"]:
extraSpecs = []
- self.convertExtraSpecs(extraSpecs, extraContent["extra_specs"], True)
+ self._convert_extra_specs(extraSpecs, extraContent["extra_specs"], True)
flavor["extraSpecs"] = extraSpecs
VimDriverUtils.replace_key_by_mapping(flavor,
self.keys_mapping)
@@ -195,7 +191,7 @@ class Flavors(APIView):
extraSpecs = request.data.pop("extraSpecs", None)
#create flavor first
- resp = self.create_flavor(sess, request)
+ resp = self._create_flavor(sess, request)
if resp.status_code == 202:
resp_body = resp.json()["flavor"]
else:
@@ -205,20 +201,20 @@ class Flavors(APIView):
flavorid = resp_body['id']
if extraSpecs:
extra_specs={}
- self.convertExtraSpecs(extraSpecs, extra_specs, False)
+ self._convert_extra_specs(extraSpecs, extra_specs, False)
# logger.debug("extraSpecs:%s" % extraSpecs)
# logger.debug("extra_specs:%s" % extra_specs)
- extraResp = self.create_flavor_extra_specs(sess, extra_specs, flavorid)
+ extraResp = self._create_flavor_extra_specs(sess, extra_specs, flavorid)
if extraResp.status_code == 200:
#combine the response body and return
tmpSpecs = []
tmpRespBody = extraResp.json()
- self.convertExtraSpecs(tmpSpecs, tmpRespBody['extra_specs'], True)
+ self._convert_extra_specs(tmpSpecs, tmpRespBody['extra_specs'], True)
resp_body.update({"extraSpecs":tmpSpecs})
else:
#rollback
- self.delete_flavor(self, request, vimid, tenantid, flavorid)
+ self._delete_flavor(self, request, vimid, tenantid, flavorid)
return extraResp
VimDriverUtils.replace_key_by_mapping(resp_body, self.keys_mapping)
@@ -232,7 +228,7 @@ class Flavors(APIView):
return Response(data=resp_body, status=resp.status_code)
except VimDriverNewtonException as e:
if sess and resp and resp.status_code == 200:
- self.delete_flavor(sess, flavorid)
+ self._delete_flavor(sess, flavorid)
return Response(data={'error': e.content}, status=e.status_code)
except HttpError as e:
@@ -242,13 +238,12 @@ class Flavors(APIView):
logger.error(traceback.format_exc())
if sess and resp and resp.status_code == 200:
- self.delete_flavor(sess, flavorid)
+ self._delete_flavor(sess, flavorid)
return Response(data={'error': str(e)},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
-
- def create_flavor(self, sess, request):
+ def _create_flavor(self, sess, request):
logger.debug("Flavors--create::> %s" % request.data)
# prepare request resource to vim instance
req_resouce = "/flavors"
@@ -261,9 +256,7 @@ class Flavors(APIView):
return sess.post(req_resouce, data=req_body,
endpoint_filter=self.service)
-
-
- def create_flavor_extra_specs(self, sess, extraspecs, flavorid):
+ def _create_flavor_extra_specs(self, sess, extraspecs, flavorid):
logger.debug("Flavors extra_specs--post::> %s" % extraspecs)
# prepare request resource to vim instance
req_resouce = "/flavors"
@@ -279,10 +272,6 @@ class Flavors(APIView):
return sess.post(req_resouce, data=req_body,
endpoint_filter=self.service)
-
-
-
-
def delete(self, request, vimid="", tenantid="", flavorid=""):
logger.debug("Flavors--delete::> %s" % request.data)
try:
@@ -291,10 +280,10 @@ class Flavors(APIView):
sess = VimDriverUtils.get_session(vim, tenantid)
#delete extra specs one by one
- resp = self.delete_flavor_extra_specs(sess, flavorid)
+ resp = self._delete_flavor_extra_specs(sess, flavorid)
#delete flavor
- resp = self.delete_flavor(sess, flavorid)
+ resp = self._delete_flavor(sess, flavorid)
#return results
return Response(status=resp.status_code)
@@ -308,20 +297,19 @@ class Flavors(APIView):
return Response(data={'error': str(e)},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
-
- def delete_flavor_extra_specs(self, sess, flavorid):
+ def _delete_flavor_extra_specs(self, sess, flavorid):
logger.debug("Flavors--delete extra::> %s" % flavorid)
#delete extra specs one by one
- resp = self.get_flavor_extra_specs(sess, flavorid)
+ resp = self._get_flavor_extra_specs(sess, flavorid)
extra_specs = resp.json()
if extra_specs and extra_specs["extra_specs"]:
for k, _ in extra_specs["extra_specs"].items():
# just try to delete extra spec, but do not care if succeeded
- self.delete_flavor_one_extra_spec(sess, flavorid, k)
+ self._delete_flavor_one_extra_spec(sess, flavorid, k)
return resp
- def delete_flavor_one_extra_spec(self, sess, flavorid, extra_spec_key):
+ def _delete_flavor_one_extra_spec(self, sess, flavorid, extra_spec_key):
logger.debug("Flavors--delete 1 extra::> %s" % extra_spec_key)
# prepare request resource to vim instance
try:
@@ -332,10 +320,9 @@ class Flavors(APIView):
else:
raise VimDriverNewtonException(message="VIM newton exception",
content="internal bug in deleting flavor extra specs: %s" % extra_spec_key,
- status_code=500)
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
- resp = sess.delete(req_resouce, endpoint_filter=self.service)
- return resp
+ return sess.delete(req_resouce, endpoint_filter=self.service)
except HttpError as e:
logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
return Response(data=e.response.json(), status=e.http_status)
@@ -344,8 +331,7 @@ class Flavors(APIView):
return Response(data={'error': str(e)},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
-
- def delete_flavor(self, sess, flavorid):
+ def _delete_flavor(self, sess, flavorid):
logger.debug("Flavors--delete basic::> %s" % flavorid)
# prepare request resource to vim instance
req_resouce = "/flavors"
@@ -356,6 +342,4 @@ class Flavors(APIView):
content="internal bug in deleting flavor",
status_code=500)
- resp = sess.delete(req_resouce, endpoint_filter=self.service)
- return resp
-
+ return sess.delete(req_resouce, endpoint_filter=self.service)