|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | + |
| 14 | +"""This module contains code related to Amazon SageMaker Collection. |
| 15 | +
|
| 16 | +These Classes helps in providing features to maintain and create collections |
| 17 | +""" |
| 18 | + |
| 19 | +from __future__ import absolute_import |
| 20 | +import json |
| 21 | +import time |
| 22 | +from typing import List |
| 23 | + |
| 24 | + |
| 25 | +from botocore.exceptions import ClientError |
| 26 | +from sagemaker.session import Session |
| 27 | + |
| 28 | + |
| 29 | +class Collection(object): |
| 30 | + """Sets up Amazon SageMaker Collection.""" |
| 31 | + |
| 32 | + def __init__(self, sagemaker_session): |
| 33 | + """Initializes a Collection instance. |
| 34 | +
|
| 35 | + The collection provides a logical grouping for model groups |
| 36 | +
|
| 37 | + Args: |
| 38 | + sagemaker_session (sagemaker.session.Session): Session object which |
| 39 | + manages interactions with Amazon SageMaker APIs and any other |
| 40 | + AWS services needed. If not specified, one is created using |
| 41 | + the default AWS configuration chain. |
| 42 | + """ |
| 43 | + self.sagemaker_session = sagemaker_session or Session() |
| 44 | + |
| 45 | + def _check_access_error(self, err: ClientError): |
| 46 | + """To check if the error is related to the access error and to provide the relavant message |
| 47 | +
|
| 48 | + Args: |
| 49 | + err: The client error that needs to be checked |
| 50 | + """ |
| 51 | + error_code = err.response["Error"]["Code"] |
| 52 | + if error_code == "AccessDeniedException": |
| 53 | + raise Exception( |
| 54 | + f"{error_code}: This account needs to attach a custom policy " |
| 55 | + "to the user role to gain access to Collections. Refer - " |
| 56 | + "https://docs.aws.amazon.com/sagemaker/latest/dg/modelcollections-permissions.html" |
| 57 | + ) |
| 58 | + |
| 59 | + def create(self, collection_name: str, parent_collection_name: str = None): |
| 60 | + """Creates a collection |
| 61 | +
|
| 62 | + Args: |
| 63 | + collection_name (str): The name of the collection to be created |
| 64 | + parent_collection_name (str): The name of the parent collection. |
| 65 | + To be None if the collection is to be created on the root level |
| 66 | + """ |
| 67 | + |
| 68 | + tag_rule_key = f"sagemaker:collection-path:{time.time()}" |
| 69 | + tags_on_collection = { |
| 70 | + "sagemaker:collection": "true", |
| 71 | + "sagemaker:collection-path:root": "true", |
| 72 | + } |
| 73 | + tag_rule_values = [collection_name] |
| 74 | + |
| 75 | + if parent_collection_name is not None: |
| 76 | + try: |
| 77 | + group_query = self.sagemaker_session.get_resource_group_query( |
| 78 | + group=parent_collection_name |
| 79 | + ) |
| 80 | + except ClientError as e: |
| 81 | + error_code = e.response["Error"]["Code"] |
| 82 | + |
| 83 | + if error_code == "NotFoundException": |
| 84 | + raise ValueError(f"Cannot find collection: {parent_collection_name}") |
| 85 | + self._check_access_error(err=e) |
| 86 | + raise |
| 87 | + if group_query.get("GroupQuery"): |
| 88 | + parent_tag_rule_query = json.loads( |
| 89 | + group_query["GroupQuery"].get("ResourceQuery", {}).get("Query", "") |
| 90 | + ) |
| 91 | + parent_tag_rule = parent_tag_rule_query.get("TagFilters", [])[0] |
| 92 | + if not parent_tag_rule: |
| 93 | + raise "Invalid parent_collection_name" |
| 94 | + parent_tag_value = parent_tag_rule["Values"][0] |
| 95 | + tags_on_collection = { |
| 96 | + parent_tag_rule["Key"]: parent_tag_value, |
| 97 | + "sagemaker:collection": "true", |
| 98 | + } |
| 99 | + tag_rule_values = [f"{parent_tag_value}/{collection_name}"] |
| 100 | + try: |
| 101 | + resource_filters = [ |
| 102 | + "AWS::SageMaker::ModelPackageGroup", |
| 103 | + "AWS::ResourceGroups::Group", |
| 104 | + ] |
| 105 | + |
| 106 | + tag_filters = [ |
| 107 | + { |
| 108 | + "Key": tag_rule_key, |
| 109 | + "Values": tag_rule_values, |
| 110 | + } |
| 111 | + ] |
| 112 | + resource_query = { |
| 113 | + "Query": json.dumps( |
| 114 | + {"ResourceTypeFilters": resource_filters, "TagFilters": tag_filters} |
| 115 | + ), |
| 116 | + "Type": "TAG_FILTERS_1_0", |
| 117 | + } |
| 118 | + collection_create_response = self.sagemaker_session.create_group( |
| 119 | + collection_name, resource_query, tags_on_collection |
| 120 | + ) |
| 121 | + return { |
| 122 | + "Name": collection_create_response["Group"]["Name"], |
| 123 | + "Arn": collection_create_response["Group"]["GroupArn"], |
| 124 | + } |
| 125 | + |
| 126 | + except ClientError as e: |
| 127 | + message = e.response["Error"]["Message"] |
| 128 | + error_code = e.response["Error"]["Code"] |
| 129 | + |
| 130 | + if error_code == "BadRequestException" and "group already exists" in message: |
| 131 | + raise ValueError("Collection with the given name already exists") |
| 132 | + |
| 133 | + self._check_access_error(err=e) |
| 134 | + raise |
| 135 | + |
| 136 | + def delete(self, collections: List[str]): |
| 137 | + """Deletes a lits of collection |
| 138 | +
|
| 139 | + Args: |
| 140 | + collections (List[str]): List of collections to be deleted |
| 141 | + Only deletes a collection if it is empty |
| 142 | + """ |
| 143 | + |
| 144 | + if len(collections) > 10: |
| 145 | + raise ValueError("Can delete upto 10 collections at a time") |
| 146 | + |
| 147 | + delete_collection_failures = [] |
| 148 | + deleted_collection = [] |
| 149 | + collection_filter = [ |
| 150 | + { |
| 151 | + "Name": "resource-type", |
| 152 | + "Values": ["AWS::ResourceGroups::Group", "AWS::SageMaker::ModelPackageGroup"], |
| 153 | + }, |
| 154 | + ] |
| 155 | + for collection in collections: |
| 156 | + try: |
| 157 | + collection_details = self.sagemaker_session.list_group_resources( |
| 158 | + group=collection, filters=collection_filter |
| 159 | + ) |
| 160 | + except ClientError as e: |
| 161 | + self._check_access_error(err=e) |
| 162 | + delete_collection_failures.append( |
| 163 | + {"collection": collection, "message": e.response["Error"]["Message"]} |
| 164 | + ) |
| 165 | + continue |
| 166 | + if collection_details.get("Resources") and len(collection_details["Resources"]) > 0: |
| 167 | + delete_collection_failures.append( |
| 168 | + {"collection": collection, "message": "Validation error: Collection not empty"} |
| 169 | + ) |
| 170 | + else: |
| 171 | + try: |
| 172 | + self.sagemaker_session.delete_resource_group(group=collection) |
| 173 | + deleted_collection.append(collection) |
| 174 | + except ClientError as e: |
| 175 | + self._check_access_error(err=e) |
| 176 | + delete_collection_failures.append( |
| 177 | + {"collection": collection, "message": e.response["Error"]["Message"]} |
| 178 | + ) |
| 179 | + return { |
| 180 | + "deleted_collections": deleted_collection, |
| 181 | + "delete_collection_failures": delete_collection_failures, |
| 182 | + } |
0 commit comments