Cloudformation: delete_stack_instances() can remove multiple regions (#6299)
This commit is contained in:
parent
fb304b87d6
commit
87d84c12e0
@ -346,10 +346,14 @@ class FakeStackInstances(BaseModel):
|
|||||||
instance.parameters = parameters or []
|
instance.parameters = parameters or []
|
||||||
|
|
||||||
def delete(self, accounts: List[str], regions: List[str]) -> None:
|
def delete(self, accounts: List[str], regions: List[str]) -> None:
|
||||||
for instance in self.stack_instances:
|
to_delete = [
|
||||||
if instance.region_name in regions and instance.account_id in accounts:
|
i
|
||||||
instance.delete()
|
for i in self.stack_instances
|
||||||
self.stack_instances.remove(instance)
|
if i.region_name in regions and i.account_id in accounts
|
||||||
|
]
|
||||||
|
for instance in to_delete:
|
||||||
|
instance.delete()
|
||||||
|
self.stack_instances.remove(instance)
|
||||||
|
|
||||||
def get_instance(self, account: str, region: str) -> FakeStackInstance: # type: ignore[return]
|
def get_instance(self, account: str, region: str) -> FakeStackInstance: # type: ignore[return]
|
||||||
for i, instance in enumerate(self.stack_instances):
|
for i, instance in enumerate(self.stack_instances):
|
||||||
|
@ -564,29 +564,42 @@ def test_update_stack_instances():
|
|||||||
@mock_cloudformation
|
@mock_cloudformation
|
||||||
def test_delete_stack_instances():
|
def test_delete_stack_instances():
|
||||||
cf_conn = boto3.client("cloudformation", region_name="us-east-1")
|
cf_conn = boto3.client("cloudformation", region_name="us-east-1")
|
||||||
cf_conn.create_stack_set(
|
tss = "teststackset"
|
||||||
StackSetName="teststackset", TemplateBody=dummy_template_json
|
cf_conn.create_stack_set(StackSetName=tss, TemplateBody=dummy_template_json)
|
||||||
)
|
|
||||||
cf_conn.create_stack_instances(
|
cf_conn.create_stack_instances(
|
||||||
StackSetName="teststackset",
|
StackSetName=tss,
|
||||||
Accounts=[ACCOUNT_ID],
|
Accounts=[ACCOUNT_ID],
|
||||||
Regions=["us-east-1", "us-west-2"],
|
Regions=["us-east-1", "us-west-2", "eu-north-1"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Delete just one
|
||||||
cf_conn.delete_stack_instances(
|
cf_conn.delete_stack_instances(
|
||||||
StackSetName="teststackset",
|
StackSetName=tss,
|
||||||
Accounts=[ACCOUNT_ID],
|
Accounts=[ACCOUNT_ID],
|
||||||
# Also delete unknown region for good measure - that should be a no-op
|
# Also delete unknown region for good measure - that should be a no-op
|
||||||
Regions=["us-east-1", "us-east-2"],
|
Regions=["us-east-1", "us-east-2"],
|
||||||
RetainStacks=False,
|
RetainStacks=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
cf_conn.list_stack_instances(StackSetName="teststackset")[
|
# Some should remain
|
||||||
"Summaries"
|
remaining_stacks = cf_conn.list_stack_instances(StackSetName=tss)["Summaries"]
|
||||||
].should.have.length_of(1)
|
assert len(remaining_stacks) == 2
|
||||||
cf_conn.list_stack_instances(StackSetName="teststackset")["Summaries"][0][
|
assert [stack["Region"] for stack in remaining_stacks] == [
|
||||||
"Region"
|
"us-west-2",
|
||||||
].should.equal("us-west-2")
|
"eu-north-1",
|
||||||
|
]
|
||||||
|
|
||||||
|
# Delete all
|
||||||
|
cf_conn.delete_stack_instances(
|
||||||
|
StackSetName=tss,
|
||||||
|
Accounts=[ACCOUNT_ID],
|
||||||
|
# Also delete unknown region for good measure - that should be a no-op
|
||||||
|
Regions=["us-west-2", "eu-north-1"],
|
||||||
|
RetainStacks=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
remaining_stacks = cf_conn.list_stack_instances(StackSetName=tss)["Summaries"]
|
||||||
|
assert len(remaining_stacks) == 0
|
||||||
|
|
||||||
|
|
||||||
@mock_cloudformation
|
@mock_cloudformation
|
||||||
|
Loading…
Reference in New Issue
Block a user