Allow respecification of a node without requiring node_id

This commit is contained in:
Matt Martz 2013-12-23 15:30:45 -06:00
parent 3d8997d219
commit 9a19792eff

View file

@ -135,11 +135,21 @@ def _activate_virtualenv(path):
execfile(activate_this, dict(__file__=activate_this)) execfile(activate_this, dict(__file__=activate_this))
def _get_node(lb, node_id): def _get_node(lb, node_id=None, address=None, port=None):
"""Return a node with the given `node_id`""" """Return a matching node"""
for node in lb.nodes: searches = {
if node.id == node_id: '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 return node
except AttributeError:
continue
return None return None
@ -230,10 +240,7 @@ def main():
except pyrax.exc.PyraxException, e: except pyrax.exc.PyraxException, e:
module.fail_json(msg='%s' % e.message) module.fail_json(msg='%s' % e.message)
if node_id: node = _get_node(lb, node_id, address, port)
node = _get_node(lb, node_id)
else:
node = None
result = _node_to_dict(node) result = _node_to_dict(node)
@ -272,22 +279,12 @@ def main():
except pyrax.exc.PyraxException, e: except pyrax.exc.PyraxException, e:
module.fail_json(msg='%s' % e.message) module.fail_json(msg='%s' % e.message)
else: # Updating an existing node else: # Updating an existing node
immutable = {
'address': address,
'port': port,
}
mutable = { mutable = {
'condition': condition, 'condition': condition,
'type': typ, 'type': typ,
'weight': weight, '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(): for name, value in mutable.items():
if value is None or value == getattr(node, name): if value is None or value == getattr(node, name):
mutable.pop(name) mutable.pop(name)