1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-28 11:18:11 +00:00

Upload images API

Conflicts:
	requirements.txt
	setup.py
This commit is contained in:
Julien Duponchelle 2015-04-24 10:15:23 +02:00
parent 61fb641215
commit bb4ebbfe64
9 changed files with 121 additions and 4 deletions

View File

@ -463,3 +463,16 @@ class DynamipsVMHandler:
vms = yield from dynamips_manager.list_images() vms = yield from dynamips_manager.list_images()
response.set_status(200) response.set_status(200)
response.json(vms) response.json(vms)
@Route.post(
r"/dynamips/vms/{filename}",
status_codes={
204: "Image uploaded",
},
raw=True,
description="Upload Dynamips image.")
def upload_vm(request, response):
dynamips_manager = Dynamips.instance()
yield from dynamips_manager.write_image(request.match_info["filename"], request.content)
response.set_status(204)

View File

@ -330,3 +330,16 @@ class IOUHandler:
vms = yield from iou_manager.list_images() vms = yield from iou_manager.list_images()
response.set_status(200) response.set_status(200)
response.json(vms) response.json(vms)
@Route.post(
r"/iou/vms/{filename}",
status_codes={
204: "Image uploaded",
},
raw=True,
description="Upload IOU image.")
def upload_vm(request, response):
iou_manager = IOU.instance()
yield from iou_manager.write_image(request.match_info["filename"], request.content)
response.set_status(204)

View File

@ -305,3 +305,16 @@ class QEMUHandler:
vms = yield from qemu_manager.list_images() vms = yield from qemu_manager.list_images()
response.set_status(200) response.set_status(200)
response.json(vms) response.json(vms)
@Route.post(
r"/qemu/vms/{filename}",
status_codes={
204: "Image uploaded",
},
raw=True,
description="Upload Qemu image.")
def upload_vm(request, response):
qemu_manager = Qemu.instance()
yield from qemu_manager.write_image(request.match_info["filename"], request.content)
response.set_status(204)

View File

@ -444,3 +444,20 @@ class BaseManager:
""" """
raise NotImplementedError raise NotImplementedError
@asyncio.coroutine
def write_image(self, filename, stream):
directory = self.get_images_directory()
path = os.path.join(directory, os.path.basename(filename))
log.info("Writting image file %s", path)
try:
os.makedirs(directory, exist_ok=True)
with open(path, 'wb+') as f:
while True:
packet = yield from stream.read(512)
if not packet:
break
f.write(packet)
os.chmod(path, stat.S_IWRITE | stat.S_IREAD | stat.S_IEXEC)
except OSError as e:
raise aiohttp.web.HTTPConflict(text="Could not write image: {} to {}".format(filename, e))

View File

@ -89,6 +89,7 @@ class Route(object):
output_schema = kw.get("output", {}) output_schema = kw.get("output", {})
input_schema = kw.get("input", {}) input_schema = kw.get("input", {})
api_version = kw.get("api_version", 1) api_version = kw.get("api_version", 1)
raw = kw.get("raw", False)
# If it's a JSON api endpoint just register the endpoint an do nothing # If it's a JSON api endpoint just register the endpoint an do nothing
if api_version is None: if api_version is None:
@ -119,8 +120,9 @@ class Route(object):
# This block is executed at each method call # This block is executed at each method call
# Non API call # Non API call
if api_version is None: if api_version is None or raw is True:
response = Response(request=request, route=route, output_schema=output_schema) response = Response(request=request, route=route, output_schema=output_schema)
yield from func(request, response) yield from func(request, response)
return response return response

View File

@ -41,6 +41,7 @@ dependencies = [
"raven>=5.2.0" "raven>=5.2.0"
] ]
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

@ -146,3 +146,22 @@ def test_vms(server, tmpdir, fake_dynamips):
response = server.get("/dynamips/vms") response = server.get("/dynamips/vms")
assert response.status == 200 assert response.status == 200
assert response.json == [{"filename": "7200.bin"}] assert response.json == [{"filename": "7200.bin"}]
def test_upload_vm(server, tmpdir):
with patch("gns3server.modules.Dynamips.get_images_directory", return_value=str(tmpdir),):
response = server.post("/dynamips/vms/test2", body="TEST", raw=True)
assert response.status == 204
with open(str(tmpdir / "test2")) as f:
assert f.read() == "TEST"
def test_upload_vm_permission_denied(server, tmpdir):
with open(str(tmpdir / "test2"), "w+") as f:
f.write("")
os.chmod(str(tmpdir / "test2"), 0)
with patch("gns3server.modules.Dynamips.get_images_directory", return_value=str(tmpdir),):
response = server.post("/dynamips/vms/test2", body="TEST", raw=True)
assert response.status == 409

View File

@ -20,6 +20,7 @@ import os
import stat import stat
import sys import sys
import uuid import uuid
import aiohttp
from tests.utils import asyncio_patch from tests.utils import asyncio_patch
from unittest.mock import patch, MagicMock, PropertyMock from unittest.mock import patch, MagicMock, PropertyMock
@ -311,9 +312,28 @@ def test_get_initial_config_with_config_file(server, project, vm):
assert response.json["content"] == "TEST" assert response.json["content"] == "TEST"
def test_vms(server, vm, tmpdir, fake_iou_bin): def test_vms(server, tmpdir, fake_iou_bin):
with patch("gns3server.modules.IOU.get_images_directory", return_value=str(tmpdir), example=True): with patch("gns3server.modules.IOU.get_images_directory", return_value=str(tmpdir)):
response = server.get("/iou/vms") response = server.get("/iou/vms", example=True)
assert response.status == 200 assert response.status == 200
assert response.json == [{"filename": "iou.bin"}] assert response.json == [{"filename": "iou.bin"}]
def test_upload_vm(server, tmpdir):
with patch("gns3server.modules.IOU.get_images_directory", return_value=str(tmpdir),):
response = server.post("/iou/vms/test2", body="TEST", raw=True)
assert response.status == 204
with open(str(tmpdir / "test2")) as f:
assert f.read() == "TEST"
def test_upload_vm_permission_denied(server, tmpdir):
with open(str(tmpdir / "test2"), "w+") as f:
f.write("")
os.chmod(str(tmpdir / "test2"), 0)
with patch("gns3server.modules.IOU.get_images_directory", return_value=str(tmpdir),):
response = server.post("/iou/vms/test2", body="TEST", raw=True)
assert response.status == 409

View File

@ -200,3 +200,22 @@ def test_vms(server, tmpdir, fake_qemu_vm):
response = server.get("/qemu/vms") response = server.get("/qemu/vms")
assert response.status == 200 assert response.status == 200
assert response.json == [{"filename": "linux.img"}] assert response.json == [{"filename": "linux.img"}]
def test_upload_vm(server, tmpdir):
with patch("gns3server.modules.Qemu.get_images_directory", return_value=str(tmpdir),):
response = server.post("/qemu/vms/test2", body="TEST", raw=True)
assert response.status == 204
with open(str(tmpdir / "test2")) as f:
assert f.read() == "TEST"
def test_upload_vm_permission_denied(server, tmpdir):
with open(str(tmpdir / "test2"), "w+") as f:
f.write("")
os.chmod(str(tmpdir / "test2"), 0)
with patch("gns3server.modules.Qemu.get_images_directory", return_value=str(tmpdir),):
response = server.post("/qemu/vms/test2", body="TEST", raw=True)
assert response.status == 409