def delete_network():
    existing_vpcs = u.get_vpc_dict()
    if VPC_NAME in existing_vpcs:
        vpc = ec2.Vpc(existing_vpcs[VPC_NAME].id)
        print("Deleting VPC %s (%s) subresources:" % (VPC_NAME, vpc.id))

        for subnet in vpc.subnets.all():
            try:
                sys.stdout.write("Deleting subnet %s ... " % subnet.id)
                sys.stdout.write(response_type(subnet.delete()) + '\n')
            except Exception as e:
                sys.stdout.write('failed\n')
                util.log_error(str(e) + '\n')

        for gateway in vpc.internet_gateways.all():
            sys.stdout.write("Deleting gateway %s ... " % gateway.id)
            # note: if instances are using VPC, this fails with
            # botocore.exceptions.ClientError: An error occurred (DependencyViolation) when calling the DetachInternetGateway operation: Network vpc-ca4abab3 has some mapped public address(es). Please unmap those public address(es) before detaching the gateway.

            sys.stdout.write('detached ... ' if u.is_good_response(
                gateway.detach_from_vpc(VpcId=vpc.id)) else ' detach_failed ')
            sys.stdout.write('deleted ' if u.is_good_response(gateway.delete(
            )) else ' delete_failed ')
            sys.stdout.write('\n')

        def desc():
            return "%s (%s)" % (route_table.id, u.get_name(route_table.tags))

        for route_table in vpc.route_tables.all():
            sys.stdout.write(f"Deleting route table {desc()} ... ")
            try:
                sys.stdout.write(response_type(route_table.delete()) + '\n')
            except Exception as e:
                sys.stdout.write('failed\n')
                util.log_error(str(e) + '\n')

        def desc():
            return "%s (%s, %s)" % (security_group.id,
                                    u.get_name(security_group.tags),
                                    security_group.group_name)

        for security_group in vpc.security_groups.all():
            # default group is undeletable, skip
            if security_group.group_name == 'default':
                continue
            sys.stdout.write('Deleting security group %s ... ' % (desc()))
            try:
                sys.stdout.write(response_type(security_group.delete()) + '\n')
            except Exception as e:
                sys.stdout.write('failed\n')
                util.log_error(str(e) + '\n')

        sys.stdout.write("Deleting VPC %s ... " % vpc.id)
        try:
            sys.stdout.write(response_type(vpc.delete()) + '\n')
        except Exception as e:
            sys.stdout.write('failed\n')
            util.log_error(str(e) + '\n')
def delete_keypair():
    keypairs = u.get_keypair_dict()
    keypair = keypairs.get(KEYPAIR_NAME, '')
    if keypair:
        try:
            sys.stdout.write("Deleting keypair %s (%s) ... " %
                             (keypair.key_name, KEYPAIR_NAME))
            sys.stdout.write(response_type(keypair.delete()) + '\n')
        except Exception as e:
            sys.stdout.write('failed\n')
            util.log_error(str(e) + '\n')

    keypair_fn = u.get_keypair_fn()
    if os.path.exists(keypair_fn):
        print("Deleting local keypair file %s" % (keypair_fn, ))
        os.system('rm -f ' + keypair_fn)
def delete_efs():
    efss = u.get_efs_dict()
    efs_id = efss.get(EFS_NAME, '')
    efs_client = u.get_efs_client()
    if efs_id:
        try:
            # delete mount targets first
            print("About to delete %s (%s)" % (efs_id, EFS_NAME))
            response = efs_client.describe_mount_targets(FileSystemId=efs_id)
            assert u.is_good_response(response)
            for mount_response in response['MountTargets']:
                id_ = mount_response['MountTargetId']
                sys.stdout.write('Deleting mount target %s ... ' % (id_, ))
                sys.stdout.flush()
                response = efs_client.delete_mount_target(MountTargetId=id_)
                print(response_type(response))

            sys.stdout.write('Deleting EFS %s (%s)... ' % (efs_id, EFS_NAME))
            sys.stdout.flush()
            u.delete_efs_by_id(efs_id)

        except Exception as e:
            sys.stdout.write(f'failed with {e}\n')
            util.log_error(str(e) + '\n')
    def delete_vpc(vpc, partial=True):
        """Deletes VPC + all resources, if "partial" set to True, only deletes associated security groups
    """
        print("Deleting VPC %s (%s) subresources:" % (VPC_NAME, vpc.id))

        # don't modify default VPC
        if not partial:
            for subnet in vpc.subnets.all():
                try:
                    sys.stdout.write("Deleting subnet %s ... " % subnet.id)
                    sys.stdout.write(response_type(subnet.delete()) + '\n')
                except Exception as e:
                    sys.stdout.write('failed\n')
                    util.log_error(str(e) + '\n')

            for gateway in vpc.internet_gateways.all():
                sys.stdout.write("Deleting gateway %s ... " % gateway.id)
                # note: if instances are using VPC, this fails with
                # botocore.exceptions.ClientError: An error occurred (DependencyViolation) when calling the DetachInternetGateway operation: Network vpc-ca4abab3 has some mapped public address(es). Please unmap those public address(es) before detaching the gateway.

                sys.stdout.write('detached ... ' if u.is_good_response(
                    gateway.detach_from_vpc(
                        VpcId=vpc.id)) else ' detach_failed ')
                sys.stdout.write('deleted ' if u.is_good_response(
                    gateway.delete()) else ' delete_failed ')
                sys.stdout.write('\n')

            def desc():
                return "%s (%s)" % (route_table.id, u.get_name(
                    route_table.tags))

            for route_table in vpc.route_tables.all():
                sys.stdout.write(f"Deleting route table {desc()} ... ")
                try:
                    sys.stdout.write(
                        response_type(route_table.delete()) + '\n')
                except Exception as e:
                    sys.stdout.write('failed\n')
                    util.log_error(str(e) + '\n')

        else:
            util.log(
                f"vpc {vpc.id} is a default VPC, only doing partial deletion")

        def desc():
            return "%s (%s, %s)" % (security_group.id,
                                    u.get_name(security_group.tags),
                                    security_group.group_name)

        ncluster_security_groups = u.get_security_group_names()
        for security_group in vpc.security_groups.all():
            # default group is undeletable, skip
            if security_group.group_name == 'default':
                continue

            # don't delete groups created outside of ncluster framework
            if security_group.group_name not in ncluster_security_groups:
                continue

            sys.stdout.write('Deleting security group %s ... ' % (desc()))
            try:
                sys.stdout.write(response_type(security_group.delete()) + '\n')
            except Exception as e:
                sys.stdout.write('failed\n')
                util.log_error(str(e) + '\n')

        if not partial:
            sys.stdout.write("Deleting VPC %s ... " % vpc.id)
            try:
                sys.stdout.write(response_type(vpc.delete()) + '\n')
            except Exception as e:
                sys.stdout.write('failed\n')
                util.log_error(str(e) + '\n')