码迷,mamicode.com
首页 > 其他好文 > 详细

国庆假第一天,补充服务器资产管理server端网卡Memory插件代码

时间:2017-09-30 21:49:54      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:def   key   host   create   inf   __init__   getattr   slot   移除   

from asset import models
class Memory(object):
def __init__(self,server_obj,info):
self.server_obj = server_obj
self.memory_dict = info

def process(self):
"""
存在内存增加,删除,更新3中可能
:return:
"""
if not self.memory_dict[‘status‘]:
record_content = self.memory_dict[‘msg‘]
models.ServerRecord.objects.create(self.server_obj.hostname, content=record_content)
return False

new_memory_info_dict = self.memory_dict[‘data‘]
new_memory_obj_queryset = self.server_obj.dic_set.all()
new_memory_slot_set = set(new_memory_info_dict.keys())
old_memory_slot_set = {obj.slot for obj in new_memory_obj_queryset}

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_list = new_memory_slot_set.intersection(old_memory_slot_set)

record_list = []

# 增加
if add_slot_list:
for slot in add_slot_list:
new_memory_info = new_memory_info_dict[slot]
add_record = ‘[%s]的在卡槽[%s]添加了内存‘ % (self.server_obj.hostname,slot)
record_list.append(add_record)
new_memory_info[‘server_obj‘] = self.server_obj
models.Memory.objects.create(**new_memory_info)

# 删除
if del_slot_list:
for slot in del_slot_list:
del_record = ‘[%s]的卡槽[%s]移除了内存‘ % (self.server_obj.hostname, slot)
record_list.append(del_record)
models.Memory.objects.filter(server_obj=self.server_obj, slot__in=del_slot_list).delete()

# 更新
if update_slot_list:
for slot in update_slot_list:
new_memory_info = new_memory_info_dict[slot]
update_memory_obj = models.Memory.objects.get(server_obj=self.server_obj, slot=slot)
for k, v in new_memory_info:
if v != getattr(update_memory_obj, k):
update_record = ‘[%s]内存卡槽[%s]的[%s]由[%s]变为[%s]‘ % (
self.server_obj.hostname, slot, k, getattr(update_memory_obj, k), v)
record_list.append(update_record)
setattr(update_memory_obj, k, v)
update_memory_obj.save()

if record_list:
models.ServerRecord.objects.create(server_obj=self.server_obj, content=‘;‘.join(record_list))

国庆假第一天,补充服务器资产管理server端网卡Memory插件代码

标签:def   key   host   create   inf   __init__   getattr   slot   移除   

原文地址:http://www.cnblogs.com/ALXPS/p/7616149.html

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