From 3acc5b92bea430db30b83d70471b0bc4f20f1464 Mon Sep 17 00:00:00 2001 From: Albert Camacho Date: Mon, 11 Mar 2019 18:13:50 -0400 Subject: [PATCH 1/2] 1101 handle and raise another exception message when oftLayer_Exception_User_Customer_DelegateIamIdInvitationToPaas occurs --- SoftLayer/managers/user.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/SoftLayer/managers/user.py b/SoftLayer/managers/user.py index 7031551c9..3c62d9112 100644 --- a/SoftLayer/managers/user.py +++ b/SoftLayer/managers/user.py @@ -243,7 +243,16 @@ def create_user(self, user_object, password): :param dictionary user_object: https://softlayer.github.io/reference/datatypes/SoftLayer_User_Customer/ """ LOGGER.warning("Creating User %s", user_object['username']) - return self.user_service.createObject(user_object, password, None) + + try: + return self.user_service.createObject(user_object, password, None) + except exceptions.SoftLayerAPIError as err: + if err.faultCode != "SoftLayer_Exception_User_Customer_DelegateIamIdInvitationToPaas": + raise + else: + raise exceptions.SoftLayerError("Your request for a new user was received, but it needs to be " + "processed by the Platform Services API first. Barring any errors on " + "the Platform Services side, your new user should be created shortly.") def edit_user(self, user_id, user_object): """Blindly sends user_object to SoftLayer_User_Customer::editObject From bcac437e99703d38d6ba5ccb6d988084237a46ed Mon Sep 17 00:00:00 2001 From: Albert Camacho Date: Tue, 12 Mar 2019 15:31:20 -0400 Subject: [PATCH 2/2] 1101 unit test --- SoftLayer/managers/user.py | 7 +++---- tests/managers/user_tests.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/SoftLayer/managers/user.py b/SoftLayer/managers/user.py index 3c62d9112..82cf62cd2 100644 --- a/SoftLayer/managers/user.py +++ b/SoftLayer/managers/user.py @@ -246,13 +246,12 @@ def create_user(self, user_object, password): try: return self.user_service.createObject(user_object, password, None) - except exceptions.SoftLayerAPIError as err: - if err.faultCode != "SoftLayer_Exception_User_Customer_DelegateIamIdInvitationToPaas": - raise - else: + except exceptions.SoftLayerAPIError as ex: + if ex.faultCode == "SoftLayer_Exception_User_Customer_DelegateIamIdInvitationToPaas": raise exceptions.SoftLayerError("Your request for a new user was received, but it needs to be " "processed by the Platform Services API first. Barring any errors on " "the Platform Services side, your new user should be created shortly.") + raise def edit_user(self, user_id, user_object): """Blindly sends user_object to SoftLayer_User_Customer::editObject diff --git a/tests/managers/user_tests.py b/tests/managers/user_tests.py index ddb8322f1..66443de04 100644 --- a/tests/managers/user_tests.py +++ b/tests/managers/user_tests.py @@ -191,3 +191,33 @@ def test_get_current_user_mask(self): result = self.manager.get_current_user(objectmask="mask[id]") self.assert_called_with('SoftLayer_Account', 'getCurrentUser', mask="mask[id]") self.assertEqual(result['id'], 12345) + + def test_create_user_handle_paas_exception(self): + user_template = {"username": "foobar", "email": "foobar@example.com"} + + self.manager.user_service = mock.Mock() + + # FaultCode IS NOT SoftLayer_Exception_User_Customer_DelegateIamIdInvitationToPaas + any_error = exceptions.SoftLayerAPIError("SoftLayer_Exception_User_Customer", + "This exception indicates an error") + + self.manager.user_service.createObject.side_effect = any_error + + try: + self.manager.create_user(user_template, "Pass@123") + except exceptions.SoftLayerAPIError as ex: + self.assertEqual(ex.faultCode, "SoftLayer_Exception_User_Customer") + self.assertEqual(ex.faultString, "This exception indicates an error") + + # FaultCode is SoftLayer_Exception_User_Customer_DelegateIamIdInvitationToPaas + paas_error = exceptions.SoftLayerAPIError("SoftLayer_Exception_User_Customer_DelegateIamIdInvitationToPaas", + "This exception does NOT indicate an error") + + self.manager.user_service.createObject.side_effect = paas_error + + try: + self.manager.create_user(user_template, "Pass@123") + except exceptions.SoftLayerError as ex: + self.assertEqual(ex.args[0], "Your request for a new user was received, but it needs to be processed by " + "the Platform Services API first. Barring any errors on the Platform Services " + "side, your new user should be created shortly.")