Example #1
0
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 = GroupSerializer(snippet)
        return Response(serializer.data)
 
    elif request.method == 'PUT':
        serializer = 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('Opsmanage.delete_group'):  
            return Response(status=status.HTTP_403_FORBIDDEN)
        snippet.delete()
        recordAssets.delay(user=str(request.user),content="删除用户组:{group_name}".format(group_name=snippet.group_name),type="group",id=id)        
        return Response(status=status.HTTP_204_NO_CONTENT)     
Example #2
0
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 = AssetsSerializer(snippet)
        return Response(serializer.data)
 
    elif request.method == 'PUT':
        serializer = 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('OpsManage.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) 
Example #3
0
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 = ServiceSerializer(snippet)
        return Response(serializer.data)
 
    elif request.method == 'PUT':
        serializer = 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('OpsManage.can_delete_assets'):
        if not request.user.has_perm('OpsManage.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)   
Example #4
0
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 = AssetsSerializer(snippet)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = 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('OpsManage.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)
Example #5
0
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 = 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 = 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 = 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('OpsManage.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)
Example #6
0
def asset_list(request,format=None):
    """
    List all order, or create a server assets order.
    """
    if request.method == 'GET':      
        snippets = Assets.objects.all()
        serializer = AssetsSerializer(snippets, many=True)
        return Response(serializer.data)     
    elif request.method == 'POST':
        serializer = 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)    
Example #7
0
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 = 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 = 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 = 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('OpsManage.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)         
Example #8
0
def group_list(request,format=None):
    """
    List all order, or create a server assets order.
    """
    if not  request.user.has_perm('Opsmanage.read_group'):
        return Response(status=status.HTTP_403_FORBIDDEN)     
    if request.method == 'GET':      
        snippets = Group.objects.all()
        serializer = GroupSerializer(snippets, many=True)
        return Response(serializer.data)     
    elif request.method == 'POST':
        if not  request.user.has_perm('Opsmanage.change_group'):
            return Response(status=status.HTTP_403_FORBIDDEN)         
        serializer = 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)
Example #9
0
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 = 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 = 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) 
Example #10
0
def asset_list(request, format=None):
    """
    List all order, or create a server assets order.
    """
    if request.method == 'GET':
        snippets = Assets.objects.all()
        serializer = AssetsSerializer(snippets, many=True)
        return Response(serializer.data)
    elif request.method == 'POST':
        serializer = 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)
Example #11
0
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 = ServiceSerializer(snippet)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = 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(
            'OpsManage.can_delete_assets'):
        if not request.user.has_perm('OpsManage.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)
Example #12
0
def assets_facts(request,args=None):
    if request.method == "POST" and request.user.has_perm('OpsManage.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)}] 
                else:resource = [{"hostname": server_assets.ip, "port": server_assets.port,"username": server_assets.username, "password": server_assets.passwd}]
            except Exception,e:
                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.objects.filter(id=server_assets.assets_id).update(sn=ds.get('serial'),model=ds.get('model'),
                                                                                     manufacturer=ds.get('manufacturer'))
                        except Exception,e:
                            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,e:
                            print e
                            return JsonResponse({'msg':"数据更新失败-写入数据失败","code":400})
                    else:
                        return JsonResponse({'msg':"数据更新失败-无法链接主机~","code":502})                    
                return JsonResponse({'msg':"数据更新成功","code":200})
Example #13
0
def assets_facts(request,args=None):
    if request.method == "POST" and request.user.has_perm('OpsManage.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)}] 
                else:resource = [{"hostname": server_assets.ip, "port": server_assets.port,"username": server_assets.username, "password": server_assets.passwd}]
            except Exception,e:
                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.objects.filter(id=server_assets.assets_id).update(sn=ds.get('serial'),model=ds.get('model'),
                                                                                     manufacturer=ds.get('manufacturer'))
                        except Exception,e:
                            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,e:
                            print e
                            return JsonResponse({'msg':"数据更新失败-写入数据失败","code":400})
                    else:
                        return JsonResponse({'msg':"数据更新失败-无法链接主机~","code":502})                    
                return JsonResponse({'msg':"数据更新成功","code":200})
Example #14
0
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 = 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 = 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)
Example #15
0
def group_list(request, format=None):
    """
    List all order, or create a server assets order.
    """
    if not request.user.has_perm('Opsmanage.read_group'):
        return Response(status=status.HTTP_403_FORBIDDEN)
    if request.method == 'GET':
        snippets = Group.objects.all()
        serializer = GroupSerializer(snippets, many=True)
        return Response(serializer.data)
    elif request.method == 'POST':
        if not request.user.has_perm('Opsmanage.change_group'):
            return Response(status=status.HTTP_403_FORBIDDEN)
        serializer = 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)
Example #16
0
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 = GroupSerializer(snippet)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = 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('Opsmanage.delete_group'):
            return Response(status=status.HTTP_403_FORBIDDEN)
        snippet.delete()
        recordAssets.delay(
            user=str(request.user),
            content="删除用户组:{group_name}".format(group_name=snippet.group_name),
            type="group",
            id=id)
        return Response(status=status.HTTP_204_NO_CONTENT)
Example #17
0
                 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,
Example #18
0
                         try:
                             Ram_Assets.objects.filter(assets=assets,device_slot=mem.get('slot')).update(
                                                     device_slot=mem.get('slot'),device_model=mem.get('serial'),
                                                     device_brand= mem.get('manufacturer'),device_volume=mem.get('size'),
                                                     device_status="Online"
                                                     )
 
                         except Exception,e:
                             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,device_slot=disk.get('slot')).update(
                                                     device_serial=disk.get('serial'),device_model=disk.get('model'),
                                                     device_brand= disk.get('manufacturer'),device_volume=disk.get('size'),
                                                     device_status="Online"
                                                      )
 
                         except Exception,e:
                             return JsonResponse({'msg':"数据更新失败-写入数据失败","code":400})  
                     else: