mirror of
https://github.com/GNS3/gns3-server
synced 2024-11-28 11:18:11 +00:00
Rename /controller/servers to /controller/hypervisors
This commit is contained in:
parent
8114c1d4be
commit
6fa2491255
@ -20,10 +20,10 @@ from ..config import Config
|
|||||||
|
|
||||||
|
|
||||||
class Controller:
|
class Controller:
|
||||||
"""The controller manage multiple gns3 servers"""
|
"""The controller manage multiple gns3 hypervisors"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._servers = {}
|
self._hypervisors = {}
|
||||||
|
|
||||||
def isEnabled(self):
|
def isEnabled(self):
|
||||||
"""
|
"""
|
||||||
@ -32,18 +32,18 @@ class Controller:
|
|||||||
"""
|
"""
|
||||||
return Config.instance().get_section_config("Server").getboolean("controller")
|
return Config.instance().get_section_config("Server").getboolean("controller")
|
||||||
|
|
||||||
def addServer(self, server):
|
def addHypervisor(self, hypervisor):
|
||||||
"""
|
"""
|
||||||
Add a server to the dictionnary of servers controlled by GNS3
|
Add a server to the dictionnary of hypervisors controlled by GNS3
|
||||||
"""
|
"""
|
||||||
self._servers[server.id] = server
|
self._hypervisors[hypervisor.id] = hypervisor
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def servers(self):
|
def hypervisors(self):
|
||||||
"""
|
"""
|
||||||
:returns: The dictionnary of servers managed by GNS3
|
:returns: The dictionnary of hypervisors managed by GNS3
|
||||||
"""
|
"""
|
||||||
return self._servers
|
return self._hypervisors
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def instance():
|
def instance():
|
||||||
|
@ -25,50 +25,50 @@ import logging
|
|||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class ServerError(ControllerError):
|
class HypervisorError(ControllerError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Server:
|
class Hypervisor:
|
||||||
"""
|
"""
|
||||||
A GNS3 server.
|
A GNS3 hypervisor.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, server_id, protocol="http", host="localhost", port=8000, user=None, password=None):
|
def __init__(self, hypervisor_id, protocol="http", host="localhost", port=8000, user=None, password=None):
|
||||||
log.info("Create server %s", server_id)
|
log.info("Create hypervisor %s", hypervisor_id)
|
||||||
self._id = server_id
|
self._id = hypervisor_id
|
||||||
self._protocol = protocol
|
self._protocol = protocol
|
||||||
self._host = host
|
self._host = host
|
||||||
self._port = port
|
self._port = port
|
||||||
self._user = user
|
self._user = user
|
||||||
self._password = password
|
self._password = password
|
||||||
self._connected = False
|
self._connected = False
|
||||||
# The remote server version
|
# The remote hypervisor version
|
||||||
# TODO: For the moment it's fake we return the controller version
|
# TODO: For the moment it's fake we return the controller version
|
||||||
self._version = __version__
|
self._version = __version__
|
||||||
|
|
||||||
# If the server is local but the server id is local
|
# If the hypervisor is local but the hypervisor id is local
|
||||||
# it's a configuration issue
|
# it's a configuration issue
|
||||||
if server_id == "local" and Config.instance().get_section_config("Server")["local"] is False:
|
if hypervisor_id == "local" and Config.instance().get_section_config("Hypervisor")["local"] is False:
|
||||||
raise ServerError("The local server is started without --local")
|
raise HypervisorError("The local hypervisor is started without --local")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def id(self):
|
def id(self):
|
||||||
"""
|
"""
|
||||||
:returns: Server identifier (string)
|
:returns: Hypervisor identifier (string)
|
||||||
"""
|
"""
|
||||||
return self._id
|
return self._id
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def host(self):
|
def host(self):
|
||||||
"""
|
"""
|
||||||
:returns: Server host (string)
|
:returns: Hypervisor host (string)
|
||||||
"""
|
"""
|
||||||
return self._host
|
return self._host
|
||||||
|
|
||||||
def __json__(self):
|
def __json__(self):
|
||||||
return {
|
return {
|
||||||
"server_id": self._id,
|
"hypervisor_id": self._id,
|
||||||
"protocol": self._protocol,
|
"protocol": self._protocol,
|
||||||
"host": self._host,
|
"host": self._host,
|
||||||
"port": self._port,
|
"port": self._port,
|
@ -15,5 +15,5 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from .server_handler import ServerHandler
|
from .hypervisor_handler import HypervisorHandler
|
||||||
from .version_handler import VersionHandler
|
from .version_handler import VersionHandler
|
||||||
|
@ -21,48 +21,48 @@ from aiohttp.web import HTTPForbidden
|
|||||||
from ....web.route import Route
|
from ....web.route import Route
|
||||||
from ....config import Config
|
from ....config import Config
|
||||||
from ....modules.project_manager import ProjectManager
|
from ....modules.project_manager import ProjectManager
|
||||||
from ....schemas.server import SERVER_CREATE_SCHEMA, SERVER_OBJECT_SCHEMA
|
from ....schemas.hypervisor import HYPERVISOR_CREATE_SCHEMA, HYPERVISOR_OBJECT_SCHEMA
|
||||||
from ....controller import Controller
|
from ....controller import Controller
|
||||||
from ....controller.server import Server
|
from ....controller.hypervisor import Hypervisor
|
||||||
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class ServerHandler:
|
class HypervisorHandler:
|
||||||
"""API entry points for server management."""
|
"""API entry points for hypervisor management."""
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@Route.post(
|
@Route.post(
|
||||||
r"/servers",
|
r"/hypervisors",
|
||||||
description="Register a server",
|
description="Register a hypervisor",
|
||||||
status_codes={
|
status_codes={
|
||||||
201: "Server added"
|
201: "Hypervisor added"
|
||||||
},
|
},
|
||||||
input=SERVER_CREATE_SCHEMA,
|
input=HYPERVISOR_CREATE_SCHEMA,
|
||||||
output=SERVER_OBJECT_SCHEMA)
|
output=HYPERVISOR_OBJECT_SCHEMA)
|
||||||
def create(request, response):
|
def create(request, response):
|
||||||
|
|
||||||
server = Server(request.json.pop("server_id"), **request.json)
|
hypervisor = Hypervisor(request.json.pop("hypervisor_id"), **request.json)
|
||||||
Controller.instance().addServer(server)
|
Controller.instance().addHypervisor(hypervisor)
|
||||||
|
|
||||||
response.set_status(201)
|
response.set_status(201)
|
||||||
response.json(server)
|
response.json(hypervisor)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@Route.post(
|
@Route.post(
|
||||||
r"/servers/shutdown",
|
r"/hypervisors/shutdown",
|
||||||
description="Shutdown the local server",
|
description="Shutdown the local hypervisor",
|
||||||
status_codes={
|
status_codes={
|
||||||
201: "Server is shutting down",
|
201: "Hypervisor is shutting down",
|
||||||
403: "Server shutdown refused"
|
403: "Hypervisor shutdown refused"
|
||||||
})
|
})
|
||||||
def shutdown(request, response):
|
def shutdown(request, response):
|
||||||
|
|
||||||
config = Config.instance()
|
config = Config.instance()
|
||||||
if config.get_section_config("Server").getboolean("local", False) is False:
|
if config.get_section_config("Hypervisor").getboolean("local", False) is False:
|
||||||
raise HTTPForbidden(text="You can only stop a local server")
|
raise HTTPForbidden(text="You can only stop a local hypervisor")
|
||||||
|
|
||||||
# close all the projects first
|
# close all the projects first
|
||||||
pm = ProjectManager.instance()
|
pm = ProjectManager.instance()
|
||||||
@ -81,7 +81,7 @@ class ServerHandler:
|
|||||||
log.error("Could not close project {}".format(e), exc_info=1)
|
log.error("Could not close project {}".format(e), exc_info=1)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# then shutdown the server itself
|
# then shutdown the hypervisor itself
|
||||||
from gns3server.web.web_server import WebServer
|
from gns3server.web.web_server import WebServer
|
||||||
server = WebServer.instance()
|
server = WebServer.instance()
|
||||||
asyncio.async(server.shutdown_server())
|
asyncio.async(server.shutdown_server())
|
@ -16,12 +16,12 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
SERVER_CREATE_SCHEMA = {
|
HYPERVISOR_CREATE_SCHEMA = {
|
||||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
"description": "Request validation to register a GNS3 server instance",
|
"description": "Request validation to register a GNS3 hypervisor instance",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"server_id": {
|
"hypervisor_id": {
|
||||||
"description": "Server identifier",
|
"description": "Server identifier",
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
@ -47,15 +47,15 @@ SERVER_CREATE_SCHEMA = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": False,
|
"additionalProperties": False,
|
||||||
"required": ["server_id", "protocol", "host", "port"]
|
"required": ["hypervisor_id", "protocol", "host", "port"]
|
||||||
}
|
}
|
||||||
|
|
||||||
SERVER_OBJECT_SCHEMA = {
|
HYPERVISOR_OBJECT_SCHEMA = {
|
||||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
"description": "Request validation to a GNS3 server object instance",
|
"description": "Request validation to a GNS3 hypervisor object instance",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"server_id": {
|
"hypervisor_id": {
|
||||||
"description": "Server identifier",
|
"description": "Server identifier",
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
@ -76,14 +76,14 @@ SERVER_OBJECT_SCHEMA = {
|
|||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
"connected": {
|
"connected": {
|
||||||
"description": "True if controller is connected to the server",
|
"description": "True if controller is connected to the hypervisor",
|
||||||
"type": "boolean"
|
"type": "boolean"
|
||||||
},
|
},
|
||||||
"version": {
|
"version": {
|
||||||
"description": "Version of the GNS3 remote server",
|
"description": "Version of the GNS3 remote hypervisor",
|
||||||
"type": ["string", "null"]
|
"type": ["string", "null"]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": False,
|
"additionalProperties": False,
|
||||||
"required": ["server_id", "protocol", "host", "port"]
|
"required": ["hypervisor_id", "protocol", "host", "port"]
|
||||||
}
|
}
|
@ -18,7 +18,7 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from gns3server.controller import Controller
|
from gns3server.controller import Controller
|
||||||
from gns3server.controller.server import Server
|
from gns3server.controller.hypervisor import Hypervisor
|
||||||
from gns3server.config import Config
|
from gns3server.config import Config
|
||||||
|
|
||||||
|
|
||||||
@ -29,12 +29,12 @@ def test_isEnabled(controller):
|
|||||||
assert controller.isEnabled()
|
assert controller.isEnabled()
|
||||||
|
|
||||||
|
|
||||||
def test_addServer(controller):
|
def test_addHypervisor(controller):
|
||||||
server1 = Server("test1")
|
hypervisor1 = Hypervisor("test1")
|
||||||
|
|
||||||
controller.addServer(server1)
|
controller.addHypervisor(hypervisor1)
|
||||||
assert len(controller.servers) == 1
|
assert len(controller.hypervisors) == 1
|
||||||
controller.addServer(Server("test1"))
|
controller.addHypervisor(Hypervisor("test1"))
|
||||||
assert len(controller.servers) == 1
|
assert len(controller.hypervisors) == 1
|
||||||
controller.addServer(Server("test2"))
|
controller.addHypervisor(Hypervisor("test2"))
|
||||||
assert len(controller.servers) == 2
|
assert len(controller.hypervisors) == 2
|
||||||
|
@ -19,36 +19,36 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from gns3server.controller.server import Server, ServerError
|
from gns3server.controller.hypervisor import Hypervisor, HypervisorError
|
||||||
from gns3server.version import __version__
|
from gns3server.version import __version__
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def server():
|
def hypervisor():
|
||||||
return Server("my_server_id", protocol="https", host="example.com", port=84, user="test", password="secure")
|
return Hypervisor("my_hypervisor_id", protocol="https", host="example.com", port=84, user="test", password="secure")
|
||||||
|
|
||||||
|
|
||||||
def test_init(server):
|
def test_init(hypervisor):
|
||||||
assert server.id == "my_server_id"
|
assert hypervisor.id == "my_hypervisor_id"
|
||||||
|
|
||||||
|
|
||||||
def test_server_local(server):
|
def test_hypervisor_local(hypervisor):
|
||||||
"""
|
"""
|
||||||
If the server is local but the server id is local
|
If the hypervisor is local but the hypervisor id is local
|
||||||
it's a configuration issue
|
it's a configuration issue
|
||||||
"""
|
"""
|
||||||
|
|
||||||
with patch("gns3server.config.Config.get_section_config", return_value={"local": False}):
|
with patch("gns3server.config.Config.get_section_config", return_value={"local": False}):
|
||||||
with pytest.raises(ServerError):
|
with pytest.raises(HypervisorError):
|
||||||
s = Server("local")
|
s = Hypervisor("local")
|
||||||
|
|
||||||
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
|
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
|
||||||
s = Server("test")
|
s = Hypervisor("test")
|
||||||
|
|
||||||
|
|
||||||
def test_json(server):
|
def test_json(hypervisor):
|
||||||
assert server.__json__() == {
|
assert hypervisor.__json__() == {
|
||||||
"server_id": "my_server_id",
|
"hypervisor_id": "my_hypervisor_id",
|
||||||
"protocol": "https",
|
"protocol": "https",
|
||||||
"host": "example.com",
|
"host": "example.com",
|
||||||
"port": 84,
|
"port": 84,
|
@ -16,21 +16,21 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
def test_server_create(http_controller, controller):
|
def test_hypervisor_create(http_controller, controller):
|
||||||
|
|
||||||
params = {
|
params = {
|
||||||
"server_id": "my_server_id",
|
"hypervisor_id": "my_hypervisor_id",
|
||||||
"protocol": "http",
|
"protocol": "http",
|
||||||
"host": "example.com",
|
"host": "example.com",
|
||||||
"port": 84,
|
"port": 84,
|
||||||
"user": "julien",
|
"user": "julien",
|
||||||
"password": "secure"
|
"password": "secure"
|
||||||
}
|
}
|
||||||
response = http_controller.post("/servers", params, example=True)
|
response = http_controller.post("/hypervisors", params, example=True)
|
||||||
assert response.status == 201
|
assert response.status == 201
|
||||||
assert response.route == "/servers"
|
assert response.route == "/hypervisors"
|
||||||
assert response.json["user"] == "julien"
|
assert response.json["user"] == "julien"
|
||||||
assert "password" not in response.json
|
assert "password" not in response.json
|
||||||
|
|
||||||
assert len(controller.servers) == 1
|
assert len(controller.hypervisors) == 1
|
||||||
assert controller.servers["my_server_id"].host == "example.com"
|
assert controller.hypervisors["my_hypervisor_id"].host == "example.com"
|
@ -39,4 +39,4 @@ def test_documentation_write(tmpdir):
|
|||||||
assert "Sample session" in content
|
assert "Sample session" in content
|
||||||
assert "literalinclude:: ../../../examples/hypervisor_post_projectsprojectidvirtualboxvms.txt" in content
|
assert "literalinclude:: ../../../examples/hypervisor_post_projectsprojectidvirtualboxvms.txt" in content
|
||||||
|
|
||||||
assert os.path.exists(str(tmpdir / "api" / "v2" / "controller" / "server.rst"))
|
assert os.path.exists(str(tmpdir / "api" / "v2" / "controller" / "hypervisor.rst"))
|
||||||
|
Loading…
Reference in New Issue
Block a user