mirror of
https://github.com/GNS3/gns3-server
synced 2024-11-12 19:38:57 +00:00
Display an upload form (upload is not yet ready)
This commit is contained in:
parent
210aa6f12a
commit
f7cd09d5fb
@ -1,9 +1,19 @@
|
||||
__all__ = ["version_handler",
|
||||
"network_handler",
|
||||
"vpcs_handler",
|
||||
"project_handler",
|
||||
"virtualbox_handler",
|
||||
"dynamips_vm_handler",
|
||||
"dynamips_device_handler",
|
||||
"iou_handler",
|
||||
"qemu_handler"]
|
||||
#
|
||||
# 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 gns3server.handlers.api import *
|
||||
from gns3server.handlers.upload_handler import UploadHandler
|
||||
|
||||
|
9
gns3server/handlers/api/__init__.py
Normal file
9
gns3server/handlers/api/__init__.py
Normal 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"]
|
@ -17,13 +17,13 @@
|
||||
|
||||
import os
|
||||
import asyncio
|
||||
from ..web.route import Route
|
||||
from ..schemas.dynamips_device import DEVICE_CREATE_SCHEMA
|
||||
from ..schemas.dynamips_device import DEVICE_UPDATE_SCHEMA
|
||||
from ..schemas.dynamips_device import DEVICE_CAPTURE_SCHEMA
|
||||
from ..schemas.dynamips_device import DEVICE_OBJECT_SCHEMA
|
||||
from ..schemas.dynamips_device import DEVICE_NIO_SCHEMA
|
||||
from ..modules.dynamips import Dynamips
|
||||
from ...web.route import Route
|
||||
from ...schemas.dynamips_device import DEVICE_CREATE_SCHEMA
|
||||
from ...schemas.dynamips_device import DEVICE_UPDATE_SCHEMA
|
||||
from ...schemas.dynamips_device import DEVICE_CAPTURE_SCHEMA
|
||||
from ...schemas.dynamips_device import DEVICE_OBJECT_SCHEMA
|
||||
from ...schemas.dynamips_device import DEVICE_NIO_SCHEMA
|
||||
from ...modules.dynamips import Dynamips
|
||||
|
||||
|
||||
class DynamipsDeviceHandler:
|
@ -19,16 +19,16 @@
|
||||
import os
|
||||
import base64
|
||||
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:
|
||||
|
@ -18,15 +18,15 @@
|
||||
import os
|
||||
|
||||
|
||||
from ..web.route import Route
|
||||
from ..modules.port_manager import PortManager
|
||||
from ..schemas.iou import IOU_CREATE_SCHEMA
|
||||
from ..schemas.iou import IOU_UPDATE_SCHEMA
|
||||
from ..schemas.iou import IOU_OBJECT_SCHEMA
|
||||
from ..schemas.iou import IOU_NIO_SCHEMA
|
||||
from ..schemas.iou import IOU_CAPTURE_SCHEMA
|
||||
from ..schemas.iou import IOU_INITIAL_CONFIG_SCHEMA
|
||||
from ..modules.iou import IOU
|
||||
from ...web.route import Route
|
||||
from ...modules.port_manager import PortManager
|
||||
from ...schemas.iou import IOU_CREATE_SCHEMA
|
||||
from ...schemas.iou import IOU_UPDATE_SCHEMA
|
||||
from ...schemas.iou import IOU_OBJECT_SCHEMA
|
||||
from ...schemas.iou import IOU_NIO_SCHEMA
|
||||
from ...schemas.iou import IOU_CAPTURE_SCHEMA
|
||||
from ...schemas.iou import IOU_INITIAL_CONFIG_SCHEMA
|
||||
from ...modules.iou import IOU
|
||||
|
||||
|
||||
class IOUHandler:
|
@ -15,9 +15,9 @@
|
||||
# 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 ..modules.port_manager import PortManager
|
||||
from ..utils.interfaces import interfaces
|
||||
from ...web.route import Route
|
||||
from ...modules.port_manager import PortManager
|
||||
from ...utils.interfaces import interfaces
|
||||
|
||||
|
||||
class NetworkHandler:
|
@ -15,10 +15,10 @@
|
||||
# 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.project import PROJECT_OBJECT_SCHEMA, PROJECT_CREATE_SCHEMA, PROJECT_UPDATE_SCHEMA
|
||||
from ..modules.project_manager import ProjectManager
|
||||
from ..modules import MODULES
|
||||
from ...web.route import Route
|
||||
from ...schemas.project import PROJECT_OBJECT_SCHEMA, PROJECT_CREATE_SCHEMA, PROJECT_UPDATE_SCHEMA
|
||||
from ...modules.project_manager import ProjectManager
|
||||
from ...modules import MODULES
|
||||
|
||||
|
||||
class ProjectHandler:
|
@ -18,14 +18,14 @@
|
||||
import os
|
||||
|
||||
|
||||
from ..web.route import Route
|
||||
from ..modules.port_manager import PortManager
|
||||
from ..schemas.qemu import QEMU_CREATE_SCHEMA
|
||||
from ..schemas.qemu import QEMU_UPDATE_SCHEMA
|
||||
from ..schemas.qemu import QEMU_OBJECT_SCHEMA
|
||||
from ..schemas.qemu import QEMU_NIO_SCHEMA
|
||||
from ..schemas.qemu import QEMU_BINARY_LIST_SCHEMA
|
||||
from ..modules.qemu import Qemu
|
||||
from ...web.route import Route
|
||||
from ...modules.port_manager import PortManager
|
||||
from ...schemas.qemu import QEMU_CREATE_SCHEMA
|
||||
from ...schemas.qemu import QEMU_UPDATE_SCHEMA
|
||||
from ...schemas.qemu import QEMU_OBJECT_SCHEMA
|
||||
from ...schemas.qemu import QEMU_NIO_SCHEMA
|
||||
from ...schemas.qemu import QEMU_BINARY_LIST_SCHEMA
|
||||
from ...modules.qemu import Qemu
|
||||
|
||||
|
||||
class QEMUHandler:
|
@ -15,9 +15,9 @@
|
||||
# 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 ...web.route import Route
|
||||
from ...schemas.version import VERSION_SCHEMA
|
||||
from ...version import __version__
|
||||
from aiohttp.web import HTTPConflict
|
||||
|
||||
|
@ -16,14 +16,14 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import os
|
||||
from ..web.route import Route
|
||||
from ..schemas.virtualbox import VBOX_CREATE_SCHEMA
|
||||
from ..schemas.virtualbox import VBOX_UPDATE_SCHEMA
|
||||
from ..schemas.virtualbox import VBOX_NIO_SCHEMA
|
||||
from ..schemas.virtualbox import VBOX_CAPTURE_SCHEMA
|
||||
from ..schemas.virtualbox import VBOX_OBJECT_SCHEMA
|
||||
from ..modules.virtualbox import VirtualBox
|
||||
from ..modules.project_manager import ProjectManager
|
||||
from ...web.route import Route
|
||||
from ...schemas.virtualbox import VBOX_CREATE_SCHEMA
|
||||
from ...schemas.virtualbox import VBOX_UPDATE_SCHEMA
|
||||
from ...schemas.virtualbox import VBOX_NIO_SCHEMA
|
||||
from ...schemas.virtualbox import VBOX_CAPTURE_SCHEMA
|
||||
from ...schemas.virtualbox import VBOX_OBJECT_SCHEMA
|
||||
from ...modules.virtualbox import VirtualBox
|
||||
from ...modules.project_manager import ProjectManager
|
||||
|
||||
|
||||
class VirtualBoxHandler:
|
@ -15,12 +15,12 @@
|
||||
# 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.vpcs import VPCS_CREATE_SCHEMA
|
||||
from ..schemas.vpcs import VPCS_UPDATE_SCHEMA
|
||||
from ..schemas.vpcs import VPCS_OBJECT_SCHEMA
|
||||
from ..schemas.vpcs import VPCS_NIO_SCHEMA
|
||||
from ..modules.vpcs import VPCS
|
||||
from ...web.route import Route
|
||||
from ...schemas.vpcs import VPCS_CREATE_SCHEMA
|
||||
from ...schemas.vpcs import VPCS_UPDATE_SCHEMA
|
||||
from ...schemas.vpcs import VPCS_OBJECT_SCHEMA
|
||||
from ...schemas.vpcs import VPCS_NIO_SCHEMA
|
||||
from ...modules.vpcs import VPCS
|
||||
|
||||
|
||||
class VPCSHandler:
|
34
gns3server/handlers/upload_handler.py
Normal file
34
gns3server/handlers/upload_handler.py
Normal 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")
|
||||
|
12
gns3server/templates/layout.html
Normal file
12
gns3server/templates/layout.html
Normal 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>
|
@ -1,20 +1,16 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
||||
<title>Upload Form for GNS3 server {{version}}</title>
|
||||
</head>
|
||||
<body>
|
||||
<p><h1>Select & Upload (v{{version}})</h1></p>
|
||||
<form enctype="multipart/form-data" action="/upload" method="post">
|
||||
File: <input type="file" name="file" />
|
||||
<br />
|
||||
<br />
|
||||
<input type="submit" value="upload" />
|
||||
</form>
|
||||
{%if items%}
|
||||
<h3>Files on {{host}}</h3>
|
||||
{%for item in items%}
|
||||
<p>{{path}}/{{item}}</a></p>
|
||||
{%end%}
|
||||
{%end%}
|
||||
</body>
|
||||
{% extends "layout.html" %}
|
||||
{% block body %}
|
||||
<h1>Select & Upload an image for GNS3</h1>
|
||||
<form enctype="multipart/form-data" action="/upload" method="post">
|
||||
File: <input type="file" name="file" />
|
||||
<br />
|
||||
<br />
|
||||
<input type="submit" value="Upload" />
|
||||
</form>
|
||||
{%if items%}
|
||||
<h3>Files on {{host}}</h3>
|
||||
{%for item in items%}
|
||||
<p>{{path}}/{{item}}</a></p>
|
||||
{%endfor%}
|
||||
{%endif%}
|
||||
{% endblock %}
|
||||
|
@ -20,9 +20,12 @@ import jsonschema
|
||||
import aiohttp.web
|
||||
import logging
|
||||
import sys
|
||||
import jinja2
|
||||
|
||||
from ..version import __version__
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
renderer = jinja2.Environment(loader=jinja2.PackageLoader('gns3server', 'templates'))
|
||||
|
||||
|
||||
class Response(aiohttp.web.Response):
|
||||
@ -47,6 +50,28 @@ class Response(aiohttp.web.Response):
|
||||
log.debug(json.loads(self.body.decode('utf-8')))
|
||||
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):
|
||||
"""
|
||||
Set the response content type to application/json and serialize
|
||||
|
@ -81,8 +81,11 @@ class Route(object):
|
||||
# This block is executed only the first time
|
||||
output_schema = kw.get("output", {})
|
||||
input_schema = kw.get("input", {})
|
||||
api_version = kw.get("version", 1)
|
||||
cls._path = "/v{version}{path}".format(path=path, version=api_version)
|
||||
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)
|
||||
|
||||
def register(func):
|
||||
route = cls._path
|
||||
@ -123,8 +126,13 @@ class Route(object):
|
||||
response.set_status(500)
|
||||
exc_type, exc_value, exc_tb = sys.exc_info()
|
||||
lines = traceback.format_exception(exc_type, exc_value, exc_tb)
|
||||
tb = "".join(lines)
|
||||
response.json({"message": tb, "status": 500})
|
||||
if api_version is not None:
|
||||
tb = "".join(lines)
|
||||
response.json({"message": tb, "status": 500})
|
||||
else:
|
||||
tb = "\n".join(lines)
|
||||
response.html("<h1>Internal error</h1><pre>{}</pre>".format(tb))
|
||||
|
||||
return response
|
||||
|
||||
cls._routes.append((method, cls._path, control_schema))
|
||||
|
@ -4,3 +4,4 @@ python-dateutil==2.3
|
||||
apache-libcloud==0.16.0
|
||||
requests==2.5.0
|
||||
aiohttp==0.14.4
|
||||
Jinja2==2.7.3
|
||||
|
3
setup.py
3
setup.py
@ -37,7 +37,8 @@ class PyTest(TestCommand):
|
||||
dependencies = ["aiohttp==0.14.4",
|
||||
"jsonschema==2.4.0",
|
||||
"apache-libcloud==0.16.0",
|
||||
"requests==2.5.0"]
|
||||
"requests==2.5.0",
|
||||
"Jinja2==2.7.3"]
|
||||
|
||||
if sys.version_info == (3, 3):
|
||||
dependencies.append("asyncio==3.4.2")
|
||||
|
@ -30,7 +30,7 @@ from gns3server.handlers import *
|
||||
from gns3server.modules import MODULES
|
||||
from gns3server.modules.port_manager import PortManager
|
||||
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
|
||||
|
@ -44,22 +44,25 @@ class Query:
|
||||
def delete(self, path, **kwargs):
|
||||
return self._fetch("DELETE", path, **kwargs)
|
||||
|
||||
def _get_url(self, path):
|
||||
return "http://{}:{}/v1{}".format(self._host, self._port, path)
|
||||
def _get_url(self, path, version):
|
||||
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
|
||||
|
||||
Options:
|
||||
- example if True the session is included inside documentation
|
||||
- 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):
|
||||
body = json.dumps(body)
|
||||
|
||||
@asyncio.coroutine
|
||||
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 = asyncio.Future()
|
||||
asyncio.async(go(future))
|
||||
@ -79,12 +82,16 @@ class Query:
|
||||
response.route = x_route.replace("/v1", "")
|
||||
|
||||
if response.body is not None:
|
||||
try:
|
||||
response.json = json.loads(response.body.decode("utf-8"))
|
||||
except ValueError:
|
||||
response.json = None
|
||||
if response.headers.get("CONTENT-TYPE", "") == "application/json":
|
||||
try:
|
||||
response.json = json.loads(response.body.decode("utf-8"))
|
||||
except ValueError:
|
||||
response.json = None
|
||||
else:
|
||||
response.html = response.body.decode("utf-8")
|
||||
else:
|
||||
response.json = {}
|
||||
response.html = ""
|
||||
if kwargs.get('example') and os.environ.get("PYTEST_BUILD_DOCUMENTATION") == "1":
|
||||
self._dump_example(method, response.route, body, response)
|
||||
return response
|
31
tests/handlers/test_upload.py
Normal file
31
tests/handlers/test_upload.py
Normal 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
|
Loading…
Reference in New Issue
Block a user