>From d7556213a22ad47f93cbe47925879eed36b6177d Mon Sep 17 00:00:00 2001
From: Ade Lee <alee@redhat.com>
Date: Wed, 19 Feb 2014 14:37:44 -0500
Subject: [PATCH 198/203] Fix minor issues from review.

1. main function in SymKeyGenerationRequest does not use correct usages
2. from_dict -> from_json() for consistency
---
 base/common/python/pki/__init__.py   | 35 +++++++++++++----------------------
 base/common/python/pki/cert.py       |  8 ++++----
 base/common/python/pki/key.py        | 27 ++++++++++++++-------------
 base/common/python/pki/systemcert.py |  2 +-
 4 files changed, 32 insertions(+), 40 deletions(-)

diff --git a/base/common/python/pki/__init__.py b/base/common/python/pki/__init__.py
index e9802a4ba44fb7854a79a5a06eb415f845130790..190d1f10fb9f7a3bc16ac8768ecc6a9cd8cbb8ff 100644
--- a/base/common/python/pki/__init__.py
+++ b/base/common/python/pki/__init__.py
@@ -198,27 +198,18 @@ class UserNotFoundException(ResourceNotFoundException):
     ''' User Not Found Exception: return code = 404 '''
 
 EXCEPTION_MAPPINGS = {
-    "com.netscape.certsrv.base.BadRequestException": "pki.BadRequestException",
-    "com.netscape.certsrv.base.ConflictingOperationException": "pki.ConflictingOperationException",
-    "com.netscape.certsrv.base.ForbiddenException": "pki.ForbiddenException",
-    "com.netscape.certsrv.base.HTTPGoneException": "pki.HTTPGoneException",
-    "com.netscape.certsrv.base.ResourceNotFoundException": "pki.ResourceNotFoundException",
-    "com.netscape.certsrv.cert.CertNotFoundException": "pki.CertNotFoundException",
-    "com.netscape.certsrv.group.GroupNotFoundException": "pki.GroupNotFoundException",
-    "com.netscape.certsrv.key.KeyNotFoundException": "pki.KeyNotFoundException",
-    "com.netscape.certsrv.profile.ProfileNotFoundException": "pki.ProfileNotFoundException",
-    "com.netscape.certsrv.request.RequestNotFoundException": "pki.RequestNotFoundException",
-    "com.netscape.certsrv.base.UserNotFoundException": "pki.UserNotFoundException",
-    "com.netscape.certsrv.base.PKIException": "pki.PKIException"}
-
-def get_class( kls ):
-    ''' Get reference to the class specified by string kls '''
-    parts = kls.split('.')
-    module = ".".join(parts[:-1])
-    mod = __import__( module )
-    for comp in parts[1:]:
-        mod = getattr(mod, comp)
-    return mod
+    "com.netscape.certsrv.base.BadRequestException": BadRequestException,
+    "com.netscape.certsrv.base.ConflictingOperationException": ConflictingOperationException,
+    "com.netscape.certsrv.base.ForbiddenException": ForbiddenException,
+    "com.netscape.certsrv.base.HTTPGoneException": HTTPGoneException,
+    "com.netscape.certsrv.base.ResourceNotFoundException": ResourceNotFoundException,
+    "com.netscape.certsrv.cert.CertNotFoundException": CertNotFoundException,
+    "com.netscape.certsrv.group.GroupNotFoundException": GroupNotFoundException,
+    "com.netscape.certsrv.key.KeyNotFoundException": KeyNotFoundException,
+    "com.netscape.certsrv.profile.ProfileNotFoundException": ProfileNotFoundException,
+    "com.netscape.certsrv.request.RequestNotFoundException": RequestNotFoundException,
+    "com.netscape.certsrv.base.UserNotFoundException": UserNotFoundException,
+    "com.netscape.certsrv.base.PKIException": PKIException}
 
 def handle_exceptions():
     ''' Decorator handling exceptions from REST methods. '''
@@ -233,7 +224,7 @@ def handle_exceptions():
             except requests.exceptions.HTTPError as exc:
                 clazz = exc.response.json()['ClassName']
                 if clazz in EXCEPTION_MAPPINGS:
-                    exception_class = get_class(EXCEPTION_MAPPINGS[clazz])
+                    exception_class = EXCEPTION_MAPPINGS[clazz]
                     pki_exception = exception_class.from_json(exc.response.json())
                     raise pki_exception
                 else:
diff --git a/base/common/python/pki/cert.py b/base/common/python/pki/cert.py
index 0a720a5e17002b48155738dbf42501bc80750581..3d05df0c34855afe0863ced8df6d439cac8c8465 100644
--- a/base/common/python/pki/cert.py
+++ b/base/common/python/pki/cert.py
@@ -35,7 +35,7 @@ class CertData(object):
         self.Encoded = None
 
     @classmethod
-    def from_dict(cls, attr_list):
+    def from_json(cls, attr_list):
         ''' Return CertData object from JSON dict '''
         cert_data = cls()
         for key in attr_list:
@@ -63,7 +63,7 @@ class CertDataInfo(object):
         self.issuedBy = None
 
     @classmethod
-    def from_dict(cls, attr_list):
+    def from_json(cls, attr_list):
         ''' Return CertDataInfo object from JSON dict '''
         cert_data_info = cls()
         for key in attr_list:
@@ -87,10 +87,10 @@ class CertDataInfos(object):
         ret = cls()
         cert_infos = json_value['CertDataInfo']
         if not isinstance(cert_infos, types.ListType):
-            ret.certInfoList.append(CertDataInfo.from_dict(cert_infos))
+            ret.certInfoList.append(CertDataInfo.from_json(cert_infos))
         else:
             for cert_info in cert_infos:
