Skip to content

Get Collection Name

Overview

Returns the actual under the hood collection name.

Parameters:

Name Type Description Default
collection_name str

Name to check.

required
api_version Optional[str]

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

None
force_actual_name bool

Edge case. If True, will check if the actual name is the name passed in. Useful if there are multiple friendly names.

False

Returns:

Type Description
str

The actual name of the collection.

Source code in purviewautomation/collections.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
def get_real_collection_name(
    self, collection_name: str, api_version: Optional[str] = None, force_actual_name: bool = False
) -> str:
    """Returns the actual under the hood collection name.

    Args:
        collection_name: Name to check.
        api_version: If None, default is "2019-11-01-preview".
        force_actual_name: Edge case. If True, will check if the
            actual name is the name passed in. Useful if there
            are multiple friendly names.

    Returns:
        The actual name of the collection.

    Raises:
        If the collection doesn't exist, will raise an error
            that no collection exists.
        If multiple friendly names exist, will raise an error
            listing the multiple friendly names.
    """
    if not api_version:
        api_version = self.collections_api_version

    collections = self.list_collections(only_names=True, api_version=api_version)
    friendly_names = [
        (name, collections[name]) for name, value in collections.items() if collection_name == value["friendlyName"]
    ]

    if collection_name not in collections and len(friendly_names) == 0:
        err_msg = (
            "collection_name parameter value error. "
            f"The collection '{collection_name}' either doesn't exist or your don't have permission to start on it. "
            "If you're trying to create a child collection, would need to be a collection admin on that collection "
            "if it exists. Name is case sensitive."
        )
        raise ValueError(err_msg)
    elif collection_name not in collections and len(friendly_names) == 1:
        return friendly_names[0][0]
    elif collection_name in collections and len(friendly_names) <= 1:
        return collection_name

    if force_actual_name:
        for i in range(len(friendly_names)):
            if friendly_names[i][0] == collection_name:
                return collection_name
    else:
        multiple_friendly_names = []
        for item in friendly_names:
            parent_name = item[1]["parentCollection"]
            parent_friendly_name = collections[parent_name]["friendlyName"]
            friendly_output = f"{item[0]}: [collection info: actual_name: {item[0]}, friendlyName: {item[1]['friendlyName']}, parentCollection: {parent_friendly_name}]"
            multiple_friendly_names.append(friendly_output)
        newline = "\n"
        err_msg = (
            f"Multiple collections exist with the friendly name '{collection_name}'. "
            f"Please choose and re-enter the first item from one of the options below in place of '{collection_name}': "
            f"{newline}{newline.join(map(str, multiple_friendly_names))} {newline}"
            f"If you want to use the collection name '{collection_name}' and it's listed as an option above as "
            f"a first item (actual_name), add the force_actual_name parameter to True. "
            "Ex: force_actual_name=True"
        )
        raise ValueError(err_msg)

Examples

Pass in either an actual or friendly collection name (For more info on what actual and friendly names mean, see: Purview Names Overview). The code will return one of the following:

  • The actual name of the collection (if it exists)

  • A friendly error stating that the collection doesn't exist (if there's no collection by that name)

  • If passing in a friendly name and multiple friendly names exist, an error will return displaying the multiple friendly names in order to choose which to use

For example, if the Purview collections look like this:

Get Collection Name

To return the actual name of test 1:

print(client.get_real_collection_name("test 1"))

Will output the actual name of the collection. In this example, it would output: mojpnk.