This is an automated email from the ASF dual-hosted git repository.

machristie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git

commit ee936e76126365fdd6f8009b679cd6a0c6915cfd
Author: Marcus Christie <[email protected]>
AuthorDate: Thu Jan 18 17:37:25 2018 -0500

    AIRAVATA-2645 Profile Service (multiplexed) client middleware
---
 django_airavata/middleware.py            | 90 +++++++++++++++++++++++++++++++-
 django_airavata/settings.py              |  3 +-
 django_airavata/settings_local.py.sample |  5 ++
 3 files changed, 95 insertions(+), 3 deletions(-)

diff --git a/django_airavata/middleware.py b/django_airavata/middleware.py
index fa10349..f407864 100644
--- a/django_airavata/middleware.py
+++ b/django_airavata/middleware.py
@@ -1,12 +1,25 @@
 
 from airavata.api import Airavata
 from airavata.api.sharing import SharingRegistryService
+from airavata.service.profile.groupmanager.cpi.constants \
+    import GROUP_MANAGER_CPI_NAME
+from airavata.service.profile.groupmanager.cpi import GroupManagerService
+from airavata.service.profile.iam.admin.services.cpi.constants \
+    import IAM_ADMIN_SERVICES_CPI_NAME
+from airavata.service.profile.iam.admin.services.cpi import IamAdminServices
+from airavata.service.profile.tenant.cpi.constants \
+    import TENANT_PROFILE_CPI_NAME
+from airavata.service.profile.tenant.cpi import TenantProfileService
+from airavata.service.profile.user.cpi.constants \
+    import USER_PROFILE_CPI_NAME
+from airavata.service.profile.user.cpi import UserProfileService
 
 from thrift import Thrift
 from thrift.transport import TSSLSocket
 from thrift.transport import TSocket
 from thrift.transport import TTransport
 from thrift.protocol import TBinaryProtocol
+from thrift.protocol.TMultiplexedProtocol import TMultiplexedProtocol
 
 from django.conf import settings
 
@@ -54,6 +67,31 @@ def get_sharing_client(transport):
 
     return SharingRegistryService.Client(protocol)
 
+
+def get_binary_protocol(transport):
+    return TBinaryProtocol.TBinaryProtocol(transport)
+
+
+def get_group_manager_client(protocol):
+    multiplex_prot = TMultiplexedProtocol(protocol, GROUP_MANAGER_CPI_NAME)
+    return GroupManagerService.Client(multiplex_prot)
+
+
+def get_iamadmin_client(protocol):
+    multiplex_prot = TMultiplexedProtocol(protocol, 
IAM_ADMIN_SERVICES_CPI_NAME)
+    return IamAdminServices.Client(multiplex_prot)
+
+
+def get_tenant_profile_client(protocol):
+    multiplex_prot = TMultiplexedProtocol(protocol, TENANT_PROFILE_CPI_NAME)
+    return TenantProfileService.Client(multiplex_prot)
+
+
+def get_user_profile_client(protocol):
+    multiplex_prot = TMultiplexedProtocol(protocol, USER_PROFILE_CPI_NAME)
+    return UserProfileService.Client(multiplex_prot)
+
+
 def airavata_client(get_response):
     "Open and close Airavata client for each request"
 
@@ -80,7 +118,6 @@ def airavata_client(get_response):
 
             if transport.isOpen():
                 transport.close()
-                logger.debug("transport closed in middleware")
         else:
             response = get_response(request)
 
@@ -114,7 +151,56 @@ def sharing_client(get_response):
 
             if transport.isOpen():
                 transport.close()
-                logger.debug("transport closed in middleware")
+        else:
+            response = get_response(request)
+
+        return response
+
+    return middleware
+
+
+def profile_service_client(get_response):
+    """Open and close Profile Service client for each request.
+
+    Usage:
+        request.profile_service['group_manager'].getGroup(
+            request.authz_token, groupId)
+    """
+    def middleware(request):
+
+        # If user is logged in create a profile service client for the request
+        if request.user.is_authenticated:
+            transport = get_transport(settings.PROFILE_SERVICE_HOST,
+                                      settings.PROFILE_SERVICE_PORT,
+                                      settings.PROFILE_SERVICE_SECURE)
+            binary_prot = get_binary_protocol(transport)
+            group_manager_client = get_group_manager_client(binary_prot)
+            iam_admin_client = get_iamadmin_client(binary_prot)
+            tenant_profile_client = get_tenant_profile_client(binary_prot)
+            user_profile_client = get_user_profile_client(binary_prot)
+
+            try:
+                transport.open()
+            except Exception as e:
+                logger.exception("Failed to open thrift connection to "
+                                 "Profile Service server")
+
+            if transport.isOpen():
+                request.profile_service = {
+                    'group_manager': group_manager_client,
+                    'iam_admin': iam_admin_client,
+                    'tenant_profile': tenant_profile_client,
+                    'user_profile': user_profile_client,
+                }
+            else:
+                # if request.profile_service is None, this will indicate to
+                # view code that the Profile Service is down
+                request.profile_service = None
+
+            response = get_response(request)
+
+            if transport.isOpen():
+                transport.close()
         else:
             response = get_response(request)
 
diff --git a/django_airavata/settings.py b/django_airavata/settings.py
index f123e60..32f4c25 100644
--- a/django_airavata/settings.py
+++ b/django_airavata/settings.py
@@ -55,7 +55,8 @@ MIDDLEWARE = [
     'django.middleware.clickjacking.XFrameOptionsMiddleware',
     'django_airavata.apps.auth.middleware.authz_token_middleware',
     'django_airavata.middleware.airavata_client',
-    'django_airavata.middleware.sharing_client',
+    # 'django_airavata.middleware.sharing_client',
+    'django_airavata.middleware.profile_service_client',
 ]
 
 ROOT_URLCONF = 'django_airavata.urls'
diff --git a/django_airavata/settings_local.py.sample 
b/django_airavata/settings_local.py.sample
index 30037c3..e40eb04 100644
--- a/django_airavata/settings_local.py.sample
+++ b/django_airavata/settings_local.py.sample
@@ -34,6 +34,11 @@ AIRAVATA_API_PORT = 8930
 AIRAVATA_API_SECURE = False
 GATEWAY_DATA_STORE_RESOURCE_ID = '...'
 
+# Profile Service Configuration
+PROFILE_SERVICE_HOST = AIRAVATA_API_HOST
+PROFILE_SERVICE_PORT = 8962
+PROFILE_SERVICE_SECURE = False
+
 # Sharing API Configuration
 SHARING_API_HOST = 'localhost'
 SHARING_API_PORT = 7878

-- 
To stop receiving notification emails like this one, please contact
"[email protected]" <[email protected]>.

Reply via email to