How to use delete_usage_plan_key method in localstack

Best Python code snippet using localstack_python

test_apigw_usage_plan_key.py

Source:test_apigw_usage_plan_key.py Github

copy

Full Screen

1#!/usr/bin/python2# TODO: License goes here3import library.apigw_usage_plan_key as apigw_usage_plan_key4from library.apigw_usage_plan_key import ApiGwUsagePlanKey5import mock6from mock import patch7from mock import create_autospec8from mock import ANY9import unittest10import boto11from botocore.exceptions import BotoCoreError12class TestApiGwUsagePlanKey(unittest.TestCase):13 def setUp(self):14 self.module = mock.MagicMock()15 self.module.check_mode = False16 self.module.exit_json = mock.MagicMock()17 self.module.fail_json = mock.MagicMock()18 self.usage_plan_key = ApiGwUsagePlanKey(self.module)19 self.usage_plan_key.client = mock.MagicMock()20 self.usage_plan_key.module.params = {21 'usage_plan_id': 'upid' ,22 'api_key_id': 'akid',23 'key_type': 'API_KEY',24 'state': 'present',25 }26 reload(apigw_usage_plan_key)27 def test_boto_module_not_found(self):28 # Setup Mock Import Function29 import __builtin__ as builtins30 real_import = builtins.__import__31 def mock_import(name, *args):32 if name == 'boto': raise ImportError33 return real_import(name, *args)34 with mock.patch('__builtin__.__import__', side_effect=mock_import):35 reload(apigw_usage_plan_key)36 ApiGwUsagePlanKey(self.module)37 self.module.fail_json.assert_called_with(msg='boto and boto3 are required for this module')38 def test_boto3_module_not_found(self):39 # Setup Mock Import Function40 import __builtin__ as builtins41 real_import = builtins.__import__42 def mock_import(name, *args):43 if name == 'boto3': raise ImportError44 return real_import(name, *args)45 with mock.patch('__builtin__.__import__', side_effect=mock_import):46 reload(apigw_usage_plan_key)47 ApiGwUsagePlanKey(self.module)48 self.module.fail_json.assert_called_with(msg='boto and boto3 are required for this module')49 @patch.object(apigw_usage_plan_key, 'boto3')50 def test_boto3_client_properly_instantiated(self, mock_boto):51 ApiGwUsagePlanKey(self.module)52 mock_boto.client.assert_called_once_with('apigateway')53 def test_process_request_calls_get_usage_plan_keys_and_stores_result_when_invoked(self):54 resp = {55 'items': [56 {'id': 'wrong_id'},57 {'id': 'akid'},58 ],59 }60 self.usage_plan_key.client.get_usage_plan_keys = mock.MagicMock(return_value=resp)61 self.usage_plan_key.process_request()62 self.assertEqual(resp['items'][1], self.usage_plan_key.me)63 self.usage_plan_key.client.get_usage_plan_keys.assert_called_once_with(usagePlanId='upid')64 def test_process_request_stores_None_result_when_not_found_in_get_usage_plan_keys_result(self):65 resp = {66 'items': [67 {'id': 'wrong id'},68 {'id': 'wronger id'},69 ],70 }71 self.usage_plan_key.client.get_usage_plan_keys = mock.MagicMock(return_value=resp)72 self.usage_plan_key.process_request()73 self.assertEqual(None, self.usage_plan_key.me)74 self.usage_plan_key.client.get_usage_plan_keys.assert_called_once_with(usagePlanId='upid')75 def test_process_request_calls_fail_json_when_get_usage_plan_keys_raises_exception(self):76 self.usage_plan_key.client.get_usage_plan_keys = mock.MagicMock(side_effect=BotoCoreError())77 self.usage_plan_key.process_request()78 self.usage_plan_key.client.get_usage_plan_keys.assert_called_once_with(usagePlanId='upid')79 self.usage_plan_key.module.fail_json.assert_called_once_with(80 msg='Error when getting usage_plan_keys from boto3: An unspecified error occurred'81 )82 @patch.object(ApiGwUsagePlanKey, '_delete_usage_plan_key', return_value='Mitchell!')83 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value={'id': 'found'})84 def test_process_request_calls_exit_json_with_expected_value_after_successful_delete(self, mr, md):85 self.usage_plan_key.module.params = {86 'usage_plan_id': 'upid',87 'api_key_id': 'akid',88 'state': 'absent',89 }90 self.usage_plan_key.process_request()91 self.usage_plan_key.module.exit_json.assert_called_once_with(changed='Mitchell!', usage_plan_key=None)92 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value={'id': 'found'})93 def test_process_request_calls_delete_usage_plan_key_when_state_absent_and_usage_plan_key_found(self, m):94 self.usage_plan_key.module.params = {95 'usage_plan_id': 'upid',96 'api_key_id': 'akid',97 'state': 'absent',98 }99 self.usage_plan_key.process_request()100 self.usage_plan_key.client.delete_usage_plan_key.assert_called_once_with(usagePlanId='upid', keyId='akid')101 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value={'id': 'found'})102 def test_process_request_skips_delete_and_calls_exit_json_with_true_when_check_mode_set_and_auth_found(self, m):103 self.usage_plan_key.module.params = {104 'usage_plan_id': 'upid',105 'api_key_id': 'akid',106 'state': 'absent',107 }108 self.usage_plan_key.module.check_mode = True109 self.usage_plan_key.process_request()110 self.assertEqual(0, self.usage_plan_key.client.delete_usage_plan_key.call_count)111 self.usage_plan_key.module.exit_json.assert_called_once_with(changed=True, usage_plan_key=None)112 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value={'id': 'found'})113 def test_process_request_calls_fail_json_when_delete_usage_plan_key_raises_error(self, m):114 self.usage_plan_key.module.params = {115 'usage_plan_id': 'upid',116 'api_key_id': 'akid',117 'state': 'absent',118 }119 self.usage_plan_key.client.delete_usage_plan_key = mock.MagicMock(side_effect=BotoCoreError)120 self.usage_plan_key.process_request()121 self.usage_plan_key.client.delete_usage_plan_key.assert_called_once_with(usagePlanId='upid', keyId='akid')122 self.usage_plan_key.module.fail_json.assert_called_once_with(123 msg='Error when deleting usage_plan_key via boto3: An unspecified error occurred'124 )125 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value=None)126 def test_process_request_skips_delete_when_usage_plan_key_not_found(self, m):127 self.usage_plan_key.module.params = {128 'usage_plan_id': 'upid',129 'api_key_id': 'akid',130 'state': 'absent',131 }132 self.usage_plan_key.process_request()133 self.assertEqual(0, self.usage_plan_key.client.delete_usage_plan_key.call_count)134 @patch.object(ApiGwUsagePlanKey, '_create_usage_plan_key', return_value=('veins', 'clogging'))135 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value=None)136 def test_process_request_calls_exit_json_with_expected_value_after_successful_create(self, mra, mca):137 self.usage_plan_key.process_request()138 self.usage_plan_key.module.exit_json.assert_called_once_with(changed='veins', usage_plan_key='clogging')139 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value=None)140 def test_process_request_returns_create_usage_plan_key_result_when_create_succeeds(self, m):141 self.usage_plan_key.client.create_usage_plan_key = mock.MagicMock(return_value='woot')142 self.usage_plan_key.process_request()143 self.usage_plan_key.module.exit_json.assert_called_once_with(changed=True, usage_plan_key='woot')144 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value=None)145 def test_process_request_calls_create_usage_plan_key_when_state_present_and_usage_plan_key_not_found(self, m):146 self.usage_plan_key.process_request()147 self.usage_plan_key.client.create_usage_plan_key.assert_called_once_with(148 usagePlanId='upid',149 keyId='akid',150 keyType='API_KEY'151 )152 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value=None)153 def test_process_request_calls_fail_json_when_create_usage_plan_key_raises_exception(self, m):154 self.usage_plan_key.client.create_usage_plan_key = mock.MagicMock(side_effect=BotoCoreError())155 self.usage_plan_key.process_request()156 self.usage_plan_key.client.create_usage_plan_key.assert_called_once_with(157 usagePlanId='upid',158 keyId='akid',159 keyType='API_KEY'160 )161 self.usage_plan_key.module.fail_json.assert_called_once_with(162 msg='Error when creating usage_plan_key via boto3: An unspecified error occurred'163 )164 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value=None)165 def test_process_request_skips_create_call_and_returns_changed_True_when_check_mode(self, m):166 self.usage_plan_key.module.check_mode = True167 self.usage_plan_key.process_request()168 self.assertEqual(0, self.usage_plan_key.client.create_usage_plan_key.call_count)169 self.usage_plan_key.module.exit_json.assert_called_once_with(changed=True, usage_plan_key=None)170 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value='something')171 def test_process_request_calls_exit_json_properly_when_state_present_and_key_exists(self, m):172 self.usage_plan_key.client.create_usage_plan_key = mock.MagicMock(side_effect=BotoCoreError())173 self.usage_plan_key.process_request()174 self.usage_plan_key.module.exit_json.assert_called_once_with(changed=False, usage_plan_key='something')175 def test_define_argument_spec(self):176 result = ApiGwUsagePlanKey._define_module_argument_spec()177 self.assertIsInstance(result, dict)178 self.assertEqual(result, dict(179 usage_plan_id=dict(required=True),180 api_key_id=dict(required=True),181 key_type=dict(required=False, default='API_KEY', choices=['API_KEY']),182 state=dict(default='present', choices=['present', 'absent']),183 ))184 @patch.object(apigw_usage_plan_key, 'AnsibleModule')185 @patch.object(apigw_usage_plan_key, 'ApiGwUsagePlanKey')186 def test_main(self, mock_ApiGwUsagePlanKey, mock_AnsibleModule):187 mock_ApiGwUsagePlanKey_instance = mock.MagicMock()188 mock_AnsibleModule_instance = mock.MagicMock()189 mock_ApiGwUsagePlanKey.return_value = mock_ApiGwUsagePlanKey_instance190 mock_AnsibleModule.return_value = mock_AnsibleModule_instance191 apigw_usage_plan_key.main()192 mock_ApiGwUsagePlanKey.assert_called_once_with(mock_AnsibleModule_instance)193 assert mock_ApiGwUsagePlanKey_instance.process_request.call_count == 1194if __name__ == '__main__':...

