import json


def _load_iam_data(path):
    """Builds a dictionary containing the information about all the
    AWS IAM resources and the actions they relate to (for more information look
    at the README.md in this directory). The keys of the dictionary are all the
    possible IAM policy actions and the values are sets containing the
    resources they allow access to. For instance:
    {'ec2:allocateaddres':{'elastic-ip', 'ipv4pool-ec2'}}"""
    data = None
    with open(path, "r") as file:
        data = json.load(file)
    actions = {}
    for service in data:
        prefix = service["prefix"]
        for privilege in service["privileges"]:
            action = privilege["privilege"].lower()
            action = f"{prefix}:{action}"
            resources = set()
            for resource_type in privilege["resource_types"]:
                if "resource_type" not in resource_type:
                    continue
                resource = resource_type["resource_type"].replace("*", "")
                if resource == "":
                    continue
                # The actions related to S3 can give access to objects, buckets
                # or both (an object is a file in a bucket). Altimeter scans
                # buckets, but not objects. So,for us, if an action give access
                # to a object, it gives access to whole the bucket.
                if prefix == "s3" and resource == "object":
                    resource = "bucket"
                resources.add(resource)
            actions[action] = resources
    return actions