Exemple #1
0
    def main(args, evpc=None):
        """
        For every AWS profile + region, dump every VPC to STDOUT.
        :param args: The parsed arguments and flags from the CLI.
        :returns: None
        """
        sessions = get_all_sessions()

        # We assume all sessions have the same list of regions.
        # This might not always be true. This is an optimization.
        regions = get_region_names(sessions[0])

        vpcs = {}

        for session in sessions:
            boto3.setup_default_session(profile_name=session.profile)
            vpcs[session.profile] = {}
            for region_name in regions:
                if region_name not in vpcs[session.profile]:
                    vpcs[session.profile][region_name] = {}
                ec2 = boto3.resource('ec2', region_name=region_name)
                for vpc in ec2.vpcs.all():
                    vpc_tags = make_tag_dict(vpc)
                    vpc_name = vpc_tags.get('Name', vpc.id)
                    vpcs[session.profile][region_name][vpc_name] = vpc.id

        print(output_formatter(vpcs, args.output_format))
    def main(args, evpc=None):
        """
        For every AWS profile + region, dump every VPC to STDOUT.
        :param args: The parsed arguments and flags from the CLI.
        :returns: None
        """
        sessions = get_all_sessions()

        # We assume all sessions have the same list of regions.
        # This might not always be true. This is an optimization.
        regions = get_region_names(sessions[0])

        vpcs = {}

        for session in sessions:
            boto3.setup_default_session(profile_name=session.profile)
            vpcs[session.profile] = {}
            for region_name in regions:
                if region_name not in vpcs[session.profile]:
                    vpcs[session.profile][region_name] = {}
                ec2 = boto3.resource('ec2', region_name=region_name)
                for vpc in ec2.vpcs.all():
                    vpc_tags = make_tag_dict(vpc)
                    vpc_name = vpc_tags.get('Name', vpc.id)
                    vpcs[session.profile][region_name][vpc_name] = vpc.id

        print(output_formatter(vpcs, args.output_format))
Exemple #3
0
def security_groups(args, evpc):
    """Output Security Groups in a Botoform compatible format."""
    sgs = {}
    for sg in evpc.security_groups.all():
        sgs[sg.group_name] = []
        for perm in sg.ip_permissions:
            ip_protocol = perm['IpProtocol']
            from_port = perm.get('FromPort', -1)
            to_port = perm.get('ToPort', -1)
            port_range = from_port
            if from_port != to_port:
                port_range = '{}-{}'.format(from_port, to_port)
            if len(perm['IpRanges']) >= 1:
                for iprange in perm['IpRanges']:
                    rule = []
                    rule.append(iprange['CidrIp'])
                    rule.append(ip_protocol)
                    rule.append(port_range)
                    sgs[sg.group_name].append(rule)
            if len(perm['UserIdGroupPairs']) >= 1:
                for pair in perm['UserIdGroupPairs']:
                    rule = []
                    related_sg = evpc.boto.ec2.SecurityGroup(
                        id=pair['GroupId'])
                    rule.append(related_sg.group_name)
                    rule.append(ip_protocol)
                    rule.append(port_range)
                    sgs[sg.group_name].append(rule)

    print(output_formatter({'security_groups': sgs}, args.output_format))
Exemple #4
0
def instances(args, evpc):
    """Output a list of instance names."""
    if args.identifiers or args.roles or args.exclude:
        instances = evpc.find_instances(args.identifiers, args.roles,
                                        args.exclude)
    else:
        instances = evpc.instances
    print(output_formatter(map(str, instances), args.output_format))
Exemple #5
0
def security_groups(args, evpc):
    """
    Output Security Groups to standard out in :ref:`Botoform Schema <schema reference>`.

    :param args: The parsed arguments and flags from the CLI.
    :param evpc: An instance of :meth:`botoform.enriched.vpc.EnrichedVPC`.

    :returns: security_groups to standard out in :ref:`Botoform Schema <schema reference>`.
    """
    sgs = {"security_groups": evpc.enriched_security_groups}
    print(output_formatter(sgs, args.output_format))
Exemple #6
0
def security_groups(args, evpc):
    """
    Output Security Groups to standard out in :ref:`Botoform Schema <schema reference>`.

    :param args: The parsed arguments and flags from the CLI.
    :param evpc: An instance of :meth:`botoform.enriched.vpc.EnrichedVPC`.

    :returns: security_groups to standard out in :ref:`Botoform Schema <schema reference>`.
    """ 
    sgs = {'security_groups' : evpc.enriched_security_groups}
    print(output_formatter(sgs, args.output_format))
Exemple #7
0
def instances(args, evpc):
    """
    Output instance roles to standard out in :ref:`Botoform Schema <schema reference>`.

    :param args: The parsed arguments and flags from the CLI.
    :param evpc: An instance of :meth:`botoform.enriched.vpc.EnrichedVPC`.

    :returns: instance_roles to standard out in :ref:`Botoform Schema <schema reference>`.
    """
    if args.identifiers or args.roles or args.exclude:
        instances = evpc.find_instances(args.identifiers, args.roles, args.exclude)
    else:
        instances = evpc.instances
    print(output_formatter(map(str, instances), args.output_format))
Exemple #8
0
    def main(args, evpc=None):
        """
        List all VPC names to STDOUT for a given AWS profile + region.

        :param args: The parsed arguments and flags from the CLI.
        :returns: None
        """
        vpc_names = []
        bconn = BotoConnections(args.region, args.profile)
        for vpc in bconn.ec2.vpcs.all():
            vpc_tags = make_tag_dict(vpc)
            vpc_name = vpc_tags.get("Name", vpc.id)
            vpc_names.append(vpc_name)
        print(output_formatter(vpc_names, args.output_format))
Exemple #9
0
def security_groups(args, evpc):
    """
    Output Security Groups to standard out in :ref:`Botoform Schema <schema reference>`.

    :param args: The parsed arguments and flags from the CLI.
    :param evpc: An instance of :meth:`botoform.enriched.vpc.EnrichedVPC`.

    :returns: security_groups to standard out in :ref:`Botoform Schema <schema reference>`.
    """ 
    sgs = {}
    for sg in evpc.security_groups.all():
        sgs[sg.group_name] = []
        for perm in sg.ip_permissions:
            ip_protocol = perm['IpProtocol']
            from_port = perm.get('FromPort', -1)
            to_port   = perm.get('ToPort', -1)
            port_range = from_port
            if from_port != to_port:
                port_range = '{}-{}'.format(from_port, to_port)
            if len(perm['IpRanges']) >= 1:
                for iprange in perm['IpRanges']:
                    rule = []
                    rule.append(iprange['CidrIp'])
                    rule.append(ip_protocol)
                    rule.append(port_range)
                    sgs[sg.group_name].append(rule)
            if len(perm['UserIdGroupPairs']) >= 1:
                for pair in perm['UserIdGroupPairs']:
                    rule = []
                    related_sg = evpc.boto.ec2.SecurityGroup(id=pair['GroupId'])
                    rule.append(related_sg.group_name)
                    rule.append(ip_protocol)
                    rule.append(port_range)
                    sgs[sg.group_name].append(rule)

    print(output_formatter({'security_groups' : sgs}, args.output_format))