1
0
mirror of https://github.com/GNS3/gns3-server synced 2025-01-01 11:40:56 +00:00

Display an upload form (upload is not yet ready)

This commit is contained in:
Julien Duponchelle 2015-02-23 11:27:07 +01:00
parent 210aa6f12a
commit f7cd09d5fb
30 changed files with 234 additions and 100 deletions

View File

@ -1,9 +1,19 @@
__all__ = ["version_handler", #
"network_handler", # Copyright (C) 2015 GNS3 Technologies Inc.
"vpcs_handler", #
"project_handler", # This program is free software: you can redistribute it and/or modify
"virtualbox_handler", # it under the terms of the GNU General Public License as published by
"dynamips_vm_handler", # the Free Software Foundation, either version 3 of the License, or
"dynamips_device_handler", # (at your option) any later version.
"iou_handler", #
"qemu_handler"] # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from gns3server.handlers.api import *
from gns3server.handlers.upload_handler import UploadHandler

View File

@ -0,0 +1,9 @@
__all__ = ["version_handler",
"network_handler",
"vpcs_handler",
"project_handler",
"virtualbox_handler",
"dynamips_vm_handler",
"dynamips_device_handler",
"iou_handler",
"qemu_handler"]

View File

@ -17,13 +17,13 @@
import os import os
import asyncio import asyncio
from ..web.route import Route from ...web.route import Route
from ..schemas.dynamips_device import DEVICE_CREATE_SCHEMA from ...schemas.dynamips_device import DEVICE_CREATE_SCHEMA
from ..schemas.dynamips_device import DEVICE_UPDATE_SCHEMA from ...schemas.dynamips_device import DEVICE_UPDATE_SCHEMA
from ..schemas.dynamips_device import DEVICE_CAPTURE_SCHEMA from ...schemas.dynamips_device import DEVICE_CAPTURE_SCHEMA
from ..schemas.dynamips_device import DEVICE_OBJECT_SCHEMA from ...schemas.dynamips_device import DEVICE_OBJECT_SCHEMA
from ..schemas.dynamips_device import DEVICE_NIO_SCHEMA from ...schemas.dynamips_device import DEVICE_NIO_SCHEMA
from ..modules.dynamips import Dynamips from ...modules.dynamips import Dynamips
class DynamipsDeviceHandler: class DynamipsDeviceHandler:

View File

@ -19,16 +19,16 @@
import os import os
import base64 import base64
import asyncio import asyncio
from ..web.route import Route
from ..schemas.dynamips_vm import VM_CREATE_SCHEMA
from ..schemas.dynamips_vm import VM_UPDATE_SCHEMA
from ..schemas.dynamips_vm import VM_CAPTURE_SCHEMA
from ..schemas.dynamips_vm import VM_OBJECT_SCHEMA
from ..schemas.dynamips_vm import VM_NIO_SCHEMA
from ..schemas.dynamips_vm import VM_CONFIGS_SCHEMA
from ..modules.dynamips import Dynamips
from ..modules.project_manager import ProjectManager
from ...web.route import Route
from ...schemas.dynamips_vm import VM_CREATE_SCHEMA
from ...schemas.dynamips_vm import VM_UPDATE_SCHEMA
from ...schemas.dynamips_vm import VM_CAPTURE_SCHEMA
from ...schemas.dynamips_vm import VM_OBJECT_SCHEMA
from ...schemas.dynamips_vm import VM_NIO_SCHEMA
from ...schemas.dynamips_vm import VM_CONFIGS_SCHEMA
from ...modules.dynamips import Dynamips
from ...modules.project_manager import ProjectManager
class DynamipsVMHandler: class DynamipsVMHandler:

View File

@ -18,15 +18,15 @@
import os import os
from ..web.route import Route from ...web.route import Route
from ..modules.port_manager import PortManager from ...modules.port_manager import PortManager
from ..schemas.iou import IOU_CREATE_SCHEMA from ...schemas.iou import IOU_CREATE_SCHEMA
from ..schemas.iou import IOU_UPDATE_SCHEMA from ...schemas.iou import IOU_UPDATE_SCHEMA
from ..schemas.iou import IOU_OBJECT_SCHEMA from ...schemas.iou import IOU_OBJECT_SCHEMA
from ..schemas.iou import IOU_NIO_SCHEMA from ...schemas.iou import IOU_NIO_SCHEMA
from ..schemas.iou import IOU_CAPTURE_SCHEMA from ...schemas.iou import IOU_CAPTURE_SCHEMA
from ..schemas.iou import IOU_INITIAL_CONFIG_SCHEMA from ...schemas.iou import IOU_INITIAL_CONFIG_SCHEMA
from ..modules.iou import IOU from ...modules.iou import IOU
class IOUHandler: class IOUHandler:

