Add support for setting protocol.

HAProxy driver will not support TCP, HTTP, or HEALTH mode
values via the new set_protocol() driver API method. Default
is HTTP. Invalid values will return a failure.
This commit is contained in:
David Shrewsbury
2012-10-09 12:25:38 -04:00
parent eada8244a6
commit cf7cec203f
3 changed files with 48 additions and 19 deletions

View File

@@ -21,6 +21,8 @@ class LBaaSController(object):
NODE_ERR = "DISABLED"
RESPONSE_FAILURE = "FAIL"
RESPONSE_SUCCESS = "PASS"
ACTION_FIELD = 'hpcs_action'
RESPONSE_FIELD = 'hpcs_response'
def __init__(self, logger, driver, json_msg):
self.logger = logger
@@ -34,12 +36,12 @@ class LBaaSController(object):
Process the JSON message and return a JSON response.
"""
if 'hpcs_action' not in self.msg:
self.logger.error("Missing HPCS_ACTION value")
self.msg['hpcs_response'] = self.RESPONSE_FAILURE
if self.ACTION_FIELD not in self.msg:
self.logger.error("Missing `%s` value" % self.ACTION_FIELD)
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_FAILURE
return self.msg
action = self.msg['hpcs_action'].upper()
action = self.msg[self.ACTION_FIELD].upper()
self.logger.debug("Requested action: %s" % action)
if action == 'CREATE':
return self._action_create()
@@ -52,8 +54,9 @@ class LBaaSController(object):
elif action == 'DELETE':
return self._action_delete()
else:
self.logger.error("Invalid HPCS_ACTION value: %s" % action)
self.msg['hpcs_response'] = self.RESPONSE_FAILURE
self.logger.error("Invalid `%s` value: %s" %
(self.ACTION_FIELD, action))
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_FAILURE
return self.msg
def _action_create(self):
@@ -61,6 +64,21 @@ class LBaaSController(object):
if 'nodes' not in self.msg:
return BadRequest("Missing 'nodes' element").to_json()
if 'protocol' in self.msg:
try:
self.driver.set_protocol(self.msg['protocol'])
except NotImplementedError:
self.logger.error(
"Selected driver does not support setting protocol."
)
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_FAILURE
return self.msg
except Exception as e:
self.logger.error("Failure trying to set protocol: %s, %s" %
(e.__class__, e))
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_FAILURE
return self.msg
for lb_node in self.msg['nodes']:
port, address = None, None
@@ -97,15 +115,15 @@ class LBaaSController(object):
)
for lb_node in self.msg['nodes']:
lb_node['condition'] = self.NODE_ERR
self.msg['hpcs_response'] = self.RESPONSE_FAILURE
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_FAILURE
except Exception as e:
self.logger.error("CREATE failed: %s, %s" % (e.__class__, e))
for lb_node in self.msg['nodes']:
lb_node['condition'] = self.NODE_ERR
self.msg['hpcs_response'] = self.RESPONSE_FAILURE
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_FAILURE
else:
self.logger.info("Activated load balancer changes")
self.msg['hpcs_response'] = self.RESPONSE_SUCCESS
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_SUCCESS
return self.msg
@@ -124,12 +142,12 @@ class LBaaSController(object):
self.logger.error(
"Selected driver does not support SUSPEND action."
)
self.msg['hpcs_response'] = self.RESPONSE_FAILURE
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_FAILURE
except Exception as e:
self.logger.error("SUSPEND failed: %s, %s" % (e.__class__, e))
self.msg['hpcs_response'] = self.RESPONSE_FAILURE
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_FAILURE
else:
self.msg['hpcs_response'] = self.RESPONSE_SUCCESS
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_SUCCESS
return self.msg
def _action_enable(self):
@@ -140,12 +158,12 @@ class LBaaSController(object):
self.logger.error(
"Selected driver does not support ENABLE action."
)
self.msg['hpcs_response'] = self.RESPONSE_FAILURE
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_FAILURE
except Exception as e:
self.logger.error("ENABLE failed: %s, %s" % (e.__class__, e))
self.msg['hpcs_response'] = self.RESPONSE_FAILURE
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_FAILURE
else:
self.msg['hpcs_response'] = self.RESPONSE_SUCCESS
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_SUCCESS
return self.msg
def _action_delete(self):
@@ -156,10 +174,10 @@ class LBaaSController(object):
self.logger.error(
"Selected driver does not support DELETE action."
)
self.msg['hpcs_response'] = self.RESPONSE_FAILURE
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_FAILURE
except Exception as e:
self.logger.error("DELETE failed: %s, %s" % (e.__class__, e))
self.msg['hpcs_response'] = self.RESPONSE_FAILURE
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_FAILURE
else:
self.msg['hpcs_response'] = self.RESPONSE_SUCCESS
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_SUCCESS
return self.msg

View File

@@ -38,6 +38,10 @@ class LoadBalancerDriver(object):
""" Add a server for which we will proxy. """
raise NotImplementedError()
def set_protocol(self, protocol):
""" Set the protocol of the instance. """
raise NotImplementedError()
def create(self):
""" Create the load balancer. """
raise NotImplementedError()

View File

@@ -25,6 +25,7 @@ class HAProxyDriver(LoadBalancerDriver):
self._backup_config = self._config_file + '.BKUP'
self._config = dict()
self.bind('0.0.0.0', 80)
self.set_protocol('HTTP')
def _config_to_string(self):
"""
@@ -44,7 +45,7 @@ class HAProxyDriver(LoadBalancerDriver):
)
output.append('defaults')
output.append(' log global')
output.append(' mode http')
output.append(' mode %s' % self._config['mode'])
output.append(' option httplog')
output.append(' option dontlognull')
output.append(' option redispatch')
@@ -154,6 +155,12 @@ class HAProxyDriver(LoadBalancerDriver):
self._config['servers'] = []
self._config['servers'].append((host, port))
def set_protocol(self, protocol):
proto = protocol.lower()
if proto not in ('tcp', 'http', 'health'):
raise Exception("Invalid protocol: %s" % protocol)
self._config['mode'] = proto
def create(self):
self._write_config()
self._restart()