def check_region(region_parameter, region_name, session): """ Region us-east-1 as a default region here This is just to list aws regions, doesn't matter default region """ client = session.client("ec2", region_name=DEFAULT_REGION) valid_region_names = [ region["RegionName"] for region in client.describe_regions(AllRegions=True)["Regions"] ] if region_parameter != "all": if region_name not in valid_region_names: message = "There is no region named: {0}".format(region_name) exit_critical(message) else: valid_region_names = [region_name] return valid_region_names
def aws_main(args) -> BaseCommand: # Check if verbose mode is enabled if args.verbose: aws_verbose() # aws profile check if "region_name" not in args: session = generate_session(profile_name=args.profile_name, region_name=None) else: session = generate_session( profile_name=args.profile_name, region_name=args.region_name ) session.get_credentials() region_name = session.region_name partition_code = get_partition(session, region_name) if "region_name" not in args: region_names = [DEFAULT_REGION] else: # checking region configuration check_region_profile( arg_region_name=args.region_name, profile_region_name=region_name ) # assuming region parameter precedes region configuration if args.region_name is not None: region_name = args.region_name # get regions region_names = check_region( region_parameter=args.region_name, region_name=region_name, session=session, partition_code=partition_code, ) if "threshold" in args: if args.threshold is not None: if args.threshold.isdigit() is False: exit_critical("Threshold must be between 0 and 100") else: if int(args.threshold) < 0 or int(args.threshold) > 100: exit_critical("Threshold must be between 0 and 100") if args.command == "aws-vpc": command = Vpc( vpc_id=args.vpc_id, region_names=region_names, session=session, partition_code=partition_code, ) elif args.command == "aws-policy": command = Policy( region_names=region_names, session=session, partition_code=partition_code ) elif args.command == "aws-iot": command = Iot( thing_name=args.thing_name, region_names=region_names, session=session, partition_code=partition_code, ) elif args.command == "aws-all": command = All( region_names=region_names, session=session, partition_code=partition_code ) elif args.command == "aws-limit": command = Limit( region_names=region_names, session=session, threshold=args.threshold, partition_code=partition_code, ) elif args.command == "aws-security": command = Security( region_names=region_names, session=session, commands=args.commands, partition_code=partition_code, ) else: raise NotImplementedError("Unknown command") return command
def check_region_profile(arg_region_name, profile_region_name): if arg_region_name is None and profile_region_name is None: exit_critical("Neither region parameter nor region config were passed")
def main(): # Entry point for the CLI. # Load commands parser = generate_parser() if len(sys.argv) <= 1: parser.print_help() return args = parser.parse_args() # Check if verbose mode is enabled if args.verbose: aws_verbose() if args.language is None or args.language not in AVAILABLE_LANGUAGES: language = "en_US" else: language = args.language # Diagram check if "diagram" not in args: diagram = False else: diagram = args.diagram # defining default language to show messages defaultlanguage = gettext.translation("messages", localedir=dirname(__file__) + "/locales", languages=[language]) defaultlanguage.install() _ = defaultlanguage.gettext # diagram version check check_diagram_version(diagram) # filters check filters: List[Filterable] = [] if "filters" in args: if args.filters is not None: filters = parse_filters(args.filters) # aws profile check if "region_name" not in args: session = generate_session(profile_name=args.profile_name, region_name=None) else: session = generate_session(profile_name=args.profile_name, region_name=args.region_name) session.get_credentials() region_name = session.region_name if "region_name" not in args: region_names = [DEFAULT_REGION] else: # checking region configuration check_region_profile(arg_region_name=args.region_name, profile_region_name=region_name) # assuming region parameter precedes region configuration if args.region_name is not None: region_name = args.region_name # get regions region_names = check_region( region_parameter=args.region_name, region_name=region_name, session=session, ) if "threshold" in args: if args.threshold is not None: if args.threshold.isdigit() is False: exit_critical(_("Threshold must be between 0 and 100")) else: if int(args.threshold) < 0 or int(args.threshold) > 100: exit_critical(_("Threshold must be between 0 and 100")) if args.command == "aws-vpc": command = Vpc( vpc_id=args.vpc_id, region_names=region_names, session=session, ) elif args.command == "aws-policy": command = Policy( region_names=region_names, session=session, ) elif args.command == "aws-iot": command = Iot( thing_name=args.thing_name, region_names=region_names, session=session, ) elif args.command == "aws-all": command = All(region_names=region_names, session=session) elif args.command == "aws-limit": command = Limit( region_names=region_names, session=session, threshold=args.threshold, ) else: raise NotImplementedError("Unknown command") if "services" in args and args.services is not None: services = args.services.split(",") else: services = [] command.run(diagram, args.verbose, services, filters)