Skip to content

Commit

Permalink
Merge pull request #1194 from GNS3/direct-file-uploads
Browse files Browse the repository at this point in the history
Endpoint for obtaining direct action on compute
  • Loading branch information
grossmj committed Oct 16, 2017
2 parents 3a5a771 + b654d78 commit 4cae6b6
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
4 changes: 4 additions & 0 deletions gns3server/controller/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,10 @@ def _getUrl(self, path):
host = "127.0.0.1"
return "{}://{}:{}/v2/compute{}".format(self._protocol, host, self._port, path)

def get_url(self, path):
""" Returns URL for specific path at Compute"""
return self._getUrl(path)

@asyncio.coroutine
def _run_http_query(self, method, path, data=None, timeout=20, raw=False):
with Timeout(timeout):
Expand Down
30 changes: 29 additions & 1 deletion gns3server/handlers/api/controller/compute_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
from gns3server.schemas.compute import (
COMPUTE_CREATE_SCHEMA,
COMPUTE_OBJECT_SCHEMA,
COMPUTE_UPDATE_SCHEMA
COMPUTE_UPDATE_SCHEMA,
COMPUTE_ENDPOINT_OUTPUT_OBJECT_SCHEMA
)

import logging
Expand Down Expand Up @@ -93,6 +94,33 @@ def images(request, response):
res = yield from compute.images(request.match_info["emulator"])
response.json(res)

@Route.get(
r"/computes/endpoint/{compute_id}/{emulator}/{action:.+}",
parameters={
"compute_id": "Compute UUID"
},
status_codes={
200: "OK",
404: "Instance doesn't exist"
},
raw=True,
output=COMPUTE_ENDPOINT_OUTPUT_OBJECT_SCHEMA,
description="Returns the endpoint for particular `compute` to specific action.")
def endpoint(request, response):
controller = Controller.instance()
compute = controller.get_compute(request.match_info["compute_id"])

path = '/{emulator}/{action}'.format(
emulator=request.match_info['emulator'],
action=request.match_info['action'])

endpoint = compute.get_url(path)

response.set_status(200)
response.json(dict(
endpoint=endpoint
))

@Route.get(
r"/computes/{compute_id}/{emulator}/{action:.+}",
parameters={
Expand Down
14 changes: 14 additions & 0 deletions gns3server/schemas/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,17 @@
"additionalProperties": False,
"required": ["compute_id", "protocol", "host", "port", "name"]
}

COMPUTE_ENDPOINT_OUTPUT_OBJECT_SCHEMA = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Output schema for obtaining endpoint on compute",
"type": "object",
"properties": {
"endpoint": {
"description": "URL to endpoint on specific compute and to particular action",
"type": "string"
},
},
"additionalProperties": False,
}

7 changes: 7 additions & 0 deletions tests/controller/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ def test_getUrl(controller):
assert compute._getUrl("/test") == "https://[::1]:84/v2/compute/test"


def test_get_url(controller):
compute = Compute("my_compute_id", protocol="https", host="localhost", port=84, controller=controller)
with patch('gns3server.controller.compute.Compute._getUrl', return_value="returned") as getURL:
assert compute.get_url("/test") == 'returned'
getURL.assert_called_once_with('/test')


def test_host_ip(controller):
compute = Compute("my_compute_id", protocol="https", host="localhost", port=84, controller=controller)
assert compute.host_ip == "127.0.0.1"
Expand Down
15 changes: 15 additions & 0 deletions tests/handlers/api/controller/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,19 @@ def test_compute_autoidlepc(http_controller, controller):
assert response.status == 200


def test_compute_endpoint(http_controller, controller):
params = {
"compute_id": "my_compute",
"protocol": "http",
"host": "localhost",
"port": 84,
"user": "julien",
"password": "secure"
}
response = http_controller.post("/computes", params)
assert response.status == 201

response = http_controller.get("/computes/endpoint/my_compute/virtualbox/images")
assert response.status == 200
assert response.json['endpoint'] == 'http://localhost:84/v2/compute/virtualbox/images'

0 comments on commit 4cae6b6

Please sign in to comment.