Skip to content

List Collections

Overview

Returns the Purview collections.

Parameters:

Name Type Description Default
only_names bool

If True, will return only the actual, friendly, and parent collection names.

False
pprint bool

If True, will pretty print the collections. If only_names is also True, will pretty print only the names listed in only_names.

False
api_version Optional[str]

If None, default is "2019-11-01-preview".

None

Returns:

Type Description

List of dictionaries containing the collection info. If only_names is True, will return a dictionary of only_names items. If pprint is True, will print the values to the screen and nothing is returned.

Source code in purviewautomation/collections.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def list_collections(self, only_names: bool = False, pprint: bool = False, api_version: Optional[str] = None):
    """Returns the Purview collections.

    Args:
        only_names: If True, will return only the actual, friendly,
            and parent collection names.
        pprint: If True, will pretty print the collections.
            If only_names is also True, will pretty print only
                the names listed in only_names.
        api_version: If None, default is "2019-11-01-preview".

    Returns:
        List of dictionaries containing the collection info.
            If only_names is True, will return a dictionary
                of only_names items.
            If pprint is True, will print the values to the screen
                and nothing is returned.
    """
    if not api_version:
        api_version = self.collections_api_version

    url = f"{self.collections_endpoint}?api-version={api_version}"
    collection_request = requests.get(url=url, headers=self.header)
    if collection_request.status_code != 200:
        if collection_request.status_code == 403:
            err_msg = (
                "Not authorized. The Service Principal or user would need to be added as a Collection Admin on at "
                "least one collection. For more info see: "
                "https://learn.microsoft.com/en-us/azure/purview/catalog-permissions"
            )
            raise ValueError(err_msg)
        else:
            collection_request.raise_for_status()

    collections = collection_request.json()["value"]

    if only_names:
        coll_dict = {}
        for coll in collections:
            if "parentCollection" not in coll:
                coll_dict[coll["name"]] = {"friendlyName": coll["friendlyName"], "parentCollection": None}
            else:
                coll_dict[coll["name"]] = {
                    "friendlyName": coll["friendlyName"],
                    "parentCollection": coll["parentCollection"]["referenceName"],
                }
        if pprint:
            pretty_print(coll_dict, sort_dicts=False)

        return coll_dict

    if pprint:
        pretty_print(collections, sort_dicts=False)

    return collections

Examples

The simpliest code is to print the collections (will provide the most detail)

print(client.list_collections())

Use the pprint (pretty print) parameter to print a cleaner output (print function is not needed)

client.list_collections(pprint=True)

To print only the real name, friendly name, and parent collection names (print function is not needed):

client.list_collections(only_names=True, pprint=True)

To return a list of the collections:

collections = client.list_collections()
    for coll in collections:
        print(coll)

To return a dictionary of the real, friendly, and parent collection names (real name is the key and friendly/parent collections are the values):

collection_names = client.list_collections(only_names=True)
for name, value in collection_names.items():
    print(name, value)

# Return just the keys (real names)
for name in collection_names:
    print(name)

# Return just the friendly names or just the parent collection names
for name in collection_names.values():
    friendly_name = name["friendlyName"]
    parent_name = name["parentCollection"]