View File

@ -15,9 +15,9 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from ..web.route import Route from ...web.route import Route
from ..modules.port_manager import PortManager from ...modules.port_manager import PortManager
from ..utils.interfaces import interfaces from ...utils.interfaces import interfaces
class NetworkHandler: class NetworkHandler:

View File

@ -15,10 +15,10 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from ..web.route import Route from ...web.route import Route
from ..schemas.project import PROJECT_OBJECT_SCHEMA, PROJECT_CREATE_SCHEMA, PROJECT_UPDATE_SCHEMA from ...schemas.project import PROJECT_OBJECT_SCHEMA, PROJECT_CREATE_SCHEMA, PROJECT_UPDATE_SCHEMA
from ..modules.project_manager import ProjectManager from ...modules.project_manager import ProjectManager
from ..modules import MODULES from ...modules import MODULES
class ProjectHandler: class ProjectHandler:

View File

@ -18,14 +18,14 @@
import os import os
from ..web.route import Route from ...web.route import Route
from ..modules.port_manager import PortManager from ...modules.port_manager import PortManager
from ..schemas.qemu import QEMU_CREATE_SCHEMA from ...schemas.qemu import QEMU_CREATE_SCHEMA
from ..schemas.qemu import QEMU_UPDATE_SCHEMA from ...schemas.qemu import QEMU_UPDATE_SCHEMA
from ..schemas.qemu import QEMU_OBJECT_SCHEMA from ...schemas.qemu import QEMU_OBJECT_SCHEMA
from ..schemas.qemu import QEMU_NIO_SCHEMA from ...schemas.qemu import QEMU_NIO_SCHEMA
from ..schemas.qemu import QEMU_BINARY_LIST_SCHEMA from ...schemas.qemu import QEMU_BINARY_LIST_SCHEMA
from ..modules.qemu import Qemu from ...modules.qemu import Qemu
class QEMUHandler: class QEMUHandler:

View File

@ -15,9 +15,9 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from ..web.route import Route from ...web.route import Route
from ..schemas.version import VERSION_SCHEMA from ...schemas.version import VERSION_SCHEMA
from ..version import __version__ from ...version import __version__
from aiohttp.web import HTTPConflict from aiohttp.web import HTTPConflict

View File

@ -16,14 +16,14 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import os import os
from ..web.route import Route from ...web.route import Route
from ..schemas.virtualbox import VBOX_CREATE_SCHEMA from ...schemas.virtualbox import VBOX_CREATE_SCHEMA
from ..schemas.virtualbox import VBOX_UPDATE_SCHEMA from ...schemas.virtualbox import VBOX_UPDATE_SCHEMA
from ..schemas.virtualbox import VBOX_NIO_SCHEMA from ...schemas.virtualbox import VBOX_NIO_SCHEMA
from ..schemas.virtualbox import VBOX_CAPTURE_SCHEMA from ...schemas.virtualbox import VBOX_CAPTURE_SCHEMA
from ..schemas.virtualbox import VBOX_OBJECT_SCHEMA from ...schemas.virtualbox import VBOX_OBJECT_SCHEMA
from ..modules.virtualbox import VirtualBox from ...modules.virtualbox import VirtualBox
from ..modules.project_manager import ProjectManager from ...modules.project_manager import ProjectManager
class VirtualBoxHandler: class VirtualBoxHandler:

View File

