Merge pull request #1751 from Destygo/topic/add_unknown_state_to_elb_unregistered_instances
Add support for unregistered instances in ELB DescribeInstancesHealth.
This commit is contained in:
commit
f3cc02002c
@ -259,12 +259,22 @@ class ELBResponse(BaseResponse):
|
|||||||
|
|
||||||
def describe_instance_health(self):
|
def describe_instance_health(self):
|
||||||
load_balancer_name = self._get_param('LoadBalancerName')
|
load_balancer_name = self._get_param('LoadBalancerName')
|
||||||
instance_ids = [list(param.values())[0] for param in self._get_list_prefix('Instances.member')]
|
provided_instance_ids = [
|
||||||
if len(instance_ids) == 0:
|
list(param.values())[0]
|
||||||
instance_ids = self.elb_backend.get_load_balancer(
|
for param in self._get_list_prefix('Instances.member')
|
||||||
load_balancer_name).instance_ids
|
]
|
||||||
|
registered_instances_id = self.elb_backend.get_load_balancer(
|
||||||
|
load_balancer_name).instance_ids
|
||||||
|
if len(provided_instance_ids) == 0:
|
||||||
|
provided_instance_ids = registered_instances_id
|
||||||
template = self.response_template(DESCRIBE_INSTANCE_HEALTH_TEMPLATE)
|
template = self.response_template(DESCRIBE_INSTANCE_HEALTH_TEMPLATE)
|
||||||
return template.render(instance_ids=instance_ids)
|
instances = []
|
||||||
|
for instance_id in provided_instance_ids:
|
||||||
|
state = "InService" \
|
||||||
|
if instance_id in registered_instances_id\
|
||||||
|
else "Unknown"
|
||||||
|
instances.append({"InstanceId": instance_id, "State": state})
|
||||||
|
return template.render(instances=instances)
|
||||||
|
|
||||||
def add_tags(self):
|
def add_tags(self):
|
||||||
|
|
||||||
@ -689,11 +699,11 @@ SET_LOAD_BALANCER_POLICIES_FOR_BACKEND_SERVER_TEMPLATE = """<SetLoadBalancerPoli
|
|||||||
DESCRIBE_INSTANCE_HEALTH_TEMPLATE = """<DescribeInstanceHealthResponse xmlns="http://elasticloadbalancing.amazonaws.com/doc/2012-06-01/">
|
DESCRIBE_INSTANCE_HEALTH_TEMPLATE = """<DescribeInstanceHealthResponse xmlns="http://elasticloadbalancing.amazonaws.com/doc/2012-06-01/">
|
||||||
<DescribeInstanceHealthResult>
|
<DescribeInstanceHealthResult>
|
||||||
<InstanceStates>
|
<InstanceStates>
|
||||||
{% for instance_id in instance_ids %}
|
{% for instance in instances %}
|
||||||
<member>
|
<member>
|
||||||
<Description>N/A</Description>
|
<Description>N/A</Description>
|
||||||
<InstanceId>{{ instance_id }}</InstanceId>
|
<InstanceId>{{ instance['InstanceId'] }}</InstanceId>
|
||||||
<State>InService</State>
|
<State>{{ instance['State'] }}</State>
|
||||||
<ReasonCode>N/A</ReasonCode>
|
<ReasonCode>N/A</ReasonCode>
|
||||||
</member>
|
</member>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
@ -723,6 +723,40 @@ def test_describe_instance_health():
|
|||||||
instances_health[0].state.should.equal('InService')
|
instances_health[0].state.should.equal('InService')
|
||||||
|
|
||||||
|
|
||||||
|
@mock_ec2
|
||||||
|
@mock_elb
|
||||||
|
def test_describe_instance_health_boto3():
|
||||||
|
elb = boto3.client('elb', region_name="us-east-1")
|
||||||
|
ec2 = boto3.client('ec2', region_name="us-east-1")
|
||||||
|
instances = ec2.run_instances(MinCount=2, MaxCount=2)['Instances']
|
||||||
|
lb_name = "my_load_balancer"
|
||||||
|
elb.create_load_balancer(
|
||||||
|
Listeners=[{
|
||||||
|
'InstancePort': 80,
|
||||||
|
'LoadBalancerPort': 8080,
|
||||||
|
'Protocol': 'HTTP'
|
||||||
|
}],
|
||||||
|
LoadBalancerName=lb_name,
|
||||||
|
)
|
||||||
|
elb.register_instances_with_load_balancer(
|
||||||
|
LoadBalancerName=lb_name,
|
||||||
|
Instances=[{'InstanceId': instances[0]['InstanceId']}]
|
||||||
|
)
|
||||||
|
instances_health = elb.describe_instance_health(
|
||||||
|
LoadBalancerName=lb_name,
|
||||||
|
Instances=[{'InstanceId': instance['InstanceId']} for instance in instances]
|
||||||
|
)
|
||||||
|
instances_health['InstanceStates'].should.have.length_of(2)
|
||||||
|
instances_health['InstanceStates'][0]['InstanceId'].\
|
||||||
|
should.equal(instances[0]['InstanceId'])
|
||||||
|
instances_health['InstanceStates'][0]['State'].\
|
||||||
|
should.equal('InService')
|
||||||
|
instances_health['InstanceStates'][1]['InstanceId'].\
|
||||||
|
should.equal(instances[1]['InstanceId'])
|
||||||
|
instances_health['InstanceStates'][1]['State'].\
|
||||||
|
should.equal('Unknown')
|
||||||
|
|
||||||
|
|
||||||
@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')
|
||||||
|
Loading…
Reference in New Issue
Block a user