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

内存相关代码

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()

  

posted @ 2017-10-05 22:54  小河马的博客  阅读(411)  评论(0编辑  收藏  举报