Skip to content

Only run OU and Account list validations if ALT was enabled #125

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

Merged
merged 1 commit into from
Feb 17, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import software.amazon.cloudformation.stackset.ResourceModel;
import software.amazon.cloudformation.stackset.StackInstances;

import static software.amazon.cloudformation.stackset.util.Comparator.isAccountLevelTargetingEnabled;

@Builder
@Data
public class AltResourceModelAnalyzer {
Expand All @@ -40,8 +42,8 @@ public void analyze(final StackInstancesPlaceHolder placeHolder) {
Set<StackInstances> currentStackInstancesGroup = currentModel == null ?
new HashSet<>() : currentModel.getStackInstancesGroup();

Validator.validateServiceMangedInstancesGroup(previousStackInstancesGroup);
Validator.validateServiceMangedInstancesGroup(currentStackInstancesGroup);
Validator.validateServiceMangedInstancesGroup(previousStackInstancesGroup, isAccountLevelTargetingEnabled(previousModel));
Validator.validateServiceMangedInstancesGroup(currentStackInstancesGroup, isAccountLevelTargetingEnabled(currentModel));

Set<String> previousModelRegionOrder = new LinkedHashSet<>();
Set<String> currentModelRegionOrder = new LinkedHashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ public void validateTemplate(
}
}

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

stackInstancesGroup.forEach(it ->
validateServiceManagedDeploymentTarget(it.getDeploymentTargets())
validateServiceManagedDeploymentTarget(it.getDeploymentTargets(), isAlt)
);
}

public static void validateServiceManagedDeploymentTarget (DeploymentTargets targets) {
public static void validateServiceManagedDeploymentTarget (DeploymentTargets targets, boolean isAlt) {

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@ public void test_Invalid_Alt_Model() {
ResourceModel modelNoneFilter = generateModel(new HashSet<>(Arrays.asList(
generateInstances(OU_1, account_1, NONE))));

ResourceModel modelNullFilter = generateModel(new HashSet<>(Arrays.asList(
StackInstances.builder().deploymentTargets(
DeploymentTargets.builder()
.organizationalUnitIds(new HashSet<>(Arrays.asList(OU_1)))
.accounts(new HashSet<>(Arrays.asList(account_1)))
.build())
.build()
)));

ResourceModel modelPartiallyInvalid = generateModel(new HashSet<>(Arrays.asList(
generateInstances(OU_1, account_1, INTER),
generateInstances(OU_1, account_1, NONE))));
Expand Down Expand Up @@ -97,12 +88,6 @@ public void test_Invalid_Alt_Model() {
);
assertThat(ex.getMessage()).contains("AccountFilterType should be specified when both OrganizationalUnitIds and Accounts are provided");

ex = assertThrows(
CfnInvalidRequestException.class,
() -> AltResourceModelAnalyzer.builder().currentModel(modelNullFilter).build().analyze(placeHolder)
);
assertThat(ex.getMessage()).contains("AccountFilterType should be specified when both OrganizationalUnitIds and Accounts are provided");

assertThrows(
CfnInvalidRequestException.class,
() -> AltResourceModelAnalyzer.builder().currentModel(modelPartiallyInvalid).build().analyze(placeHolder)
Expand Down