-                ret.certInfoList.append(CertDataInfo.from_dict(cert_info))
+                ret.certInfoList.append(CertDataInfo.from_json(cert_info))
         return ret
 
 class CertSearchRequest(object):
diff --git a/base/common/python/pki/key.py b/base/common/python/pki/key.py
index 1e959785cd9eec8b41e6773e01dc3ebcbd21e1bb..235825746c441cc281c26a382e68d37b4518b4d1 100644
--- a/base/common/python/pki/key.py
+++ b/base/common/python/pki/key.py
@@ -59,7 +59,7 @@ class KeyData(object):
         self.wrappedPrivateData = None
 
     @classmethod
-    def from_dict(cls, attr_list):
+    def from_json(cls, attr_list):
         ''' Return a KeyData object from a JSON dict '''
         key_data = cls()
         for key in attr_list:
@@ -83,7 +83,7 @@ class KeyInfo(object):
         self.size = None
 
     @classmethod
-    def from_dict(cls, attr_list):
+    def from_json(cls, attr_list):
         ''' Return KeyInfo from JSON dict '''
         key_info = cls()
         for key in attr_list:
@@ -115,10 +115,10 @@ class KeyInfoCollection(object):
         ret = cls()
         infos = json_value['entries']
         if not isinstance(infos, types.ListType):
-            ret.key_infos.append(KeyInfo.from_dict(infos))
+            ret.key_infos.append(KeyInfo.from_json(infos))
         else:
             for info in infos:
-                ret.key_infos.append(KeyInfo.from_dict(info))
+                ret.key_infos.append(KeyInfo.from_json(info))
         return ret
 
 class KeyRequestInfo(object):
@@ -135,7 +135,7 @@ class KeyRequestInfo(object):
         self.requestStatus = None
 
     @classmethod
-    def from_dict(cls, attr_list):
+    def from_json(cls, attr_list):
         ''' Return a KeyRequestInfo object from a JSON dict. '''
         key_request_info = cls()
         for key in attr_list:
@@ -174,10 +174,10 @@ class KeyRequestInfoCollection(object):
         ret = cls()
         infos = json_value['entries']
         if not isinstance(infos, types.ListType):
-            ret.key_requests.append(KeyRequestInfo.from_dict(infos))
+            ret.key_requests.append(KeyRequestInfo.from_json(infos))
         else:
             for info in infos:
-                ret.key_requests.append(KeyRequestInfo.from_dict(info))
+                ret.key_requests.append(KeyRequestInfo.from_json(info))
         return ret
 
 class KeyRequestResponse(object):
@@ -199,10 +199,10 @@ class KeyRequestResponse(object):
         ret = cls()
 
         if 'RequestInfo' in json_value:
-            ret.requestInfo = KeyRequestInfo.from_dict(json_value['RequestInfo'])
+            ret.requestInfo = KeyRequestInfo.from_json(json_value['RequestInfo'])
 
         if 'KeyData' in json_value:
-            ret.keyData = KeyData.from_dict(json_value['KeyData'])
+            ret.keyData = KeyData.from_json(json_value['KeyData'])
         return ret
 
     def get_key_id(self):
@@ -322,7 +322,7 @@ class KeyClient(object):
         url = self.keyURL + '/retrieve'
         keyRequest = json.dumps(data, cls=encoder.CustomTypeEncoder, sort_keys=True)
         response = self.connection.post(url, keyRequest, self.headers)
-        return KeyData.from_dict(response.json())
+        return KeyData.from_json(response.json())
 
     @pki.handle_exceptions()
     def request_key_retrieval(self, key_id, request_id, trans_wrapped_session_key=None,
@@ -377,7 +377,7 @@ class KeyClient(object):
         ''' Return a KeyRequestInfo object for a specific request. '''
         url = self.keyRequestsURL + '/' + request_id
         response = self.connection.get(url, self.headers)
-        return KeyRequestInfo.from_dict(response.json())
+        return KeyRequestInfo.from_json(response.json())
 
     @pki.handle_exceptions()
     def create_request(self, request):
@@ -463,7 +463,7 @@ class KeyClient(object):
         ''' Get the info in the KeyRecord for a specific secret in the DRM. '''
         url = self.keyURL + '/' + key_id
         response = self.connection.get(url, headers=self.headers)
-        return KeyInfo.from_dict(response.json())
+        return KeyInfo.from_json(response.json())
 
     @pki.handle_exceptions()
     def modify_key_status(self, key_id, status):
@@ -483,7 +483,8 @@ def main():
     ''' Some unit tests - basically printing different types of requests '''
     print "printing symkey generation request"
     client_id = "vek 123"
-    gen_request = SymKeyGenerationRequest(client_id, 128, "AES", "encrypt,decrypt")
+    usages = [SymKeyGenerationRequest.DECRYPT_USAGE, SymKeyGenerationRequest.ENCRYPT_USAGE]
+    gen_request = SymKeyGenerationRequest(client_id, 128, "AES", usages)
     print json.dumps(gen_request, cls=encoder.CustomTypeEncoder, sort_keys=True)
 
     print "printing key recovery request"
diff --git a/base/common/python/pki/systemcert.py b/base/common/python/pki/systemcert.py
index 2f51de6f337f5b115937fde2ade3fdb33ff27ee6..f91bbeca0d653645685ee59227feeaf30d0c0896 100644
--- a/base/common/python/pki/systemcert.py
+++ b/base/common/python/pki/systemcert.py
@@ -43,5 +43,5 @@ class SystemCertClient(object):
         ''' Return transport certificate '''
         url = self.cert_url +  '/transport'
         response = self.connection.get(url, self.headers)
-        cert_data = CertData.from_dict(response.json())
+        cert_data = CertData.from_json(response.json())
         return cert_data.Encoded
-- 
1.8.4.2

