コード例 #1
0
def instance_create_view(request):
    count = request.DATA.get("instance", u"1")
    try:
        count = int(count)
    except:
        count = 1

    pay_type = request.data['pay_type']
    pay_num = int(request.data['pay_num'])

    if count > settings.BATCH_INSTANCE:
        return Response({"OPERATION_STATUS": OPERATION_FAILED},
                    status=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE)

    network_id = request.DATA.get("network_id", u"0")
    try:
        network = Network.objects.get(pk=network_id)
    except Network.DoesNotExist:
        pass
    else:
        if not network.router:
            msg = _("Your selected network has not linked to any router.")
            return Response({"OPERATION_STATUS": OPERATION_FAILED,
                            "msg": msg}, status=status.HTTP_409_CONFLICT)

    has_error, msg = False, None
    for i in range(count):
        serializer = InstanceSerializer(data=request.data, context={"request": request}) 
        if serializer.is_valid():
            name = request.DATA.get("name", "Server")
            if i > 0:
                name = "%s-%04d" % (name, i)
            ins = serializer.save(name=name)

            Operation.log(ins, obj_name=ins.name, action="launch", result=1)
            workflow = Workflow.get_default(ResourceType.INSTANCE)

            if settings.SITE_CONFIG['WORKFLOW_ENABLED'] and workflow:
                ins.status = INSTANCE_STATE_APPLYING
                ins.save()

                FlowInstance.create(ins, request.user, workflow, request.DATA['password'])
                msg = _("Your application for instance \"%(instance_name)s\" is successful, "
                        "please waiting for approval result!") % {'instance_name': ins.name}
            else:
                instance_create_task.delay(ins, password=request.DATA["password"])
                Order.for_instance(ins, pay_type=pay_type, pay_num=pay_num)
                msg = _("Your instance is created, please wait for instance booting.")
        else:
            has_error = True
            break

    if has_error: 
        return Response({"OPERATION_STATUS": OPERATION_FAILED},
                        status=status.HTTP_400_BAD_REQUEST) 
    else:
        return Response({"OPERATION_STATUS": OPERATION_SUCCESS,
                          "msg": msg}, status=status.HTTP_201_CREATED)
コード例 #2
0
ファイル: views.py プロジェクト: lyndonChen/eonboard
def instance_create_view(request):
    serializer = InstanceSerializer(data=request.data, context={"request": request}) 
    if serializer.is_valid():
        ins = serializer.save()
        Operation.log(ins, obj_name=ins.name, action="launch", result=1)
        instance_create_task.delay(ins, password=request.DATA["password"])
        return Response({"OPERATION_STATUS": 1}, status=status.HTTP_201_CREATED)
    else:
        return Response({"OPERATION_STATUS": 0}, status=status.HTTP_400_BAD_REQUEST) 
コード例 #3
0
ファイル: views.py プロジェクト: CannedFish/evercloud_repo
def batch_create(request, user_id):
    LOG.info("** user_id is ***" + str(user_id))
    user_data_center = UserDataCenter.objects.filter(
        user__id=request.user.id)[0]
    LOG.info("*** user_data_center ***" + str(user_data_center))
    user_tenant_uuid = user_data_center.tenant_uuid
    LOG.info("*** user_tenant_uuid is ***" + str(user_tenant_uuid))

    pay_type = request.data['pay_type']
    pay_num = int(request.data['pay_num'])

    network_id = request.DATA.get("network_id", u"0")
    try:
        network = Network.objects.get(pk=network_id)
    except Network.DoesNotExist:
        pass
    else:
        # VLAN mode: we do not have to add router to network.
        if settings.VLAN_ENABLED == False:
            if not network.router:
                msg = _("Your selected network has not linked to any router.")
                return Response(
                    {
                        "OPERATION_STATUS": OPERATION_FAILED,
                        "msg": msg
                    },
                    status=status.HTTP_409_CONFLICT)
    has_error, msg = False, None
    user = User.objects.all().get(id=user_id)
    username = user.username
    serializer = InstanceSerializer(data=request.data,
                                    context={"request": request})
    if serializer.is_valid():
        name = request.DATA.get("name", "Server")
        name = "%s-%s" % (name, username)
        #name = "%s-%04d" % (name, i)
        LOG.info(name)
        ins = serializer.save(name=name, assigneduser=user)
        Operation.log(ins, obj_name=ins.name, action="launch", result=1)
        workflow = Workflow.get_default(ResourceType.INSTANCE)
        instance_create_task.delay(ins,
                                   password=request.DATA["password"],
                                   user_tenant_uuid=user_tenant_uuid)
        Order.for_instance(ins, pay_type=pay_type, pay_num=pay_num)

        #msg = _("Your instance is created, please wait for instance booting.")
    else:
        has_error = True
    if has_error:
        return {'name': name, 'user': user_id, 'status': 'failed'}
    else:
        return {'name': name, 'user': user_id, 'status': 'succeed'}
コード例 #4
0
def instance_create_view(request):
    serializer = InstanceSerializer(data=request.data, context={"request": request}) 
    if serializer.is_valid():
        ins = serializer.save()

        Operation.log(ins, obj_name=ins.name, action="launch", result=1)

        workflow = Workflow.get_default(ResourceType.INSTANCE)

        if settings.SITE_CONFIG['WORKFLOW_ENABLED'] and workflow:
            ins.status = INSTANCE_STATE_APPLYING
            ins.save()

            FlowInstance.create(ins, request.user, workflow, request.DATA['password'])
            msg = _("Your application for instance \"%(instance_name)s\" is successful, "
                    "please waiting for approval result!") % {'instance_name': ins.name}
        else:
            instance_create_task.delay(ins, password=request.DATA["password"])
            msg = _("Your instance is created, please wait for instance booting.")
        return Response({"OPERATION_STATUS": 1,
                         "msg": msg}, status=status.HTTP_201_CREATED)
    else:
        return Response({"OPERATION_STATUS": 0}, status=status.HTTP_400_BAD_REQUEST)