Full Screen

Full Screen

apigw_usage_plan_key.py

Source:apigw_usage_plan_key.py Github

copy

Full Screen

...144 resp = item145 except BotoCoreError as e:146 self.module.fail_json(msg="Error when getting usage_plan_keys from boto3: {}".format(e))147 return resp148 def _delete_usage_plan_key(self):149 """150 Delete usage_plan_key that matches the returned id151 :return: True152 """153 try:154 if not self.module.check_mode:155 self.client.delete_usage_plan_key(156 usagePlanId=self.module.params['usage_plan_id'],157 keyId=self.module.params['api_key_id']158 )159 return True160 except BotoCoreError as e:161 self.module.fail_json(msg="Error when deleting usage_plan_key via boto3: {}".format(e))162 def _create_usage_plan_key(self):163 """164 Create usage_plan_key from provided args165 :return: True, result from create_usage_plan_key166 """167 usage_plan_key = None168 changed = False169 try:170 changed = True171 if not self.module.check_mode:172 usage_plan_key = self.client.create_usage_plan_key(173 usagePlanId=self.module.params['usage_plan_id'],174 keyId=self.module.params['api_key_id'],175 keyType=self.module.params.get('key_type')176 )177 except BotoCoreError as e:178 self.module.fail_json(msg="Error when creating usage_plan_key via boto3: {}".format(e))179 return (changed, usage_plan_key)180 def process_request(self):181 """182 Process the user's request -- the primary code path183 :return: Returns either fail_json or exit_json184 """185 usage_plan_key = None186 changed = False187 self.me = self._retrieve_usage_plan_key()188 if self.module.params.get('state', 'present') == 'absent' and self.me is not None:189 changed = self._delete_usage_plan_key()190 elif self.module.params.get('state', 'present') == 'present':191 if self.me is None:192 (changed, usage_plan_key) = self._create_usage_plan_key()193 else:194 usage_plan_key = self.me195 self.module.exit_json(changed=changed, usage_plan_key=usage_plan_key)196def main():197 """198 Instantiates the module and calls process_request.199 :return: none200 """201 module = AnsibleModule(202 argument_spec=ApiGwUsagePlanKey._define_module_argument_spec(),203 supports_check_mode=True...

Full Screen

Full Screen

clearAPI.py

Source:clearAPI.py Github

copy

Full Screen

...8 clientAPI.delete_rest_api(id=api['id'])9 time.sleep(61)10for plan in clientAPI.get_usage_plans(limit=500)['items']:11 for key in clientAPI.get_usage_plan_keys(usagePlanId=plan['id'])['items']:12 clientAPI.delete_usage_plan_key(usagePlanId=plan['id'], keyId=key['id'])13 clientAPI.delete_usage_plan(usagePlanId=plan['id'])14for key in clientAPI.get_api_keys(limit=500)['items']:...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful