def create(self, data): node = Node() changes = [] for k, v in data.iteritems(): setattr(node, k, v) cs = Changeset() cs.field = k cs.old_value = v self.Session.add(cs) changes.append(cs) self.Session.add(node) 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.link = 'Node (Added): %s' % node.hostname activity.changesets = changes self.Session.add(activity) self.Session.commit() return { "status": "Added %s" % node.hostname, "info": { "id": node.id } }
def node_insert_db(node_data): schema, data = node_data # Setup schema to match attributes for i, key in enumerate(schema): for word, replacement in node_replacements: if key == word: schema[i] = replacement break else: schema[i] = None for item in data: node = Node() zipped = zip(schema, item) for key, value in zipped: if key: node.__setattr__(key, value) Session.add(node) Session.commit()
def update_xen_nodes(xen_list): for item in xen_list: node = Session.query(Node).filter( Node.hostname == item.hostname).first() if not node: node = Node() node.hostname = item.hostname node.memory = item.mem node.xen_instance = item.id node.location = item.location node.rack = item.rack node.rack_u = item.rack_u Session.add(node) Session.commit() return True
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)