Beispiel #1
0
def define_workflow(request):

    workflow_id, name, resource_type = retrieve_params(
        request.data, 'id', 'name', 'resource_type')

    step_ids, step_names, step_approvers = retrieve_list_params(
        request.data, 'step_ids[]', 'step_names[]', 'step_approvers[]')

    if workflow_id:
        workflow = Workflow.objects.get(pk=workflow_id)
    else:
        workflow = Workflow(is_default=False)

    workflow.name = name
    workflow.resource_type = resource_type
    workflow.save()

    original_steps, new_steps = workflow.steps.all(), []
    step_orders = range(len(step_ids))

    last_step = None
    for index in step_orders:

        pk, name, approver_id = step_ids[index], step_names[index], step_approvers[index]

        if pk:
            step = Step.objects.get(pk=pk)
        else:
            step = Step()
            step.workflow = workflow

        step.name = name
        step.order = index
        step.approver = UserProxy.normal_users.get(pk=approver_id)
        step.save()

        if last_step:
            last_step.next = step
            last_step.save()

        last_step = step

        new_steps.append(step)

    for step in original_steps:

        if step not in new_steps:
            step.delete()

    workflow = Workflow.objects.get(pk=workflow.pk)

    return Response({"success": True,
                     "msg": _('Workflow is saved.'),
                     "data": WorkflowSerializer(workflow).data})
Beispiel #2
0
def volume_create_view(request):
    try:
        serializer = VolumeSerializer(data=request.data,
                                      context={"request": request})
        if not serializer.is_valid():
            return fail(msg=_('Data is not valid.'),
                        status=status.HTTP_400_BAD_REQUEST)

        volume = serializer.save()
        Operation.log(volume, obj_name=volume.name, action="create", result=1)
        workflow = Workflow.get_default(ResourceType.VOLUME)

        if settings.SITE_CONFIG['WORKFLOW_ENABLED'] and workflow:

            volume.status = VOLUME_STATE_APPLYING
            volume.save()

            FlowInstance.create(volume, request.user, workflow, None)
            msg = _("Your application for %(size)d GB volume is successful, "
                    "please waiting for approval result!") \
                % {'size': volume.size}
            return success(msg=msg)
        else:
            try:
                volume_create_task.delay(volume)
                return success(msg=_('Creating volume'),
                               status=status.HTTP_201_CREATED)
            except Exception as e:
                LOG.exception("Failed to create volume, msg: %s", e)
                volume.status = VOLUME_STATE_ERROR
                volume.save()
                return error()
    except Exception as e:
        LOG.exception("create volume error, msg:[%s]", e)
        return error()
Beispiel #3
0
def create_view(request):
    floating = Floating.objects.create(
        ip="N/A",
        status=FLOATING_ALLOCATE,
        bandwidth=int(request.POST["bandwidth"]),
        user=request.user,
        user_data_center=UserDataCenter.objects.get(
            pk=request.session["UDC_ID"]))

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

    Operation.log(floating, obj_name=floating.ip, action='allocate', result=1)

    workflow = Workflow.get_default(ResourceType.FLOATING)

    if settings.SITE_CONFIG['WORKFLOW_ENABLED'] and workflow:

        floating.status = FLOATING_APPLYING
        floating.save()

        FlowInstance.create(floating, request.user, workflow, None)
        msg = _(
            "Your application for %(bandwidth)d Mbps floating ip is successful, "
            "please waiting for approval result!") % {
                'bandwidth': floating.bandwidth
            }
    else:
        msg = _("Your operation is successful, please wait for allocation.")
        allocate_floating_task.delay(floating)
        Order.for_floating(floating, pay_type, pay_num)

    return Response({"OPERATION_STATUS": 1, 'msg': msg})
Beispiel #4
0
def volume_create_view(request):
    try:
        serializer = VolumeSerializer(data=request.data,
                                      context={"request": request})
        if not serializer.is_valid():
            return fail(msg=_('Data is not valid.'),
                        status=status.HTTP_400_BAD_REQUEST)

        volume = serializer.save()
        Operation.log(volume, obj_name=volume.name, action="create", result=1)
        workflow = Workflow.get_default(ResourceType.VOLUME)

        if settings.SITE_CONFIG['WORKFLOW_ENABLED'] and workflow:

            volume.status = VOLUME_STATE_APPLYING
            volume.save()

            FlowInstance.create(volume, request.user, workflow, None)
            msg = _("Your application for %(size)d GB volume is successful, "
                    "please waiting for approval result!") \
                % {'size': volume.size}
            return success(msg=msg)
        else:
            try:
                volume_create_task.delay(volume)
                return success(msg=_('Creating volume'),
                               status=status.HTTP_201_CREATED)
            except Exception as e:
                LOG.exception("Failed to create volume, msg: %s", e)
                volume.status = VOLUME_STATE_ERROR
                volume.save()
                return error()
    except Exception as e:
        LOG.exception("create volume error, msg:[%s]", e)
        return error()
