def get_active_sessions(self, project_name, printable=True): validate_config_file() sessions = self.all_active_sessions remaining_times = { session_name: util.get_remaining_time(session_details['expiration']) for session_name, session_details in sessions.items() } if not remaining_times: print( util.yellow_text( f'- No active sessions present. Run `aws-sessions-switcher -l` to see all possible role assumptions you can make' )) return else: headers = [ 'session_name', 'remaining_time', 'configured_to_be_used_with_aws_command' ] rows = [ util.get_session_row(session_name, remaining_time, sessions[session_name]) for session_name, remaining_time in remaining_times.items() ] if printable: util.print_table(headers, rows) print( f'Note: If {util.yellow_text("`configured_to_be_used_with_aws_command`")}is False,\n' f'run {util.green_text("`aws-sessions-switcher sessions switch`")} and select this session to activate it' ) return [session for session in remaining_times]
def handle_environments_sub_operations(self, sub_action): if sub_action: if sub_action == config.ACTION_ADD: # TODO: ADD A NEW ENVIRONMENT print( util.yellow_text( 'Not supported currently. It will be available in later versions...' )) elif sub_action == config.ACTION_DELETE: # TODO: DELETE AN ENVIRONMENT print( util.yellow_text( 'Not supported currently. It will be available in later versions...' )) else: if self.args.get(config.VAR_PROJECT_NAME): self.aws_assume.list_project_environments( self.args.get(config.VAR_PROJECT_NAME)) else: self.aws_assume.list_all_environments()
def configure(write_mode='w', check_file_existence=True): if check_file_existence and os.path.isfile(config.AWS_ASSUME_CONFIG_PATH): util.error_log( 'File %s already exists.' 'Run `aws-sessions-switcher projects add` if you want to add a new project configuration.' 'Type \'aws-sessions-switcher -h\' to see all the available sub-commands' % (config.AWS_ASSUME_CONFIG_PATH, )) else: _collector = config_collector.ConfigCollector() answers = _collector.collect() if not answers: return cfg_parser = configparser.ConfigParser() cfg_parser[ f"{answers['project_name']}-{answers['project_environment']}"] = answers with open(config.AWS_ASSUME_CONFIG_PATH, write_mode) as configfile: cfg_parser.write(configfile) print( util.yellow_text( f"Note: Make sure to put your security credentials under " f"\"{util.get_base_aws_profile_for_project(answers['project_name'])}\" " f"section of your AWS Credentials"))