Skip to content

Create Collections

Overview

Create collections.

Can create any of the following: -One collection -Multiple Collections -One collection hierarchy (multiple parent/child relationships) -Multiple collection hierarchies

Parameters:

Name Type Description Default
start_collection str

Existing collection name. Use list_collections(only_names, pprint=True) to see all of the collection names. Accepts friendly and actual names.

required
collection_names Union[str, List[str]]

collection name or names.

required
force_actual_name bool

Edge Case. If multiple duplicate friendly names and one of the actual names is the name passed in.

False
api_version Optional[str]

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

None
**kwargs

safe_delete_friendly_name: Used during the safe delete functionality. Don't call directly.

{}

Returns:

Type Description
None

Prints the successfully created collection/collections. If the collection/collections already exist, nothing prints.

Source code in purviewautomation/collections.py
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
def create_collections(
    self,
    start_collection: str,
    collection_names: Union[str, List[str]],
    force_actual_name: bool = False,
    api_version: Optional[str] = None,
    **kwargs,
) -> None:  # TODO Need to update this
    """Create collections.

    Can create any of the following:
    -One collection
    -Multiple Collections
    -One collection hierarchy (multiple parent/child relationships)
    -Multiple collection hierarchies

    Args:
        start_collection: Existing collection name.
            Use list_collections(only_names, pprint=True) to see
            all of the collection names. Accepts friendly and
            actual names.
        collection_names: collection name or names.
        force_actual_name: Edge Case. If multiple duplicate friendly names
            and one of the actual names is the name passed in.
        api_version: If None, default is "2019-11-01-preview".
        **kwargs:
            safe_delete_friendly_name: Used during the safe delete
            functionality. Don't call directly.

    Returns:
        Prints the successfully created collection/collections.
            If the collection/collections already exist, nothing prints.
    """
    if not api_version:
        api_version = self.collections_api_version

    coll_dict = self.list_collections(only_names=True, api_version=api_version)
    start_collection = self.get_real_collection_name(start_collection, api_version, force_actual_name)

    collection_list = []
    if not isinstance(collection_names, (str, list)):
        err = """The collection_names parameter has
                 to be a string or list type.
              """
        raise ValueError(err)
    elif isinstance(collection_names, str):
        collection_names = [collection_names]

    collection_names_list = [[name] for name in collection_names]
    for item in collection_names_list:
        for name in item:
            if "/" in name:
                names = name.split("/")
                names = [name.strip() for name in names]
                collection_list.append(names)
            else:
                names = [name.strip() for name in item]
                collection_list.append(names)

    for colls in collection_list:
        updated_collection_list = self._return_updated_collection_list(
            start_collection, colls, coll_dict, api_version
        )
        for index, name in enumerate(updated_collection_list):
            if index == 0:
                if name in coll_dict and coll_dict[name]["parentCollection"].lower() == start_collection.lower():
                    continue
                else:
                    if "safe_delete_friendly_name" in kwargs:
                        friendly_name = kwargs["safe_delete_friendly_name"]
                    else:
                        friendly_name = colls[index]
                try:
                    request = self._return_request_info(
                        name=name,
                        friendly_name=friendly_name,
                        parent_collection=start_collection,
                        api_version=api_version,
                    )
                    print(request.content)
                    print("\n")
                except Exception as e:
                    raise e

            else:
                if (
                    name in coll_dict
                    and coll_dict[name]["parentCollection"].lower() == updated_collection_list[index - 1].lower()
                ):
                    continue
                else:
                    friendly_name = colls[index]
                    parent_collection = updated_collection_list[index - 1]
                try:
                    request = self._return_request_info(
                        name=name,
                        friendly_name=friendly_name,
                        parent_collection=parent_collection,
                        api_version=api_version,
                    )
                    print(request.content)
                    print("\n")
                except Exception as e:
                    raise e

Important

  • Code will automatically trim leading and trailing whitespaces in the collection names.
    • Ex: " test1", "test1 ", " test1 " would all equal "test1"
  • When creating a collection hierarchy See: Collection Hierarchies, the code trims leading/trailing whitespaces before and after the /. Ex: "test1 / test 2" would equal "test1/test2" (test1 is one collection and test2 is another collection).
  • Spaces in names are allowed: "my collection" is different than "mycollection".
  • Multiple spaces are allowed: "my new collection"
  • Collection names are case sensitive. "My-Company" and "my-company" are two different names.

Examples

The start_collection has to already exist in Purview. Pass in either the friendly name or the real name of the collection.

Create One Collection

If the Purview collections look like this:

Collections

Example 1: Create one collection starting from any of the three listed (purview-test-2, My-Company, test 1):

client.create_collections(start_collection="purview-test-2", 
                          collection_names="new collection 1"
                        )

This will create a collection under purview-test-2 (same level as the My-Company collection): Collections

Example 2: Create from test 1. sub test coll will be listed as a child of test 1. The keyword arguments can be ommited if desired:

client.create_collections("test 1", "sub test coll")

Collections

Create Multiple Collections

To create multiple collections, pass in a list to the collection_names parameter. The subcoll1 and subcoll2 collections would be children of test 1:

client.create_collections(start_collection="test 1", ["subcoll 1", "subcoll2"])

Collections

Example 2: Create four collections under subcoll 1:

colls = ["newcoll1", "newcoll2", "Random Collection", "Another Random Collection"]
client.create_collections("subcoll 1", colls)

Collections

Create One Collection Hierarcy

To create a collection hierarchy, add / for parent/child relationships. If the Purview collections look like this:

Collections

Example 1: Create a collection hierarchy under My-Company. subcoll1 would be a child under My-Company (referenced by the /), subcoll2 would be a child under subcoll1, and sub coll 3 would be a child under subcoll 2

client.create_collections(start_collection="My-Company", 
                          collection_names="subcoll1/subcoll2/sub coll 3"
                        )

Collections

Create Multiple Collection Hierarchies

If the Purview collections look like this:

Collections

Example 1: Create two collection hierarchies under My-Company, pass in a list of hierarchies:

first_hierarchy = "test 1/test 2/test 3/test4"
second_hierarchy = "second test 1/secondtest2/second test 3/ second test4"

client.create_collections(start_collection="My-Company",
                          collection_names=[first_hierarchy, second_hierarchy])

Collections

Hierarchies can be of different length. Create three hierarchies under My-Company:

short_hierarchy = "First Collection/Second Collection"
longer_hierarchy = "Longer Hierarhcy/Longer Hierarchy2/Another Collection"
even_longer_hierarchy = "long hier1/long hier2/long hier3/long hier4/long hier5"

client.create_collections("My-Company", 
                          [short_hierarchy, longer_hierarchy, even_longer_hierarchy]
                        )

Collections