def service_list(request, format=None): """ List all order, or create a server assets order. """ if request.method == 'GET': snippets = Service_Assets.objects.all() serializer = serializers.ServiceSerializer(snippets, many=True) return Response(serializer.data) elif request.method == 'POST': del request.data['project_name'] try: service = Service_Assets.objects.create(**request.data) except Exception, ex: return Response({"msg": str(ex)}, status=status.HTTP_400_BAD_REQUEST) try: snippet = Service_Assets.objects.get(id=service.id) serializer = serializers.ServiceSerializer(snippet) recordAssets.delay( user=str(request.user), content="添加业务类型名称:{service_name}".format( service_name=request.data.get("service_name")), type="service", id=serializer.data.get('id')) except Exception, ex: logger.error(msg="添加service失败: {ex}".format(ex=str(ex))) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def project_detail(request, id, format=None): try: snippet = Project_Assets.objects.get(id=id) except Project_Assets.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = serializers.ProjectSerializer(snippet) return Response(serializer.data) elif request.method == 'PUT' and request.user.has_perm( 'OMBA.can_change_project_assets'): serializer = serializers.ProjectSerializer(snippet, data=request.data) old_name = snippet.project_name if serializer.is_valid(): serializer.save() recordAssets.delay( user=str(request.user), content="修改项目为:{old_name} -> {project_name}".format( old_name=old_name, project_name=request.data.get("project_name")), type="project", id=id) return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE' and request.user.has_perm( 'OMBA.can_delete_rroject_assets'): if not request.user.has_perm('OMBA.can_delete_rroject_Assets'): return Response(status=status.HTTP_403_FORBIDDEN) snippet.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def asset_detail(request, id, format=None): """ Retrieve, update or delete a server assets instance. """ try: snippet = Assets.objects.get(id=id) except Assets.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = serializers.AssetsSerializer(snippet) return Response(serializer.data) elif request.method == 'PUT': serializer = serializers.AssetsSerializer(snippet, data=request.data) if serializer.is_valid(): serializer.save() recordAssets.delay(user=str(request.user), content="更新资产:{name}".format(name=snippet.name), type="assets", id=id) return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': if not request.user.has_perm('OMBA.delete_asset_assets'): return Response(status=status.HTTP_403_FORBIDDEN) snippet.delete() recordAssets.delay(user=str(request.user), content="删除资产:{name}".format(name=snippet.name), type="assets", id=id) return Response(status=status.HTTP_204_NO_CONTENT)
def asset_server_detail(request, id, format=None): """ Retrieve, update or delete a server assets instance. """ try: snippet = Server_Assets.objects.get(id=id) except Server_Assets.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = serializers.ServerSerializer(snippet) return Response(serializer.data) elif request.method == 'PUT': '''如果更新字段包含assets则先更新总资产表''' if (request.data.get('data')): data = request.data.get('data') else: data = request.data if (data.get('assets')): assets_data = data.pop('assets') try: assets_snippet = Assets.objects.get(id=snippet.assets.id) assets = serializers.AssetsSerializer(assets_snippet, data=assets_data) except Assets.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if assets.is_valid(): assets.save() recordAssets.delay( user=str(request.user), content="修改服务器资产:{ip}".format(ip=snippet.ip), type="server", id=id) serializer = serializers.ServerSerializer(snippet, data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': if not request.user.has_perm('OMBA.can_delete_server_assets'): return Response(status=status.HTTP_403_FORBIDDEN) snippet.delete() try: assets_snippet = Assets.objects.get(id=snippet.assets.id) assets_snippet.delete() recordAssets.delay(user=str(request.user), content="删除服务器资产:{ip}".format(ip=snippet.ip), type="server", id=id) except Assets.DoesNotExist: pass return Response(status=status.HTTP_204_NO_CONTENT)
def project_list(request, format=None): if request.method == 'GET': snippets = Project_Assets.objects.all() serializer = serializers.ProjectSerializer(snippets, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = serializers.ProjectSerializer(data=request.data) if serializer.is_valid(): serializer.save() recordAssets.delay( user=str(request.user), content="添加项目名称:{project_name}".format( project_name=request.data.get("project_name")), type="project", id=serializer.data.get('id')) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def asset_list(request, format=None): """ List all order, or create a server assets order. """ if request.method == 'GET': snippets = Assets.objects.all() serializer = serializers.AssetsSerializer(snippets, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = serializers.AssetsSerializer(data=request.data) if serializer.is_valid(): serializer.save() recordAssets.delay( user=str(request.user), content="添加资产:{name}".format(name=request.data.get("name")), type="assets", id=serializer.data.get('id')) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def service_detail(request, id, format=None): """ Retrieve, update or delete a server assets instance. """ try: snippet = Service_Assets.objects.get(id=id) except Service_Assets.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = serializers.ServiceSerializer(snippet) return Response(serializer.data) elif request.method == 'PUT': serializer = serializers.ServiceSerializer(snippet, data=request.data) old_name = snippet.service_name if serializer.is_valid(): serializer.save() recordAssets.delay( user=str(request.user), content="修改业务类型为:{old_name} -> {service_name}".format( old_name=old_name, service_name=request.data.get("service_name")), type="service", id=id) return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE' and request.user.has_perm( 'OMBA.can_delete_assets'): if not request.user.has_perm('OMBA.can_delete_service_assets'): return Response(status=status.HTTP_403_FORBIDDEN) snippet.delete() recordAssets.delay(user=str(request.user), content="删除业务类型:{service_name}".format( service_name=snippet.service_name), type="service", id=id) return Response(status=status.HTTP_204_NO_CONTENT)
def asset_net_list(request, format=None): """ List all order, or create a new net assets. """ if request.method == 'GET': snippets = Network_Assets.objects.all() serializer = serializers.NetworkSerializer(snippets, many=True) return Response(serializer.data) elif request.method == 'POST': if (request.data.get('data')): data = request.data.get('data') else: data = request.data serializer = serializers.NetworkSerializer(data=data) if serializer.is_valid(): serializer.save() recordAssets.delay( user=str(request.user), content="添加网络设备资产:{ip}".format(ip=data.get("ip")), type="net", id=serializer.data.get('id')) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def group_list(request, format=None): """ List all order, or create a server assets order. """ if not request.user.has_perm('OMBA.read_group'): return Response(status=status.HTTP_403_FORBIDDEN) if request.method == 'GET': snippets = Group.objects.all() serializer = serializers.GroupSerializer(snippets, many=True) return Response(serializer.data) elif request.method == 'POST': if not request.user.has_perm('OMBA.change_group'): return Response(status=status.HTTP_403_FORBIDDEN) serializer = serializers.GroupSerializer(data=request.data) if serializer.is_valid(): serializer.save() recordAssets.delay(user=str(request.user), content="添加用户组:{group_name}".format( group_name=request.data.get("name")), type="group", id=serializer.data.get('id')) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def group_detail(request, id, format=None): """ Retrieve, update or delete a server assets instance. """ try: snippet = Group.objects.get(id=id) except Group.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = serializers.GroupSerializer(snippet) return Response(serializer.data) elif request.method == 'PUT': serializer = serializers.GroupSerializer(snippet, data=request.data) old_name = snippet.name if serializer.is_valid(): serializer.save() recordAssets.delay( user=str(request.user), content="修改用户组名称:{old_name} -> {group_name}".format( old_name=old_name, group_name=request.data.get("name")), type="group", id=id) return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': if not request.user.has_perm('OMBA.delete_group'): return Response(status=status.HTTP_403_FORBIDDEN) snippet.delete() recordAssets.delay( user=str(request.user), content="删除用户组:{group_name}".format(group_name=snippet.name), type="group", id=id) return Response(status=status.HTTP_204_NO_CONTENT)
def assets_facts(request, args=None): if request.method == "POST" and request.user.has_perm( 'OMBA.change_server_assets'): server_id = request.POST.get('server_id') genre = request.POST.get('type') if genre == 'setup': try: server_assets = Server_Assets.objects.get( id=request.POST.get('server_id')) if server_assets.keyfile == 1: resource = [{ "hostname": server_assets.ip, "port": int(server_assets.port), "username": server_assets.username }] else: resource = [{ "hostname": server_assets.ip, "port": server_assets.port, "username": server_assets.username, "password": server_assets.passwd }] except Exception, ex: logger.error(msg="更新资产失败: {ex}".format(ex=str(ex))) return JsonResponse({'msg': "数据更新失败-查询不到该主机资料", "code": 502}) ANS = ANSRunner(resource) ANS.run_model(host_list=[server_assets.ip], module_name='setup', module_args="") data = ANS.handle_cmdb_data(ANS.get_model_result()) if data: for ds in data: status = ds.get('status') if status == 0: try: assets = Assets.objects.get( id=server_assets.assets_id) Assets.objects.filter( id=server_assets.assets_id).update( sn=ds.get('serial'), model=ds.get('model'), manufacturer=ds.get('manufacturer')) except Exception, ex: logger.error(msg="获取服务器信息失败: {ex}".format( ex=str(ex))) return JsonResponse({ 'msg': "数据更新失败-查询不到该主机的资产信息", "code": 403 }) try: Server_Assets.objects.filter(id=server_id).update( cpu_number=ds.get('cpu_number'), kernel=ds.get('kernel'), selinux=ds.get('selinux'), hostname=ds.get('hostname'), system=ds.get('system'), cpu=ds.get('cpu'), disk_total=ds.get('disk_total'), cpu_core=ds.get('cpu_core'), swap=ds.get('swap'), ram_total=ds.get('ram_total'), vcpu_number=ds.get('vcpu_number')) recordAssets.delay(user=str(request.user), content="修改服务器资产:{ip}".format( ip=server_assets.ip), type="server", id=server_assets.id) except Exception, ex: logger.error(msg="更新服务器信息失败: {ex}".format( ex=str(ex))) return JsonResponse({ 'msg': "数据更新失败-写入数据失败", "code": 400 }) for nk in ds.get('nks'): macaddress = nk.get('macaddress') count = NetworkCard_Assets.objects.filter( assets=assets, macaddress=macaddress).count() if count > 0: try: NetworkCard_Assets.objects.filter( assets=assets, macaddress=macaddress).update( assets=assets, device=nk.get('device'), ip=nk.get('address'), module=nk.get('module'), mtu=nk.get('mtu'), active=nk.get('active')) except Exception, ex: logger.warn(msg="更新服务器网卡信息失败: {ex}".format( ex=str(ex))) else: try: NetworkCard_Assets.objects.create( assets=assets, device=nk.get('device'), macaddress=nk.get('macaddress'), ip=nk.get('address'), module=nk.get('module'), mtu=nk.get('mtu'), active=nk.get('active')) except Exception, ex: logger.warn(msg="写入服务器网卡信息失败: {ex}".format( ex=str(ex)))
return JsonResponse({ 'msg': "数据更新失败-写入数据失败", "code": 400 }) else: try: Ram_Assets.objects.create( device_slot=mem.get('slot'), device_model=mem.get('serial'), device_brand=mem.get('manufacturer'), device_volume=mem.get('size'), device_status="Online", assets=assets) recordAssets.delay( user=str(request.user), content="修改服务器资产:{ip}".format( ip=server_assets.ip), type="server", id=server_assets.id) except Exception, e: return JsonResponse({ 'msg': "数据更新失败-写入数据失败", "code": 400 }) if ds.get('disk_info'): for disk in ds.get('disk_info'): if Disk_Assets.objects.filter( assets=assets, device_slot=disk.get('slot')).count() > 0: try: Disk_Assets.objects.filter( assets=assets,