Update ELBs to use newer querystring querying methods.

This commit is contained in:
Steve Pulec 2015-07-18 09:08:27 -04:00
parent 740377b9da
commit 9f02da4c56
2 changed files with 68 additions and 135 deletions

View File

@ -8,12 +8,7 @@ from boto.ec2.elb.attributes import (
AccessLogAttribute, AccessLogAttribute,
CrossZoneLoadBalancingAttribute, CrossZoneLoadBalancingAttribute,
) )
from boto.ec2.elb.policies import ( from boto.ec2.elb.policies import Policies
Policies,
AppCookieStickinessPolicy,
LBCookieStickinessPolicy,
OtherPolicy,
)
from moto.core import BaseBackend from moto.core import BaseBackend
@ -43,7 +38,7 @@ class FakeBackend(object):
def __init__(self, instance_port): def __init__(self, instance_port):
self.instance_port = instance_port self.instance_port = instance_port
self.policy_names = [] self.policy_names = []
def __repr__(self): def __repr__(self):
return "FakeBackend(inp: %s, policies: %s)" % (self.instance_port, self.policy_names) return "FakeBackend(inp: %s, policies: %s)" % (self.instance_port, self.policy_names)
@ -62,19 +57,19 @@ class FakeLoadBalancer(object):
self.policies.app_cookie_stickiness_policies = [] self.policies.app_cookie_stickiness_policies = []
self.policies.lb_cookie_stickiness_policies = [] self.policies.lb_cookie_stickiness_policies = []
for protocol, lb_port, instance_port, ssl_certificate_id in ports: for port in ports:
listener = FakeListener( listener = FakeListener(
protocol=protocol, protocol=port['protocol'],
load_balancer_port=lb_port, load_balancer_port=port['load_balancer_port'],
instance_port=instance_port, instance_port=port['instance_port'],
ssl_certificate_id=ssl_certificate_id ssl_certificate_id=port.get('sslcertificate_id'),
) )
self.listeners.append(listener) self.listeners.append(listener)
# it is unclear per the AWS documentation as to when or how backend # it is unclear per the AWS documentation as to when or how backend
# information gets set, so let's guess and set it here *shrug* # information gets set, so let's guess and set it here *shrug*
backend = FakeBackend( backend = FakeBackend(
instance_port = instance_port, instance_port=port['instance_port'],
) )
self.backends.append(backend) self.backends.append(backend)
@ -134,6 +129,7 @@ class FakeLoadBalancer(object):
return attributes return attributes
class ELBBackend(BaseBackend): class ELBBackend(BaseBackend):
def __init__(self): def __init__(self):
@ -147,7 +143,11 @@ class ELBBackend(BaseBackend):
def create_load_balancer_listeners(self, name, ports): def create_load_balancer_listeners(self, name, ports):
balancer = self.load_balancers.get(name, None) balancer = self.load_balancers.get(name, None)
if balancer: if balancer:
for protocol, lb_port, instance_port, ssl_certificate_id in ports: for port in ports:
protocol = port['protocol']
instance_port = port['instance_port']
lb_port = port['load_balancer_port']
ssl_certificate_id = port.get('sslcertificate_id')
for listener in balancer.listeners: for listener in balancer.listeners:
if lb_port == listener.load_balancer_port: if lb_port == listener.load_balancer_port:
break break

View File

