2014-02-13 17:58:03 +00:00
|
|
|
import confluent.exceptions as exc
|
2013-11-02 22:24:04 +00:00
|
|
|
import confluent.messages as msg
|
2013-11-03 15:36:03 +00:00
|
|
|
import confluent.config.attributes as allattributes
|
2013-11-02 22:24:04 +00:00
|
|
|
|
2013-11-02 23:45:10 +00:00
|
|
|
def retrieve(nodes, element, configmanager, inputdata):
|
2014-02-18 18:22:07 +00:00
|
|
|
if nodes is not None:
|
|
|
|
return retrieve_nodes(nodes, element, configmanager, inputdata)
|
|
|
|
elif element[0] == 'nodegroup':
|
|
|
|
return retrieve_nodegroup(element[1], element[3], configmanager, inputdata)
|
|
|
|
|
|
|
|
|
|
|
|
def retrieve_nodegroup(nodegroup, element, configmanager, inputdata):
|
|
|
|
grpcfg = configmanager.get_nodegroup_attributes(nodegroup)
|
|
|
|
if element == 'all':
|
|
|
|
nodes = []
|
|
|
|
if 'nodes' in grpcfg:
|
|
|
|
nodes = list(grpcfg['nodes'])
|
|
|
|
yield msg.ListAttributes(kv={'nodes': nodes},
|
|
|
|
desc="The nodes belonging to this group")
|
|
|
|
for attribute in sorted(allattributes.node.iterkeys()):
|
|
|
|
if attribute == 'groups':
|
|
|
|
continue
|
|
|
|
if attribute in grpcfg:
|
2014-02-18 18:48:26 +00:00
|
|
|
val = grpcfg[attribute]
|
2014-02-18 18:22:07 +00:00
|
|
|
else:
|
2014-02-18 18:48:26 +00:00
|
|
|
val = {'value': None}
|
2014-02-18 18:22:07 +00:00
|
|
|
if attribute.startswith('secret.'):
|
|
|
|
yield msg.CryptedAttributes(
|
2014-02-18 18:48:26 +00:00
|
|
|
kv={attribute: val},
|
2014-02-18 18:22:07 +00:00
|
|
|
desc=allattributes.node[attribute]['description'])
|
|
|
|
elif isinstance(val, list):
|
|
|
|
raise Exception("TODO")
|
|
|
|
else:
|
|
|
|
yield msg.Attributes(
|
2014-02-18 18:48:26 +00:00
|
|
|
kv={attribute: val['value']},
|
2014-02-18 18:22:07 +00:00
|
|
|
desc=allattributes.node[attribute]['description'])
|
|
|
|
if element == 'current':
|
|
|
|
for attribute in sorted(grpcfg.iterkeys()):
|
|
|
|
currattr = grpcfg[attribute]
|
|
|
|
desc=""
|
|
|
|
if attribute == 'nodes':
|
|
|
|
desc = 'The nodes belonging to this group'
|
|
|
|
else:
|
2014-02-20 00:10:58 +00:00
|
|
|
try:
|
|
|
|
desc = allattributes.node[attribute]['description']
|
|
|
|
except KeyError:
|
|
|
|
desc = 'Unknown'
|
2014-02-18 18:22:07 +00:00
|
|
|
if 'value' in currattr:
|
|
|
|
yield msg.Attributes(
|
|
|
|
kv={attribute: currattr['value']},
|
|
|
|
desc=desc)
|
|
|
|
elif 'cryptvalue' in currattr:
|
|
|
|
yield msg.CryptedAttributes(
|
|
|
|
kv={attribute: currattr},
|
|
|
|
desc=desc)
|
|
|
|
elif isinstance(currattr, set):
|
|
|
|
yield msg.ListAttributes(
|
|
|
|
kv={attribute: list(currattr)},
|
|
|
|
desc=desc)
|
|
|
|
elif isinstance(currattr, list):
|
|
|
|
yield msg.ListAttributes(
|
|
|
|
kv={attribute: currattr},
|
|
|
|
desc=desc)
|
|
|
|
else:
|
|
|
|
raise Exception("BUGGY ATTRIBUTE FOR NODE")
|
|
|
|
|
|
|
|
|
|
|
|
def retrieve_nodes(nodes, element, configmanager, inputdata):
|
2013-11-02 22:24:04 +00:00
|
|
|
attributes = configmanager.get_node_attributes(nodes)
|
2013-11-07 19:39:34 +00:00
|
|
|
if element[-1] == 'all':
|
2013-11-03 15:36:03 +00:00
|
|
|
for node in nodes:
|
|
|
|
for attribute in sorted(allattributes.node.iterkeys()):
|
|
|
|
if attribute in attributes[node]: #have a setting for it
|
|
|
|
val = attributes[node][attribute]
|
2013-11-13 20:12:57 +00:00
|
|
|
elif attribute == 'groups': # no setting, provide a blank
|
|
|
|
val = []
|
2013-11-03 15:36:03 +00:00
|
|
|
else: # no setting, provide a blank
|
2014-02-13 17:58:03 +00:00
|
|
|
val = {'value': None}
|
2013-11-03 15:36:03 +00:00
|
|
|
if attribute.startswith('secret.'):
|
|
|
|
yield msg.CryptedAttributes(node,
|
2014-02-13 17:58:03 +00:00
|
|
|
{attribute: val},
|
|
|
|
allattributes.node[attribute]['description'])
|
2013-11-13 20:12:57 +00:00
|
|
|
elif isinstance(val, list):
|
|
|
|
yield msg.ListAttributes(node,
|
2014-02-13 17:58:03 +00:00
|
|
|
{attribute: val},
|
|
|
|
allattributes.node[attribute]['description'])
|
2013-11-03 15:36:03 +00:00
|
|
|
else:
|
|
|
|
yield msg.Attributes(node,
|
2014-02-13 17:58:03 +00:00
|
|
|
{attribute: val['value']},
|
|
|
|
allattributes.node[attribute]['description'])
|
2013-11-07 19:39:34 +00:00
|
|
|
elif element[-1] == 'current':
|
2013-11-03 15:36:03 +00:00
|
|
|
for node in attributes.iterkeys():
|
|
|
|
for attribute in sorted(attributes[node].iterkeys()):
|
|
|
|
currattr = attributes[node][attribute]
|
2014-02-20 00:10:58 +00:00
|
|
|
try:
|
|
|
|
desc = allattributes.node[attribute]['description']
|
|
|
|
except KeyError:
|
|
|
|
desc = 'Unknown'
|
2013-11-03 15:36:03 +00:00
|
|
|
if 'value' in currattr:
|
|
|
|
yield msg.Attributes(node,
|
2014-02-13 17:58:03 +00:00
|
|
|
{attribute: currattr['value']},
|
2014-02-20 00:10:58 +00:00
|
|
|
desc)
|
2013-11-03 15:36:03 +00:00
|
|
|
elif 'cryptvalue' in currattr:
|
|
|
|
yield msg.CryptedAttributes(node,
|
2014-02-20 00:10:58 +00:00
|
|
|
{attribute: currattr}, desc)
|
2013-11-13 15:36:43 +00:00
|
|
|
elif isinstance(currattr, list):
|
|
|
|
yield msg.ListAttributes(node,
|
2014-02-20 00:10:58 +00:00
|
|
|
{attribute: currattr}, desc)
|
2013-11-03 15:36:03 +00:00
|
|
|
else:
|
|
|
|
raise Exception("BUGGY ATTRIBUTE FOR NODE")
|
2013-11-03 15:21:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def update(nodes, element, configmanager, inputdata):
|
2014-02-18 18:22:07 +00:00
|
|
|
if nodes is not None:
|
|
|
|
return update_nodes(nodes, element, configmanager, inputdata)
|
|
|
|
elif element[0] == 'nodegroup':
|
|
|
|
return update_nodegroup(element[1], element[3], configmanager, inputdata)
|
|
|
|
raise Exception("This line should never be reached")
|
|
|
|
|
|
|
|
def update_nodegroup(group, element, configmanager, inputdata):
|
|
|
|
try:
|
|
|
|
configmanager.set_group_attributes({group: inputdata.attribs})
|
|
|
|
except ValueError:
|
|
|
|
raise exc.InvalidArgumentException()
|
|
|
|
return retrieve_nodegroup(group, element, configmanager, inputdata)
|
|
|
|
|
|
|
|
def update_nodes(nodes, element, configmanager, inputdata):
|
2013-11-03 15:21:54 +00:00
|
|
|
updatedict = {}
|
|
|
|
for node in nodes:
|
|
|
|
updatenode = inputdata.get_attributes(node)
|
|
|
|
if updatenode:
|
|
|
|
updatedict[node] = updatenode
|
2014-02-13 17:58:03 +00:00
|
|
|
try:
|
|
|
|
configmanager.set_node_attributes(updatedict)
|
|
|
|
except ValueError:
|
|
|
|
raise exc.InvalidArgumentException()
|
2013-11-13 19:52:32 +00:00
|
|
|
return retrieve(nodes, element, configmanager, inputdata)
|