码迷,mamicode.com
首页 > 数据库 > 详细

cmdb资产管理系统server端资产新增和修改(内存和网卡)

时间:2017-10-05 23:37:40      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:drs   mem   cts   []   ipa   div   int   修改   serve   

内存相关代码

from repository import models

class Memory(object):
    def __init__(self,server_obj,info):
        self.server_obj=server_obj
        self.memory_dict=info
    def process(self):
        # 更新内存信息
        new_memory_info_dict = self.memory_dict[‘data‘]
        old_memory_info_list = self.server_obj.memory.all()
        new_memory_slot_set = set(new_memory_info_dict.keys())
        old_memory_slot_set = {obj.slot for obj in old_memory_info_list}
        add_slot_list = new_memory_slot_set.difference(old_memory_slot_set)
        del_slot_list = old_memory_slot_set.difference(new_memory_slot_set)
        update_slot = old_memory_slot_set.intersection(new_memory_slot_set)
        # 新增内存
        memory_record_list = []
        for slot in add_slot_list:
            value = new_memory_info_dict[slot]
            value[‘server_obj‘] = self.server_obj
            record = "添加内存……"
            memory_record_list.append(record)
            models.Memory.objects.create(**value)
        # 删除内存
        models.Memory.objects.filter(server_obj=self.server_obj, slot__in=del_slot_list).delete()
        # 更新内存
        for slot in update_slot:
            value = new_memory_info_dict[slot]
            obj = models.Memory.objects.filter(server_obj=self.server_obj, slot=slot)
            for k, new_v in value.items():
                old_v = getattr(obj, k, )
                if old_v != new_v:
                    setattr(obj, k, new_v)
            obj.save()

网卡相关代码

网卡配置与其他的硬件不同,value里没有槽位信息,直接**value信息录入不全,我把slot当做网卡的name,新增网卡对象时一一创建每个字段

from repository import models

class Nic(object):
    def __init__(self,server_obj,info):
        self.server_obj=server_obj
        self.nic_dict=info
    def process(self):
        # 更新网卡信息
        new_nic_info_dict = self.nic_dict[‘data‘]
        old_nic_info_list = self.server_obj.nic.all()
        new_nic_slot_set = set(new_nic_info_dict.keys())
        old_nic_slot_set = {obj.slot for obj in old_nic_info_list}
        add_slot_list = new_nic_slot_set.difference(old_nic_slot_set)
        del_slot_list = old_nic_slot_set.difference(new_nic_slot_set)
        update_slot = old_nic_slot_set.intersection(new_nic_slot_set)
        # 新增网卡
        nic_record_list = []
        for slot in add_slot_list:
            value = new_nic_info_dict[slot]
            # print(value)
            value[‘server_obj‘] = self.server_obj
            record = "添加网卡……"
            nic_record_list.append(record)
            models.NIC.objects.create(
                name=slot,
                hwaddr=value[‘hwaddr‘],
                netmask=value[‘netmask‘],
                ipaddrs=value[‘ipaddrs‘],
                up=value[‘up‘],
                server_obj=value[‘server_obj‘]
            )
        # 删除网卡
        models.NIC.objects.filter(server_obj=self.server_obj, slot__in=del_slot_list).delete()
        # 更新网卡
        for slot in update_slot:
            value = new_nic_info_dict[slot]
            obj = models.NIC.objects.filter(server_obj=self.server_obj, slot=slot)
            for k, new_v in value.items():
                old_v = getattr(obj, k, )
                if old_v != new_v:
                    setattr(obj, k, new_v)
            obj.save()

  

cmdb资产管理系统server端资产新增和修改(内存和网卡)

标签:drs   mem   cts   []   ipa   div   int   修改   serve   

原文地址:http://www.cnblogs.com/xiaohema/p/7630225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!