@ -15,12 +15,12 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from ..web.route import Route from ...web.route import Route
from ..schemas.vpcs import VPCS_CREATE_SCHEMA from ...schemas.vpcs import VPCS_CREATE_SCHEMA
from ..schemas.vpcs import VPCS_UPDATE_SCHEMA from ...schemas.vpcs import VPCS_UPDATE_SCHEMA
from ..schemas.vpcs import VPCS_OBJECT_SCHEMA from ...schemas.vpcs import VPCS_OBJECT_SCHEMA
from ..schemas.vpcs import VPCS_NIO_SCHEMA from ...schemas.vpcs import VPCS_NIO_SCHEMA
from ..modules.vpcs import VPCS from ...modules.vpcs import VPCS
class VPCSHandler: class VPCSHandler:

View File

@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from ..web.route import Route
from ..schemas.version import VERSION_SCHEMA
from ..version import __version__
from aiohttp.web import HTTPConflict
class UploadHandler:
@classmethod
@Route.get(
r"/upload",
description="Manage upload of GNS3 images",
api_version=None
)
def index(request, response):
response.template("upload.html")

View File

@ -0,0 +1,12 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>GNS3 Server</title>
</head>
<body>
{% block body %}{% endblock %}
</body>
<small>
Powered by GNS3 {{gns3_version}}
</small>
</html>

View File

@ -1,20 +1,16 @@
<html> {% extends "layout.html" %}
<head> {% block body %}
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <h1>Select & Upload an image for GNS3</h1>
<title>Upload Form for GNS3 server {{version}}</title> <form enctype="multipart/form-data" action="/upload" method="post">
</head> File: <input type="file" name="file" />
<body> <br />
<p><h1>Select & Upload (v{{version}})</h1></p> <br />
<form enctype="multipart/form-data" action="/upload" method="post"> <input type="submit" value="Upload" />
File: <input type="file" name="file" /> </form>
<br /> {%if items%}
<br /> <h3>Files on {{host}}</h3>
<input type="submit" value="upload" /> {%for item in items%}
</form> <p>{{path}}/{{item}}</a></p>
{%if items%} {%endfor%}
<h3>Files on {{host}}</h3> {%endif%}
{%for item in items%} {% endblock %}
<p>{{path}}/{{item}}</a></p>
{%end%}
{%end%}
</body>

View File

@ -20,9 +20,12 @@ import jsonschema
import aiohttp.web import aiohttp.web
import logging import logging
import sys import sys
import jinja2
from ..version import __version__ from ..version import __version__
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
renderer = jinja2.Environment(loader=jinja2.PackageLoader('gns3server', 'templates'))
class Response(aiohttp.web.Response): class Response(aiohttp.web.Response):
@ -47,6 +50,28 @@ class Response(aiohttp.web.Response):
log.debug(json.loads(self.body.decode('utf-8'))) log.debug(json.loads(self.body.decode('utf-8')))
return super().start(request) return super().start(request)
def html(self, answer):
"""
Set the response content type to text/html and serialize
the content.
:param anwser The response as a Python object
"""
self.content_type = "text/html"
self.body = answer.encode('utf-8')
def template(self, template_filename, **kwargs):
"""
Render a template
:params template: Template name
:params kwargs: Template parameters
"""
template = renderer.get_template(template_filename)
kwargs["gns3_version"] = __version__
self.html(template.render(**kwargs))
def json(self, answer): def json(self, answer):
""" """
Set the response content type to application/json and serialize Set the response content type to application/json and serialize

View File

@ -81,7 +81,10 @@ class Route(object):
# This block is executed only the first time # This block is executed only the first time
output_schema = kw.get("output", {}) output_schema = kw.get("output", {})
input_schema = kw.get("input", {}) input_schema = kw.get("input", {})
api_version = kw.get("version", 1) api_version = kw.get("api_version", 1)
if api_version is None:
cls._path = path
else:
cls._path = "/v{version}{path}".format(path=path, version=api_version) cls._path = "/v{version}{path}".format(path=path, version=api_version)
def register(func): def register(func):
@ -123,8 +126,13 @@ class Route(object):
response.set_status(500) response.set_status(500)
exc_type, exc_value, exc_tb = sys.exc_info() exc_type, exc_value, exc_tb = sys.exc_info()
lines = traceback.format_exception(exc_type, exc_value, exc_tb) lines = traceback.format_exception(exc_type, exc_value, exc_tb)
if api_version is not None:
tb = "".join(lines) tb = "".join(lines)
response.json({"message": tb, "status": 500}) response.json({"message": tb, "status": 500})
else:
tb = "\n".join(lines)
response.html("<h1>Internal error</h1><pre>{}</pre>".format(tb))
return response return response
cls._routes.append((method, cls._path, control_schema)) cls._routes.append((method, cls._path, control_schema))