@ -6,9 +6,7 @@ from boto.ec2.elb.attributes import (
CrossZoneLoadBalancingAttribute, CrossZoneLoadBalancingAttribute,
) )
from boto.ec2.elb.policies import ( from boto.ec2.elb.policies import (
Policies,
AppCookieStickinessPolicy, AppCookieStickinessPolicy,
LBCookieStickinessPolicy,
OtherPolicy, OtherPolicy,
) )
@ -26,20 +24,9 @@ class ELBResponse(BaseResponse):
""" """
u'Scheme': [u'internet-facing'], u'Scheme': [u'internet-facing'],
""" """
load_balancer_name = self.querystring.get('LoadBalancerName')[0] load_balancer_name = self._get_param('LoadBalancerName')
availability_zones = [value[0] for key, value in self.querystring.items() if "AvailabilityZones.member" in key] availability_zones = self._get_multi_param("AvailabilityZones.member")
ports = [] ports = self._get_list_prefix("Listeners.member")
port_index = 1
while True:
try:
protocol = self.querystring['Listeners.member.{0}.Protocol'.format(port_index)][0]
except KeyError:
break
lb_port = self.querystring['Listeners.member.{0}.LoadBalancerPort'.format(port_index)][0]
instance_port = self.querystring['Listeners.member.{0}.InstancePort'.format(port_index)][0]
ssl_certificate_id = self.querystring.get('Listeners.member.{0}.SSLCertificateId'.format(port_index), [None])[0]
ports.append([protocol, lb_port, instance_port, ssl_certificate_id])
port_index += 1
self.elb_backend.create_load_balancer( self.elb_backend.create_load_balancer(
name=load_balancer_name, name=load_balancer_name,
@ -50,19 +37,8 @@ class ELBResponse(BaseResponse):
return template.render() return template.render()
def create_load_balancer_listeners(self): def create_load_balancer_listeners(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0] load_balancer_name = self._get_param('LoadBalancerName')
ports = [] ports = self._get_list_prefix("Listeners.member")
port_index = 1
while True:
try:
protocol = self.querystring['Listeners.member.{0}.Protocol'.format(port_index)][0]
except KeyError:
break
lb_port = self.querystring['Listeners.member.{0}.LoadBalancerPort'.format(port_index)][0]
instance_port = self.querystring['Listeners.member.{0}.InstancePort'.format(port_index)][0]
ssl_certificate_id = self.querystring.get('Listeners.member.{0}.SSLCertificateId'.format(port_index)[0], None)
ports.append([protocol, lb_port, instance_port, ssl_certificate_id])
port_index += 1
self.elb_backend.create_load_balancer_listeners(name=load_balancer_name, ports=ports) self.elb_backend.create_load_balancer_listeners(name=load_balancer_name, ports=ports)
@ -70,55 +46,47 @@ class ELBResponse(BaseResponse):
return template.render() return template.render()
def describe_load_balancers(self): def describe_load_balancers(self):
names = [value[0] for key, value in self.querystring.items() if "LoadBalancerNames.member" in key] names = self._get_multi_param("LoadBalancerNames.member")
load_balancers = self.elb_backend.describe_load_balancers(names) load_balancers = self.elb_backend.describe_load_balancers(names)
template = self.response_template(DESCRIBE_LOAD_BALANCERS_TEMPLATE) template = self.response_template(DESCRIBE_LOAD_BALANCERS_TEMPLATE)
return template.render(load_balancers=load_balancers) return template.render(load_balancers=load_balancers)
def delete_load_balancer_listeners(self): def delete_load_balancer_listeners(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0] load_balancer_name = self._get_param('LoadBalancerName')
ports = [] ports = self._get_multi_param("LoadBalancerPorts.member")
port_index = 1 ports = [int(port) for port in ports]
while True:
try:
port = self.querystring['LoadBalancerPorts.member.{0}'.format(port_index)][0]
except KeyError:
break
port_index += 1
ports.append(int(port))
self.elb_backend.delete_load_balancer_listeners(load_balancer_name, ports) self.elb_backend.delete_load_balancer_listeners(load_balancer_name, ports)
template = self.response_template(DELETE_LOAD_BALANCER_LISTENERS) template = self.response_template(DELETE_LOAD_BALANCER_LISTENERS)
return template.render() return template.render()
def delete_load_balancer(self): def delete_load_balancer(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0] load_balancer_name = self._get_param('LoadBalancerName')
self.elb_backend.delete_load_balancer(load_balancer_name) self.elb_backend.delete_load_balancer(load_balancer_name)
template = self.response_template(DELETE_LOAD_BALANCER_TEMPLATE) template = self.response_template(DELETE_LOAD_BALANCER_TEMPLATE)
return template.render() return template.render()
def configure_health_check(self): def configure_health_check(self):
check = self.elb_backend.configure_health_check( check = self.elb_backend.configure_health_check(
load_balancer_name=self.querystring.get('LoadBalancerName')[0], load_balancer_name=self._get_param('LoadBalancerName'),
timeout=self.querystring.get('HealthCheck.Timeout')[0], timeout=self._get_param('HealthCheck.Timeout'),
healthy_threshold=self.querystring.get('HealthCheck.HealthyThreshold')[0], healthy_threshold=self._get_param('HealthCheck.HealthyThreshold'),
unhealthy_threshold=self.querystring.get('HealthCheck.UnhealthyThreshold')[0], unhealthy_threshold=self._get_param('HealthCheck.UnhealthyThreshold'),
interval=self.querystring.get('HealthCheck.Interval')[0], interval=self._get_param('HealthCheck.Interval'),
target=self.querystring.get('HealthCheck.Target')[0], target=self._get_param('HealthCheck.Target'),
) )
template = self.response_template(CONFIGURE_HEALTH_CHECK_TEMPLATE) template = self.response_template(CONFIGURE_HEALTH_CHECK_TEMPLATE)
return template.render(check=check) return template.render(check=check)
def register_instances_with_load_balancer(self): def register_instances_with_load_balancer(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0] load_balancer_name = self._get_param('LoadBalancerName')
instance_ids = [value[0] for key, value in self.querystring.items() if "Instances.member" in key] instance_ids = [value[0] for key, value in self.querystring.items() if "Instances.member" in key]
template = self.response_template(REGISTER_INSTANCES_TEMPLATE) template = self.response_template(REGISTER_INSTANCES_TEMPLATE)
load_balancer = self.elb_backend.register_instances(load_balancer_name, instance_ids) load_balancer = self.elb_backend.register_instances(load_balancer_name, instance_ids)
return template.render(load_balancer=load_balancer) return template.render(load_balancer=load_balancer)
def set_load_balancer_listener_sslcertificate(self): def set_load_balancer_listener_sslcertificate(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0] load_balancer_name = self._get_param('LoadBalancerName')
ssl_certificate_id = self.querystring['SSLCertificateId'][0] ssl_certificate_id = self.querystring['SSLCertificateId'][0]
lb_port = self.querystring['LoadBalancerPort'][0] lb_port = self.querystring['LoadBalancerPort'][0]
@ -128,67 +96,58 @@ class ELBResponse(BaseResponse):
return template.render() return template.render()
def deregister_instances_from_load_balancer(self): def deregister_instances_from_load_balancer(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0] load_balancer_name = self._get_param('LoadBalancerName')
instance_ids = [value[0] for key, value in self.querystring.items() if "Instances.member" in key] instance_ids = [value[0] for key, value in self.querystring.items() if "Instances.member" in key]
template = self.response_template(DEREGISTER_INSTANCES_TEMPLATE) template = self.response_template(DEREGISTER_INSTANCES_TEMPLATE)
load_balancer = self.elb_backend.deregister_instances(load_balancer_name, instance_ids) load_balancer = self.elb_backend.deregister_instances(load_balancer_name, instance_ids)
return template.render(load_balancer=load_balancer) return template.render(load_balancer=load_balancer)
def describe_load_balancer_attributes(self): def describe_load_balancer_attributes(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0] load_balancer_name = self._get_param('LoadBalancerName')
load_balancer = self.elb_backend.describe_load_balancers(load_balancer_name)[0] load_balancer = self.elb_backend.get_load_balancer(load_balancer_name)
template = self.response_template(DESCRIBE_ATTRIBUTES_TEMPLATE) template = self.response_template(DESCRIBE_ATTRIBUTES_TEMPLATE)
return template.render(attributes=load_balancer.attributes) return template.render(attributes=load_balancer.attributes)
def modify_load_balancer_attributes(self): def modify_load_balancer_attributes(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0] load_balancer_name = self._get_param('LoadBalancerName')
load_balancer = self.elb_backend.describe_load_balancers(load_balancer_name)[0] load_balancer = self.elb_backend.get_load_balancer(load_balancer_name)
def parse_attribute(attribute_name): cross_zone = self._get_dict_param("LoadBalancerAttributes.CrossZoneLoadBalancing.")
"""
Transform self.querystring parameters matching `LoadBalancerAttributes.attribute_name.attribute_key`
into a dictionary of (attribute_name, attribute_key)` pairs.
"""
attribute_prefix = "LoadBalancerAttributes." + attribute_name
return dict((key.lstrip(attribute_prefix), value[0]) for key, value in self.querystring.items() if key.startswith(attribute_prefix))
cross_zone = parse_attribute("CrossZoneLoadBalancing")
if cross_zone: if cross_zone:
attribute = CrossZoneLoadBalancingAttribute() attribute = CrossZoneLoadBalancingAttribute()
attribute.enabled = cross_zone["Enabled"] == "true" attribute.enabled = cross_zone["enabled"] == "true"
self.elb_backend.set_cross_zone_load_balancing_attribute(load_balancer_name, attribute) self.elb_backend.set_cross_zone_load_balancing_attribute(load_balancer_name, attribute)
access_log = parse_attribute("AccessLog") access_log = self._get_dict_param("LoadBalancerAttributes.AccessLog.")
if access_log: if access_log:
attribute = AccessLogAttribute() attribute = AccessLogAttribute()
attribute.enabled = access_log["Enabled"] == "true" attribute.enabled = access_log["enabled"] == "true"
attribute.s3_bucket_name = access_log["S3BucketName"] attribute.s3_bucket_name = access_log['s3_bucket_name']
attribute.s3_bucket_prefix = access_log["S3BucketPrefix"] attribute.s3_bucket_prefix = access_log['s3_bucket_prefix']
attribute.emit_interval = access_log["EmitInterval"] attribute.emit_interval = access_log["emit_interval"]
self.elb_backend.set_access_log_attribute(load_balancer_name, attribute) self.elb_backend.set_access_log_attribute(load_balancer_name, attribute)
connection_draining = parse_attribute("ConnectionDraining") connection_draining = self._get_dict_param("LoadBalancerAttributes.ConnectionDraining.")
if connection_draining: if connection_draining:
attribute = ConnectionDrainingAttribute() attribute = ConnectionDrainingAttribute()
attribute.enabled = connection_draining["Enabled"] == "true" attribute.enabled = connection_draining["enabled"] == "true"
attribute.timeout = connection_draining["Timeout"] attribute.timeout = connection_draining["timeout"]
self.elb_backend.set_connection_draining_attribute(load_balancer_name, attribute) self.elb_backend.set_connection_draining_attribute(load_balancer_name, attribute)
connection_settings = parse_attribute("ConnectionSettings") connection_settings = self._get_dict_param("LoadBalancerAttributes.ConnectionSettings.")
if connection_settings: if connection_settings:
attribute = ConnectionSettingAttribute() attribute = ConnectionSettingAttribute()
attribute.idle_timeout = connection_settings["IdleTimeout"] attribute.idle_timeout = connection_settings["idle_timeout"]
self.elb_backend.set_connection_settings_attribute(load_balancer_name, attribute) self.elb_backend.set_connection_settings_attribute(load_balancer_name, attribute)
template = self.response_template(MODIFY_ATTRIBUTES_TEMPLATE) template = self.response_template(MODIFY_ATTRIBUTES_TEMPLATE)
return template.render(attributes=load_balancer.attributes) return template.render(attributes=load_balancer.attributes)
def create_load_balancer_policy(self): def create_load_balancer_policy(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0] load_balancer_name = self._get_param('LoadBalancerName')
load_balancer = self.elb_backend.describe_load_balancers(load_balancer_name)[0]
other_policy = OtherPolicy() other_policy = OtherPolicy()
policy_name = [value[0] for key, value in self.querystring.items() if "PolicyName" in key][0] policy_name = self._get_param("PolicyName")
other_policy.policy_name = policy_name other_policy.policy_name = policy_name
self.elb_backend.create_lb_other_policy(load_balancer_name, other_policy) self.elb_backend.create_lb_other_policy(load_balancer_name, other_policy)
@ -197,14 +156,11 @@ class ELBResponse(BaseResponse):
return template.render() return template.render()
def create_app_cookie_stickiness_policy(self): def create_app_cookie_stickiness_policy(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0] load_balancer_name = self._get_param('LoadBalancerName')
load_balancer = self.elb_backend.describe_load_balancers(load_balancer_name)[0]
policy = AppCookieStickinessPolicy() policy = AppCookieStickinessPolicy()
policy_name = [value[0] for key, value in self.querystring.items() if "PolicyName" in key][0] policy.policy_name = self._get_param("PolicyName")
policy.policy_name = policy_name policy.cookie_name = self._get_param("CookieName")
cookie_name = [value[0] for key, value in self.querystring.items() if "CookieName" in key][0]
policy.cookie_name = cookie_name
self.elb_backend.create_app_cookie_stickiness_policy(load_balancer_name, policy) self.elb_backend.create_app_cookie_stickiness_policy(load_balancer_name, policy)
@ -212,15 +168,13 @@ class ELBResponse(BaseResponse):
return template.render() return template.render()
def create_lbcookie_stickiness_policy(self): def create_lbcookie_stickiness_policy(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0] load_balancer_name = self._get_param('LoadBalancerName')
load_balancer = self.elb_backend.describe_load_balancers(load_balancer_name)[0]
policy = AppCookieStickinessPolicy() policy = AppCookieStickinessPolicy()
policy_name = [value[0] for key, value in self.querystring.items() if "PolicyName" in key][0] policy.policy_name = self._get_param("PolicyName")
policy.policy_name = policy_name cookie_expirations = self._get_param("CookieExpirationPeriod")
cookie_expirations = [value[0] for key, value in self.querystring.items() if "CookieExpirationPeriod" in key]
if cookie_expirations: if cookie_expirations:
policy.cookie_expiration_period = int(cookie_expirations[0]) policy.cookie_expiration_period = int(cookie_expirations)
else: else:
policy.cookie_expiration_period = None policy.cookie_expiration_period = None
@ -230,59 +184,38 @@ class ELBResponse(BaseResponse):
return template.render() return template.render()
def set_load_balancer_policies_of_listener(self): def set_load_balancer_policies_of_listener(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0] load_balancer_name = self._get_param('LoadBalancerName')
load_balancer = self.elb_backend.describe_load_balancers(load_balancer_name)[0] load_balancer = self.elb_backend.get_load_balancer(load_balancer_name)
load_balancer_port = int(self.querystring.get('LoadBalancerPort')[0]) load_balancer_port = int(self._get_param('LoadBalancerPort'))
mb_listener = [l for l in load_balancer.listeners if int(l.load_balancer_port) == load_balancer_port] mb_listener = [l for l in load_balancer.listeners if int(l.load_balancer_port) == load_balancer_port]
if mb_listener: if mb_listener:
policies = [] policies = self._get_multi_param("PolicyNames.member")
policy_index = 1
while True:
try:
policy = self.querystring['PolicyNames.member.{0}'.format(policy_index)][0]
except KeyError:
break
policy_index += 1
policies.append(str(policy))
self.elb_backend.set_load_balancer_policies_of_listener(load_balancer_name, load_balancer_port, policies) self.elb_backend.set_load_balancer_policies_of_listener(load_balancer_name, load_balancer_port, policies)
# else: explode? # else: explode?
template = self.response_template(SET_LOAD_BALANCER_POLICIES_OF_LISTENER_TEMPLATE) template = self.response_template(SET_LOAD_BALANCER_POLICIES_OF_LISTENER_TEMPLATE)
return template.render() return template.render()
def set_load_balancer_policies_for_backend_server(self): def set_load_balancer_policies_for_backend_server(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0] load_balancer_name = self.querystring.get('LoadBalancerName')[0]
load_balancer = self.elb_backend.describe_load_balancers(load_balancer_name)[0] load_balancer = self.elb_backend.get_load_balancer(load_balancer_name)
instance_port = int(self.querystring.get('InstancePort')[0]) instance_port = int(self.querystring.get('InstancePort')[0])
mb_backend = [b for b in load_balancer.backends if int(b.instance_port) == instance_port] mb_backend = [b for b in load_balancer.backends if int(b.instance_port) == instance_port]
if mb_backend: if mb_backend:
policies = [] policies = self._get_multi_param('PolicyNames.member')
policy_index = 1
while True:
try:
policy = self.querystring['PolicyNames.member.{0}'.format(policy_index)][0]
except KeyError:
break
policy_index += 1
policies.append(str(policy))
self.elb_backend.set_load_balancer_policies_of_backend_server(load_balancer_name, instance_port, policies) self.elb_backend.set_load_balancer_policies_of_backend_server(load_balancer_name, instance_port, policies)
# else: explode? # else: explode?
template = self.response_template(SET_LOAD_BALANCER_POLICIES_FOR_BACKEND_SERVER_TEMPLATE) template = self.response_template(SET_LOAD_BALANCER_POLICIES_FOR_BACKEND_SERVER_TEMPLATE)
return template.render() return template.render()
def describe_instance_health(self): def describe_instance_health(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0] load_balancer_name = self._get_param('LoadBalancerName')
instance_ids = [value[0] for key, value in self.querystring.items() if "Instances.member" in key] instance_ids = [value[0] for key, value in self.querystring.items() if "Instances.member" in key]
if len(instance_ids) == 0: if len(instance_ids) == 0:
instance_ids = self.elb_backend.describe_load_balancers(load_balancer_name)[0].instance_ids instance_ids = self.elb_backend.get_load_balancer(load_balancer_name).instance_ids
template = self.response_template(DESCRIBE_INSTANCE_HEALTH_TEMPLATE) template = self.response_template(DESCRIBE_INSTANCE_HEALTH_TEMPLATE)
return template.render(instance_ids=instance_ids) return template.render(instance_ids=instance_ids)