Beispiel #5
0
def create_view(request):
    floating = Floating.objects.create(
        ip="N/A",
        status=FLOATING_ALLOCATE,
        bandwidth=int(request.POST["bandwidth"]),
        user=request.user,
        user_data_center=UserDataCenter.objects.get(pk=request.session["UDC_ID"])
    )

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

    Operation.log(floating, obj_name=floating.ip, action='allocate', result=1)

    workflow = Workflow.get_default(ResourceType.FLOATING)

    if settings.SITE_CONFIG['WORKFLOW_ENABLED'] and workflow:

        floating.status = FLOATING_APPLYING
        floating.save()

        FlowInstance.create(floating, request.user, workflow, None)
        msg = _("Your application for %(bandwidth)d Mbps floating ip is successful, "
                "please waiting for approval result!") % {'bandwidth': floating.bandwidth}
    else:
        msg = _("Your operation is successful, please wait for allocation.")
        allocate_floating_task.delay(floating)
        Order.for_floating(floating, pay_type, pay_num)

    return Response({"OPERATION_STATUS": 1, 'msg': msg})
Beispiel #6
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)
Beispiel #7
0
def volume_create_view(request):
    try:

        os_volume_type = request.data['os_volume_type']
        os_volume_type = ast.literal_eval(os_volume_type)
        os_volume_type = os_volume_type['name']

        LOG.info("********** os volume type is ************" +
                 str(os_volume_type))
        request.data['os_volume_type'] = os_volume_type
        LOG.info("********** os volume type is ************" +
                 str(os_volume_type))

        serializer = VolumeSerializer(data=request.data,
                                      context={"request": request})
        if not serializer.is_valid():
            #return fail(msg=_('Data is not valid.'),
            return fail(msg='{}'.format(serializer.errors),
                        status=status.HTTP_400_BAD_REQUEST)

        pay_type = request.data['pay_type']
        LOG.info("********** os volume type is ************" +
                 str(os_volume_type))
        pay_num = int(request.data['pay_num'])

        volume = serializer.save()
        Operation.log(volume, obj_name=volume.name, action="create", result=1)
        workflow = Workflow.get_default(ResourceType.VOLUME)

        if settings.SITE_CONFIG['WORKFLOW_ENABLED'] and workflow:

            volume.status = VOLUME_STATE_APPLYING
            volume.save()

            FlowInstance.create(volume, request.data.user, workflow,
                                None)  # TODO test this
            msg = _("Your application for %(size)d GB volume is successful, "
                    "please waiting for approval result!") \
                % {'size': volume.size}
            return success(msg=msg)
        else:
            try:
                volume_create_task.delay(volume, os_volume_type)
                Order.for_volume(volume, pay_type=pay_type, pay_num=pay_num)
                return success(msg=_('Creating volume'),
                               status=status.HTTP_201_CREATED)
            except Exception as e:
                LOG.exception("Failed to create volume, msg: %s", e)
                volume.status = VOLUME_STATE_ERROR
                volume.save()
                return error()
    except Exception as e:
        LOG.exception("create volume error, msg:[%s]", e)
        return error()
Beispiel #8
0
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'}
Beispiel #9
0
def define_workflow(request):

    workflow_id, name, resource_type = retrieve_params(request.data, 'id',
                                                       'name', 'resource_type')

    step_ids, step_names, step_approvers = retrieve_list_params(
        request.data, 'step_ids[]', 'step_names[]', 'step_approvers[]')

    if workflow_id:
        workflow = Workflow.objects.get(pk=workflow_id)
    else:
        workflow = Workflow(is_default=False)

    workflow.name = name
    workflow.resource_type = resource_type
    workflow.save()

    original_steps, new_steps = workflow.steps.all(), []
    step_orders = range(len(step_ids))

    last_step = None
    for index in step_orders:

        pk, name, approver_id = step_ids[index], step_names[
            index], step_approvers[index]

        if pk:
            step = Step.objects.get(pk=pk)
        else:
            step = Step()
            step.workflow = workflow

        step.name = name
        step.order = index
        step.approver = UserProxy.normal_users.get(pk=approver_id)
        step.save()

        if last_step:
            last_step.next = step
            last_step.save()

        last_step = step

        new_steps.append(step)

    for step in original_steps:

        if step not in new_steps:
            step.delete()

    workflow = Workflow.objects.get(pk=workflow.pk)

    return Response({
        "success": True,
        "msg": _('Workflow is saved.'),
        "data": WorkflowSerializer(workflow).data
    })
Beispiel #10
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)