Exemplo n.º 1
0
    def post(self, request, app_id):
        context = Util.get_context(request)
        app = App.objects.get(id=app_id, user=request.user)
        context['app_id'] = app_id
        context['app_name'] = app.name
        adapter = DjangoAdapter(app_id, request)
        cmd = request.POST.get('cmd', None)
        if cmd == 'upload_marketplace_logic':
            title = request.POST.get('title')
            description = request.POST.get('description')
            category = request.POST.get('category')
            logo_image = request.FILES.get('logo_image')
            content = request.POST.get('content')
            function_name = request.POST.get('function_name')
            with adapter.open_api_logic() as logic_api:
                result = logic_api.get_function(function_name)
                if 'error' in result:
                    return JsonResponse(result)
                logic_function = result['item']
                handler = logic_function['handler']
                runtime = logic_function['runtime']
                function_zip_b64 = logic_api.get_function_zip_b64(
                    function_name)['item']['base64']
                function_zip_b64 = function_zip_b64.encode('utf-8')
                function_zip = base64.b64decode(function_zip_b64)
                function_zip_name = '{}.zip'.format(function_name)
                function_zip_file = SimpleUploadedFile(
                    function_zip_name, function_zip,
                    'application/octet-stream')

            marketplace_logic = MarketplaceLogic(user=request.user)
            marketplace_logic.title = title
            marketplace_logic.description = description
            marketplace_logic.category = category
            marketplace_logic.logo_image = logo_image
            marketplace_logic.content = content
            marketplace_logic.function_zip_file = function_zip_file
            marketplace_logic.function_name = function_name
            marketplace_logic.handler = handler
            marketplace_logic.runtime = runtime
            marketplace_logic.save()
            return JsonResponse(
                data={'marketplace_logic_id': marketplace_logic.id})
Exemplo n.º 2
0
    def get(self, request, app_id, function_name):
        context = Util.get_context(request)
        context['app_id'] = app_id

        adapter = DjangoAdapter(app_id, request)
        with adapter.open_api_logic() as logic_api:
            function = logic_api.get_function(function_name)['item']
            file_paths = logic_api.get_function_file_paths(function_name).get(
                'file_paths', [])
            handler_prefix = '/'.join(function['handler'].split('.')[:-1])
            current_path = None
            for file_path in file_paths:
                if file_path.startswith(handler_prefix):
                    current_path = file_path

            context['function'] = function
            context['file_paths'] = file_paths
            context['current_path'] = current_path
            context['current_file'] = logic_api.get_function_file(
                function_name, current_path).get('item')
        return render(request,
                      'dashboard/app/logic_edit.html',
                      context=context)
Exemplo n.º 3
0
 def post(self, request, app_id, function_name):
     context = Util.get_context(request)
     context['app_id'] = app_id
     cmd = request.POST.get('cmd', None)
     adapter = DjangoAdapter(app_id, request)
     with adapter.open_api_logic() as logic_api:
         if cmd == 'get_function_file':
             function_name = request.POST.get('function_name')
             file_path = request.POST.get('file_path')
             result = logic_api.get_function_file(function_name, file_path)
             return JsonResponse(result)
         elif cmd == 'put_function_file':
             function_name = request.POST.get('function_name')
             file_path = request.POST.get('file_path')
             file_content = request.POST.get('file_content')
             file_type = request.POST.get('file_type', 'text')
             result = logic_api.put_function_file(function_name, file_path,
                                                  file_content, file_type)
             allocate_resource_in_background(adapter)
             return JsonResponse(result)
         elif cmd == 'update_function':
             function_name = request.POST.get('function_name', None)
             description = request.POST.get('description', None)
             handler = request.POST.get('handler', None)
             runtime = request.POST.get('runtime', None)
             sdk_config = get_sdk_config(adapter)
             result = logic_api.update_function(function_name=function_name,
                                                description=description,
                                                handler=handler,
                                                runtime=runtime,
                                                sdk_config=sdk_config)
             return JsonResponse(result)
         elif cmd == 'get_function_file_paths':
             function_name = request.POST.get('function_name')
             item = logic_api.get_function_file_paths(function_name)
             return JsonResponse(item)
