Skip to content

Commit 2bf9b48

Browse files
committed
Expand allowed tagging arn member names
Allows for the resource name component, the arn name component, or both name components to be specified. Empty names, non-matching names, or names that have repeat components will fail.
1 parent 595a391 commit 2bf9b48

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed

smithy-aws-traits/src/main/java/software/amazon/smithy/aws/traits/tagging/TaggingShapeUtils.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import software.amazon.smithy.model.shapes.Shape;
1919
import software.amazon.smithy.model.shapes.ShapeId;
2020
import software.amazon.smithy.model.shapes.StructureShape;
21+
import software.amazon.smithy.utils.StringUtils;
2122

2223
/**
2324
* Logic for validating that a shape looks like a tag.
@@ -30,7 +31,7 @@ final class TaggingShapeUtils {
3031
private static final Pattern TAG_PROPERTY_REGEX = Pattern
3132
.compile("^[T|t]ag(s|[L|l]ist)$");
3233
private static final Pattern RESOURCE_ARN_REGEX = Pattern
33-
.compile("^([R|r]esource)?([A|a]rn|ARN)$");
34+
.compile("^([R|r]esource)?([A|a]rn|ARN)?$");
3435
private static final Pattern TAG_KEYS_REGEX = Pattern
3536
.compile("^[T|t]ag[K|k]eys$");
3637

@@ -41,32 +42,29 @@ static String getDesiredTagsPropertyName() {
4142
return "[T|t]ags";
4243
}
4344

44-
// Recommended name is more limited than the accepted regular expression.
45-
static String getDesiredArnName() {
46-
return "[R|r]esourceArn";
47-
}
48-
49-
// Recommended name is more limited than the accepted regular expression.
50-
static String getDesiredTagKeysName() {
51-
return "[T|t]agKeys";
52-
}
53-
5445
// Used to validate tag property name and tag member name.
5546
static boolean isTagDesiredName(String memberName) {
5647
return TAG_PROPERTY_REGEX.matcher(memberName).matches();
5748
}
5849

5950
// Used for checking if member name is good for resource ARN input.
6051
static boolean isArnMemberDesiredName(String memberName) {
61-
return RESOURCE_ARN_REGEX.matcher(memberName).matches();
52+
if (StringUtils.isEmpty(memberName)) {
53+
return false;
54+
}
55+
56+
return memberName
57+
.replaceFirst("^[R|r]esource", "")
58+
.replaceFirst("[A|a]rn|ARN$", "")
59+
.isEmpty();
6260
}
6361

6462
// Used for checking if member name is good for tag keys input for untag operation.
6563
static boolean isTagKeysDesiredName(String memberName) {
6664
return TAG_KEYS_REGEX.matcher(memberName).matches();
6765
}
6866

69-
static boolean hasResourceArnInput(Map<String, MemberShape> inputMembers, Model model) {
67+
private static boolean hasResourceArnInput(Map<String, MemberShape> inputMembers, Model model) {
7068
for (Map.Entry<String, MemberShape> memberEntry : inputMembers.entrySet()) {
7169
if (isArnMemberDesiredName(memberEntry.getKey())
7270
&& model.expectShape(memberEntry.getValue().getTarget()).isStringShape()) {
@@ -89,7 +87,7 @@ static boolean verifyTagsShape(Model model, Shape tagShape) {
8987
return verifyTagListShape(model, tagShape) || verifyTagMapShape(model, tagShape);
9088
}
9189

92-
static boolean verifyTagListShape(Model model, Shape tagShape) {
90+
private static boolean verifyTagListShape(Model model, Shape tagShape) {
9391
if (tagShape.isListShape()) {
9492
ListShape listShape = tagShape.asListShape().get();
9593
Shape listTargetShape = model.expectShape(listShape.getMember().getTarget());
@@ -108,7 +106,7 @@ static boolean verifyTagListShape(Model model, Shape tagShape) {
108106
return false;
109107
}
110108

111-
static boolean verifyTagMapShape(Model model, Shape tagShape) {
109+
private static boolean verifyTagMapShape(Model model, Shape tagShape) {
112110
if (tagShape.isMapShape()) {
113111
MapShape mapShape = tagShape.asMapShape().get();
114112
Shape valueTargetShape = model.expectShape(mapShape.getValue().getTarget());
@@ -189,7 +187,7 @@ static boolean isTagPropertyInInput(
189187
return false;
190188
}
191189

192-
static boolean isTagPropertyInShape(
190+
private static boolean isTagPropertyInShape(
193191
String tagPropertyName,
194192
Shape shape,
195193
PropertyBindingIndex propertyBindingIndex
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package software.amazon.smithy.aws.traits.tagging;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
import java.util.List;
10+
import org.junit.jupiter.params.ParameterizedTest;
11+
import org.junit.jupiter.params.provider.Arguments;
12+
import org.junit.jupiter.params.provider.MethodSource;
13+
import software.amazon.smithy.utils.ListUtils;
14+
15+
public class TaggingShapeUtilsTest {
16+
public static List<Arguments> arnMembers() {
17+
return ListUtils.of(
18+
Arguments.of("", false),
19+
Arguments.of("foo", false),
20+
Arguments.of("fooArn", false),
21+
Arguments.of("resourceFoo", false),
22+
Arguments.of("arnResource", false),
23+
Arguments.of("resourceResourceArn", false),
24+
Arguments.of("resourceArnARN", false),
25+
Arguments.of("resourceArn", true),
26+
Arguments.of("resource", true),
27+
Arguments.of("Resource", true),
28+
Arguments.of("arn", true),
29+
Arguments.of("Arn", true),
30+
Arguments.of("ARN", true),
31+
Arguments.of("ResourceARN", true));
32+
}
33+
34+
@ParameterizedTest
35+
@MethodSource("arnMembers")
36+
public void isArnMemberDesiredName(String arnMember, boolean expected) {
37+
assertEquals(expected, TaggingShapeUtils.isArnMemberDesiredName(arnMember));
38+
}
39+
}

0 commit comments

Comments
 (0)