Skip to content

Commit

Permalink
ELBv2: remove_tags() now throws a ResourceNotFound Exception (#8225)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bharat23 authored Oct 15, 2024
1 parent 1a2a733 commit 947004f
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions moto/elbv2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1990,6 +1990,7 @@ def add_tags(self, resource_arns: List[str], tags: List[Dict[str, str]]) -> None

def remove_tags(self, resource_arns: List[str], tag_keys: List[str]) -> None:
for arn in resource_arns:
self._get_resource_by_arn(arn)
self.tagging_service.untag_resource_using_names(arn, tag_keys)

def describe_tags(self, resource_arns: List[str]) -> Dict[str, Dict[str, str]]:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_elbv2/test_elbv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ def test_add_remove_tags():

conn.remove_tags(ResourceArns=[lb["LoadBalancerArn"]], TagKeys=["a"])

with pytest.raises(ClientError) as exc:
# add a random string in the ARN to make the resource non-existent
BAD_LB_ARN = lb["LoadBalancerArn"] + "randomstring"
conn.remove_tags(ResourceArns=[BAD_LB_ARN], TagKeys=["a"])
err = exc.value.response["Error"]
assert err["Code"] == "LoadBalancerNotFound"

tags = {
d["Key"]: d["Value"]
for d in conn.describe_tags(ResourceArns=[lb["LoadBalancerArn"]])[
Expand Down
35 changes: 35 additions & 0 deletions tests/test_elbv2/test_elbv2_listener_rule_tags.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pytest
from botocore.exceptions import ClientError

from moto import mock_aws

from .test_elbv2 import create_load_balancer
Expand Down Expand Up @@ -83,3 +86,35 @@ def test_listener_rule_add_remove_tags():
{"Key": "b", "Value": "b"},
{"Key": "c", "Value": "b"},
]


@mock_aws
def test_remove_tags_to_invalid_listener_rule():
response, _, _, _, _, elbv2 = create_load_balancer()
load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn")

listener_arn = elbv2.create_listener(
LoadBalancerArn=load_balancer_arn,
Protocol="HTTP",
Port=80,
DefaultActions=[],
)["Listeners"][0]["ListenerArn"]
rule_arn = elbv2.create_rule(
ListenerArn=listener_arn,
Priority=100,
Conditions=[create_condition],
Actions=[default_action],
Tags=[{"Key": "k1", "Value": "v1"}],
)["Rules"][0]["RuleArn"]

# add a random string in the ARN to make the resource non-existent
BAD_ARN = rule_arn + "randostring"

# test for exceptions on remove tags
with pytest.raises(ClientError) as err:
elbv2.remove_tags(ResourceArns=[BAD_ARN], TagKeys=["a"])

assert err.value.response["Error"]["Code"] == "RuleNotFound"
assert (
err.value.response["Error"]["Message"] == "The specified rule does not exist."
)
29 changes: 29 additions & 0 deletions tests/test_elbv2/test_elbv2_listener_tags.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pytest
from botocore.exceptions import ClientError

from moto import mock_aws

from .test_elbv2 import create_load_balancer
Expand Down Expand Up @@ -73,3 +76,29 @@ def test_listener_add_remove_tags():
{"Key": "b", "Value": "b"},
{"Key": "c", "Value": "b"},
]


@mock_aws
def test_remove_tags_to_invalid_listener():
response, _, _, _, _, elbv2 = create_load_balancer()
load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn")

listener_arn = elbv2.create_listener(
LoadBalancerArn=load_balancer_arn,
Protocol="HTTP",
Port=80,
DefaultActions=[],
)["Listeners"][0]["ListenerArn"]

# add a random string in the ARN to make the resource non-existent
BAD_ARN = listener_arn + "randomstring"

# test for exceptions on remove tags
with pytest.raises(ClientError) as err:
elbv2.remove_tags(ResourceArns=[BAD_ARN], TagKeys=["a"])

assert err.value.response["Error"]["Code"] == "ListenerNotFound"
assert (
err.value.response["Error"]["Message"]
== "The specified listener does not exist."
)
35 changes: 35 additions & 0 deletions tests/test_elbv2/test_elbv2_target_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,41 @@ def test_create_target_group_with_tags():
assert tags == [{"Key": "key2", "Value": "val2"}]


@mock_aws
def test_remove_tags_to_invalid_target_group():
response, vpc, _, _, _, elbv2 = create_load_balancer()

response = elbv2.create_target_group(
Name="a-target",
Protocol="HTTP",
Port=8080,
VpcId=vpc.id,
HealthCheckProtocol="HTTP",
HealthCheckPort="8080",
HealthCheckPath="/",
HealthCheckIntervalSeconds=5,
HealthCheckTimeoutSeconds=3,
HealthyThresholdCount=5,
UnhealthyThresholdCount=2,
Matcher={"HttpCode": "200"},
Tags=[{"Key": "key1", "Value": "val1"}],
)
target_group = response["TargetGroups"][0]
target_group_arn = target_group["TargetGroupArn"]

# add a random string in the ARN to make the resource non-existent
BAD_ARN = target_group_arn + "randomstring"

# test for exceptions on remove tags
with pytest.raises(ClientError) as err:
elbv2.remove_tags(ResourceArns=[BAD_ARN], TagKeys=["a"])

assert err.value.response["Error"]["Code"] == "TargetGroupNotFound"
assert (
err.value.response["Error"]["Message"] == "One or more target groups not found"
)


@mock_aws
def test_create_target_group_and_listeners():
response, vpc, _, _, _, conn = create_load_balancer()
Expand Down

0 comments on commit 947004f

Please sign in to comment.