Skip to content

Commit 7080e66

Browse files
authored
Only run OU and Account list validations if ALT was enabled (#125)
1 parent 245a48e commit 7080e66

File tree

3 files changed

+8
-21
lines changed

3 files changed

+8
-21
lines changed

aws-cloudformation-stackset/src/main/java/software/amazon/cloudformation/stackset/util/AltResourceModelAnalyzer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import software.amazon.cloudformation.stackset.ResourceModel;
1717
import software.amazon.cloudformation.stackset.StackInstances;
1818

19+
import static software.amazon.cloudformation.stackset.util.Comparator.isAccountLevelTargetingEnabled;
20+
1921
@Builder
2022
@Data
2123
public class AltResourceModelAnalyzer {
@@ -40,8 +42,8 @@ public void analyze(final StackInstancesPlaceHolder placeHolder) {
4042
Set<StackInstances> currentStackInstancesGroup = currentModel == null ?
4143
new HashSet<>() : currentModel.getStackInstancesGroup();
4244

43-
Validator.validateServiceMangedInstancesGroup(previousStackInstancesGroup);
44-
Validator.validateServiceMangedInstancesGroup(currentStackInstancesGroup);
45+
Validator.validateServiceMangedInstancesGroup(previousStackInstancesGroup, isAccountLevelTargetingEnabled(previousModel));
46+
Validator.validateServiceMangedInstancesGroup(currentStackInstancesGroup, isAccountLevelTargetingEnabled(currentModel));
4547

4648
Set<String> previousModelRegionOrder = new LinkedHashSet<>();
4749
Set<String> currentModelRegionOrder = new LinkedHashSet<>();

aws-cloudformation-stackset/src/main/java/software/amazon/cloudformation/stackset/util/Validator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ public void validateTemplate(
7676
}
7777
}
7878

79-
public static void validateServiceMangedInstancesGroup (final Collection<StackInstances> stackInstancesGroup) {
79+
public static void validateServiceMangedInstancesGroup (final Collection<StackInstances> stackInstancesGroup, boolean isAlt) {
8080
if (CollectionUtils.isNullOrEmpty(stackInstancesGroup)) return;
8181

8282
stackInstancesGroup.forEach(it ->
83-
validateServiceManagedDeploymentTarget(it.getDeploymentTargets())
83+
validateServiceManagedDeploymentTarget(it.getDeploymentTargets(), isAlt)
8484
);
8585
}
8686

87-
public static void validateServiceManagedDeploymentTarget (DeploymentTargets targets) {
87+
public static void validateServiceManagedDeploymentTarget (DeploymentTargets targets, boolean isAlt) {
8888

8989
if (targets == null) {
9090
throw new CfnInvalidRequestException("DeploymentTargets should be specified");
@@ -106,7 +106,7 @@ public static void validateServiceManagedDeploymentTarget (DeploymentTargets tar
106106
final Set<String> accounts = CollectionUtils.isNullOrEmpty(targets.getAccounts()) ?
107107
new HashSet<>() : targets.getAccounts();
108108

109-
if (Objects.equals(filter, NONE) && !CollectionUtils.isNullOrEmpty(accounts)) {
109+
if (isAlt && (Objects.equals(filter, NONE) && !CollectionUtils.isNullOrEmpty(accounts))) {
110110
throw new CfnInvalidRequestException("AccountFilterType should be specified when both OrganizationalUnitIds and Accounts are provided");
111111
}
112112

aws-cloudformation-stackset/src/test/java/software/amazon/cloudformation/stackset/util/AltResourceModelAnalyzerTest.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,6 @@ public void test_Invalid_Alt_Model() {
5959
ResourceModel modelNoneFilter = generateModel(new HashSet<>(Arrays.asList(
6060
generateInstances(OU_1, account_1, NONE))));
6161

62-
ResourceModel modelNullFilter = generateModel(new HashSet<>(Arrays.asList(
63-
StackInstances.builder().deploymentTargets(
64-
DeploymentTargets.builder()
65-
.organizationalUnitIds(new HashSet<>(Arrays.asList(OU_1)))
66-
.accounts(new HashSet<>(Arrays.asList(account_1)))
67-
.build())
68-
.build()
69-
)));
70-
7162
ResourceModel modelPartiallyInvalid = generateModel(new HashSet<>(Arrays.asList(
7263
generateInstances(OU_1, account_1, INTER),
7364
generateInstances(OU_1, account_1, NONE))));
@@ -97,12 +88,6 @@ public void test_Invalid_Alt_Model() {
9788
);
9889
assertThat(ex.getMessage()).contains("AccountFilterType should be specified when both OrganizationalUnitIds and Accounts are provided");
9990

100-
ex = assertThrows(
101-
CfnInvalidRequestException.class,
102-
() -> AltResourceModelAnalyzer.builder().currentModel(modelNullFilter).build().analyze(placeHolder)
103-
);
104-
assertThat(ex.getMessage()).contains("AccountFilterType should be specified when both OrganizationalUnitIds and Accounts are provided");
105-
10691
assertThrows(
10792
CfnInvalidRequestException.class,
10893
() -> AltResourceModelAnalyzer.builder().currentModel(modelPartiallyInvalid).build().analyze(placeHolder)

0 commit comments

Comments
 (0)