CMDB服务器管理系统【s5day90】:创建资产更新服务器硬盘信息

1、创建硬件资产信息

import json
from django.shortcuts import render,HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.conf import settings
from repository import models
from .plugins import PluginManger

@csrf_exempt
def server(request):
    # 客户端提交的最新资产数据
    server_dict = json.loads(request.body.decode('utf-8'))

    # 检查server表中是否有当前资产信息【主机名是唯一标识】
    if not server_dict['basic']['status']:
        return HttpResponse('臣妾做不到')
		
hostname = server_dict['basic']['data']['hostname']
server_obj = models.Server.objects.filter(hostname=hostname).first()

	if not server_obj:
		#创建服务器信息
		tmp = {}
		tmp.update(server_dict['basic']['data'])
		tmp.update(server_dict['board']['data'])
		server_dict = models.Server.object.create(**tmp)
		#网卡、内存、硬盘...
		
		#硬盘
		disk_info_dict = server_dict['disk']['data']
		for item in disk_info_dict.values():
			item['server_obj'] = server_obj
			#item['server_obj'] = server_obj
			models.Disk.object.create(**item)
			
		#内存	
		mem_info_dict = server_dict['memory']['data']
		for item in mem_info_dict.values():
			item['server_obj']= server_obj
			models.Memory.object.create(**item)
		 
		#网卡	
		nic_info_dict = server_dict['nic']['data']
		for k,v in nic_info_dict.values():
			v['server_obj'] = server_obj
			v['name'] = k
			models.NIC.object.create(**v)	
		else:	

2、更新服务器信息

#更新server表	
		tmp = {}	
		tmp.update(server_dict['basic']['data'])	
		tmp.update(server_dict['board']['data'])	
			
		#服务器数据更新	
		tmp.pop('hostname')	
		record_list = []	
		for k,new_val in tmp.item():	
			old_val != new_val:	
			if old_val!= new_val:	
				record = "[%s]的[%s]由[%s]变更为[%s]" % (hostname,k,old_val.new_val)	
				record_list.append(record)	
				setattr(server_obj,k,new_val)	
		server_obj.save()	
		if record_list:	
			models.ServerRecord.object.create(server_obj=server_obj,content=';'.join(record_list))
			

3、更新硬盘信息

class Disk(object):
    def __init__(self,server_obj,info):
        self.server_obj = server_obj
        self.disk_dict = info

    def process(self):
        # 硬盘、网卡和内存
        new_disk_info_dict = self.disk_dict['data']
        """
        {
            '0': {'slot': '0', 'pd_type': 'SAS', 'capacity': '279.396', 'model': 'SEAGATE ST300MM0006     LS08S0K2B5NV'},
            '1': {'slot': '1', 'pd_type': 'SAS', 'capacity': '279.396', 'model': 'SEAGATE ST300MM0006     LS08S0K2B5AH'},
            '2': {'slot': '2', 'pd_type': 'SATA', 'capacity': '476.939', 'model': 'S1SZNSAFA01085L     Samsung SSD 850 PRO 512GB               EXM01B6Q'},
            '3': {'slot': '3', 'pd_type': 'SATA', 'capacity': '476.939', 'model': 'S1AXNSAF912433K     Samsung SSD 840 PRO Series              DXM06B0Q'},
            '4': {'slot': '4', 'pd_type': 'SATA', 'capacity': '476.939', 'model': 'S1AXNSAF303909M     Samsung SSD 840 PRO Series              DXM05B0Q'},
            '5': {'slot': '5', 'pd_type': 'SATA', 'capacity': '476.939', 'model': 'S1AXNSAFB00549A     Samsung SSD 840 PRO Series
        }"""
        new_disk_info_list = self.server_obj.disk.all()
        """
        [
            obj,
            obj,
            obj,
        ]
        """
        new_disk_slot_set = set(new_disk_info_dict.keys())
        old_disk_slot_set = {obj.slot for obj in new_disk_info_list}

        # add_slot_list = new_disk_slot_set - old_disk_slot_set
        add_slot_list = new_disk_slot_set.difference(old_disk_slot_set)
        del_slot_list = old_disk_slot_set.difference(new_disk_slot_set)
        update_slot_list = old_disk_slot_set.intersection(new_disk_slot_set)

        add_record_list = []
        # 增加 [2,5]
        for slot in add_slot_list:
            value = new_disk_info_dict[slot]
            tmp = "添加硬盘..."
            add_record_list.append(tmp)
            value['server_obj'] = self.server_obj
            models.Disk.objects.create(**value)
        # 删除 [4,6]
        models.Disk.objects.filter(server_obj=self.server_obj, slot__in=del_slot_list).delete()

        # 更新 [7,8]
        for slot in update_slot_list:
            value = new_disk_info_dict[
                slot]  # {'slot': '0', 'pd_type': 'SAS', 'capacity': '279.396', 'model': 'SEAGATE ST300MM0006     LS08S0K2B5NV'}
            obj = models.Disk.objects.filter(server_obj=self.server_obj, slot=slot).first()
            for k, new_val in value.items():
                old_val = getattr(obj, k)
                if old_val != new_val:
                    setattr(obj, k, new_val)
            obj.save()

    def add_disk(self):
        pass

    def del_disk(self):
        pass

    def update_disk(self):
        pass

  

posted @ 2018-07-29 12:40  活的潇洒80  阅读(351)  评论(0编辑  收藏  举报