View File

@ -4,3 +4,4 @@ python-dateutil==2.3
apache-libcloud==0.16.0 apache-libcloud==0.16.0
requests==2.5.0 requests==2.5.0
aiohttp==0.14.4 aiohttp==0.14.4
Jinja2==2.7.3

View File

@ -37,7 +37,8 @@ class PyTest(TestCommand):
dependencies = ["aiohttp==0.14.4", dependencies = ["aiohttp==0.14.4",
"jsonschema==2.4.0", "jsonschema==2.4.0",
"apache-libcloud==0.16.0", "apache-libcloud==0.16.0",
"requests==2.5.0"] "requests==2.5.0",
"Jinja2==2.7.3"]
if sys.version_info == (3, 3): if sys.version_info == (3, 3):
dependencies.append("asyncio==3.4.2") dependencies.append("asyncio==3.4.2")

View File

@ -30,7 +30,7 @@ from gns3server.handlers import *
from gns3server.modules import MODULES from gns3server.modules import MODULES
from gns3server.modules.port_manager import PortManager from gns3server.modules.port_manager import PortManager
from gns3server.modules.project_manager import ProjectManager from gns3server.modules.project_manager import ProjectManager
from tests.api.base import Query from tests.handlers.api.base import Query
# Prevent execution of external binaries # Prevent execution of external binaries

View File

@ -44,22 +44,25 @@ class Query:
def delete(self, path, **kwargs): def delete(self, path, **kwargs):
return self._fetch("DELETE", path, **kwargs) return self._fetch("DELETE", path, **kwargs)
def _get_url(self, path): def _get_url(self, path, version):
return "http://{}:{}/v1{}".format(self._host, self._port, path) if version is None:
return "http://{}:{}{}".format(self._host, self._port, path)
return "http://{}:{}/v{}{}".format(self._host, self._port, version, path)
def _fetch(self, method, path, body=None, **kwargs): def _fetch(self, method, path, body=None, api_version = 1, **kwargs):
"""Fetch an url, parse the JSON and return response """Fetch an url, parse the JSON and return response
Options: Options:
- example if True the session is included inside documentation - example if True the session is included inside documentation
- raw do not JSON encode the query - raw do not JSON encode the query
- api_version Version of API, None if no version
""" """
if body is not None and not kwargs.get("raw", False): if body is not None and not kwargs.get("raw", False):
body = json.dumps(body) body = json.dumps(body)
@asyncio.coroutine @asyncio.coroutine
def go(future): def go(future):
response = yield from aiohttp.request(method, self._get_url(path), data=body) response = yield from aiohttp.request(method, self._get_url(path, api_version), data=body)
future.set_result(response) future.set_result(response)
future = asyncio.Future() future = asyncio.Future()
asyncio.async(go(future)) asyncio.async(go(future))
@ -79,12 +82,16 @@ class Query:
response.route = x_route.replace("/v1", "") response.route = x_route.replace("/v1", "")
if response.body is not None: if response.body is not None:
if response.headers.get("CONTENT-TYPE", "") == "application/json":
try: try:
response.json = json.loads(response.body.decode("utf-8")) response.json = json.loads(response.body.decode("utf-8"))
except ValueError: except ValueError:
response.json = None response.json = None
else:
response.html = response.body.decode("utf-8")
else: else:
response.json = {} response.json = {}
response.html = ""
if kwargs.get('example') and os.environ.get("PYTEST_BUILD_DOCUMENTATION") == "1": if kwargs.get('example') and os.environ.get("PYTEST_BUILD_DOCUMENTATION") == "1":
self._dump_example(method, response.route, body, response) self._dump_example(method, response.route, body, response)
return response return response

View File

@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
This test suite check /version endpoint
It's also used for unittest the HTTP implementation.
"""
from gns3server.version import __version__
def test_version_index_upload(server):
response = server.get('/upload', api_version=None)
assert response.status == 200
html = response.html
assert "GNS3 Server" in html
assert "Select & Upload" in html