From 6d456d4431b9b960266e6bb737c56260537d9e03 Mon Sep 17 00:00:00 2001 From: Mirus Lu Date: Thu, 22 Aug 2024 11:12:45 -0700 Subject: [PATCH 1/7] Added ability to select all profiles by typing in 'a' or 'A' in the profile selection screen --- gimme_aws_creds/main.py | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/gimme_aws_creds/main.py b/gimme_aws_creds/main.py index b110602..2cd3431 100644 --- a/gimme_aws_creds/main.py +++ b/gimme_aws_creds/main.py @@ -420,25 +420,29 @@ def _get_user_int_selections_many(self, min_int, max_int, max_retries=5): for _ in range(max_retries): selections = set() error = False + input_values = self.ui.input('Selections (comma separated; "A" for all profiles): ').split(',') - for value in self.ui.input('Selections (comma separated): ').split(','): - value = value.strip() - - if not value: - continue - - try: - selection = int(value) - except ValueError: - self.ui.warning('Invalid selection {}, must be an integer value.'.format(repr(value))) - error = True - continue - - if min_int <= selection <= max_int: - selections.add(value) - else: - self.ui.warning( - 'Selection {} out of range <{}, {}>'.format(repr(selection), min_int, max_int)) + if 'a' in input_values or 'A' in input_values: + selections = set(range(min_int, max_int)) + else: + for value in input_values: + value = value.strip() + + if not value: + continue + + try: + selection = int(value) + except ValueError: + self.ui.warning('Invalid selection {}, must be an integer value.'.format(repr(value))) + error = True + continue + + if min_int <= selection <= max_int: + selections.add(value) + else: + self.ui.warning( + 'Selection {} out of range <{}, {}>'.format(repr(selection), min_int, max_int)) if error: continue From 486746f15851174fa54b31ddb21c0e760b58b584 Mon Sep 17 00:00:00 2001 From: Mirus Lu Date: Thu, 22 Aug 2024 11:29:11 -0700 Subject: [PATCH 2/7] Added messaging when all profiles selected --- gimme_aws_creds/main.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/gimme_aws_creds/main.py b/gimme_aws_creds/main.py index 2cd3431..aa10ed0 100644 --- a/gimme_aws_creds/main.py +++ b/gimme_aws_creds/main.py @@ -420,10 +420,11 @@ def _get_user_int_selections_many(self, min_int, max_int, max_retries=5): for _ in range(max_retries): selections = set() error = False - input_values = self.ui.input('Selections (comma separated; "A" for all profiles): ').split(',') + input_values = self.ui.input('Selections (comma separated; "[A]" for all profiles): ').lower().split(',') - if 'a' in input_values or 'A' in input_values: + if 'a' in input_values or 'all' in input_values: selections = set(range(min_int, max_int)) + self.ui.message("Generating credentials for profile indices {} to {} ".format(min_int, max_int)) else: for value in input_values: value = value.strip() @@ -434,7 +435,7 @@ def _get_user_int_selections_many(self, min_int, max_int, max_retries=5): try: selection = int(value) except ValueError: - self.ui.warning('Invalid selection {}, must be an integer value.'.format(repr(value))) + self.ui.warning('Invalid selection {}, must be an integer value, or "A" for all profiles'.format(repr(value))) error = True continue From cc6119d3a069f77cc2d3e1e5c48e9fac45e67de6 Mon Sep 17 00:00:00 2001 From: Mirus Lu Date: Thu, 22 Aug 2024 11:32:45 -0700 Subject: [PATCH 3/7] Cleaned up wording around prompt --- gimme_aws_creds/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gimme_aws_creds/main.py b/gimme_aws_creds/main.py index aa10ed0..e2b5f60 100644 --- a/gimme_aws_creds/main.py +++ b/gimme_aws_creds/main.py @@ -420,7 +420,7 @@ def _get_user_int_selections_many(self, min_int, max_int, max_retries=5): for _ in range(max_retries): selections = set() error = False - input_values = self.ui.input('Selections (comma separated; "[A]" for all profiles): ').lower().split(',') + input_values = self.ui.input('Selections (comma separated; specify "A" for all profiles): ').lower().split(',') if 'a' in input_values or 'all' in input_values: selections = set(range(min_int, max_int)) From 9e15f720ed80fe11680f1f247572541d24f6ada4 Mon Sep 17 00:00:00 2001 From: Mirus Lu Date: Thu, 22 Aug 2024 12:18:20 -0700 Subject: [PATCH 4/7] Fixed range logic when populating selection set when users specify 'all' from role selection screen --- gimme_aws_creds/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gimme_aws_creds/main.py b/gimme_aws_creds/main.py index e2b5f60..d427838 100644 --- a/gimme_aws_creds/main.py +++ b/gimme_aws_creds/main.py @@ -423,7 +423,7 @@ def _get_user_int_selections_many(self, min_int, max_int, max_retries=5): input_values = self.ui.input('Selections (comma separated; specify "A" for all profiles): ').lower().split(',') if 'a' in input_values or 'all' in input_values: - selections = set(range(min_int, max_int)) + selections = set(range(min_int, max_int+1)) self.ui.message("Generating credentials for profile indices {} to {} ".format(min_int, max_int)) else: for value in input_values: @@ -449,6 +449,7 @@ def _get_user_int_selections_many(self, min_int, max_int, max_retries=5): continue if selections: + self.ui.message('Selections: {}'.format(selections)) return selections return set() From 18aeef6b1f783bb0c4c53ca309c1b6222efff78d Mon Sep 17 00:00:00 2001 From: Mirus Lu Date: Thu, 22 Aug 2024 12:20:55 -0700 Subject: [PATCH 5/7] Code cleanup --- gimme_aws_creds/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/gimme_aws_creds/main.py b/gimme_aws_creds/main.py index d427838..7040295 100644 --- a/gimme_aws_creds/main.py +++ b/gimme_aws_creds/main.py @@ -449,7 +449,6 @@ def _get_user_int_selections_many(self, min_int, max_int, max_retries=5): continue if selections: - self.ui.message('Selections: {}'.format(selections)) return selections return set() From 0eeec0c0b8829f02477a884ec60207b5cfbb87b0 Mon Sep 17 00:00:00 2001 From: Mirus Lu Date: Thu, 22 Aug 2024 12:25:14 -0700 Subject: [PATCH 6/7] Added tests for new functionality that allows users to specify "a" or "all" to select all roles from the role selection screen" --- tests/test_main.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index f6469b2..92b92b7 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -47,12 +47,26 @@ def test_choose_roles_app_2(self, mock): self.assertRaises(errors.GimmeAWSCredsExitBase, creds._choose_roles, self.APP_INFO) self.assertRaises(errors.GimmeAWSCredsExitBase, creds._choose_app, self.AWS_INFO) - @patch('builtins.input', return_value='a') - def test_choose_roles_app_a(self, mock): + @patch('builtins.input', return_value='b') + def test_choose_roles_app_b(self, mock): creds = GimmeAWSCreds() self.assertRaises(errors.GimmeAWSCredsExitBase, creds._choose_roles, self.APP_INFO) self.assertRaises(errors.GimmeAWSCredsExitBase, creds._choose_app, self.AWS_INFO) + @patch('builtins.input', return_value='all') + def test_choose_roles_app_select_all(self, mock): + creds = GimmeAWSCreds() + selections = creds._choose_roles(self.APP_INFO) + expected_selections = {self.APP_INFO[0].role, self.APP_INFO[1].role} + self.assertEqual(selections, expected_selections) + + @patch('builtins.input', return_value='a') + def test_choose_roles_app_select_all_a(self, mock): + creds = GimmeAWSCreds() + selections = creds._choose_roles(self.APP_INFO) + expected_selections = {self.APP_INFO[0].role, self.APP_INFO[1].role} + self.assertEqual(selections, expected_selections) + def test_get_selected_app_from_config_0(self): creds = GimmeAWSCreds() @@ -96,10 +110,10 @@ def test_get_selected_roles_multiple_list(self): selections = creds._get_selected_roles(['test1', 'test2'], self.APP_INFO) self.assertEqual(selections, {'test1', 'test2'}) - def test_get_selected_roles_all(self): + def test_get_selected_roles_a(self): creds = GimmeAWSCreds() - selections = creds._get_selected_roles('all', self.APP_INFO) + selections = creds._get_selected_roles('a', self.APP_INFO) self.assertEqual(selections, {'test1', 'test2'}) @patch('builtins.input', return_value='0') From f0041bf82aa07a3d0fc53238e23fd05a8d7d20f8 Mon Sep 17 00:00:00 2001 From: Mirus Lu Date: Thu, 22 Aug 2024 12:53:09 -0700 Subject: [PATCH 7/7] Reverted unnecessary change to helper test method --- tests/test_main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index 92b92b7..ca4883f 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -110,10 +110,10 @@ def test_get_selected_roles_multiple_list(self): selections = creds._get_selected_roles(['test1', 'test2'], self.APP_INFO) self.assertEqual(selections, {'test1', 'test2'}) - def test_get_selected_roles_a(self): + def test_get_selected_roles_all(self): creds = GimmeAWSCreds() - selections = creds._get_selected_roles('a', self.APP_INFO) + selections = creds._get_selected_roles('all', self.APP_INFO) self.assertEqual(selections, {'test1', 'test2'}) @patch('builtins.input', return_value='0')