#!/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() returnJsonResponse (the Response. __Dict__ ) # @method_decorator (auth.api_auth) # DEF POST (Self, Request, * args, ** kwargs): # "" " # update or add asset information # : param Request: # : param args: # : param kwargs: # : return: successful 1000; 1001 interfaces authorization failed; asset does not exist in the database 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] update complete'% hostname} # # SERVER_INFO Update on servers all information # # # The host name to the database obtaining information # server_obj = models.Server.objects.filter (hostname = hostname) .select_related ( 'Asset') First (). # IF Not server_obj: # RET [ 'code'] = 1002 # RET [ 'message' ] = '[% s] does not exist assets'% hostname # return jsonResponse (RET) # # for K, V in config.PLUGINS_DICT.items (): # The 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 successfully; authorization failed interfaces 1001; 1002 asset does not exist in the database "" " 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] update complete ' % hostname} # a SERVER_INFO latest report server all the information # Get the database according to the host name information server_obj = models.Server.objects.filter (hostname = hostname) .select_related ( ' Asset ' ) .first () IF Not server_obj: RET [ ' code ' ] = 1002 RET [ ' Message ' ] = ' [% S] asset does not exist ' % hostname return jsonResponse (RET) # ======== "server_obj server object; SERVER_INFO" ========== # hard disk or card or memory # hard: CRUD # 1. server_obj hard negative association table, obtaining information database in the hard disc # [ # { 'Slot': "#. 1", 'size': '100'}, # { 'slot': "# 2", 'size': '60'}, # { 'slot': "#. 3" , 'size': '88'}, # ] # old_list = [ '#. 1', '# 2', '#. 3'] # 2. SERVER_INFO [ 'Disk'] reported a new hard disk data # { # "# . 1 ": { 'slot':" #. 1 ", 'size': '90'}, # " #. 4 ": { 'slot':" #. 4 ", 'size': '40'}, # } # = new_list The [ '# 1', '# 4'] # 3. update [ '# 1'] delete [ '# 2', '# 3'] increase [ '# 4'] #4. # 增加 ['#4'] """ for i in ['#4']: data_dict DIC = [I] models.Diks.objces.create(**data_dict) """ return JsonResponse (right)
The CMDB project lasting asset flow
Guess you like
Origin www.cnblogs.com/jintian/p/11297353.html
Recommended
Ranking