Skip to content

Commit

Permalink
add aws region and profile switcher
Browse files Browse the repository at this point in the history
  • Loading branch information
snigdhasjg committed Nov 28, 2023
1 parent 8c8c981 commit d1b1777
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
3 changes: 2 additions & 1 deletion aws_fusion/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging

from importlib.metadata import version
from .commands import open_browser, iam_user_credentials, generate_okta_device_auth_credentials, init
from .commands import open_browser, iam_user_credentials, generate_okta_device_auth_credentials, init, config_switch


def main():
Expand All @@ -17,6 +17,7 @@ def main():
iam_user_credentials.setup(subparsers, global_parser)
generate_okta_device_auth_credentials.setup(subparsers, global_parser)
init.setup(subparsers, global_parser)
config_switch.setup(subparsers, global_parser)

args = main_parser.parse_args()

Expand Down
59 changes: 59 additions & 0 deletions aws_fusion/commands/config_switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import inquirer
import boto3
import os
import sys


def setup(subparsers, parent_parser):
summary = 'Switching between aws config'
parser = subparsers.add_parser('config-switch', description=summary, help=summary, parents=[parent_parser])

switch_subparsers = parser.add_subparsers(dest='switch_command', required=True, help='Available switch commands')

profile_switch_summary = "Switch between available aws profile"
profile_switch_parser = switch_subparsers.add_parser('profile', description=profile_switch_summary, help=profile_switch_summary, parents=[parent_parser])
profile_switch_parser.set_defaults(func=switch_profile)

region_switch_summary = "Switch between available aws region"
region_switch_parser = switch_subparsers.add_parser('region', description=region_switch_summary, help=region_switch_summary, parents=[parent_parser])
region_switch_parser.set_defaults(func=switch_region)


def switch_profile(args):
session = boto3.Session(region_name=os.getenv("AWS_REGION", os.getenv("AWS_DEFAULT_REGION")), profile_name=os.getenv("AWS_PROFILE"))
available_profiles = session.available_profiles

questions = [
inquirer.List(
"profile",
message="Choose a profile",
choices=available_profiles,
default=session.profile_name,
carousel=True
),
]

answers = inquirer.prompt(questions, theme=inquirer.themes.GreenPassion())
profile = answers.get('profile')
command = '$env:' if sys.platform == 'win32' else 'export '
print(f'{command}AWS_PROFILE="{profile}"')


def switch_region(args):
session = boto3.Session(region_name=os.getenv("AWS_REGION", os.getenv("AWS_DEFAULT_REGION")), profile_name=os.getenv("AWS_PROFILE"))
available_regions = session.get_available_regions('ec2')

questions = [
inquirer.List(
"region",
message="Choose a region",
choices=available_regions,
default=session.region_name,
carousel=True
),
]

answers = inquirer.prompt(questions, theme=inquirer.themes.GreenPassion())
region = answers.get('region')
command = '$env:' if sys.platform == 'win32' else 'export '
print(f'{command}AWS_REGION="{region}"')
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def find_version(*file_paths):
'pyperclip>=1.8',
'keyring>=24.3',
'beautifulsoup4>=4.12',
'requests>=2.31'
'requests>=2.31',
'inquirer>=3.1'
],
author='Snigdhajyoti Ghosh',
author_email='[email protected]',
Expand Down

0 comments on commit d1b1777

Please sign in to comment.