Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(domain): tag update without replacement #32

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aws-sagemaker-domain/.rpdk-config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"artifact_type": "RESOURCE",
"typeName": "AWS::SageMaker::Domain",
"language": "java",
"runtime": "java8",
"runtime": "java8.al2",
"entrypoint": "software.amazon.sagemaker.domain.HandlerWrapper::handleRequest",
"testEntrypoint": "software.amazon.sagemaker.domain.HandlerWrapper::testEntrypoint",
"settings": {
Expand Down
13 changes: 11 additions & 2 deletions aws-sagemaker-domain/aws-sagemaker-domain.json
Original file line number Diff line number Diff line change
Expand Up @@ -492,15 +492,21 @@
"SubnetIds",
"VpcId"
],
"tagging": {
"taggable": true,
"tagOnCreate": true,
"tagUpdatable": true,
"cloudFormationSystemTags": true,
"tagProperty": "/properties/Tags"
},
"createOnlyProperties": [
"/properties/AppNetworkAccessType",
"/properties/AuthMode",
"/properties/DomainName",
"/properties/DomainSettings/RStudioServerProDomainSettings/DefaultResourceSpec",
"/properties/KmsKeyId",
"/properties/SubnetIds",
"/properties/VpcId",
"/properties/Tags"
"/properties/VpcId"
],
"writeOnlyProperties": [
"/properties/Tags"
Expand All @@ -524,6 +530,7 @@
"sagemaker:DescribeDomain",
"sagemaker:DescribeImage",
"sagemaker:DescribeImageVersion",
"sagemaker:ListTags",
"iam:CreateServiceLinkedRole",
"iam:PassRole",
"efs:CreateFileSystem",
Expand All @@ -540,8 +547,10 @@
},
"update": {
"permissions": [
"sagemaker:AddTags",
"sagemaker:CreateApp",
"sagemaker:UpdateDomain",
"sagemaker:DeleteTags",
"sagemaker:DescribeDomain",
"sagemaker:DescribeImage",
"sagemaker:DescribeImageVersion",
Expand Down
8 changes: 4 additions & 4 deletions aws-sagemaker-domain/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ _Required_: Yes

_Type_: String

_Maximum_: <code>63</code>
_Maximum Length_: <code>63</code>

_Pattern_: <code>^[a-zA-Z0-9](-*[a-zA-Z0-9]){0,62}</code>

Expand All @@ -115,7 +115,7 @@ _Required_: No

_Type_: String

_Maximum_: <code>2048</code>
_Maximum Length_: <code>2048</code>

_Pattern_: <code>.*</code>

Expand All @@ -139,7 +139,7 @@ _Required_: No

_Type_: List of <a href="tag.md">Tag</a>

_Update requires_: [Replacement](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-update-behaviors.html#update-replacement)
_Update requires_: [No interruption](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-update-behaviors.html#update-no-interrupt)

#### VpcId

Expand All @@ -149,7 +149,7 @@ _Required_: Yes

_Type_: String

_Maximum_: <code>32</code>
_Maximum Length_: <code>32</code>

_Pattern_: <code>[-0-9a-zA-Z]+</code>

Expand Down
4 changes: 2 additions & 2 deletions aws-sagemaker-domain/docs/customimage.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ _Required_: Yes

_Type_: String

_Maximum_: <code>63</code>
_Maximum Length_: <code>63</code>

_Pattern_: <code>^[a-zA-Z0-9](-*[a-zA-Z0-9]){0,62}</code>

Expand All @@ -48,7 +48,7 @@ _Required_: Yes

_Type_: String

_Maximum_: <code>63</code>
_Maximum Length_: <code>63</code>

_Pattern_: <code>^[a-zA-Z0-9]([-.]?[a-zA-Z0-9]){0,62}$</code>

Expand Down
4 changes: 2 additions & 2 deletions aws-sagemaker-domain/docs/defaultspacesettings.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ _Required_: No

_Type_: String

_Minimum_: <code>20</code>
_Minimum Length_: <code>20</code>

_Maximum_: <code>2048</code>
_Maximum Length_: <code>2048</code>

_Pattern_: <code>^arn:aws[a-z\-]*:iam::\d{12}:role/?[a-zA-Z_0-9+=,.@\-_/]+$</code>

Expand Down
6 changes: 3 additions & 3 deletions aws-sagemaker-domain/docs/resourcespec.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ _Required_: No

_Type_: String

_Maximum_: <code>256</code>
_Maximum Length_: <code>256</code>

_Pattern_: <code>^arn:aws(-[\w]+)*:sagemaker:.+:[0-9]{12}:image/[a-z0-9]([-.]?[a-z0-9])*$</code>

Expand All @@ -60,7 +60,7 @@ _Required_: No

_Type_: String

_Maximum_: <code>256</code>
_Maximum Length_: <code>256</code>

_Pattern_: <code>^arn:aws(-[\w]+)*:sagemaker:.+:[0-9]{12}:image-version/[a-z0-9]([-.]?[a-z0-9])*/[0-9]+$</code>

Expand All @@ -74,7 +74,7 @@ _Required_: No

_Type_: String

_Maximum_: <code>256</code>
_Maximum Length_: <code>256</code>

_Pattern_: <code>arn:aws[a-z\-]*:sagemaker:[a-z0-9\-]*:[0-9]{12}:studio-lifecycle-config/.*</code>

Expand Down
4 changes: 2 additions & 2 deletions aws-sagemaker-domain/docs/rstudioserverprodomainsettings.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ _Required_: Yes

_Type_: String

_Minimum_: <code>20</code>
_Minimum Length_: <code>20</code>

_Maximum_: <code>2048</code>
_Maximum Length_: <code>2048</code>

_Pattern_: <code>^arn:aws[a-z\-]*:iam::\d{12}:role/?[a-zA-Z_0-9+=,.@\-_/]+$</code>

Expand Down
4 changes: 2 additions & 2 deletions aws-sagemaker-domain/docs/sharingsettings.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ _Required_: No

_Type_: String

_Maximum_: <code>2048</code>
_Maximum Length_: <code>2048</code>

_Pattern_: <code>.*</code>

Expand All @@ -60,7 +60,7 @@ _Required_: No

_Type_: String

_Maximum_: <code>1024</code>
_Maximum Length_: <code>1024</code>

_Pattern_: <code>^(https|s3)://([^/]+)/?(.*)$</code>

Expand Down
8 changes: 4 additions & 4 deletions aws-sagemaker-domain/docs/tag.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ _Required_: Yes

_Type_: String

_Minimum_: <code>1</code>
_Minimum Length_: <code>1</code>

_Maximum_: <code>128</code>
_Maximum Length_: <code>128</code>

_Update requires_: [No interruption](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-update-behaviors.html#update-no-interrupt)

Expand All @@ -40,9 +40,9 @@ _Required_: Yes

_Type_: String

_Minimum_: <code>1</code>
_Minimum Length_: <code>1</code>

_Maximum_: <code>128</code>
_Maximum Length_: <code>128</code>

_Update requires_: [No interruption](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-update-behaviors.html#update-no-interrupt)

4 changes: 2 additions & 2 deletions aws-sagemaker-domain/docs/usersettings.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ _Required_: No

_Type_: String

_Minimum_: <code>20</code>
_Minimum Length_: <code>20</code>

_Maximum_: <code>2048</code>
_Maximum Length_: <code>2048</code>

_Pattern_: <code>^arn:aws[a-z\-]*:iam::\d{12}:role/?[a-zA-Z_0-9+=,.@\-_/]+$</code>

Expand Down
3 changes: 3 additions & 0 deletions aws-sagemaker-domain/resource-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ Resources:
- "kms:Decrypt"
- "kms:DescribeKey"
- "kms:GenerateDataKeyWithoutPlainText"
- "sagemaker:AddTags"
- "sagemaker:CreateApp"
- "sagemaker:CreateDomain"
- "sagemaker:DeleteApp"
- "sagemaker:DeleteDomain"
- "sagemaker:DeleteTags"
- "sagemaker:DescribeDomain"
- "sagemaker:DescribeImage"
- "sagemaker:DescribeImageVersion"
- "sagemaker:ListDomains"
- "sagemaker:ListTags"
- "sagemaker:UpdateDomain"
Resource: "*"
Outputs:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package software.amazon.sagemaker.domain;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.google.common.collect.Sets;

import software.amazon.awssdk.services.sagemaker.SageMakerClient;
import software.amazon.awssdk.services.sagemaker.model.AddTagsRequest;
import software.amazon.awssdk.services.sagemaker.model.DeleteTagsRequest;
import software.amazon.awssdk.services.sagemaker.model.ListTagsRequest;
import software.amazon.awssdk.services.sagemaker.model.ListTagsResponse;
import software.amazon.cloudformation.proxy.ProxyClient;
import software.amazon.cloudformation.proxy.ResourceHandlerRequest;

public class TaggingHelper {

public static List<Tag> consolidateTags(ResourceHandlerRequest<ResourceModel> request) {
Map<String, String> systemTags = request.getSystemTags();
List<Tag> customerTags = request.getDesiredResourceState().getTags();
Map<String, String> resourceTags = request.getDesiredResourceTags();

Map<String, String> tags = new HashMap<>();

if (resourceTags != null) {
tags.putAll(resourceTags);
}

if (systemTags != null) {
tags.putAll(systemTags);
}

if (customerTags != null) {
for (Tag e : customerTags) {
tags.put(e.getKey(), e.getValue());
}
}

return tags.entrySet().stream().map(e -> Tag.builder().key(e.getKey()).value(e.getValue()).build()).collect(Collectors.toList());
}

@SuppressWarnings("resource")
public static List<Tag> listTagsForResource(String resourceArn,
ProxyClient<SageMakerClient> proxyClient) {
ListTagsRequest listTagsRequest = ListTagsRequest.builder().resourceArn(resourceArn).build();
ListTagsResponse listTagsResponse = proxyClient.injectCredentialsAndInvokeV2(listTagsRequest, proxyClient.client()::listTags);
return transformTags(listTagsResponse.tags()).stream().collect(Collectors.toList());
}

@SuppressWarnings("resource")
public static void addTags(String resourceArn,
Set<Tag> tags,
ProxyClient<SageMakerClient> proxyClient) {
AddTagsRequest addTagsRequest = AddTagsRequest.builder()
.resourceArn(resourceArn)
.tags(tags.stream().map(tag -> software.amazon.awssdk.services.sagemaker.model.Tag.builder().key(tag.getKey()).value(tag.getValue()).build()).collect(Collectors.toList()))
.build();
proxyClient.injectCredentialsAndInvokeV2(addTagsRequest, proxyClient.client()::addTags);
}

@SuppressWarnings("resource")
public static void removeTags(String resourceArn,
Set<Tag> tags,
ProxyClient<SageMakerClient> proxyClient) {
Collection<String> tagKeys = tags.stream().map(Tag::getKey).collect(Collectors.toSet());
DeleteTagsRequest deleteTagsRequest = DeleteTagsRequest.builder().resourceArn(resourceArn).tagKeys(tagKeys).build();
proxyClient.injectCredentialsAndInvokeV2(deleteTagsRequest, proxyClient.client()::deleteTags);
}

public static void updateTags(String resourceArn,
Set<Tag> previousTags,
Set<Tag> currentTags,
ProxyClient<SageMakerClient> proxyClient) {
Set<Tag> tagsToAdd = Sets.difference(currentTags, previousTags);
Set<Tag> tagsToRemove = Sets.difference(previousTags, currentTags);
if (!tagsToRemove.isEmpty()) {
removeTags(resourceArn, tagsToRemove, proxyClient);
}
if (!tagsToAdd.isEmpty()) {
addTags(resourceArn, tagsToAdd, proxyClient);
}
}

public static Set<Tag> transformTags(List<software.amazon.awssdk.services.sagemaker.model.Tag> tags) {
Set<Tag> filteredTags = new HashSet<>();
if (tags != null) {
for (software.amazon.awssdk.services.sagemaker.model.Tag e : tags) {
if (!e.key().toLowerCase().startsWith("aws:")) {
filteredTags.add(Tag.builder().key(e.key()).value(e.value()).build());
}
}
}

return filteredTags;
}

public static Set<Tag> transformTags(Map<String, String> tags) {
Set<Tag> filteredTags = new HashSet<>();
if (tags != null) {
for (Map.Entry<String, String> e : tags.entrySet()) {
if (!e.getKey().toLowerCase().startsWith("aws:")) {
filteredTags.add(Tag.builder().key(e.getKey()).value(e.getValue()).build());
}
}
}

return filteredTags;
}

}
Loading