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 as 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(user=str(request.user), content="添加业务类型名称:{service_name}".format( service_name=request.data.get("service_name")), type="service", id=serializer.data.get('id')) except Exception as ex: logger.error(msg="添加service失败: {ex}".format(ex=str(ex))) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.data)
def k8s_list(request, format=None): """ List all order, or create a server assets order. """ if request.method == 'GET': snippets = K8s_Assets.objects.all() serializer = serializers.K8sSerializer(snippets, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = serializers.K8sSerializer(data=request.data) if serializer.is_valid(): serializer.save() recordAssets(user=str(request.user), content="添加k8s资产:{k8s_name}".format( k8s_name=request.data.get("k8s_name")), type="k8s", 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_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 serializer = serializers.ServerSerializer(snippet, data=data) if serializer.is_valid(): serializer.save() recordAssets(user=str(request.user), content="修改服务器资产:{ip}".format(ip=snippet.ip), type="server", 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.can_delete_server_assets'): return Response(status=status.HTTP_403_FORBIDDEN) try: snippet.delete() recordAssets(user=str(request.user), content="删除服务器资产:{ip}".format(ip=snippet.ip), type="server", id=id) except Server_Assets.DoesNotExist: pass return Response(status=status.HTTP_204_NO_CONTENT)
def jenkins_detail(request, id, format=None): """ Retrieve, update or delete a server assets instance. """ try: snippet = Jenkins_Assets.objects.get(id=id) except Jenkins_Assets.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = serializers.JenkinsSerializer(snippet) return Response(serializer.data) elif request.method == 'PUT': old_name = snippet.jenkins_name serializer = serializers.JenkinsSerializer(snippet, data=request.data) if serializer.is_valid(): serializer.save() recordAssets( user=str(request.user), content="修改jenkins配置:{old_name} -> {jenkins_name}".format( old_name=old_name, jenkins_name=request.data.get("jenkins_name")), type="jenkins", 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.can_delete_jenkins_assets'): return Response(status=status.HTTP_403_FORBIDDEN) snippet.delete() recordAssets(user=str(request.user), content="删除jenkins配置:{jenkins_name}".format( jenkins_name=snippet.jenkins_name), type="jenkins", id=id) return Response(status=status.HTTP_204_NO_CONTENT)
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(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)