import json


def getNodeGroups(session, url, details=False):
    """
    Return a list of node group objects (by the index endpoint).

    Passing details=True will get all information for each node group.
    """
    groups = []
    page = 1
    per_page = 100

    done = False
    while not done:
        new_node_groups = session.get("{}/api/v2/node_groups.json".format(url), params={"page": page, "per_page": per_page}).json()
        groups += new_node_groups
        page += 1
        done = True if len(new_node_groups) < per_page else False

    if details:
        detailed_groups = []
        for group in groups:
            detailed_group = session.get("{}/api/v2/node_groups/{}.json".format(url, group["id"])).json()
            detailed_group["scan_options"] = json.loads(detailed_group["scan_options"])
            detailed_groups.append(detailed_group)
        return detailed_groups
    return groups