From e821770505b5107100a80d0f046ec9d0e3da3446 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 23 Dec 2013 15:30:45 -0600 Subject: [PATCH 1/2] Allow respecification of a node without requiring node_id --- library/cloud/rax_clb_nodes | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/library/cloud/rax_clb_nodes b/library/cloud/rax_clb_nodes index 04ec11fc94..30514abde9 100644 --- a/library/cloud/rax_clb_nodes +++ b/library/cloud/rax_clb_nodes @@ -135,11 +135,21 @@ def _activate_virtualenv(path): execfile(activate_this, dict(__file__=activate_this)) -def _get_node(lb, node_id): - """Return a node with the given `node_id`""" - for node in lb.nodes: - if node.id == node_id: - return node +def _get_node(lb, node_id=None, address=None, port=None): + """Return a matching node""" + searches = { + 'id': node_id, + 'address': address, + 'port': port + } + + for node in getattr(lb, 'nodes', []): + try: + if all(getattr(node, attr) == value + for (attr, value) in searches.items() if value is not None): + return node + except AttributeError: + continue return None @@ -230,10 +240,7 @@ def main(): except pyrax.exc.PyraxException, e: module.fail_json(msg='%s' % e.message) - if node_id: - node = _get_node(lb, node_id) - else: - node = None + node = _get_node(lb, node_id, address, port) result = _node_to_dict(node) @@ -272,22 +279,12 @@ def main(): except pyrax.exc.PyraxException, e: module.fail_json(msg='%s' % e.message) else: # Updating an existing node - immutable = { - 'address': address, - 'port': port, - } - mutable = { 'condition': condition, 'type': typ, 'weight': weight, } - for name, value in immutable.items(): - if value: - module.fail_json( - msg='Attribute %s cannot be modified' % name) - for name, value in mutable.items(): if value is None or value == getattr(node, name): mutable.pop(name) From 8a98773089a5329d31d40e89997b75affbc37173 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Sun, 6 Apr 2014 19:17:13 -0500 Subject: [PATCH 2/2] Simplify node matching --- library/cloud/rax_clb_nodes | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/library/cloud/rax_clb_nodes b/library/cloud/rax_clb_nodes index 30514abde9..dc0950dca5 100644 --- a/library/cloud/rax_clb_nodes +++ b/library/cloud/rax_clb_nodes @@ -137,19 +137,18 @@ def _activate_virtualenv(path): def _get_node(lb, node_id=None, address=None, port=None): """Return a matching node""" - searches = { - 'id': node_id, - 'address': address, - 'port': port - } - for node in getattr(lb, 'nodes', []): - try: - if all(getattr(node, attr) == value - for (attr, value) in searches.items() if value is not None): - return node - except AttributeError: - continue + match_list = [] + if node_id is not None: + match_list.append(getattr(node, 'id', None) == node_id) + if address is not None: + match_list.append(getattr(node, 'address', None) == address) + if port is not None: + match_list.append(getattr(node, 'port', None) == port) + + if match_list and all(match_list): + return node + return None