From 6690e8a5ef6570cc744fd1287b5aff362eda2fff Mon Sep 17 00:00:00 2001 From: David Shrewsbury Date: Mon, 30 Sep 2013 12:47:49 -0400 Subject: [PATCH] Add support for key=value node options This changes allows additional node options to be defined after the ip:port portion. Format will be: IP:PORT:key=value:key=value:... First two node options supported are 'weight' and 'backup'. Also fixes a minor bug where 'raise' was being called without defining what type of exception to raise. This works ok to re-raise an existing exception, but raising a new exception requires a type. Change-Id: I9b9883d100983a242002c95378bdbb672937ed90 --- doc/command.rst | 10 ++++++---- doc/examples.rst | 14 ++++++++++++++ libraclient/libraapi.py | 37 ++++++++++++++++++++++++++++++------- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/doc/command.rst b/doc/command.rst index 44b0fed..460ea45 100644 --- a/doc/command.rst +++ b/doc/command.rst @@ -99,9 +99,10 @@ Create a load balancer The Galera option adds support for deadlock avoidance in Galera clusters, see `Serveral Nine's Blog `_ on this. -.. option:: --node +.. option:: --node - The IP and port for a load balancer node (can be used multiple times to add multiple nodes) + The IP and port for a load balancer node (can be used multiple times to add multiple nodes). + Additional node options may be specified after the ip:port portion in a option=value format. .. option:: --vip @@ -226,9 +227,10 @@ Add a node to a load balancer The ID of the load balancer -.. option:: --node +.. option:: --node - The node address in ip:port format (can be used multiple times to add multiple nodes) + The node address in ip:port format (can be used multiple times to add multiple nodes). + Additional node options may be specified after the ip:port portion in a option=value format. .. program:: libra_client node-delete diff --git a/doc/examples.rst b/doc/examples.rst index 670c288..6b1b3d3 100644 --- a/doc/examples.rst +++ b/doc/examples.rst @@ -28,6 +28,20 @@ Client will then return a table similar to the below: | [{u'ipVersion': u'IPV_4', u'type': u'PUBLIC', u'id': u'52', u'address': u'15.185.224.62'}] | [{u'status': u'ONLINE', u'id': u'2311', u'port': u'80', u'condition': u'ENABLED', u'address': u'192.168.1.1'}, {u'status': u'ONLINE', u'id': u'2312', u'port': u'80', u'condition': u'ENABLED', u'address': u'192.168.1.2'}] | +--------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +Create a Load Balancer with Node Options +---------------------------------------- + +.. code-block:: bash + + libra_client --os_auth_url=https://company.com/openstack/auth/url \ + --os_username=username --os_password=pasword --os_tenant_name=tenant \ + --os_region_name=region create --name=my_load_balancer \ + --node 192.168.1.1:80:weight=1 --node 192.168.1.2:80:weight=2 + +Nearly identical to the above example, this creates a new load balancer +with two nodes, but one is more heavily weighted than the other, causing +it to accept more traffic. + Create a Shared Load Balancer ----------------------------- diff --git a/libraclient/libraapi.py b/libraclient/libraapi.py index b77962f..efa8c91 100644 --- a/libraclient/libraapi.py +++ b/libraclient/libraapi.py @@ -283,14 +283,37 @@ class LibraAPI(object): out_nodes = [] try: for node in nodes: - addr = node.split(':') + nodeopts = node.split(':') + ipaddr = nodeopts[0] + port = nodeopts[1] + weight, backup = None, None + # Test IP valid # TODO: change to pton when we want to support IPv6 - socket.inet_aton(addr[0]) + socket.inet_aton(ipaddr) # Test port valid - if int(addr[1]) < 0 or int(addr[1]) > 65535: - raise - out_nodes.append({'address': addr[0], 'port': addr[1]}) - except: - raise Exception("Invalid IP:port specified for --node") + if int(port) < 0 or int(port) > 65535: + raise Exception('Port out of range') + + # Process the rest of the node options as key=value + for kv in nodeopts[2:]: + key, value = kv.split('=') + key = key.lower() + value = value.upper() + if key == 'weight': + weight = int(value) + elif key == 'backup': + backup = value # 'TRUE' or 'FALSE' + else: + raise Exception("Unknown node option '%s'" % key) + + node_def = {'address': ipaddr, 'port': port} + if weight: + node_def['weight'] = weight + if backup: + node_def['backup'] = backup + + out_nodes.append(node_def) + except Exception as e: + raise Exception("Invalid value specified for --node: %s" % e) return out_nodes