ELB: describe_instance_health() should check LB existence (#5706)

This commit is contained in:
Bert Blommers 2022-11-23 18:10:18 -01:00 committed by GitHub
parent f7c504b0eb
commit e5a1115834
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 3 deletions

View File

@ -23,10 +23,17 @@ class CertificateNotFoundException(ELBClientError):
class LoadBalancerNotFoundError(ELBClientError): class LoadBalancerNotFoundError(ELBClientError):
def __init__(self, cidr): def __init__(self, name):
super().__init__( super().__init__(
"LoadBalancerNotFound", "LoadBalancerNotFound",
f"The specified load balancer does not exist: {cidr}", f"The specified load balancer does not exist: {name}",
)
class NoActiveLoadBalancerFoundError(ELBClientError):
def __init__(self, name):
super().__init__(
"LoadBalancerNotFound", f"There is no ACTIVE Load Balancer named '{name}'"
) )

View File

@ -14,6 +14,7 @@ from .exceptions import (
EmptyListenersError, EmptyListenersError,
InvalidSecurityGroupError, InvalidSecurityGroupError,
LoadBalancerNotFoundError, LoadBalancerNotFoundError,
NoActiveLoadBalancerFoundError,
PolicyNotFoundError, PolicyNotFoundError,
TooManyTagsError, TooManyTagsError,
CertificateNotFoundException, CertificateNotFoundException,
@ -372,8 +373,11 @@ class ELBBackend(BaseBackend):
return policies return policies
def describe_instance_health(self, lb_name, instances): def describe_instance_health(self, lb_name, instances):
elb = self.get_load_balancer(lb_name)
if elb is None:
raise NoActiveLoadBalancerFoundError(name=lb_name)
provided_ids = [i["InstanceId"] for i in instances] provided_ids = [i["InstanceId"] for i in instances]
registered_ids = self.get_load_balancer(lb_name).instance_ids registered_ids = elb.instance_ids
ec2_backend = ec2_backends[self.account_id][self.region_name] ec2_backend = ec2_backends[self.account_id][self.region_name]
if len(provided_ids) == 0: if len(provided_ids) == 0:
provided_ids = registered_ids provided_ids = registered_ids

View File

@ -933,6 +933,17 @@ def test_describe_instance_health__with_instance_ids():
instances_health[2]["State"].should.equal("OutOfService") instances_health[2]["State"].should.equal("OutOfService")
@mock_elb
def test_describe_instance_health_of_unknown_lb():
elb = boto3.client("elb", region_name="us-east-1")
with pytest.raises(ClientError) as exc:
elb.describe_instance_health(LoadBalancerName="what")
err = exc.value.response["Error"]
err["Code"].should.equal("LoadBalancerNotFound")
err["Message"].should.equal("There is no ACTIVE Load Balancer named 'what'")
@mock_elb @mock_elb
def test_add_remove_tags(): def test_add_remove_tags():
client = boto3.client("elb", region_name="us-east-1") client = boto3.client("elb", region_name="us-east-1")