Skip to content

Commit

Permalink
Update ELBs to use newer querystring querying methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
spulec committed Jul 18, 2015
1 parent 740377b commit 9f02da4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 135 deletions.
30 changes: 15 additions & 15 deletions moto/elb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@
AccessLogAttribute,
CrossZoneLoadBalancingAttribute,
)
from boto.ec2.elb.policies import (
Policies,
AppCookieStickinessPolicy,
LBCookieStickinessPolicy,
OtherPolicy,
)
from boto.ec2.elb.policies import Policies
from moto.core import BaseBackend


Expand Down Expand Up @@ -43,7 +38,7 @@ class FakeBackend(object):
def __init__(self, instance_port):
self.instance_port = instance_port
self.policy_names = []

def __repr__(self):
return "FakeBackend(inp: %s, policies: %s)" % (self.instance_port, self.policy_names)

Expand All @@ -62,19 +57,19 @@ def __init__(self, name, zones, ports):
self.policies.app_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(
protocol=protocol,
load_balancer_port=lb_port,
instance_port=instance_port,
ssl_certificate_id=ssl_certificate_id
protocol=port['protocol'],
load_balancer_port=port['load_balancer_port'],
instance_port=port['instance_port'],
ssl_certificate_id=port.get('sslcertificate_id'),
)
self.listeners.append(listener)

# 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*
backend = FakeBackend(
instance_port = instance_port,
instance_port=port['instance_port'],
)
self.backends.append(backend)

Expand Down Expand Up @@ -134,6 +129,7 @@ def get_default_attributes(cls):

return attributes


class ELBBackend(BaseBackend):

def __init__(self):
Expand All @@ -147,7 +143,11 @@ def create_load_balancer(self, name, zones, ports):
def create_load_balancer_listeners(self, name, ports):
balancer = self.load_balancers.get(name, None)
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:
if lb_port == listener.load_balancer_port:
break
Expand Down
173 changes: 53 additions & 120 deletions moto/elb/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
CrossZoneLoadBalancingAttribute,
)
from boto.ec2.elb.policies import (
Policies,
AppCookieStickinessPolicy,
LBCookieStickinessPolicy,
OtherPolicy,
)

Expand All @@ -26,20 +24,9 @@ def create_load_balancer(self):
"""
u'Scheme': [u'internet-facing'],
"""
load_balancer_name = self.querystring.get('LoadBalancerName')[0]
availability_zones = [value[0] for key, value in self.querystring.items() if "AvailabilityZones.member" in key]
ports = []
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
load_balancer_name = self._get_param('LoadBalancerName')
availability_zones = self._get_multi_param("AvailabilityZones.member")
ports = self._get_list_prefix("Listeners.member")

self.elb_backend.create_load_balancer(
name=load_balancer_name,
Expand All @@ -50,75 +37,56 @@ def create_load_balancer(self):
return template.render()

def create_load_balancer_listeners(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0]
ports = []
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
load_balancer_name = self._get_param('LoadBalancerName')
ports = self._get_list_prefix("Listeners.member")

self.elb_backend.create_load_balancer_listeners(name=load_balancer_name, ports=ports)

template = self.response_template(CREATE_LOAD_BALANCER_LISTENERS_TEMPLATE)
return template.render()

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)
template = self.response_template(DESCRIBE_LOAD_BALANCERS_TEMPLATE)
return template.render(load_balancers=load_balancers)

def delete_load_balancer_listeners(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0]
ports = []
port_index = 1
while True:
try:
port = self.querystring['LoadBalancerPorts.member.{0}'.format(port_index)][0]
except KeyError:
break

port_index += 1
ports.append(int(port))
load_balancer_name = self._get_param('LoadBalancerName')
ports = self._get_multi_param("LoadBalancerPorts.member")
ports = [int(port) for port in ports]

self.elb_backend.delete_load_balancer_listeners(load_balancer_name, ports)
template = self.response_template(DELETE_LOAD_BALANCER_LISTENERS)
return template.render()

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)
template = self.response_template(DELETE_LOAD_BALANCER_TEMPLATE)
return template.render()

def configure_health_check(self):
check = self.elb_backend.configure_health_check(
load_balancer_name=self.querystring.get('LoadBalancerName')[0],
timeout=self.querystring.get('HealthCheck.Timeout')[0],
healthy_threshold=self.querystring.get('HealthCheck.HealthyThreshold')[0],
unhealthy_threshold=self.querystring.get('HealthCheck.UnhealthyThreshold')[0],
interval=self.querystring.get('HealthCheck.Interval')[0],
target=self.querystring.get('HealthCheck.Target')[0],
load_balancer_name=self._get_param('LoadBalancerName'),
timeout=self._get_param('HealthCheck.Timeout'),
healthy_threshold=self._get_param('HealthCheck.HealthyThreshold'),
unhealthy_threshold=self._get_param('HealthCheck.UnhealthyThreshold'),
interval=self._get_param('HealthCheck.Interval'),
target=self._get_param('HealthCheck.Target'),
)
template = self.response_template(CONFIGURE_HEALTH_CHECK_TEMPLATE)
return template.render(check=check)

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]
template = self.response_template(REGISTER_INSTANCES_TEMPLATE)
load_balancer = self.elb_backend.register_instances(load_balancer_name, instance_ids)
return template.render(load_balancer=load_balancer)

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]
lb_port = self.querystring['LoadBalancerPort'][0]

Expand All @@ -128,67 +96,58 @@ def set_load_balancer_listener_sslcertificate(self):
return template.render()

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]
template = self.response_template(DEREGISTER_INSTANCES_TEMPLATE)
load_balancer = self.elb_backend.deregister_instances(load_balancer_name, instance_ids)
return template.render(load_balancer=load_balancer)

