def show_information(display): """ Summary: Displays information to user Returns: Success or Failure, TYPE: bool """ def valid(input): try: if userchoice_mapping(input) is None: stdout_message( 'Please choose a letter associated with one of the choices.', indent=12) return False elif isinstance(int(input), int) or isinstance( float(input), float): stdout_message(message='You must choose a letter', indent=12) return False except ValueError: # valid string pass return True if os.path.exists(FILE_PATH) and display in ('files', 'profiles'): files = os.listdir(FILE_PATH) profiles = list(filter(lambda x: x.endswith('.profile'), files)) if profiles: # display user menu print( '\t_______________________________________________________\n') print(bd + '\t\t\tLocal AWS Account Profiles' + rst) print( '\t_______________________________________________________\n') for index, file in enumerate(profiles): print( '\t\t({}): {}'.format(userchoice_mapping(index + 1), Colors.BRIGHT_PURPLE + file + rst)) answer = input('\n\tSelect an option to display [quit]: ') # process user input if answer: if valid(answer): if int(userchoice_mapping(answer)) in range(1, index + 2): return file_contents( profiles[int(userchoice_mapping(answer)) - 1]) return True else: # user answer out of selection range stdout_message( message= 'Please choose a letter associated with one of the choices.', indent=12) return False
def safe_choice(sel_index, user_choice): if sel_index == 'letters': return user_choice elif isinstance(user_choice, int): return user_choice elif isinstance(user_choice, str): try: return int(user_choice) except TypeError: return userchoice_mapping(user_choice)
def userdata_lookup(debug): """ Summary. Instance Profile role user selection Returns: iam instance profile role ARN (str) or None """ # setup table x = VeryPrettyTable(border=True, header=True, padding_width=2) field_max_width = 70 x.field_names = [ bd + '#' + frame, bd + 'Filename' + frame, bd + 'Path' + frame, bd + 'CreateDate' + frame, bd + 'LastModified' + frame ] # cell alignment x.align[bd + '#' + frame] = 'c' x.align[bd + 'Filename' + frame] = 'c' x.align[bd + 'Path' + frame] = 'l' x.align[bd + 'CreateDate' + frame] = 'c' x.align[bd + 'LastModified' + frame] = 'c' filenames = source_local_userdata() paths = source_local_userdata(paths=True) ctimes = [time.ctime(os.path.getctime(x)) for x in paths] mtimes = [time.ctime(os.path.getmtime(x)) for x in paths] # populate table lookup = {} for index, path in enumerate(paths): lookup[index] = paths[index] x.add_row( [ rst + userchoice_mapping(index) + '.' + frame, rst + filenames[index] + frame, rst + path + frame, rst + ctimes[index] + frame, rst + mtimes[index] + frame ] ) # Table showing selections print(f'\n\tUserdata Scripts (local filesystem: ~/.config/ec2tools/userdata)\n'.expandtabs(26)) display_table(x, tabspaces=4) return choose_resource(lookup)
def valid(input): try: if userchoice_mapping(input) is None: stdout_message( 'Please choose a letter associated with one of the choices.', indent=12) return False elif isinstance(int(input), int) or isinstance( float(input), float): stdout_message(message='You must choose a letter', indent=12) return False except ValueError: # valid string pass return True
def get_subnet(profile, region, debug): """ Summary. Returns subnet user selection in given region Args: :profile (str): profile_name from local awscli configuration :region (str): AWS region code Returns: subnet id chosen by user """ # setup table x = VeryPrettyTable(border=True, header=True, padding_width=2) field_max_width = 30 x.field_names = [ bd + '#' + frame, bd + 'SubnetId' + frame, bd + 'AZ' + frame, bd + 'CIDR' + frame, bd + 'Ip Assign' + frame, bd + 'State' + frame, bd + 'VpcId' + frame ] #subnets = get_contents(account_file)[region]['Subnets'] subnets = profile_subnets(profile, region) # populate table lookup = {} for index, row in enumerate(subnets): for k, v in row.items(): lookup[index] = k x.add_row([ rst + userchoice_mapping(index) + '.' + frame, rst + k + frame, rst + v['AvailabilityZone'] + frame, rst + v['CidrBlock'] + frame, rst + v['IpAddresses'] + frame, rst + v['State'] + frame, rst + v['VpcId'] + frame ]) # Table showing selections print(f'\n\tSubnets in region {bd + region + rst}\n'.expandtabs(30)) display_table(x) return choose_resource(lookup)
def keypair_lookup(profile, region, debug): """ Summary. Returns name of keypair user selection in given region Args: :profile (str): profile_name from local awscli configuration :region (str): AWS region code Returns: keypair name chosen by user """ # setup table x = VeryPrettyTable(border=True, header=True, padding_width=2) field_max_width = 30 x.field_names = [bd + '#' + frame, bd + 'Keypair' + frame] # cell alignment x.align[bd + '#' + frame] = 'c' x.align[bd + 'Keypair' + frame] = 'l' keypairs = profile_keypairs(parse_profiles(profile), region)[region] # populate table lookup = {} for index, keypair in enumerate(keypairs): lookup[index] = keypair x.add_row([ rst + userchoice_mapping(index) + '.' + frame, rst + keypair + frame ]) # Table showing selections print(f'\n\tKeypairs in region {bd + region + rst}\n'.expandtabs(26)) display_table(x, tabspaces=16) return choose_resource(lookup)
def sg_lookup(profile, region, debug): """ Summary. Returns securitygroup user selection in given region Args: :profile (str): profile_name from local awscli configuration :region (str): AWS region code Returns: securitygroup ID chosen by user """ padding = 2 field_max_width = 50 max_gn, max_desc = 10, 10 # starting value to find max length of a table field (chars) x = VeryPrettyTable(border=True, header=True, padding_width=padding) sgs = profile_securitygroups(profile, region) for index, row in enumerate(sgs): for k, v in row.items(): if len(v['GroupName']) > max_gn: max_gn = len(v['GroupName']) if len(v['Description']) > max_desc: max_desc = len(v['GroupName']) if debug: print('max_gn = {}'.format(max_gn)) print('max_desc = {}'.format(max_desc)) # GroupName header tabspaces_gn = int(max_gn / 4) - int(len('GroupName') / 2) + padding tab_gn = '\t'.expandtabs(tabspaces_gn) # Description header tabspaces_desc = int(max_desc / 4) - int(len('Description') / 2) + padding tab_desc = '\t'.expandtabs(tabspaces_desc) x.field_names = [ bd + ' # ' + frame, bd + 'GroupId' + frame, tab_gn + bd + 'GroupName' + frame, bd + 'VpcId' + frame, tab_desc + bd + 'Description' + frame ] # cell alignment x.align = 'c' x.align[tab_gn + bd + 'GroupName' + frame] = 'l' x.align[tab_desc + bd + 'Description' + frame] = 'l' # populate table lookup = {} for index, row in enumerate(sgs): for k, v in row.items(): lookup[index] = k x.add_row([ rst + userchoice_mapping(index) + '.' + frame, rst + k + frame, rst + v['GroupName'][:field_max_width] + frame, rst + v['VpcId'] + frame, rst + v['Description'][:field_max_width] + frame ]) # Table showing selections print( f'\n\tSecurity Groups in region {bd + region + rst}\n'.expandtabs(30)) display_table(x) return choose_resource(lookup)
def choose_resource(choices, selector='letters', default='a'): """ Summary. validate user choice of options Args: :choices (dict): lookup table by key, for value selected from options displayed via stdout Returns: user selected resource identifier """ def safe_choice(sel_index, user_choice): if sel_index == 'letters': return user_choice elif isinstance(user_choice, int): return user_choice elif isinstance(user_choice, str): try: return int(user_choice) except TypeError: return userchoice_mapping(user_choice) validate = True try: while validate: choice = input( '\n\tEnter a letter to select [%s]: '.expandtabs(8) % (choices[userchoice_mapping(default)] if selector == 'letters' else choices[int(default)])) or default # prevent entering of letters for choice if numbered selector index choice = safe_choice(selector, choice) index_range = [x for x in choices] if range_test( 0, max(index_range), userchoice_mapping(choice) if selector == 'letters' else int(choice)): resourceid = choices[userchoice_mapping( choice)] if selector == 'letters' else choices[int(choice)] validate = False else: stdout_message( 'You must enter a %s between %s and %s' % ('letter' if selector == 'letters' else 'number', userchoice_mapping(index_range[0]) if selector == 'letters' else index_range[0], userchoice_mapping(index_range[-1]) if selector == 'letters' else index_range[-1])) except KeyError: resourceid = None choice = [k for k, v in choices.items() if v is None] except TypeError as e: logger.exception(f'Typed input caused an exception. Error {e}') sys.exit(1) stdout_message('You selected choice {}, {}'.format(choice, resourceid)) return resourceid