Exemplo n.º 4
0
    def post(self, request, app_id):
        context = Util.get_context(request)
        context['app_id'] = app_id
        cmd = request.POST.get('cmd', None)
        adapter = DjangoAdapter(app_id, request)
        with adapter.open_api_logic() as logic_api:
            if cmd == 'create_function':
                zip_file = request.FILES['zip_file']
                function_name = request.POST['function_name']
                description = request.POST['description']
                runtime = request.POST['runtime']
                handler = request.POST['handler']
                use_logging = request.POST['use_logging'] == 'true'
                use_traceback = request.POST['use_traceback'] == 'true'
                use_standalone = request.POST['use_standalone'] == 'true'
                sdk_config = get_sdk_config(adapter, use_localhost=False)

                zip_file.seek(0)
                zip_file_bin = zip_file.read()
                zip_file_bytes = zip_file_bin
                zip_file_bin = base64.b64encode(zip_file_bin)
                zip_file_bin = zip_file_bin.decode('utf-8')
                if not description:
                    description = None

                try:
                    requirements_text = get_requirements_text_from_zipfile(
                        zip_file_bytes)
                except Exception as ex:
                    requirements_text = None
                    print(ex)
                    print(traceback.format_exc())

                requirements_zip_file_id = None
                response_stdout = None
                if requirements_text:
                    print('requirements_text:', requirements_text)
                    with adapter.open_sdk() as sdk_client:
                        response = sdk_client.logic_create_packages_zip(
                            requirements_text)
                        print('logic_create_packages_zip:', response)
                        requirements_zip_file_id = response.get(
                            'zip_file_id', None)
                        response_stdout = response.get('response_stdout', None)
                        if not requirements_zip_file_id:
                            raise RuntimeError(
                                'Requirements.txt error! retry or check this.')
                # response = logic_api.create_packages_zip(requirements_text)
                # print('logic_create_packages_zip:', response)
                # requirements_zip_file_id = response.get('zip_file_id', None)

                response_function_creation = logic_api.create_function(
                    function_name, description, runtime, handler, sdk_config,
                    zip_file_bin, True, use_logging, use_traceback,
                    requirements_zip_file_id, use_standalone)
                return JsonResponse({
                    'package_install_response_stdout':
                    response_stdout,
                    'response_function_creation':
                    response_function_creation
                })
            elif cmd == 'create_function_test':
                test_name = request.POST.get('test_name')
                function_name = request.POST.get('function_name')
                test_input = request.POST.get('test_input')
                logic_api.create_function_test(test_name, function_name,
                                               test_input)
            elif cmd == 'run_function':
                with adapter.open_sdk() as sdk_client:
                    # TODO SDK Sign-up Login and authentication takes a lot of time.
                    #  We are planning to store the SDK ID Password in the session.
                    function_name = request.POST.get('function_name')
                    payload = request.POST.get('payload')
                    payload = json.loads(payload)
                    start = time.time()
                    data = sdk_client.logic_run_function(
                        function_name, payload)
                    end = time.time()
                    data['duration'] = end - start
                    # print('data:', data)
                    return JsonResponse(data)
            elif cmd == 'delete_function_test':
                test_name = request.POST.get('test_name')
                logic_api.delete_function_test(test_name)
            elif cmd == 'delete_function':
                function_name = request.POST.get('function_name')
                function_version = request.POST.get('function_version', None)
                logic_api.delete_function(function_name, function_version)
            elif cmd == 'create_webhook':
                name = request.POST.get('name')
                description = request.POST.get('description')
                function_name = request.POST.get('function_name')
                logic_api.create_webhook(name, description, function_name)
            elif cmd == 'delete_webhook':
                name = request.POST.get('name')
                logic_api.delete_webhook(name)

        return redirect(request.path_info)  # Redirect back
Exemplo n.º 5
0
    def post(self, request, app_id, marketplace_logic_id):
        context = Util.get_context(request)
        app = App.objects.get(id=app_id, user=request.user)
        context['app_id'] = app_id
        context['app_name'] = app.name
        adapter = DjangoAdapter(app_id, request)
        cmd = request.POST.get('cmd', None)
        if cmd == 'edit_marketplace_logic':
            title = request.POST.get('title')
            description = request.POST.get('description')
            category = request.POST.get('category')
            logo_image = request.FILES.get('logo_image')
            content = request.POST.get('content')
            function_name = request.POST.get('function_name')

            change_logo_image = request.POST.get('change_logo_image', False)
            change_function = request.POST.get('change_function', False)

            if change_logo_image == 'true':
                change_logo_image = True
            elif change_logo_image == 'false':
                change_logo_image = False

            if change_function == 'true':
                change_function = True
            elif change_function == 'false':
                change_function = False

            with adapter.open_api_logic() as logic_api:
                logic_function = logic_api.get_function(function_name)['item']
                handler = logic_function['handler']
                runtime = logic_function['runtime']
                function_zip_b64 = logic_api.get_function_zip_b64(
                    function_name)['item']['base64']
                function_zip_b64 = function_zip_b64.encode('utf-8')
                function_zip = base64.b64decode(function_zip_b64)
                function_zip_name = '{}.zip'.format(function_name)
                function_zip_file = SimpleUploadedFile(
                    function_zip_name, function_zip,
                    'application/octet-stream')

            marketplace_logic = MarketplaceLogic.objects.get(
                id=marketplace_logic_id, user=request.user)
            marketplace_logic.title = title
            marketplace_logic.description = description
            marketplace_logic.category = category

            if change_logo_image:
                marketplace_logic.logo_image = logo_image

            if change_function:
                # Needs security verification by AWS-I team
                marketplace_logic.verified = False
                marketplace_logic.function_zip_file = function_zip_file

            marketplace_logic.function_name = function_name
            marketplace_logic.handler = handler
            marketplace_logic.runtime = runtime
            marketplace_logic.content = content
            marketplace_logic.save()
            return JsonResponse(
                data={'marketplace_logic_id': marketplace_logic.id})