mirror of
https://github.com/GNS3/gns3-server
synced 2024-11-28 11:18:11 +00:00
API for editing a file on a Node
This commit is contained in:
parent
11af6f4f82
commit
7e40eb02e6
@ -358,7 +358,7 @@ class Compute:
|
||||
return "{}://{}:{}/v2/compute{}".format(self._protocol, self._host, self._port, path)
|
||||
|
||||
@asyncio.coroutine
|
||||
def _run_http_query(self, method, path, data=None, timeout=10):
|
||||
def _run_http_query(self, method, path, data=None, timeout=10, raw=False):
|
||||
with Timeout(timeout):
|
||||
url = self._getUrl(path)
|
||||
headers = {}
|
||||
@ -370,7 +370,7 @@ class Compute:
|
||||
if hasattr(data, '__json__'):
|
||||
data = json.dumps(data.__json__())
|
||||
# Stream the request
|
||||
elif isinstance(data, aiohttp.streams.StreamReader) or isinstance(data, io.BufferedIOBase):
|
||||
elif isinstance(data, aiohttp.streams.StreamReader) or isinstance(data, io.BufferedIOBase) or isinstance(data, bytes):
|
||||
chunked = True
|
||||
headers['content-type'] = 'application/octet-stream'
|
||||
else:
|
||||
@ -378,12 +378,12 @@ class Compute:
|
||||
|
||||
response = yield from self._session().request(method, url, headers=headers, data=data, auth=self._auth, chunked=chunked)
|
||||
body = yield from response.read()
|
||||
if body:
|
||||
if body and not raw:
|
||||
body = body.decode()
|
||||
|
||||
if response.status >= 300:
|
||||
# Try to decode the GNS3 error
|
||||
if body:
|
||||
if body and not raw:
|
||||
try:
|
||||
msg = json.loads(body)["message"]
|
||||
except (KeyError, ValueError):
|
||||
@ -412,12 +412,16 @@ class Compute:
|
||||
else:
|
||||
raise NotImplementedError("{} status code is not supported".format(response.status))
|
||||
if body and len(body):
|
||||
if raw:
|
||||
response.body = body
|
||||
else:
|
||||
try:
|
||||
response.json = json.loads(body)
|
||||
except ValueError:
|
||||
raise aiohttp.web.HTTPConflict(text="The server {} is not a GNS3 server".format(self._id))
|
||||
else:
|
||||
response.json = {}
|
||||
response.body = b""
|
||||
return response
|
||||
|
||||
@asyncio.coroutine
|
||||
|
@ -15,6 +15,7 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import os
|
||||
import aiohttp
|
||||
|
||||
from gns3server.web.route import Route
|
||||
@ -314,3 +315,77 @@ class NodeHandler:
|
||||
idle = yield from node.dynamips_idlepc_proposals()
|
||||
response.json(idle)
|
||||
response.set_status(200)
|
||||
|
||||
@Route.get(
|
||||
r"/projects/{project_id}/nodes/{node_id}/files/{path:.+}",
|
||||
parameters={
|
||||
"project_id": "Project UUID",
|
||||
"node_id": "Node UUID"
|
||||
},
|
||||
status_codes={
|
||||
204: "Instance reloaded",
|
||||
400: "Invalid request",
|
||||
404: "Instance doesn't exist"
|
||||
},
|
||||
description="Get a file in the node directory")
|
||||
def get_file(request, response):
|
||||
|
||||
project = Controller.instance().get_project(request.match_info["project_id"])
|
||||
node = project.get_node(request.match_info["node_id"])
|
||||
path = request.match_info["path"]
|
||||
path = os.path.normpath(path)
|
||||
|
||||
# Raise error if user try to escape
|
||||
if path[0] == ".":
|
||||
raise aiohttp.web.HTTPForbidden
|
||||
|
||||
node_type = node.node_type
|
||||
if node_type == "dynamips":
|
||||
path = "/project-files/{}/{}".format(node_type, path)
|
||||
else:
|
||||
path = "/project-files/{}/{}/{}".format(node_type, node.id, path)
|
||||
|
||||
res = yield from node.compute.http_query("GET", "/projects/{project_id}/files{path}".format(project_id=project.id, path=path), timeout=None, raw=True)
|
||||
response.set_status(200)
|
||||
response.content_type = "application/octet-stream"
|
||||
response.enable_chunked_encoding()
|
||||
response.content_length = None
|
||||
response.start(request)
|
||||
|
||||
response.write(res.body)
|
||||
yield from response.write_eof()
|
||||
|
||||
@Route.post(
|
||||
r"/projects/{project_id}/nodes/{node_id}/files/{path:.+}",
|
||||
parameters={
|
||||
"project_id": "Project UUID",
|
||||
"node_id": "Node UUID"
|
||||
},
|
||||
status_codes={
|
||||
204: "Instance reloaded",
|
||||
400: "Invalid request",
|
||||
404: "Instance doesn't exist"
|
||||
},
|
||||
raw=True,
|
||||
description="Write a file in the node directory")
|
||||
def post_file(request, response):
|
||||
|
||||
project = Controller.instance().get_project(request.match_info["project_id"])
|
||||
node = project.get_node(request.match_info["node_id"])
|
||||
path = request.match_info["path"]
|
||||
path = os.path.normpath(path)
|
||||
|
||||
# Raise error if user try to escape
|
||||
if path[0] == ".":
|
||||
raise aiohttp.web.HTTPForbidden
|
||||
|
||||
node_type = node.node_type
|
||||
if node_type == "dynamips":
|
||||
path = "/project-files/{}/{}".format(node_type, path)
|
||||
else:
|
||||
path = "/project-files/{}/{}/{}".format(node_type, node.id, path)
|
||||
|
||||
data = yield from request.content.read()
|
||||
|
||||
res = yield from node.compute.http_query("POST", "/projects/{project_id}/files{path}".format(project_id=project.id, path=path), data=data, timeout=None, raw=True)
|
||||
response.set_status(201)
|
||||
|
@ -199,3 +199,28 @@ def test_dynamips_idlepc_proposals(http_controller, tmpdir, project, compute, no
|
||||
response = http_controller.get("/projects/{}/nodes/{}/dynamips/idlepc_proposals".format(project.id, node.id), example=True)
|
||||
assert response.status == 200
|
||||
assert response.json == ["0x60606f54", "0x33805a22"]
|
||||
|
||||
|
||||
def test_get_file(http_controller, tmpdir, project, node, compute):
|
||||
response = MagicMock()
|
||||
response.body = b"world"
|
||||
compute.http_query = AsyncioMagicMock(return_value=response)
|
||||
response = http_controller.get("/projects/{project_id}/nodes/{node_id}/files/hello".format(project_id=project.id, node_id=node.id), raw=True)
|
||||
assert response.status == 200
|
||||
assert response.body == b'world'
|
||||
|
||||
compute.http_query.assert_called_with("GET", "/projects/{project_id}/files/project-files/vpcs/{node_id}/hello".format(project_id=project.id, node_id=node.id), timeout=None, raw=True)
|
||||
|
||||
response = http_controller.get("/projects/{project_id}/nodes/{node_id}/files/../hello".format(project_id=project.id, node_id=node.id), raw=True)
|
||||
assert response.status == 403
|
||||
|
||||
|
||||
def test_post_file(http_controller, tmpdir, project, node, compute):
|
||||
compute.http_query = AsyncioMagicMock()
|
||||
response = http_controller.post("/projects/{project_id}/nodes/{node_id}/files/hello".format(project_id=project.id, node_id=node.id), body=b"hello", raw=True)
|
||||
assert response.status == 201
|
||||
|
||||
compute.http_query.assert_called_with("POST", "/projects/{project_id}/files/project-files/vpcs/{node_id}/hello".format(project_id=project.id, node_id=node.id), data=b'hello', timeout=None, raw=True)
|
||||
|
||||
response = http_controller.get("/projects/{project_id}/nodes/{node_id}/files/../hello".format(project_id=project.id, node_id=node.id), raw=True)
|
||||
assert response.status == 403
|
||||
|
Loading…
Reference in New Issue
Block a user