mirror of https://github.com/GNS3/gns3-server
This allow to push to the controller information about the connection to a server.pull/565/head
parent
84eb8356e8
commit
aad69e9650
@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2016 GNS3 Technologies Inc.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
class Server:
|
||||
"""
|
||||
A GNS3 server.
|
||||
"""
|
||||
|
||||
def __init__(self, server_id, protocol="http", host="localhost", port=8000, user=None, password=None):
|
||||
self._id = server_id
|
||||
self._protocol = protocol
|
||||
self._host = host
|
||||
self._port = port
|
||||
self._user = user
|
||||
self._password = password
|
||||
self._connected = False
|
||||
# The remote server version
|
||||
self._version = None
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
"""
|
||||
:returns: Server identifier (string)
|
||||
"""
|
||||
return self._id
|
||||
|
||||
@property
|
||||
def host(self):
|
||||
"""
|
||||
:returns: Server host (string)
|
||||
"""
|
||||
return self._host
|
||||
|
||||
def __json__(self):
|
||||
return {
|
||||
"server_id": self._id,
|
||||
"protocol": self._protocol,
|
||||
"host": self._host,
|
||||
"port": self._port,
|
||||
"user": self._user,
|
||||
"connected": self._connected,
|
||||
"version": self._version
|
||||
}
|
||||
|
||||
def __eq__(self, other):
|
||||
if not isinstance(other, Server):
|
||||
return False
|
||||
return other._id == self._id
|
@ -0,0 +1,89 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2015 GNS3 Technologies Inc.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
SERVER_CREATE_SCHEMA = {
|
||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||
"description": "Request validation to register a GNS3 server instance",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"server_id": {
|
||||
"description": "Server identifier",
|
||||
"type": "string"
|
||||
},
|
||||
"protocol": {
|
||||
"description": "Server protocol",
|
||||
"enum": ["http", "https"]
|
||||
},
|
||||
"host": {
|
||||
"description": "Server host",
|
||||
"type": "string"
|
||||
},
|
||||
"port": {
|
||||
"description": "Server port",
|
||||
"type": "integer"
|
||||
},
|
||||
"user": {
|
||||
"description": "User for auth",
|
||||
"type": "string"
|
||||
},
|
||||
"password": {
|
||||
"description": "Password for auth",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"additionalProperties": False,
|
||||
"required": ["server_id", "protocol", "host", "port"]
|
||||
}
|
||||
|
||||
SERVER_OBJECT_SCHEMA = {
|
||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||
"description": "Request validation to a GNS3 server object instance",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"server_id": {
|
||||
"description": "Server identifier",
|
||||
"type": "string"
|
||||
},
|
||||
"protocol": {
|
||||
"description": "Server protocol",
|
||||
"enum": ["http", "https"]
|
||||
},
|
||||
"host": {
|
||||
"description": "Server host",
|
||||
"type": "string"
|
||||
},
|
||||
"port": {
|
||||
"description": "Server port",
|
||||
"type": "integer"
|
||||
},
|
||||
"user": {
|
||||
"description": "User for auth",
|
||||
"type": "string"
|
||||
},
|
||||
"connected": {
|
||||
"description": "True if controller is connected to the server",
|
||||
"type": "boolean"
|
||||
},
|
||||
"version": {
|
||||
"description": "Version of the GNS3 remote server",
|
||||
"type": ["string", "null"]
|
||||
}
|
||||
},
|
||||
"additionalProperties": False,
|
||||
"required": ["server_id", "protocol", "host", "port"]
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2016 GNS3 Technologies Inc.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# 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 pytest
|
||||
|
||||
from gns3server.controller.server import Server
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def server():
|
||||
return Server("my_server_id", protocol="https", host="example.com", port=84, user="test", password="secure")
|
||||
|
||||
|
||||
def test_init(server):
|
||||
assert server.id == "my_server_id"
|
||||
|
||||
|
||||
def test_json(server):
|
||||
assert server.__json__() == {
|
||||
"server_id": "my_server_id",
|
||||
"protocol": "https",
|
||||
"host": "example.com",
|
||||
"port": 84,
|
||||
"user": "test",
|
||||
"connected": False,
|
||||
"version": None
|
||||
}
|
||||
|
||||
|
||||
def test__eq__(server):
|
||||
assert server != 1
|
||||
assert server == server
|
||||
assert server == Server("my_server_id")
|
||||
assert server != Server("test")
|
@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2016 GNS3 Technologies Inc.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
def test_server_create(server, controller):
|
||||
|
||||
params = {
|
||||
"server_id": "my_server_id",
|
||||
"protocol": "http",
|
||||
"host": "example.com",
|
||||
"port": 84,
|
||||
"user": "julien",
|
||||
"password": "secure"
|
||||
}
|
||||
response = server.post("/servers", params)
|
||||
assert response.status == 201
|
||||
assert response.route == "/servers"
|
||||
assert response.json["user"] == "julien"
|
||||
assert "password" not in response.json
|
||||
|
||||
assert len(controller.servers) == 1
|
||||
assert controller.servers["my_server_id"].host == "example.com"
|
Loading…
Reference in new issue