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: val = grpcfg[attribute] else: val = {'value': None} if attribute.startswith('secret.'): yield msg.CryptedAttributes( kv={attribute: val}, desc=allattributes.node[attribute]['description']) elif isinstance(val, list): raise Exception("TODO") else: yield msg.Attributes( kv={attribute: val}, desc=allattributes.node[attribute]['description']) if element == 'current': for attribute in sorted(grpcfg.iterkeys()): currattr = grpcfg[attribute] if attribute == 'nodes': desc = 'The nodes belonging to this group' elif attribute == 'noderange': desc = 'A dynamic noderange that this group refers to in noderange expansion' else: try: desc = allattributes.node[attribute]['description'] except KeyError: desc = 'Unknown' if 'value' in currattr or 'expression' in currattr: yield msg.Attributes(kv={attribute: currattr}, 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: print attribute print repr(currattr) raise Exception("BUGGY ATTRIBUTE FOR NODEGROUP")
def retrieve_nodes(nodes, element, configmanager, inputdata): attributes = configmanager.get_node_attributes(nodes) if element[-1] == 'all': for node in util.natural_sort(nodes): theattrs = set(allattributes.node).union(set(attributes[node])) for attribute in sorted(theattrs): if attribute in attributes[node]: # have a setting for it val = attributes[node][attribute] elif attribute == 'groups': # no setting, provide a blank val = [] else: # no setting, provide a blank val = {'value': None} if attribute.startswith('secret.') or attribute.startswith( 'crypted.'): yield msg.CryptedAttributes( node, {attribute: val}, allattributes.node.get(attribute, {}).get('description', '')) elif isinstance(val, list): yield msg.ListAttributes( node, {attribute: val}, allattributes.node.get(attribute, {}).get('description', '')) else: yield msg.Attributes( node, {attribute: val}, allattributes.node.get(attribute, {}).get('description', '')) elif element[-1] == 'current': for node in util.natural_sort(list(attributes)): for attribute in sorted(attributes[node]): currattr = attributes[node][attribute] try: desc = allattributes.node[attribute]['description'] except KeyError: desc = '' if 'value' in currattr or 'expression' in currattr: yield msg.Attributes(node, {attribute: currattr}, desc) elif 'cryptvalue' in currattr or 'hashvalue' in currattr: yield msg.CryptedAttributes(node, {attribute: currattr}, desc) elif isinstance(currattr, list): yield msg.ListAttributes(node, {attribute: currattr}, desc) else: print(attribute) print(repr(currattr)) raise Exception("BUGGY ATTRIBUTE FOR NODE")
def show_user(name, configmanager): userobj = configmanager.get_user(name) rv = {} for attr in attrscheme.user: rv[attr] = None if attr == 'password': if 'cryptpass' in userobj: rv['password'] = {'cryptvalue': True} yield msg.CryptedAttributes( kv={'password': rv['password']}, desc=attrscheme.user[attr]['description']) else: if attr in userobj: rv[attr] = userobj[attr] yield msg.Attributes(kv={attr: rv[attr]}, desc=attrscheme.user[attr]['description']) if 'role' in userobj: yield msg.Attributes(kv={'role': userobj['role']})
def retrieve_nodes(nodes, element, configmanager, inputdata): attributes = configmanager.get_node_attributes(nodes) if element[-1] == 'all': 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] elif attribute == 'groups': # no setting, provide a blank val = [] else: # no setting, provide a blank val = {'value': None} if attribute.startswith('secret.'): yield msg.CryptedAttributes( node, {attribute: val}, allattributes.node[attribute]['description']) elif isinstance(val, list): yield msg.ListAttributes( node, {attribute: val}, allattributes.node[attribute]['description']) else: yield msg.Attributes( node, {attribute: val}, allattributes.node[attribute]['description']) elif element[-1] == 'current': for node in attributes.iterkeys(): for attribute in sorted(attributes[node].iterkeys()): currattr = attributes[node][attribute] try: desc = allattributes.node[attribute]['description'] except KeyError: desc = 'Unknown' if 'value' in currattr or 'expression' in currattr: yield msg.Attributes(node, {attribute: currattr}, desc) elif 'cryptvalue' in currattr: yield msg.CryptedAttributes( node, {attribute: currattr}, desc) elif isinstance(currattr, list): yield msg.ListAttributes( node, {attribute: currattr}, desc) else: print attribute print repr(currattr) raise Exception("BUGGY ATTRIBUTE FOR NODE")
def retrieve_nodegroup(nodegroup, element, configmanager, inputdata): try: grpcfg = configmanager.get_nodegroup_attributes(nodegroup) except KeyError: if not configmanager.is_nodegroup(nodegroup): raise exc.NotFoundException( 'Invalid nodegroup: {0} not found'.format(nodegroup)) raise if element == 'all': theattrs = set(allattributes.node).union(set(grpcfg)) theattrs.add('nodes') for attribute in sorted(theattrs): if attribute == 'groups': continue if attribute == 'nodes': yield msg.ListAttributes( kv={'nodes': list(grpcfg.get('nodes', []))}, desc="The nodes belonging to this group") continue if attribute in grpcfg: val = grpcfg[attribute] else: val = {'value': None} if attribute.startswith('secret.'): yield msg.CryptedAttributes( kv={attribute: val}, desc=allattributes.node[attribute]['description']) elif isinstance(val, list): yield msg.ListAttributes(kv={attribute: val}, desc=allattributes.node.get( attribute, {}).get('description', '')) else: yield msg.Attributes(kv={attribute: val}, desc=allattributes.node.get( attribute, {}).get('description', '')) if element == 'current': for attribute in sorted(list(grpcfg)): currattr = grpcfg[attribute] if attribute == 'nodes': desc = 'The nodes belonging to this group' elif attribute == 'noderange': desc = 'A dynamic noderange that this group refers to in noderange expansion' else: try: desc = allattributes.node[attribute]['description'] except KeyError: desc = '' if 'value' in currattr or 'expression' in currattr: yield msg.Attributes(kv={attribute: currattr}, 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: print attribute print repr(currattr) raise Exception("BUGGY ATTRIBUTE FOR NODEGROUP")
def show_usergroup(groupname, configmanager): groupinfo = configmanager.get_usergroup(groupname) for attr in groupinfo: yield msg.Attributes(kv={attr: groupinfo[attr]})
def handle_path(path, operation, configmanager, inputdata=None, autostrip=True): """Given a full path request, return an object. The plugins should generally return some sort of iterator. An exception is made for console/session, which should return a class with connect(), read(), write(bytes), and close() """ pathcomponents = path.split('/') del pathcomponents[0] # discard the value from leading / if pathcomponents[-1] == '': del pathcomponents[-1] if not pathcomponents: # root collection list return enumerate_collections(rootcollections) elif pathcomponents[0] == 'noderange': return handle_node_request(configmanager, inputdata, operation, pathcomponents, autostrip) elif pathcomponents[0] == 'deployment': return handle_deployment(configmanager, inputdata, pathcomponents, operation) elif pathcomponents[0] == 'nodegroups': return handle_nodegroup_request(configmanager, inputdata, pathcomponents, operation) elif pathcomponents[0] == 'nodes': # single node request of some sort return handle_node_request(configmanager, inputdata, operation, pathcomponents, autostrip) elif pathcomponents[0] == 'discovery': return disco.handle_api_request(configmanager, inputdata, operation, pathcomponents) elif pathcomponents[0] == 'networking': return macmap.handle_api_request(configmanager, inputdata, operation, pathcomponents) elif pathcomponents[0] == 'version': return (msg.Attributes(kv={'version': confluent.__version__}), ) elif pathcomponents[0] == 'usergroups': # TODO: when non-administrator accounts exist, # they must only be allowed to see their own user try: usergroup = pathcomponents[1] except IndexError: # it's just users/ if operation == 'create': inputdata = msg.get_input_message(pathcomponents, operation, inputdata, configmanager=configmanager) create_usergroup(inputdata.attribs, configmanager) return iterate_collections(configmanager.list_usergroups(), forcecollection=False) if usergroup not in configmanager.list_usergroups(): raise exc.NotFoundException("Invalid usergroup %s" % usergroup) if operation == 'retrieve': return show_usergroup(usergroup, configmanager) elif operation == 'delete': return delete_usergroup(usergroup, configmanager) elif operation == 'update': inputdata = msg.get_input_message(pathcomponents, operation, inputdata, configmanager=configmanager) update_usergroup(usergroup, inputdata.attribs, configmanager) return show_usergroup(usergroup, configmanager) elif pathcomponents[0] == 'users': # TODO: when non-administrator accounts exist, # they must only be allowed to see their own user try: user = pathcomponents[1] except IndexError: # it's just users/ if operation == 'create': inputdata = msg.get_input_message(pathcomponents, operation, inputdata, configmanager=configmanager) create_user(inputdata.attribs, configmanager) return iterate_collections(configmanager.list_users(), forcecollection=False) if user not in configmanager.list_users(): raise exc.NotFoundException("Invalid user %s" % user) if operation == 'retrieve': return show_user(user, configmanager) elif operation == 'delete': return delete_user(user, configmanager) elif operation == 'update': inputdata = msg.get_input_message(pathcomponents, operation, inputdata, configmanager=configmanager) update_user(user, inputdata.attribs, configmanager) return show_user(user, configmanager) elif pathcomponents[0] == 'events': try: element = pathcomponents[1] except IndexError: if operation != 'retrieve': raise exc.InvalidArgumentException('Target is read-only') return (msg.ChildCollection('decode'), ) if element != 'decode': raise exc.NotFoundException() if operation == 'update': return alerts.decode_alert(inputdata, configmanager) elif pathcomponents[0] == 'discovery': return handle_discovery(pathcomponents[1:], operation, configmanager, inputdata) else: raise exc.NotFoundException()