码迷,mamicode.com
首页 > 数据库 > 详细

CMDB项目之持久化资产流程

时间:2019-08-04 10:38:54      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:views   time   host   更新   last   one   load   set   *args   

技术图片
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import json
import importlib
from django.views import View
from django.http import JsonResponse
from django.shortcuts import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator

from utils import auth
from api import config
from repository import models
from api.service import asset

@auth.api_auth
def asset(request):
    pass


class AssetView(View):
    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        return super(AssetView, self).dispatch(request, *args, **kwargs)

    @method_decorator(auth.api_auth)
    def get(self, request, *args, **kwargs):
        """
        获取今日未更新的资产 - 适用SSH或Salt客户端
        :param request:
        :param args:
        :param kwargs:
        :return:
        """

        # test = {‘user‘: ‘用户名‘, ‘pwd‘: ‘密码‘}
        # result = json.dumps(test,ensure_ascii=True)
        # result = json.dumps(test,ensure_ascii=False)
        # return HttpResponse(result)

        # test = {‘user‘: ‘用户名‘, ‘pwd‘: ‘密码‘}
        # result = json.dumps(test,ensure_ascii=True)
        # result = json.dumps(test,ensure_ascii=False)
        # return HttpResponse(result,content_type=‘application/json‘)

        # test = {‘user‘: ‘用户名‘, ‘pwd‘: ‘密码‘}
        # return JsonResponse(test, json_dumps_params={"ensure_ascii": False})

        response = asset.get_untreated_servers()
        return JsonResponse(response.__dict__)

    # @method_decorator(auth.api_auth)
    # def post(self, request, *args, **kwargs):
    #     """
    #     更新或者添加资产信息
    #     :param request:
    #     :param args:
    #     :param kwargs:
    #     :return: 1000 成功;1001 接口授权失败;1002 数据库中资产不存在
    #     """
    #
    #     server_info = json.loads(request.body.decode(‘utf-8‘))
    #     server_info = json.loads(server_info)
    #     # ret = {‘code‘: 1000, ‘message‘: ‘‘}
    #     # print(server_info)
    #     hostname = server_info[‘hostname‘]
    #
    #     ret = {‘code‘: 1000, ‘message‘: ‘[%s]更新完成‘ % hostname}
    #     # server_info 最新汇报服务器所有信息
    #
    #     # 根据主机名去数据库中获取相关信息
    #     server_obj = models.Server.objects.filter(hostname=hostname).select_related(‘asset‘).first()
    #     if not server_obj:
    #         ret[‘code‘] = 1002
    #         ret[‘message‘] = ‘[%s]资产不存在‘ % hostname
    #         return JsonResponse(ret)
    #
    #     for k, v in config.PLUGINS_DICT.items():
    #         module_path, cls_name = v.rsplit(‘.‘, 1)
    #         cls = getattr(importlib.import_module(module_path), cls_name)
    #         response = cls.process(server_obj, server_info, None)
    #         if not response.status:
    #             ret[‘code‘] = 1003
    #             ret[‘message‘] = "[%s]资产更新异常" % hostname
    #         if hasattr(cls, ‘update_last_time‘):
    #             cls.update_last_time(server_obj, None)
    #
    #     return JsonResponse(ret)

    @method_decorator(auth.api_auth)
    def post(self, request, *args, **kwargs):
        """
        更新或者添加资产信息
        :param request:
        :param args:
        :param kwargs:
        :return: 1000 成功;1001 接口授权失败;1002 数据库中资产不存在
        """

        server_info = json.loads(request.body.decode(utf-8))
        server_info = json.loads(server_info)
        # ret = {‘code‘: 1000, ‘message‘: ‘‘}
        # print(server_info)
        hostname = server_info[hostname]

        ret = {code: 1000, message: [%s]更新完成 % hostname}
        # server_info 最新汇报服务器所有信息

        # 根据主机名去数据库中获取相关信息
        server_obj = models.Server.objects.filter(hostname=hostname).select_related(asset).first()
        if not server_obj:
            ret[code] = 1002
            ret[message] = [%s]资产不存在 % hostname
            return JsonResponse(ret)

        # ========》 server_obj服务器对象 ;server_info  《==========
        # 硬盘 或 网卡 或 内存
        # 硬盘:增删改
        # 1. server_obj反向关联硬盘表,获取数据库中硬盘信息
        # [
        #     {‘slot‘: "#1", ‘size‘: ‘100‘},
        #     {‘slot‘: "#2", ‘size‘: ‘60‘},
        #     {‘slot‘: "#3", ‘size‘: ‘88‘},
        # ]
        # old_list = [‘#1‘,‘#2‘,‘#3‘]
        # 2. server_info[‘disk‘] 新汇报的硬盘数据
        # {
        #     "#1":{‘slot‘: "#1", ‘size‘: ‘90‘},
        #     "#4":{‘slot‘: "#4", ‘size‘: ‘40‘},
        # }
        # new_list = [‘#1‘,‘#4‘]
        #3. 更新[‘#1‘] 删除[‘#2‘,‘#3‘] 增加 [‘#4‘]

        #4. # 增加 [‘#4‘]
        """
            for i in  [‘#4‘]:
                data_dict = dic[i]
                models.Diks.objces.create(**data_dict)


       """




        return JsonResponse(ret)
View Code

技术图片技术图片技术图片技术图片技术图片

CMDB项目之持久化资产流程

标签:views   time   host   更新   last   one   load   set   *args   

原文地址:https://www.cnblogs.com/jintian/p/11297353.html

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