summaryrefslogtreecommitdiffstats
path: root/windriver
diff options
context:
space:
mode:
Diffstat (limited to 'windriver')
-rw-r--r--windriver/titanium_cloud/extensions/urls.py3
-rw-r--r--windriver/titanium_cloud/extensions/views/epacaps.py2
-rw-r--r--windriver/titanium_cloud/extensions/views/extensions.py2
-rw-r--r--windriver/titanium_cloud/extensions/views/fcaps.py321
-rw-r--r--windriver/titanium_cloud/proxy/tests/test_identity_proxy.py167
-rw-r--r--windriver/titanium_cloud/proxy/views/identityV3.py2
-rw-r--r--windriver/titanium_cloud/proxy/views/services.py2
-rw-r--r--windriver/titanium_cloud/registration/views/registration.py2
-rw-r--r--windriver/titanium_cloud/resource/tests/test_capacity.py10
-rw-r--r--windriver/titanium_cloud/resource/tests/test_events.py362
-rw-r--r--windriver/titanium_cloud/resource/views/events.py102
-rw-r--r--windriver/titanium_cloud/settings.py2
-rw-r--r--windriver/titanium_cloud/urls.py4
13 files changed, 645 insertions, 336 deletions
diff --git a/windriver/titanium_cloud/extensions/urls.py b/windriver/titanium_cloud/extensions/urls.py
index afaad828..891faeb4 100644
--- a/windriver/titanium_cloud/extensions/urls.py
+++ b/windriver/titanium_cloud/extensions/urls.py
@@ -17,13 +17,10 @@ from rest_framework.urlpatterns import format_suffix_patterns
from titanium_cloud.extensions.views import extensions
from titanium_cloud.extensions.views import epacaps
-from titanium_cloud.extensions.views import fcaps
-
urlpatterns = [
url(r'^sions/?$', extensions.Extensions.as_view()),
url(r'^sions/epa-caps/?$', epacaps.EpaCaps.as_view()),
- url(r'^sions/guest-monitor/(?P<vserverid>[0-9a-zA-Z_-]+)/?$', fcaps.GuestMonitor.as_view()),
]
urlpatterns = format_suffix_patterns(urlpatterns)
diff --git a/windriver/titanium_cloud/extensions/views/epacaps.py b/windriver/titanium_cloud/extensions/views/epacaps.py
index 7efb71a6..025d55df 100644
--- a/windriver/titanium_cloud/extensions/views/epacaps.py
+++ b/windriver/titanium_cloud/extensions/views/epacaps.py
@@ -23,7 +23,7 @@ from newton_base.extensions import epacaps as newton_epacaps
logger = logging.getLogger(__name__)
-#DEBUG=True
+# DEBUG=True
class EpaCaps(newton_epacaps.EpaCaps):
diff --git a/windriver/titanium_cloud/extensions/views/extensions.py b/windriver/titanium_cloud/extensions/views/extensions.py
index d331b7b5..2cd91bf5 100644
--- a/windriver/titanium_cloud/extensions/views/extensions.py
+++ b/windriver/titanium_cloud/extensions/views/extensions.py
@@ -27,7 +27,7 @@ from newton_base.extensions import extensions as newton_extensions
logger = logging.getLogger(__name__)
-#DEBUG=True
+# DEBUG=True
class Extensions(newton_extensions.Extensions):
diff --git a/windriver/titanium_cloud/extensions/views/fcaps.py b/windriver/titanium_cloud/extensions/views/fcaps.py
deleted file mode 100644
index c36b2642..00000000
--- a/windriver/titanium_cloud/extensions/views/fcaps.py
+++ /dev/null
@@ -1,321 +0,0 @@
-# Copyright (c) 2017-2018 Wind River Systems, Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-import logging
-import json
-import traceback
-import threading
-
-from django.core.cache import cache
-
-from keystoneauth1.exceptions import HttpError
-from rest_framework import status
-from rest_framework.response import Response
-from rest_framework.views import APIView
-
-from django.conf import settings
-from common.exceptions import VimDriverNewtonException
-from newton_base.util import VimDriverUtils
-from common.msapi import extsys
-
-
-
-#from newton.extensions.views import fcaps as newton_fcaps
-
-logger = logging.getLogger(__name__)
-
-#DEBUG=True
-
-#dict to store running worker threads
-running_threads = {}
-running_thread_lock = threading.Lock()
-
-class GuestMonitorWorker (threading.Thread):
- service = {'service_type': 'platform',
- 'interface': 'public'}
- def __init__(self, vimid, tenantid=None):
- threading.Thread.__init__(self)
- self.vimid = vimid
- self.tenantid = tenantid
- self.eventid = '700.213' #Guest Heartbeat failed for instance
-
- def run(self):
- logger.debug("start GuestMonitorWorker %s,%s" % (self.vimid, self.tenantid))
-
- viminfo = VimDriverUtils.get_vim_info(self.vimid)
- sess = VimDriverUtils.get_session(viminfo, tenantid=self.tenantid)
-
- thread_info = running_threads.get(self.vimid)
- if not thread_info:
- return
-
- while thread_info.get('state') == 'start':
- #wait for jobs
- vservers = thread_info.get('vservers') if thread_info else None
- if not vservers:
- continue
-
- # do jobs
- for (vserverid, vserverinfo) in vservers.items():
- status_code, heartbeat_event = \
- self.monitor_heartbeat(self.vimid, self.tenantid, vserverid, viminfo, sess)
-
- if status_code == status.HTTP_403_FORBIDDEN:
- #invalid tenant, so remove this job
-
- running_thread_lock.acquire()
- thread_info['state'] = 'error'
- running_thread_lock.release()
-
- return #exit this thread since error
-
- if heartbeat_event:
- #report to VES
- #tbd
- pass
- else:
- continue
-
- running_thread_lock.acquire()
- thread_info['state'] = 'stopped'
- running_thread_lock.release()
-
- logger.debug("stop GuestMonitorWorker %s, %s, %s" % (self.vimid, self.tenantid, self.vserverid))
-# running_thread_lock.acquire()
-# running_threads.pop(self.vimid)
-# running_thread_lock.release()
-
- def monitor_heartbeat(self, vimid, tenantid, vserverid, viminfo, session):
- logger.debug("GuestMonitorWorker--monitor_heartbeat::> %s" % (vserverid))
- try:
- # prepare request resource to vim instance
- req_resouce = "/v1/event_log?q.field=entity_instance_id&\
- q.field=event_log_id&\
- q.op=eq&q.op=eq&q.type=&q.type=&\
- q.value=tenant\%%s.instance\%%s&\
- q.value=%s" % (tenantid, vserverid, self.eventid)
-
- resp = session.get(req_resouce, endpoint_filter=self.service,
- headers={"Content-Type": "application/json",
- "Accept": "application/json"})
-
- logger.debug("response status code of monitor_heartbeat %s" % resp.status_code)
-
- return resp.status_code, resp.json() if resp.content else None
-
- except HttpError as e:
- logger.error("monitor_heartbeat, HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
- return e.http_status, e.response.json()
- except Exception as e:
- logger.error(traceback.format_exc())
- logger.error("Failed to monitor_heartbeat:%s" % str(e))
- return e.http_status, e.response.json()
-
-
-class GuestMonitor(APIView):
-
- def __init__(self):
- self.proxy_prefix = settings.MULTICLOUD_PREFIX
- self._logger = logger
-
- def post(self, request, vimid="", vserverid=""):
- '''Start guest monitoring on specified virtual server'''
- self._logger.debug("GuestMonitor--post::data> %s" % request.data)
- self._logger.debug("GuestMonitor--post::vimid > %s" % vimid)
-
- try:
- # populate proxy identity url
- cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)
-
- tenant_name = request.data.get('tenantName')
- tenant_id = request.data.get('tenantID')
- ves_url = request.data.get('vesurl')
-
- # prepare request resource to vim instance
- # get token:
- viminfo = VimDriverUtils.get_vim_info(vimid)
- # the tenant should have the privilege to access the event-log API
- # usually it is 'admin'. Otherwise the 403 will be returned.
- sess = None
- if tenant_id:
- sess = VimDriverUtils.get_session(viminfo, tenantid=tenant_id)
- else:
- sess = VimDriverUtils.get_session(viminfo, tenantname=tenant_name)
-
- #now try to convert tenant_name to tenant_id
- #tbd
-
- thread_info = running_threads[vimid]
-
- if thread_info and thread_info['state'] == 'error':
- #the thread is in error state, so recreate with new tenant_id
- running_thread_lock.acquire()
- running_threads.pop(vimid)
- running_thread_lock.release()
- thread_info = None
-
- if not thread_info:
- tmp_thread = GuestMonitorWorker(vimid, tenant_id)
- if not tmp_thread:
- raise VimDriverNewtonException(message="internal error",
- content="Fail to spawn thread for Guest Monitoring",
- status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
- thread_info = {
- 'thread': tmp_thread,
- 'tenantid':tenant_id,
- 'vservers':{},
- 'state':'start'
- }
-
- running_thread_lock.acquire()
- running_threads[vimid] = thread_info
- running_thread_lock.release()
- tmp_thread.start()
- else:
- thread_info['state'] = 'start'
-
-
- vservers = thread_info.get('vservers')
- vservers[vserverid] = {'vesurl': ves_url}
-
- return Response(status=status.HTTP_202_ACCEPTED)
-
- except VimDriverNewtonException as e:
- return Response(data={'error': e.content}, status=e.status_code)
- except HttpError as e:
- self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
- return Response(data=e.response.json(), status=e.http_status)
- except Exception as e:
- self._logger.error(traceback.format_exc())
- return Response(data={'error': str(e)},
- status=status.HTTP_500_INTERNAL_SERVER_ERROR)
-
-
- def GET(self, request, vimid="", vserverid=""):
- '''query guest monitoring on specified virtual server'''
- self._logger.debug("GuestMonitor--get::data> %s" % request.data)
- self._logger.debug("GuestMonitor--get::vimid > %s" % vimid)
-
- try:
- # populate proxy identity url
- cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)
-
- tenant_name = request.data.get('tenantName')
- tenant_id = request.data.get('tenantID')
- vserver_id = vserverid
-
- # prepare request resource to vim instance
- # get token:
- viminfo = VimDriverUtils.get_vim_info(vimid)
- # the tenant should have the privilege to access the event-log API
- # usually it is 'admin'. Otherwise the 403 will be returned.
- sess = None
- if tenant_id:
- sess = VimDriverUtils.get_session(viminfo, tenantid=tenant_id)
- else:
- sess = VimDriverUtils.get_session(viminfo, tenantname=tenant_name)
-
- #now try to convert tenant_name to tenant_id, and vserver_name to vserver_id
- #tbd
-
- thread_info = running_threads[vimid]
- if not thread_info \
- or not thread_info.get('vservers') \
- or not thread_info.get('vservers').get(vserverid):
- status_code = status.HTTP_204_NO_CONTENT
- content = {'error':
- 'Guest Monitor job is not created for this virtual server,\
- vim id: %s, vserver id: %s'
- % (self.vimid, vserverid)}
- pass
- elif thread_info['state'] == 'error':
- status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
- content = {'error':
- 'Guest Monitor job for this virtual server \
- (vim id: %s, vserver id: %s) failed due to: %s'
- % (self.vimid, vserverid, thread_info.get('message'))}
- pass
- else:
- vserverinfo = thread_info.get('vservers').get(vserverid)
- content = vserverinfo.get('message')
- status_code = vserverinfo.get('status') or status.HTTP_200_OK
- pass
-
- #return Response(status=status.HTTP_202_ACCEPTED)
- return Response(status=status_code, data=content)
-
- except VimDriverNewtonException as e:
- return Response(data={'error': e.content}, status=e.status_code)
- except HttpError as e:
- self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
- return Response(data=e.response.json(), status=e.http_status)
- except Exception as e:
- self._logger.error(traceback.format_exc())
- return Response(data={'error': str(e)},
- status=status.HTTP_500_INTERNAL_SERVER_ERROR)
-
-
-
- def DELETE(self, request, vimid="", vserverid=""):
- '''Stop guest monitoring on specified virtual server'''
- self._logger.debug("GuestMonitor--delete::data> %s" % request.data)
- self._logger.debug("GuestMonitor--delete::vimid > %s" % vimid)
-
- try:
- # populate proxy identity url
- cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)
-
- tenant_name = request.data.get('tenantName')
- tenant_id = request.data.get('tenantID')
-
- # prepare request resource to vim instance
- # get token:
- viminfo = VimDriverUtils.get_vim_info(vimid)
- # the tenant should have the privilege to access the event-log API
- # usually it is 'admin'. Otherwise the 403 will be returned.
- sess = None
- if tenant_id:
- sess = VimDriverUtils.get_session(viminfo, tenantid=tenant_id)
- else:
- sess = VimDriverUtils.get_session(viminfo, tenantname=tenant_name)
-
- #now try to convert tenant_name to tenant_id, and vserver_name to vserver_id
- #tbd
-
- thread_info = running_threads[vimid]
- if not thread_info:
- status_code = status.HTTP_204_NO_CONTENT
- else:
- vservers = thread_info.get('vservers')
- if vservers.get(vserverid):
- vservers.pop(vserverid)
-
- running_thread_lock.acquire()
- if len(vservers.items()) == 0:
- thread_info.stop()
- running_threads.pop(vimid)
- running_thread_lock.release()
- status_code = status.HTTP_202_ACCEPTED
-
- return Response(status=status_code)
-
- except VimDriverNewtonException as e:
- return Response(data={'error': e.content}, status=e.status_code)
- except HttpError as e:
- self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
- return Response(data=e.response.json(), status=e.http_status)
- except Exception as e:
- self._logger.error(traceback.format_exc())
- return Response(data={'error': str(e)},
- status=status.HTTP_500_INTERNAL_SERVER_ERROR)
diff --git a/windriver/titanium_cloud/proxy/tests/test_identity_proxy.py b/windriver/titanium_cloud/proxy/tests/test_identity_proxy.py
index cd7dd23c..81231220 100644
--- a/windriver/titanium_cloud/proxy/tests/test_identity_proxy.py
+++ b/windriver/titanium_cloud/proxy/tests/test_identity_proxy.py
@@ -574,3 +574,170 @@ class TestIdentityService(unittest.TestCase):
self.assertEqual(mock_info.MOCK_TOKEN_ID,
context['access']['token']["id"])
self.assertIsNotNone(context['access']['serviceCatalog'])
+
+ @mock.patch.object(VimDriverUtils, 'get_vim_info')
+ @mock.patch.object(VimDriverUtils, 'get_session')
+ @mock.patch.object(VimDriverUtils, 'get_auth_state')
+ @mock.patch.object(VimDriverUtils, 'update_token_cache')
+ def test_token_with_tenantname(self, mock_update_token_cache, mock_get_auth_state,
+ mock_get_session, mock_get_vim_info):
+ '''
+ test API: get token
+ :param mock_update_token_cache:
+ :param mock_get_auth_state:
+ :param mock_get_session:
+ :param mock_get_vim_info:
+ :return:
+ '''
+
+ # mock VimDriverUtils APIs
+ mock_session_specs = ["get"]
+ mock_session_get_response = {'status': 200}
+ mock_session = mock.Mock(name='mock_session',
+ spec=mock_session_specs)
+ mock_session.get.return_value = mock_session_get_response
+
+ mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
+ mock_get_session.return_value = mock_session
+ mock_get_auth_state.return_value = json.dumps(mock_auth_state)
+ mock_update_token_cache.return_value = mock_info.MOCK_TOKEN_ID
+
+ # simulate client to make the request
+ token_data = {
+ "auth": {
+ "identity": {
+ "methods": ["password"],
+ "password": {
+ "user": {
+ "name": "demo",
+ "domain": {"name": "Default"},
+ "password": "demo"
+ }
+ }
+ },
+ "scope": {
+ "project": {
+ "domain": {"name":"Default"},
+ "name": "Integration"
+ }
+ }
+ }
+ }
+
+ response = self.client.post(
+ "/api/multicloud-titanium_cloud/v0/windriver-hudson-dc_RegionOne/identity/v3/auth/tokens",
+ data=json.dumps(token_data), content_type='application/json')
+ self.failUnlessEqual(status.HTTP_201_CREATED,
+ response.status_code)
+ context = response.json()
+
+ self.assertEqual(mock_info.MOCK_TOKEN_ID,
+ response['X-Subject-Token'])
+ self.assertIsNotNone(context['token']['catalog'])
+
+ @mock.patch.object(VimDriverUtils, 'get_vim_info')
+ @mock.patch.object(VimDriverUtils, 'get_session')
+ @mock.patch.object(VimDriverUtils, 'get_auth_state')
+ @mock.patch.object(VimDriverUtils, 'update_token_cache')
+ def test_tokensV2_with_tenantname(self, mock_update_token_cache, mock_get_auth_state,
+ mock_get_session, mock_get_vim_info):
+ '''
+ test API: get token
+ :param mock_update_token_cache:
+ :param mock_get_auth_state:
+ :param mock_get_session:
+ :param mock_get_vim_info:
+ :return:
+ '''
+
+ # mock VimDriverUtils APIs
+ mock_session_specs = ["get"]
+ mock_session_get_response = {'status': 200}
+ mock_session = mock.Mock(name='mock_session',
+ spec=mock_session_specs)
+ mock_session.get.return_value = mock_session_get_response
+
+ mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
+ mock_get_session.return_value = mock_session
+ mock_get_auth_state.return_value = json.dumps(mock_auth_state)
+ mock_update_token_cache.return_value = mock_info.MOCK_TOKEN_ID
+
+ # simulate client to make the request
+ token_data = {
+ "auth": {
+ "tenantName": "Integration",
+ "passwordCredentials": {
+ "username": "demo",
+ "password": "demo"
+ }
+ }
+ }
+
+ response = self.client.post(
+ "/api/multicloud-titanium_cloud/v0/windriver-hudson-dc_RegionOne/identity/v2.0/tokens",
+ data=json.dumps(token_data), content_type='application/json')
+ self.failUnlessEqual(status.HTTP_200_OK,
+ response.status_code)
+ context = response.json()
+
+ self.assertIsNotNone(context['access']['token'])
+ self.assertEqual(mock_info.MOCK_TOKEN_ID,
+ context['access']['token']["id"])
+ self.assertIsNotNone(context['access']['serviceCatalog'])
+
+ @mock.patch.object(VimDriverUtils, 'get_vim_info')
+ @mock.patch.object(VimDriverUtils, 'get_session')
+ @mock.patch.object(VimDriverUtils, 'get_auth_state')
+ @mock.patch.object(VimDriverUtils, 'update_token_cache')
+ def test_token_with_projectid(self, mock_update_token_cache, mock_get_auth_state,
+ mock_get_session, mock_get_vim_info):
+ '''
+ test API: get token
+ :param mock_update_token_cache:
+ :param mock_get_auth_state:
+ :param mock_get_session:
+ :param mock_get_vim_info:
+ :return:
+ '''
+
+ # mock VimDriverUtils APIs
+ mock_session_specs = ["get"]
+ mock_session_get_response = {'status': 200}
+ mock_session = mock.Mock(name='mock_session',
+ spec=mock_session_specs)
+ mock_session.get.return_value = mock_session_get_response
+
+ mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
+ mock_get_session.return_value = mock_session
+ mock_get_auth_state.return_value = json.dumps(mock_auth_state)
+ mock_update_token_cache.return_value = mock_info.MOCK_TOKEN_ID
+
+ # simulate client to make the request
+ token_data = {
+ "auth": {
+ "identity": {
+ "methods": ["password"],
+ "password": {
+ "user": {
+ "name": "demo",
+ "password": "demo"
+ }
+ }
+ },
+ "scope": {
+ "project": {"id": "dd327af0542e47d7853e0470fe9ad625"}
+ }
+ }
+ }
+
+ response = self.client.post(
+ "/api/multicloud-titanium_cloud/v0/windriver-hudson-dc_RegionOne/identity/v3/auth/tokens",
+ data=json.dumps(token_data), content_type='application/json')
+ self.failUnlessEqual(status.HTTP_201_CREATED,
+ response.status_code)
+ context = response.json()
+
+ self.assertEqual(mock_info.MOCK_TOKEN_ID,
+ response['X-Subject-Token'])
+ self.assertIsNotNone(context['token']['catalog'])
+
diff --git a/windriver/titanium_cloud/proxy/views/identityV3.py b/windriver/titanium_cloud/proxy/views/identityV3.py
index c831d017..eaeeca47 100644
--- a/windriver/titanium_cloud/proxy/views/identityV3.py
+++ b/windriver/titanium_cloud/proxy/views/identityV3.py
@@ -19,7 +19,7 @@ from newton_base.proxy import identityV3 as newton_identityV3
logger = logging.getLogger(__name__)
-#DEBUG=True
+# DEBUG=True
class Tokens(newton_identityV3.Tokens):
diff --git a/windriver/titanium_cloud/proxy/views/services.py b/windriver/titanium_cloud/proxy/views/services.py
index c1d4f194..d7c1dc05 100644
--- a/windriver/titanium_cloud/proxy/views/services.py
+++ b/windriver/titanium_cloud/proxy/views/services.py
@@ -21,7 +21,7 @@ from newton_base.proxy import services as newton_services
logger = logging.getLogger(__name__)
-#DEBUG=True
+# DEBUG=True
class Services(newton_services.Services):
diff --git a/windriver/titanium_cloud/registration/views/registration.py b/windriver/titanium_cloud/registration/views/registration.py
index bdf89a15..769040ff 100644
--- a/windriver/titanium_cloud/registration/views/registration.py
+++ b/windriver/titanium_cloud/registration/views/registration.py
@@ -26,7 +26,7 @@ from keystoneauth1.exceptions import HttpError
logger = logging.getLogger(__name__)
-DEBUG=True
+# DEBUG=True
class Registry(newton_registration.Registry):
diff --git a/windriver/titanium_cloud/resource/tests/test_capacity.py b/windriver/titanium_cloud/resource/tests/test_capacity.py
index 3dae1080..e4dec752 100644
--- a/windriver/titanium_cloud/resource/tests/test_capacity.py
+++ b/windriver/titanium_cloud/resource/tests/test_capacity.py
@@ -88,9 +88,8 @@ class TestCapacity(test_base.TestRequest):
]
})
- response = self.client.post((
- "/api/%s/v0/windriver-hudson-dc_RegionOne/"
- "capacity_check" % test_base.MULTIVIM_VERSION),
+ response = self.client.post(
+ "/api/multicloud-titanium_cloud/v0/windriver-hudson-dc_RegionOne/capacity_check",
TEST_REQ_SUCCESS_SOURCE,
HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
@@ -110,9 +109,8 @@ class TestCapacity(test_base.TestRequest):
]
})
- response = self.client.post((
- "/api/%s/v0/windriver-hudson-dc_RegionOne/"
- "capacity_check" % test_base.MULTIVIM_VERSION),
+ response = self.client.post(
+ "/api/multicloud-titanium_cloud/v0/windriver-hudson-dc_RegionOne/capacity_check",
TEST_REQ_FAILED_SOURCE,
HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
diff --git a/windriver/titanium_cloud/resource/tests/test_events.py b/windriver/titanium_cloud/resource/tests/test_events.py
new file mode 100644
index 00000000..3db76d66
--- /dev/null
+++ b/windriver/titanium_cloud/resource/tests/test_events.py
@@ -0,0 +1,362 @@
+# Copyright (c) 2017-2018 Wind River Systems, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import mock
+
+from rest_framework import status
+
+from common.utils import restcall
+from newton_base.tests import mock_info
+from newton_base.tests import test_base
+from newton_base.util import VimDriverUtils
+
+MOCK_GET_SERVERS_DETAIL_RESPONSE = {
+ "servers" : [
+ {
+ "accessIPv4" : "",
+ "OS-EXT-SRV-ATTR:instance_name" : "instance-0000000a",
+ "OS-SRV-USG:terminated_at" : "",
+ "accessIPv6" : "",
+ "config_drive" : "",
+ "OS-DCF:diskConfig" : "AUTO",
+ "updated" : "2018-03-27T02:17:12Z",
+ "metadata" : {},
+ "id" : "12f5b1d0-fe5c-469f-a7d4-b62a91134bf8",
+ "flavor" : {
+ "id" : "60edb520-5826-4ae7-9e07-709b19ba6f39",
+ "links" : [
+ {
+ "rel" : "bookmark",
+ "href" : "http://192.168.100.100:8774/ad979139d5ea4a84b21b3620c0e4761e/flavors/60edb520-5826-4ae7-9e07-709b19ba6f39"
+ }
+ ]
+ },
+ "links" : [
+ {
+ "rel" : "self",
+ "href" : "http://192.168.100.100:8774/v2.1/ad979139d5ea4a84b21b3620c0e4761e/servers/12f5b1d0-fe5c-469f-a7d4-b62a91134bf8"
+ },
+ {
+ "rel" : "bookmark",
+ "href" : "http://192.168.100.100:8774/ad979139d5ea4a84b21b3620c0e4761e/servers/12f5b1d0-fe5c-469f-a7d4-b62a91134bf8"
+ }
+ ],
+ "OS-EXT-SRV-ATTR:host" : "compute-0",
+ "OS-EXT-AZ:availability_zone" : "nova",
+ "name" : "test1",
+ "wrs-res:pci_devices" : "",
+ "hostId" : "b3479a460f5effda10c6fdb860e824be631026c1d09f551479180577",
+ "user_id" : "777155411f3042c9b7e3194188d6f85d",
+ "status" : "PAUSED",
+ "OS-EXT-STS:power_state" : 3,
+ "OS-EXT-SRV-ATTR:hypervisor_hostname" : "compute-0",
+ "tenant_id" : "ad979139d5ea4a84b21b3620c0e4761e",
+ "OS-SRV-USG:launched_at" : "2018-03-27T02:16:40.000000",
+ "OS-EXT-STS:vm_state" : "paused",
+ "wrs-if:nics" : [
+ {
+ "nic1" : {
+ "mac_address" : "fa:16:3e:5f:1a:76",
+ "network" : "mgmt",
+ "port_id" : "6c225c23-abe3-42a8-8909-83471503d5d4",
+ "vif_model" : "virtio",
+ "vif_pci_address" : "",
+ "mtu" : 9216
+ }
+ },
+ {
+ "nic2" : {
+ "mac_address" : "fa:16:3e:7c:7b:d7",
+ "network" : "data0",
+ "port_id" : "cbea2fec-c9b8-48ec-a964-0e3e255841bc",
+ "vif_model" : "virtio",
+ "vif_pci_address" : "",
+ "mtu" : 9216
+ }
+ }
+ ],
+ "wrs-sg:server_group" : "",
+ "OS-EXT-STS:task_state" : "",
+ "wrs-res:topology" : "node:0, 1024MB, pgsize:2M, 1s,1c,2t, vcpus:0,1, pcpus:5,21, siblings:{0,1}, pol:ded, thr:pre\nnode:1, 1024MB, pgsize:2M, 1s,1c,2t, vcpus:2,3, pcpus:8,24, siblings:{2,3}, pol:ded, thr:pre",
+ "wrs-res:vcpus" : [
+ 4,
+ 4,
+ 4
+ ],
+ "key_name" : "",
+ "image" : {
+ "id" : "7ba636ef-5dfd-4e67-ad32-cd23ee74e1eb",
+ "links" : [
+ {
+ "rel" : "bookmark",
+ "href" : "http://192.168.100.100:8774/ad979139d5ea4a84b21b3620c0e4761e/images/7ba636ef-5dfd-4e67-ad32-cd23ee74e1eb"
+ }
+ ]
+ },
+ "created" : "2018-03-27T02:16:32Z",
+ "addresses" : {
+ "data0" : [
+ {
+ "OS-EXT-IPS:type" : "fixed",
+ "version" : 4,
+ "OS-EXT-IPS-MAC:mac_addr" : "fa:16:3e:7c:7b:d7",
+ "addr" : "192.168.2.8"
+ }
+ ],
+ "mgmt" : [
+ {
+ "OS-EXT-IPS:type" : "fixed",
+ "version" : 4,
+ "OS-EXT-IPS-MAC:mac_addr" : "fa:16:3e:5f:1a:76",
+ "addr" : "192.168.1.6"
+ }
+ ]
+ },
+ "os-extended-volumes:volumes_attached" : []
+ },
+ {
+ "accessIPv4" : "",
+ "OS-EXT-SRV-ATTR:instance_name" : "instance-00000009",
+ "OS-SRV-USG:terminated_at" : "",
+ "accessIPv6" : "",
+ "config_drive" : "",
+ "OS-DCF:diskConfig" : "AUTO",
+ "updated" : "2018-03-27T02:12:21Z",
+ "metadata" : {},
+ "id" : "3f1b0375-a1db-4d94-b336-f32c82c0d7ec",
+ "flavor" : {
+ "id" : "0d3b1381-1626-4f6b-869b-4a4d5d42085e",
+ "links" : [
+ {
+ "rel" : "bookmark",
+ "href" : "http://192.168.100.100:8774/ad979139d5ea4a84b21b3620c0e4761e/flavors/0d3b1381-1626-4f6b-869b-4a4d5d42085e"
+ }
+ ]
+ },
+ "links" : [
+ {
+ "rel" : "self",
+ "href" : "http://192.168.100.100:8774/v2.1/ad979139d5ea4a84b21b3620c0e4761e/servers/3f1b0375-a1db-4d94-b336-f32c82c0d7ec"
+ },
+ {
+ "rel" : "bookmark",
+ "href" : "http://192.168.100.100:8774/ad979139d5ea4a84b21b3620c0e4761e/servers/3f1b0375-a1db-4d94-b336-f32c82c0d7ec"
+ }
+ ],
+ "OS-EXT-SRV-ATTR:host" : "compute-0",
+ "OS-EXT-AZ:availability_zone" : "nova",
+ "name" : "test2",
+ "wrs-res:pci_devices" : "",
+ "hostId" : "b3479a460f5effda10c6fdb860e824be631026c1d09f551479180577",
+ "user_id" : "777155411f3042c9b7e3194188d6f85d",
+ "status" : "ACTIVE",
+ "OS-EXT-STS:power_state" : 1,
+ "OS-EXT-SRV-ATTR:hypervisor_hostname" : "compute-0",
+ "tenant_id" : "ad979139d5ea4a84b21b3620c0e4761e",
+ "OS-SRV-USG:launched_at" : "2018-03-27T02:12:21.000000",
+ "OS-EXT-STS:vm_state" : "active",
+ "wrs-if:nics" : [
+ {
+ "nic1" : {
+ "mac_address" : "fa:16:3e:54:f8:a6",
+ "network" : "mgmt",
+ "port_id" : "30e2f51c-4473-4650-9ae9-a35e5d7ad452",
+ "vif_model" : "avp",
+ "vif_pci_address" : "",
+ "mtu" : 9216
+ }
+ }
+ ],
+ "wrs-sg:server_group" : "",
+ "OS-EXT-STS:task_state" : "",
+ "wrs-res:topology" : "node:0, 4096MB, pgsize:2M, 1s,3c,1t, vcpus:0-2, pcpus:4,20,7, pol:ded, thr:pre",
+ "progress" : 0,
+ "wrs-res:vcpus" : [
+ 3,
+ 3,
+ 3
+ ],
+ "key_name" : "",
+ "image" : {
+ "id" : "7ba636ef-5dfd-4e67-ad32-cd23ee74e1eb",
+ "links" : [
+ {
+ "rel" : "bookmark",
+ "href" : "http://192.168.100.100:8774/ad979139d5ea4a84b21b3620c0e4761e/images/7ba636ef-5dfd-4e67-ad32-cd23ee74e1eb"
+ }
+ ]
+ },
+ "created" : "2018-03-27T02:10:26Z",
+ "addresses" : {
+ "mgmt" : [
+ {
+ "OS-EXT-IPS:type" : "fixed",
+ "version" : 4,
+ "OS-EXT-IPS-MAC:mac_addr" : "fa:16:3e:54:f8:a6",
+ "addr" : "192.168.1.11"
+ }
+ ]
+ },
+ "os-extended-volumes:volumes_attached" : []
+ },
+ {
+ "accessIPv4" : "",
+ "OS-EXT-SRV-ATTR:instance_name" : "instance-00000008",
+ "OS-SRV-USG:terminated_at" : "",
+ "accessIPv6" : "",
+ "config_drive" : "",
+ "OS-DCF:diskConfig" : "AUTO",
+ "updated" : "2018-03-27T02:12:15Z",
+ "metadata" : {},
+ "id" : "1b6f6671-b680-42cd-89e9-fc4ddd5d2e02",
+ "flavor" : {
+ "id" : "0d3b1381-1626-4f6b-869b-4a4d5d42085e",
+ "links" : [
+ {
+ "rel" : "bookmark",
+ "href" : "http://192.168.100.100:8774/ad979139d5ea4a84b21b3620c0e4761e/flavors/0d3b1381-1626-4f6b-869b-4a4d5d42085e"
+ }
+ ]
+ },
+ "links" : [
+ {
+ "rel" : "self",
+ "href" : "http://192.168.100.100:8774/v2.1/ad979139d5ea4a84b21b3620c0e4761e/servers/1b6f6671-b680-42cd-89e9-fc4ddd5d2e02"
+ },
+ {
+ "rel" : "bookmark",
+ "href" : "http://192.168.100.100:8774/ad979139d5ea4a84b21b3620c0e4761e/servers/1b6f6671-b680-42cd-89e9-fc4ddd5d2e02"
+ }
+ ],
+ "OS-EXT-SRV-ATTR:host" : "compute-0",
+ "OS-EXT-AZ:availability_zone" : "nova",
+ "name" : "test3",
+ "wrs-res:pci_devices" : "",
+ "hostId" : "b3479a460f5effda10c6fdb860e824be631026c1d09f551479180577",
+ "user_id" : "777155411f3042c9b7e3194188d6f85d",
+ "status" : "ACTIVE",
+ "OS-EXT-STS:power_state" : 1,
+ "OS-EXT-SRV-ATTR:hypervisor_hostname" : "compute-0",
+ "tenant_id" : "ad979139d5ea4a84b21b3620c0e4761e",
+ "OS-SRV-USG:launched_at" : "2018-03-27T02:12:15.000000",
+ "OS-EXT-STS:vm_state" : "active",
+ "wrs-if:nics" : [
+ {
+ "nic1" : {
+ "mac_address" : "fa:16:3e:4e:9b:75",
+ "network" : "mgmt",
+ "port_id" : "72d13987-1d94-4a64-aa1a-973869ae1cad",
+ "vif_model" : "avp",
+ "vif_pci_address" : "",
+ "mtu" : 9216
+ }
+ }
+ ],
+ "wrs-sg:server_group" : "",
+ "OS-EXT-STS:task_state" : "",
+ "wrs-res:topology" : "node:0, 4096MB, pgsize:2M, 1s,3c,1t, vcpus:0-2, pcpus:19,3,22, pol:ded, thr:pre",
+ "progress" : 0,
+ "wrs-res:vcpus" : [
+ 3,
+ 3,
+ 3
+ ],
+ "key_name" : "",
+ "image" : {
+ "id" : "7ba636ef-5dfd-4e67-ad32-cd23ee74e1eb",
+ "links" : [
+ {
+ "rel" : "bookmark",
+ "href" : "http://192.168.100.100:8774/ad979139d5ea4a84b21b3620c0e4761e/images/7ba636ef-5dfd-4e67-ad32-cd23ee74e1eb"
+ }
+ ]
+ },
+ "created" : "2018-03-27T02:10:01Z",
+ "addresses" : {
+ "mgmt" : [
+ {
+ "OS-EXT-IPS:type" : "fixed",
+ "version" : 4,
+ "OS-EXT-IPS-MAC:mac_addr" : "fa:16:3e:4e:9b:75",
+ "addr" : "192.168.1.8"
+ }
+ ]
+ },
+ "os-extended-volumes:volumes_attached" : []
+ }
+ ]
+}
+
+SUCCESS_VMSTATE_RESPONSE = {
+'result':[
+ {
+ 'name': 'test1',
+ 'power_state': 3,
+ 'id': '12f5b1d0-fe5c-469f-a7d4-b62a91134bf8',
+ 'state': 'paused',
+ 'tenant_id': 'ad979139d5ea4a84b21b3620c0e4761e',
+ 'host': 'compute-0',
+ 'availability_zone': 'nova',
+ 'launched_at': '2018-03-27T02:16:40.000000'
+ },
+ {
+ 'name': 'test2',
+ 'power_state': 1,
+ 'id': '3f1b0375-a1db-4d94-b336-f32c82c0d7ec',
+ 'state': 'active',
+ 'tenant_id': 'ad979139d5ea4a84b21b3620c0e4761e',
+ 'host': 'compute-0',
+ 'availability_zone': 'nova',
+ 'launched_at': '2018-03-27T02:12:21.000000'
+ },
+ {
+ 'name': 'test3',
+ 'power_state': 1,
+ 'id': '1b6f6671-b680-42cd-89e9-fc4ddd5d2e02',
+ 'state': 'active',
+ 'tenant_id': 'ad979139d5ea4a84b21b3620c0e4761e',
+ 'host': 'compute-0',
+ 'availability_zone': 'nova',
+ 'launched_at': '2018-03-27T02:12:15.000000'
+ }
+ ]
+}
+
+class TestEvents(test_base.TestRequest):
+ def setUp(self):
+ super(TestEvents, self).setUp()
+
+ def _get_mock_response(self, return_value=None):
+ mock_response = mock.Mock(spec=test_base.MockResponse)
+ mock_response.status_code = status.HTTP_200_OK
+ mock_response.json.return_value = return_value
+ return mock_response
+
+ @mock.patch.object(VimDriverUtils, 'get_session')
+ @mock.patch.object(VimDriverUtils, 'get_vim_info')
+ def test_events_check_success(self, mock_get_vim_info, mock_get_session):
+ mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
+ mock_get_session.return_value = test_base.get_mock_session(
+ ["get"], {
+ "side_effect": [
+ self._get_mock_response(MOCK_GET_SERVERS_DETAIL_RESPONSE),
+ ]
+ })
+
+ response = self.client.post(
+ "/api/multicloud-titanium_cloud/v0/windriver-hudson-dc_RegionOne/events_check",
+ HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
+
+ self.assertEquals(status.HTTP_200_OK, response.status_code)
+ self.assertEqual(SUCCESS_VMSTATE_RESPONSE, response.data)
diff --git a/windriver/titanium_cloud/resource/views/events.py b/windriver/titanium_cloud/resource/views/events.py
new file mode 100644
index 00000000..0c413e57
--- /dev/null
+++ b/windriver/titanium_cloud/resource/views/events.py
@@ -0,0 +1,102 @@
+# Copyright (c) 2017-2018 Wind River Systems, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import logging
+import json
+import traceback
+
+from rest_framework import status
+
+from django.conf import settings
+from common.exceptions import VimDriverNewtonException
+from newton_base.util import VimDriverUtils
+
+from keystoneauth1.exceptions import HttpError
+from rest_framework import status
+from rest_framework.response import Response
+from rest_framework.views import APIView
+from common.msapi import extsys
+
+
+logger = logging.getLogger(__name__)
+
+
+class EventsCheck(APIView):
+
+ def __init__(self):
+ self._logger = logger
+
+ def post(self, request, vimid=""):
+ self._logger.info("vimid, data> %s, %s" % (vimid, request.data))
+ self._logger.debug("META> %s" % request.META)
+
+ try :
+ resource_demand = request.data
+
+ # get token:
+ cloud_owner, regionid = extsys.decode_vim_id(vimid)
+ interface = 'public'
+ service = {'service_type': 'compute',
+ 'interface': interface,
+ 'region_id': regionid}
+
+ tenant_name = None
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenant_name)
+
+ # get servers detail info
+ req_resouce = "/servers/detail"
+ self._logger.info("check servers detail> URI:%s" % req_resouce)
+ resp = sess.get(req_resouce, endpoint_filter=service)
+ self._logger.info("check servers detail> status:%s" % resp.status_code)
+ content = resp.json()
+ self._logger.debug("check servers detail> resp data:%s" % content)
+
+ # extract server status info
+ if len(content['servers']):
+ servers = content['servers']
+ resp_vmstate = []
+ for num in range(0, len(servers)):
+ vmstate = {
+ 'name' : servers[num]['name'],
+ 'state' : servers[num]['OS-EXT-STS:vm_state'],
+ 'power_state' : servers[num]['OS-EXT-STS:power_state'],
+ 'launched_at' : servers[num]['OS-SRV-USG:launched_at'],
+ 'id' : servers[num]['id'],
+ 'host' : servers[num]['OS-EXT-SRV-ATTR:host'],
+ 'availability_zone' : servers[num]['OS-EXT-AZ:availability_zone'],
+ 'tenant_id' : servers[num]['tenant_id']
+ }
+
+ resp_vmstate.append(vmstate)
+
+ self._logger.info("RESP with data> result:%s" % resp_vmstate)
+ return Response(data={'result': resp_vmstate}, status=status.HTTP_200_OK)
+
+ except VimDriverNewtonException as e:
+ self._logger.error("Plugin exception> status:%s,error:%s"
+ % (e.status_code, e.content))
+ return Response(data={'result': resp_vmstate,'error': e.content}, status=e.status_code)
+
+ except HttpError as e:
+ self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ resp = e.response.json()
+ resp.update({'result': resp_vmstate})
+ return Response(data=e.response.json(), status=e.http_status)
+
+ except Exception as e:
+ self._logger.error(traceback.format_exc())
+ return Response(data={'result': resp_vmstate, 'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
diff --git a/windriver/titanium_cloud/settings.py b/windriver/titanium_cloud/settings.py
index f05991a9..c3da724f 100644
--- a/windriver/titanium_cloud/settings.py
+++ b/windriver/titanium_cloud/settings.py
@@ -32,7 +32,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = '3o-wney!99y)^h3v)0$j16l9=fdjxcb+a8g+q3tfbahcnu2b0o'
# SECURITY WARNING: don't run with debug turned on in production!
-#DEBUG = True
+# DEBUG = True
ALLOWED_HOSTS = ['*']
diff --git a/windriver/titanium_cloud/urls.py b/windriver/titanium_cloud/urls.py
index 5b8521f9..a6fcdcfb 100644
--- a/windriver/titanium_cloud/urls.py
+++ b/windriver/titanium_cloud/urls.py
@@ -17,6 +17,7 @@ from django.conf.urls import include, url
from titanium_cloud.registration.views import registration
from newton_base.openoapi import tenants
from titanium_cloud.resource.views import capacity
+from titanium_cloud.resource.views import events
urlpatterns = [
url(r'^', include('titanium_cloud.swagger.urls')),
@@ -36,6 +37,9 @@ urlpatterns = [
# CapacityCheck
url(r'^api/multicloud-titanium_cloud/v0/(?P<vimid>[0-9a-zA-Z_-]+)/capacity_check/?$',
capacity.CapacityCheck.as_view()),
+ # events
+ url(r'^api/multicloud-titanium_cloud/v0/(?P<vimid>[0-9a-zA-Z_-]+)/events_check/?$',
+ events.EventsCheck.as_view()),
]