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

Add an endpoint for exporting the initial config file

This commit is contained in:
Julien Duponchelle 2015-02-17 14:52:51 +01:00
parent 57348d0508
commit 0e98497a99
4 changed files with 80 additions and 4 deletions

View File

@ -25,6 +25,7 @@ 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 ..modules.iou import IOU from ..modules.iou import IOU
@ -293,3 +294,21 @@ class IOUHandler:
port_number = int(request.match_info["port_number"]) port_number = int(request.match_info["port_number"])
yield from vm.stop_capture(adapter_number, port_number) yield from vm.stop_capture(adapter_number, port_number)
response.set_status(204) response.set_status(204)
@Route.get(
r"/projects/{project_id}/iou/vms/{vm_id}/initial_config",
status_codes={
200: "Initial config retrieved",
400: "Invalid request",
404: "Instance doesn't exist"
},
output=IOU_INITIAL_CONFIG_SCHEMA,
description="Retrieve the initial config content")
def show_initial_config(request, response):
iou_manager = IOU.instance()
vm = iou_manager.get_vm(request.match_info["vm_id"],
project_id=request.match_info["project_id"])
response.set_status(200)
response.json({"content": vm.initial_config,
"path": vm.relative_initial_config_file})

View File

@ -889,6 +889,21 @@ class IOUVM(BaseVM):
else: else:
return None return None
@property
def relative_initial_config_file(self):
"""
Returns the initial config file relative to the project directory.
It's compatible with pre 1.3 topologies.
:returns: path to config file. None if the file doesn't exist
"""
path = os.path.join(self.working_dir, 'initial-config.cfg')
if os.path.exists(path):
return path.replace(self.project.path, "")[1:]
else:
return None
@asyncio.coroutine @asyncio.coroutine
def start_capture(self, adapter_number, port_number, output_file, data_link_type="DLT_EN10MB"): def start_capture(self, adapter_number, port_number, output_file, data_link_type="DLT_EN10MB"):
""" """

View File

@ -281,3 +281,23 @@ IOU_CAPTURE_SCHEMA = {
"additionalProperties": False, "additionalProperties": False,
"required": ["capture_file_name", "data_link_type"] "required": ["capture_file_name", "data_link_type"]
} }
IOU_INITIAL_CONFIG_SCHEMA = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Request validation to get the initial configuration file",
"type": "object",
"properties": {
"content": {
"description": "Content of the initial configuration file",
"type": ["string", "null"],
"minLength": 1,
},
"path": {
"description": "Relative path on the server of the initial configuration file",
"type": ["string", "null"],
"minLength": 1,
},
},
"additionalProperties": False,
"required": ["content", "path"]
}

View File

@ -47,7 +47,9 @@ def vm(server, project, base_params):
def initial_config_file(project, vm): def initial_config_file(project, vm):
return os.path.join(project.path, "project-files", "iou", vm["vm_id"], "initial-config.cfg") directory = os.path.join(project.path, "project-files", "iou", vm["vm_id"])
os.makedirs(directory, exist_ok=True)
return os.path.join(directory, "initial-config.cfg")
def test_iou_create(server, project, base_params): def test_iou_create(server, project, base_params):
@ -208,18 +210,38 @@ def test_iou_start_capture(server, vm, tmpdir):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM.start_capture", return_value=True) as mock: with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM.start_capture", return_value=True) as mock:
params = {"capture_file_name": "test.pcap", "data_link_type": "DLT_EN10MB"} params = {"capture_file_name": "test.pcap", "data_link_type": "DLT_EN10MB"}
response = server.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=params) response = server.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=params, example=True)
assert mock.called assert mock.called
assert response.status == 200 assert response.status == 200
assert "test.pcap" in response.json["pcap_file_path"] assert "test.pcap" in response.json["pcap_file_path"]
def test_iou_stop_capture(server, vm, tmpdir): def test_iou_stop_capture(server, vm):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM.stop_capture", return_value=True) as mock: with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM.stop_capture", return_value=True) as mock:
response = server.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"])) response = server.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called assert mock.called
assert response.status == 204 assert response.status == 204
def test_get_initial_config_without_config_file(server, vm):
response = server.get("/projects/{project_id}/iou/vms/{vm_id}/initial_config".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 200
assert response.json["content"] == None
assert response.json["path"] == None
def test_get_initial_config_with_config_file(server, project, vm):
path = initial_config_file(project, vm)
with open(path, "w+") as f:
f.write("TEST")
response = server.get("/projects/{project_id}/iou/vms/{vm_id}/initial_config".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 200
assert response.json["content"] == "TEST"
assert response.json["path"] == "project-files/iou/{vm_id}/initial-config.cfg".format(vm_id=vm["vm_id"])