Repository: byu-oit/awslogin

Issue:
Add optional parameter for region
Add an optional parameter to specify a region other than us-west-2. Use case is a Alexa and lex and potentially others aren't available in us-west-2

Discussion:

Patch:
diff --git a/README.rst b/README.rst
index ce35b06..0bdbd4b 100644
--- a/README.rst
+++ b/README.rst
@@ -66,6 +66,7 @@ To use it:
    arguments as well.
 -  Run ``awslogin --profile <profile name>`` to specifiy an alternative
    profile
+-  Run ``awslogin --region <region name>`` to specify a different region. The default region is *us-west-2*.
 -  Run ``awslogin --status`` for the current status of the default profile
 -  Run ``awslogin --status --profile dev`` for the current status of the
    dev profile
diff --git a/byu_awslogin/index.py b/byu_awslogin/index.py
index 7abb2fc..b0a82a6 100755
--- a/byu_awslogin/index.py
+++ b/byu_awslogin/index.py
@@ -22,7 +22,7 @@ except ImportError:
 from .util.data_cache import get_status, load_cached_adfs_auth
 from .login import cached_login, non_cached_login
 
-__VERSION__ = '0.12.3'
+__VERSION__ = '0.12.4'
 
 # Enable VT Mode on windows terminal code from:
 # https://bugs.python.org/issue29059
@@ -41,8 +41,9 @@ if platform.system().lower() == 'windows':
 @click.option('-a', '--account', help='Account to login with')
 @click.option('-r', '--role', help='Role to use after login')
 @click.option('-p', '--profile', default='default', help='Profile to use store credentials. Defaults to default')
+@click.option('--region', default='us-west-2', help="The AWS region you will be hitting")
 @click.option('-s', '--status', is_flag=True, default=False, help='Display current logged in status. Use profile all to see all statuses')
-def cli(account, role, profile, status):
+def cli(account, role, profile, region, status):
     _ensure_min_python_version()
 
     # Display status and exit if the user specified the "-s" flag
@@ -53,10 +54,10 @@ def cli(account, role, profile, status):
     # Use cached SAML assertion if already logged in, else login to ADFS
     adfs_auth_result = load_cached_adfs_auth()
     if adfs_auth_result:
-        cached_login(account, role, profile, adfs_auth_result)
+        cached_login(account, role, profile, region, adfs_auth_result)
         pass
     else:
-        non_cached_login(account, role, profile)
+        non_cached_login(account, role, profile, region)
 
 
 def _ensure_min_python_version():
diff --git a/byu_awslogin/login.py b/byu_awslogin/login.py
index deb5354..01abe74 100644
--- a/byu_awslogin/login.py
+++ b/byu_awslogin/login.py
@@ -8,16 +8,16 @@ from .util.consoleeffects import Colors
 from .util.data_cache import load_last_netid, write_to_config_file, write_to_cred_file, cache_adfs_auth
 
 
-def cached_login(account, role, profile, adfs_auth_result):
+def cached_login(account, role, profile, region, adfs_auth_result):
     saml_assertion = get_saml_assertion(adfs_auth_result)
 
     if not saml_assertion: # Not logged in anymore, so need to re-login
-        non_cached_login(account, role, profile)
+        non_cached_login(account, role, profile, region)
     else: # Still logged in and got a SAML assertion properly
-        _perform_login(adfs_auth_result, saml_assertion, account, role, profile, None)
+        _perform_login(adfs_auth_result, saml_assertion, account, role, profile, None, region)
 
 
-def non_cached_login(account, role, profile):
+def non_cached_login(account, role, profile, region):
     # Get the federated credentials from the user
     net_id, username = _get_user_ids(profile)
     password = getpass.getpass()
@@ -35,10 +35,10 @@ def non_cached_login(account, role, profile):
     del password
 
     saml_assertion = get_saml_assertion(adfs_auth_result)
-    _perform_login(adfs_auth_result, saml_assertion, account, role, profile, net_id)
+    _perform_login(adfs_auth_result, saml_assertion, account, role, profile, net_id, region)
 
 
-def _perform_login(adfs_auth_result, saml_assertion, account, role, profile, net_id):
+def _perform_login(adfs_auth_result, saml_assertion, account, role, profile, net_id, region):
     account_roles_to_assume = _prompt_for_roles_to_assume(saml_assertion, account, role)
 
     # Assume all requested roles and set in the environment
@@ -50,7 +50,7 @@ def _perform_login(adfs_auth_result, saml_assertion, account, role, profile, net
             profile = account_role_to_assume.account_name
 
         write_to_cred_file(profile, aws_session_token)
-        write_to_config_file(profile, net_id, 'us-west-2', account_role_to_assume.role_name,
+        write_to_config_file(profile, net_id, region, account_role_to_assume.role_name,
                              account_role_to_assume.account_name)
         _print_status_message(account_role_to_assume)
 


Test patch:
diff --git a/test/byu_awslogin/test_login.py b/test/byu_awslogin/test_login.py
index 70f60dc..ce1b289 100644
--- a/test/byu_awslogin/test_login.py
+++ b/test/byu_awslogin/test_login.py
@@ -21,7 +21,7 @@ def test_cached_login(mock_cache_adfs_auth, mock_write_to_config_file,
     ]
     mock_assume_role.return_value = "FakeSessionToken"
 
-    cached_login("FakeAccount", None, "default", {})
+    cached_login("FakeAccount", None, "default", {}, 'us-west-2')
 
     assert mock_get_saml_assertion.call_count == 1
     assert mock_prompt_roles.call_count == 1
@@ -54,7 +54,7 @@ def test_non_cached_login(mock_cache_adfs_auth, mock_write_to_config_file,
     ]
     mock_assume_role.return_value = "FakeSessionToken"
 
-    non_cached_login("FakeAccount", None, "default")
+    non_cached_login("FakeAccount", None, "default", 'us-west-2')
 
     assert mock_get_user_ids.call_count == 1
     assert mock_get_password.call_count == 1