def describe_load_balancer_attributes(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0]
load_balancer = self.elb_backend.describe_load_balancers(load_balancer_name)[0]
load_balancer_name = self._get_param('LoadBalancerName')
load_balancer = self.elb_backend.get_load_balancer(load_balancer_name)
template = self.response_template(DESCRIBE_ATTRIBUTES_TEMPLATE)
return template.render(attributes=load_balancer.attributes)

def modify_load_balancer_attributes(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0]
load_balancer = self.elb_backend.describe_load_balancers(load_balancer_name)[0]

def parse_attribute(attribute_name):
"""
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))
load_balancer_name = self._get_param('LoadBalancerName')
load_balancer = self.elb_backend.get_load_balancer(load_balancer_name)

cross_zone = parse_attribute("CrossZoneLoadBalancing")
cross_zone = self._get_dict_param("LoadBalancerAttributes.CrossZoneLoadBalancing.")
if cross_zone:
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)

access_log = parse_attribute("AccessLog")
access_log = self._get_dict_param("LoadBalancerAttributes.AccessLog.")
if access_log:
attribute = AccessLogAttribute()
attribute.enabled = access_log["Enabled"] == "true"
attribute.s3_bucket_name = access_log["S3BucketName"]
attribute.s3_bucket_prefix = access_log["S3BucketPrefix"]
attribute.emit_interval = access_log["EmitInterval"]
attribute.enabled = access_log["enabled"] == "true"
attribute.s3_bucket_name = access_log['s3_bucket_name']
attribute.s3_bucket_prefix = access_log['s3_bucket_prefix']
attribute.emit_interval = access_log["emit_interval"]
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:
attribute = ConnectionDrainingAttribute()
attribute.enabled = connection_draining["Enabled"] == "true"
attribute.timeout = connection_draining["Timeout"]
attribute.enabled = connection_draining["enabled"] == "true"
attribute.timeout = connection_draining["timeout"]
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:
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)

template = self.response_template(MODIFY_ATTRIBUTES_TEMPLATE)
return template.render(attributes=load_balancer.attributes)

def create_load_balancer_policy(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0]
load_balancer = self.elb_backend.describe_load_balancers(load_balancer_name)[0]
load_balancer_name = self._get_param('LoadBalancerName')

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

self.elb_backend.create_lb_other_policy(load_balancer_name, other_policy)
Expand All @@ -197,30 +156,25 @@ def create_load_balancer_policy(self):
return template.render()

def create_app_cookie_stickiness_policy(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0]
load_balancer = self.elb_backend.describe_load_balancers(load_balancer_name)[0]
load_balancer_name = self._get_param('LoadBalancerName')

policy = AppCookieStickinessPolicy()
policy_name = [value[0] for key, value in self.querystring.items() if "PolicyName" in key][0]
policy.policy_name = policy_name
cookie_name = [value[0] for key, value in self.querystring.items() if "CookieName" in key][0]
policy.cookie_name = cookie_name
policy.policy_name = self._get_param("PolicyName")
policy.cookie_name = self._get_param("CookieName")

self.elb_backend.create_app_cookie_stickiness_policy(load_balancer_name, policy)

template = self.response_template(CREATE_LOAD_BALANCER_POLICY_TEMPLATE)
return template.render()

def create_lbcookie_stickiness_policy(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0]
load_balancer = self.elb_backend.describe_load_balancers(load_balancer_name)[0]
load_balancer_name = self._get_param('LoadBalancerName')

policy = AppCookieStickinessPolicy()
policy_name = [value[0] for key, value in self.querystring.items() if "PolicyName" in key][0]
policy.policy_name = policy_name
cookie_expirations = [value[0] for key, value in self.querystring.items() if "CookieExpirationPeriod" in key]
policy.policy_name = self._get_param("PolicyName")
cookie_expirations = self._get_param("CookieExpirationPeriod")
if cookie_expirations:
policy.cookie_expiration_period = int(cookie_expirations[0])
policy.cookie_expiration_period = int(cookie_expirations)
else:
policy.cookie_expiration_period = None

Expand All @@ -230,59 +184,38 @@ def create_lbcookie_stickiness_policy(self):
return template.render()

def set_load_balancer_policies_of_listener(self):
load_balancer_name = self.querystring.get('LoadBalancerName')[0]
load_balancer = self.elb_backend.describe_load_balancers(load_balancer_name)[0]
load_balancer_port = int(self.querystring.get('LoadBalancerPort')[0])
load_balancer_name = self._get_param('LoadBalancerName')
load_balancer = self.elb_backend.get_load_balancer(load_balancer_name)
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]
if mb_listener:
policies = []
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))

policies = self._get_multi_param("PolicyNames.member")
self.elb_backend.set_load_balancer_policies_of_listener(load_balancer_name, load_balancer_port, policies)
# else: explode?

template = self.response_template(SET_LOAD_BALANCER_POLICIES_OF_LISTENER_TEMPLATE)
return template.render()

def set_load_balancer_policies_for_backend_server(self):
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])

mb_backend = [b for b in load_balancer.backends if int(b.instance_port) == instance_port]
if mb_backend:
policies = []
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))

policies = self._get_multi_param('PolicyNames.member')
self.elb_backend.set_load_balancer_policies_of_backend_server(load_balancer_name, instance_port, policies)

# else: explode?

template = self.response_template(SET_LOAD_BALANCER_POLICIES_FOR_BACKEND_SERVER_TEMPLATE)
return template.render()

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]
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)
return template.render(instance_ids=instance_ids)

Expand Down

0 comments on commit 9f02da4

Please sign in to comment.