1. 程式人生 > >cmdb資產管理系統server端資產新增和修改(內存和網卡)

cmdb資產管理系統server端資產新增和修改(內存和網卡)

att 相關 all creat 對象 clas eat pad init

內存相關代碼

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端資產新增和修改(內存和網卡)