Esempio n. 1
0
    def workflow_approve_callback(self, flow_instance):
        from cloud.instance_task import instance_create_task

        try:
            instance_create_task.delay(self, password=flow_instance.extra_data)

            self.status = INSTANCE_STATE_WAITING
            self.save()

            content = title = _('Your application for instance "%(instance_name)s" is approved! ') \
                % {'instance_name': self.name}
            Notification.info(flow_instance.owner,
                              title,
                              content,
                              is_auto=True)
        except:

            self.status = INSTANCE_STATE_ERROR
            self.save()

            title = _('Error happened to your application for %(instance_name)s') \
                % {'instance_name': self.name}

            content = _(
                'Your application for instance "%(instance_name)s" is approved, '
                'but an error happened when creating instance.') % {
                    'instance_name': self.name
                }

            Notification.error(flow_instance.owner,
                               title,
                               content,
                               is_auto=True)
Esempio n. 2
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)
Esempio n. 3
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)
        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) 
Esempio n. 4
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'}
Esempio n. 5
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) 
Esempio n. 6
0
    def workflow_approve_callback(self, flow_instance):
        from cloud.instance_task import instance_create_task

        try:
            instance_create_task.delay(self, password=flow_instance.extra_data)

            self.status = INSTANCE_STATE_WAITING
            self.save()

            content = title = _('Your application for instance "%(instance_name)s" is approved! ') \
                % {'instance_name': self.name}
            Notification.info(flow_instance.owner, title, content, is_auto=True)
        except:

            self.status = INSTANCE_STATE_ERROR
            self.save()

            title = _('Error happened to your application for %(instance_name)s') \
                % {'instance_name': self.name}

            content = _('Your application for instance "%(instance_name)s" is approved, '
                        'but an error happened when creating instance.') % {'instance_name': self.name}

            Notification.error(flow_instance.owner, title, content, is_auto=True)