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

Return compute port information via API. Ref #1420.

This commit is contained in:
grossmj 2018-10-05 12:48:20 +02:00
parent eb97ebf311
commit 22f7ca0243
5 changed files with 98 additions and 2 deletions

View File

@ -67,6 +67,13 @@ class PortManager:
cls._instance = cls()
return cls._instance
def __json__(self):
return {"console_port_range": self._console_port_range,
"console_ports": list(self._used_tcp_ports),
"udp_port_range": self._udp_port_range,
"udp_ports": list(self._used_udp_ports)}
@property
def console_host(self):

View File

@ -769,3 +769,15 @@ class Controller:
yield from project.delete()
self.remove_project(project)
return res
@asyncio.coroutine
def compute_ports(self, compute_id):
"""
Get the ports used by a compute.
:param compute_id: ID of the compute
"""
compute = self.get_compute(compute_id)
response = yield from compute.get("/network/ports")
return response.json

View File

@ -49,3 +49,11 @@ class NetworkHandler:
network_interfaces = interfaces()
response.json(network_interfaces)
@Route.get(
r"/network/ports",
description="List all the ports used by the server")
def network_ports(request, response):
m = PortManager.instance()
response.json(m)

View File

@ -22,7 +22,8 @@ from gns3server.schemas.compute import (
COMPUTE_CREATE_SCHEMA,
COMPUTE_OBJECT_SCHEMA,
COMPUTE_UPDATE_SCHEMA,
COMPUTE_ENDPOINT_OUTPUT_OBJECT_SCHEMA
COMPUTE_ENDPOINT_OUTPUT_OBJECT_SCHEMA,
COMPUTE_PORTS_OBJECT_SCHEMA
)
import logging
@ -214,4 +215,19 @@ class ComputeHandler:
controller = Controller.instance()
res = yield from controller.autoidlepc(request.match_info["compute_id"], request.json["platform"], request.json["image"], request.json["ram"])
response.json(res)
response.set_status(200)
@Route.get(
r"/computes/{compute_id}/ports",
parameters={
"compute_id": "Compute UUID"
},
status_codes={
200: "Ports information returned",
},
description="Get ports used by a compute",
output=COMPUTE_PORTS_OBJECT_SCHEMA)
def ports(request, response):
controller = Controller.instance()
res = yield from controller.compute_ports(request.match_info["compute_id"])
response.json(res)

View File

@ -127,3 +127,56 @@ COMPUTE_ENDPOINT_OUTPUT_OBJECT_SCHEMA = {
"additionalProperties": False,
}
COMPUTE_PORTS_OBJECT_SCHEMA = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Output schema for open ports on a compute",
"type": "object",
"properties": {
"console_port_range": {
"description": "Console port range",
"type": "array",
"uniqueItems": True,
"minItems": 2,
"maxItems": 2,
"items": {
"type": "integer",
"minimum": 1,
"maximum": 65535
}
},
"console_ports": {
"description": "Console ports used by the compute",
"type": "array",
"uniqueItems": True,
"items": {
"type": "integer",
"minimum": 1,
"maximum": 65535
}
},
"udp_port_range": {
"description": "UDP port range",
"type": "array",
"uniqueItems": True,
"minItems": 2,
"maxItems": 2,
"items": {
"type": "integer",
"minimum": 1,
"maximum": 65535
}
},
"udp_ports": {
"description": "UDP ports used by the compute",
"type": "array",
"uniqueItems": True,
"items": {
"type": "integer",
"minimum": 1,
"maximum": 65535
}
},
},
"additionalProperties": False,
}