def sg_access_map(ip_perm_list):
    """Convert the ip_perm_list into a dictionary where the
    key is the tuple (protocol, port-range) and the value is
    the set of CIDRs that are allowed access for that protocol/port(s).
    """
    access_map = {}
    for ip_perm in ip_perm_list:
        if ip_perm.from_port is not None:
            from_port = int(ip_perm.from_port)
        else:
            from_port = None
        if ip_perm.to_port is not None:
            to_port = int(ip_perm.to_port)
        else:
            to_port = None
        target = (ip_perm.ip_protocol.upper(), from_port, to_port)
        for grant in ip_perm.grants:
            if grant.cidr_ip:
                if grant.cidr_ip.endswith("/32"):
                    grant_id = grant.cidr_ip.split('/', 1)[0]
                else:
                    grant_id = grant.cidr_ip
            elif grant.group_id:
                grant_id = grant.group_id
            else:
                continue
            if target not in access_map:
                access_map[target] = set()
            access_map[target].add(grant_id)
    return access_map