def create_many(self, data_set): activity = Activity() activity.user_id = self.user_id activity.activity_type_id = self.Session\ .query(ActivityType)\ .filter(ActivityType.name == 'node')\ .filter(ActivityType.action == 'add')\ .first().id activity.children = [] self.Session.add(activity) for data in data_set['items']: node = Node() sub_activity = Activity() sub_activity.activity_type_id = activity.activity_type_id sub_activity.user_id = self.user_id sub_activity.changesets = [] for k,v in data.iteritems(): setattr(node, k, v) cs = Changeset() cs.field = k cs.old_value = v self.Session.add(cs) sub_activity.changesets.append(cs) self.Session.add(sub_activity) self.Session.add(node) activity.children.append(sub_activity) activity.link = 'Nodes (Added): %s new nodes added' % len( activity.children) self.Session.add(activity) self.Session.commit() return 'Added %s groups' % len(activity.children)
def create_many(self, data_set): activity = Activity() activity.user_id = self.user_id activity.activity_type_id = self.Session\ .query(ActivityType)\ .filter(ActivityType.name == 'node')\ .filter(ActivityType.action == 'add')\ .first().id activity.children = [] self.Session.add(activity) for data in data_set['items']: node = Node() sub_activity = Activity() sub_activity.activity_type_id = activity.activity_type_id sub_activity.user_id = self.user_id sub_activity.changesets = [] for k, v in data.iteritems(): setattr(node, k, v) cs = Changeset() cs.field = k cs.old_value = v self.Session.add(cs) sub_activity.changesets.append(cs) self.Session.add(sub_activity) self.Session.add(node) activity.children.append(sub_activity) activity.link = 'Nodes (Added): %s new nodes added' % len( activity.children) self.Session.add(activity) self.Session.commit() return 'Added %s groups' % len(activity.children)
def update_many(self, item_ids, data): '''Given node ids and the group names to remove them from''' activity = Activity() activity.user_id = self.user_id activity.children = [] if data.has_key('add_groups'): activity.activity_type_id = self.Session\ .query(ActivityType)\ .filter(ActivityType.name == 'group')\ .filter(ActivityType.action == 'add')\ .first().id elif data.has_key('remove_groups'): activity.activity_type_id = self.Session\ .query(ActivityType)\ .filter(ActivityType.name == 'group')\ .filter(ActivityType.action == 'remove')\ .first().id elif data.has_key('add_nagios'): activity.activity_type_id = self.Session\ .query(ActivityType)\ .filter(ActivityType.name == 'nagios')\ .filter(ActivityType.action == 'enable')\ .first().id elif data.has_key('remove_nagios'): activity.activity_type_id = self.Session\ .query(ActivityType)\ .filter(ActivityType.name == 'nagios')\ .filter(ActivityType.action == 'disable')\ .first().id elif data.has_key('add_puppet'): activity.activity_type_id = self.Session.query(ActivityType)\ .filter(ActivityType.name == 'puppet')\ .filter(ActivityType.action == 'enable')\ .first().id elif data.has_key('remove_puppet'): activity.activity_type_id = self.Session.query(ActivityType)\ .filter(ActivityType.name == 'puppet')\ .filter(ActivityType.action == 'disable')\ .first().id else: return False if data.has_key('add_nagios') or data.has_key('remove_nagios'): if data.has_key('add_nagios'): nagios_status = True else: nagios_status = False changes = 0 for cur_node in self.Session.query(Node)\ .filter(Node.id.in_(item_ids))\ .all(): if not cur_node.nagios == nagios_status: changes += 1 cs = Changeset() cs.field = 'nagios' cs.old_value = str(cur_node.nagios) cs.new_value = str(nagios_status) cur_node.nagios = nagios_status self.Session.add(cur_node) sub_activity = Activity() sub_activity.user_id = activity.user_id sub_activity.activity_type_id = activity.activity_type_id sub_activity.link = cur_node.hostname sub_activity.changesets = [cs] self.Session.add(sub_activity) activity.children.append(sub_activity) self.Session.add(activity) self.Session.commit() elif data.has_key('add_puppet') or data.has_key('remove_puppet'): if data.has_key('add_puppet'): puppet_status = True else: puppet_status = False changes = 0 for cur_node in self.Session.query(Node)\ .filter(Node.id.in_(item_ids))\ .all(): if not cur_node.puppet == puppet_status: changes += 1 cs = Changeset() cs.field = 'puppet' cs.old_value = str(cur_node.puppet) cs.new_value = str(puppet_status) cur_node.puppet = puppet_status self.Session.add(cur_node) sub_activity = Activity() sub_activity.user_id = activity.user_id sub_activity.activity_type_id = activity.activity_type_id sub_activity.link = cur_node.hostname sub_activity.changesets = [cs] self.Session.add(sub_activity) activity.children.append(sub_activity) self.Session.add(activity) self.Session.commit() else: self.Session.add(activity) groups = self.Session.query(Group)\ .filter(Group.name.in_(data['groups']))\ .all() if not groups: return False changes = 0 for node in self.Session.query(Node)\ .filter(Node.id.in_(item_ids))\ .all(): old_groups = node.groups if data.has_key('add_groups'): node.groups = node.groups + \ [g for g in groups if g not in node.groups] elif data.has_key('remove_groups'): node.groups = [\ g for g in node.groups if g not in groups] test = list(set(node.groups).difference(set(old_groups))) if data.has_key('remove_groups'): test = not test if test: self.Session.add(node) self.Session.commit() cs = Changeset() cs.field = 'groups' cs.old_value = ', '.join(['%s' % g for g in old_groups]) cs.new_value = ', '.join(['%s' % g for g in node.groups]) self.Session.add(cs) self.Session.commit() sub_activity = Activity() sub_activity.user_id = activity.user_id sub_activity.activity_type_id = activity.activity_type_id sub_activity.link = node.hostname sub_activity.changesets = [cs] self.Session.add(sub_activity) activity.children.append(sub_activity) self.Session.add(activity) self.Session.commit() changes += 1 if changes == 0: return 'No changes were made' if data.has_key('add_groups'): activity.link = 'Group (Update): Added %s nodes to %s' % (changes, ', '.join(data['groups'])) else: activity.link = 'Group (Update): Removed %s nodes from %s' % (changes, ', '.join(data['groups'])) self.Session.add(activity) self.Session.commit() return activity.link[16:]
def update_many(self, item_ids, data): '''Given node ids and the group names to remove them from''' activity = Activity() activity.user_id = self.user_id activity.children = [] if data.has_key('add_groups'): activity.activity_type_id = self.Session\ .query(ActivityType)\ .filter(ActivityType.name == 'group')\ .filter(ActivityType.action == 'add')\ .first().id elif data.has_key('remove_groups'): activity.activity_type_id = self.Session\ .query(ActivityType)\ .filter(ActivityType.name == 'group')\ .filter(ActivityType.action == 'remove')\ .first().id elif data.has_key('add_nagios'): activity.activity_type_id = self.Session\ .query(ActivityType)\ .filter(ActivityType.name == 'nagios')\ .filter(ActivityType.action == 'enable')\ .first().id elif data.has_key('remove_nagios'): activity.activity_type_id = self.Session\ .query(ActivityType)\ .filter(ActivityType.name == 'nagios')\ .filter(ActivityType.action == 'disable')\ .first().id elif data.has_key('add_puppet'): activity.activity_type_id = self.Session.query(ActivityType)\ .filter(ActivityType.name == 'puppet')\ .filter(ActivityType.action == 'enable')\ .first().id elif data.has_key('remove_puppet'): activity.activity_type_id = self.Session.query(ActivityType)\ .filter(ActivityType.name == 'puppet')\ .filter(ActivityType.action == 'disable')\ .first().id else: return False if data.has_key('add_nagios') or data.has_key('remove_nagios'): if data.has_key('add_nagios'): nagios_status = True else: nagios_status = False changes = 0 for cur_node in self.Session.query(Node)\ .filter(Node.id.in_(item_ids))\ .all(): if not cur_node.nagios == nagios_status: changes += 1 cs = Changeset() cs.field = 'nagios' cs.old_value = str(cur_node.nagios) cs.new_value = str(nagios_status) cur_node.nagios = nagios_status self.Session.add(cur_node) sub_activity = Activity() sub_activity.user_id = activity.user_id sub_activity.activity_type_id = activity.activity_type_id sub_activity.link = cur_node.hostname sub_activity.changesets = [cs] self.Session.add(sub_activity) activity.children.append(sub_activity) self.Session.add(activity) self.Session.commit() elif data.has_key('add_puppet') or data.has_key('remove_puppet'): if data.has_key('add_puppet'): puppet_status = True else: puppet_status = False changes = 0 for cur_node in self.Session.query(Node)\ .filter(Node.id.in_(item_ids))\ .all(): if not cur_node.puppet == puppet_status: changes += 1 cs = Changeset() cs.field = 'puppet' cs.old_value = str(cur_node.puppet) cs.new_value = str(puppet_status) cur_node.puppet = puppet_status self.Session.add(cur_node) sub_activity = Activity() sub_activity.user_id = activity.user_id sub_activity.activity_type_id = activity.activity_type_id sub_activity.link = cur_node.hostname sub_activity.changesets = [cs] self.Session.add(sub_activity) activity.children.append(sub_activity) self.Session.add(activity) self.Session.commit() else: self.Session.add(activity) groups = self.Session.query(Group)\ .filter(Group.name.in_(data['groups']))\ .all() if not groups: return False changes = 0 for node in self.Session.query(Node)\ .filter(Node.id.in_(item_ids))\ .all(): old_groups = node.groups if data.has_key('add_groups'): node.groups = node.groups + \ [g for g in groups if g not in node.groups] elif data.has_key('remove_groups'): node.groups = [\ g for g in node.groups if g not in groups] test = list(set(node.groups).difference(set(old_groups))) if data.has_key('remove_groups'): test = not test if test: self.Session.add(node) self.Session.commit() cs = Changeset() cs.field = 'groups' cs.old_value = ', '.join( ['%s' % g for g in old_groups]) cs.new_value = ', '.join( ['%s' % g for g in node.groups]) self.Session.add(cs) self.Session.commit() sub_activity = Activity() sub_activity.user_id = activity.user_id sub_activity.activity_type_id = activity.activity_type_id sub_activity.link = node.hostname sub_activity.changesets = [cs] self.Session.add(sub_activity) activity.children.append(sub_activity) self.Session.add(activity) self.Session.commit() changes += 1 if changes == 0: return 'No changes were made' if data.has_key('add_groups'): activity.link = 'Group (Update): Added %s nodes to %s' % ( changes, ', '.join(data['groups'])) else: activity.link = 'Group (Update): Removed %s nodes from %s' % ( changes, ', '.join(data['groups'])) self.Session.add(activity) self.Session.commit() return activity.link[16:]