Memory-related code
From repository import Modelsclass Memory (object): Def __init__ (self,server_obj,info): Self.server_obj=server_ob J Self.memory_dict=info def process (self): # update memory Information 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.differ ence (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) # new Memory Memory_record_list = [] for slots in add_ Slot_list:value = New_memory_info_dict[slot] value[' server_obj '] = self.server_obj Recor D = "Add Memory ..." memory_record_list.append (record) models. Memory.objects.create (**value) # Delete memory models. Memory.objeCts.filter (Server_obj=self.server_obj, slot__in=del_slot_list). Delete () # Update memory for slots 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 ()
Network card related code
Network card configuration and other hardware, the value does not have slot information, direct **value information input is not complete, I take the slot as the name of the network card, add the network card object to create each field
From repository import Modelsclass Nic (object): Def __init__ (self,server_obj,info): Self.server_obj=server_obj Self.nic_dict=info def process (self): # update nic information 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) # new Nic nic_record_list = [] for slot in add_slot_list:value = New_nic_info_dict[slo T] # Print (value) value[' server_obj ' = self.server_obj record = "Add Nic ..." 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 ') # Remove the NIC models. NIC.objects.filter (Server_obj=self.server_obj, slot__in=del_slot_list). Delete () # Update NIC for slots in UPDATE_SL Ot: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 Asset Management System server-side asset additions and modifications (memory and NIC)