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()
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})
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})
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)
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()
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)