1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-25 16:28:11 +00:00

Return a compute name it could be different of compute id

This commit is contained in:
Julien Duponchelle 2016-05-23 11:20:52 +02:00
parent 3e6aec016b
commit f6a3899603
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
6 changed files with 48 additions and 5 deletions

View File

@ -38,7 +38,7 @@ class Compute:
A GNS3 compute. A GNS3 compute.
""" """
def __init__(self, compute_id, controller=None, protocol="http", host="localhost", port=8000, user=None, password=None): def __init__(self, compute_id, controller=None, protocol="http", host="localhost", port=8000, user=None, password=None, name=None):
assert controller is not None assert controller is not None
log.info("Create compute %s", compute_id) log.info("Create compute %s", compute_id)
self._id = compute_id self._id = compute_id
@ -52,6 +52,7 @@ class Compute:
self._set_auth(user, password) self._set_auth(user, password)
self._session = aiohttp.ClientSession() self._session = aiohttp.ClientSession()
self._version = None self._version = None
self.name = name
# If the compute is local but the compute id is local # If the compute is local but the compute id is local
# it's a configuration issue # it's a configuration issue
@ -79,6 +80,22 @@ class Compute:
""" """
return self._version return self._version
@property
def name(self):
"""
:returns: Compute name
"""
return self._name
@name.setter
def name(self, name):
if name is not None:
self._name = name
elif self._id == "local":
self._name = "local"
else:
self._name = "{}://{}:{}".format(self._protocol, self._host, self._port)
@property @property
def connected(self): def connected(self):
""" """
@ -133,6 +150,7 @@ class Compute:
def __json__(self): def __json__(self):
return { return {
"compute_id": self._id, "compute_id": self._id,
"name": self._name,
"protocol": self._protocol, "protocol": self._protocol,
"host": self._host, "host": self._host,
"port": self._port, "port": self._port,

View File

@ -25,6 +25,10 @@ COMPUTE_CREATE_SCHEMA = {
"description": "Server identifier", "description": "Server identifier",
"type": "string" "type": "string"
}, },
"name": {
"description": "Server name",
"type": "string"
},
"protocol": { "protocol": {
"description": "Server protocol", "description": "Server protocol",
"enum": ["http", "https"] "enum": ["http", "https"]
@ -59,6 +63,10 @@ COMPUTE_OBJECT_SCHEMA = {
"description": "Server identifier", "description": "Server identifier",
"type": "string" "type": "string"
}, },
"name": {
"description": "Server name",
"type": "string"
},
"protocol": { "protocol": {
"description": "Server protocol", "description": "Server protocol",
"enum": ["http", "https"] "enum": ["http", "https"]
@ -85,5 +93,5 @@ COMPUTE_OBJECT_SCHEMA = {
} }
}, },
"additionalProperties": False, "additionalProperties": False,
"required": ["compute_id", "protocol", "host", "port"] "required": ["compute_id", "protocol", "host", "port", "name"]
} }

View File

@ -29,6 +29,7 @@ in futur GNS3 versions.
<table border="1"> <table border="1">
<tr> <tr>
<th>ID</th> <th>ID</th>
<th>Name</th>
<th>Version</th> <th>Version</th>
<th>Connected</th> <th>Connected</th>
<th>Protocol</th> <th>Protocol</th>
@ -38,6 +39,7 @@ in futur GNS3 versions.
{% for compute in controller.computes.values() %} {% for compute in controller.computes.values() %}
<tr> <tr>
<td>{{compute.id}}</td> <td>{{compute.id}}</td>
<td>{{compute.name}}</td>
<td>{{compute.version}}</td> <td>{{compute.version}}</td>
<td>{{compute.connected}}</td> <td>{{compute.connected}}</td>
<td>{{compute.protocol}}</td> <td>{{compute.protocol}}</td>

View File

@ -39,6 +39,16 @@ def test_init(compute):
assert compute.id == "my_compute_id" assert compute.id == "my_compute_id"
def test_name():
c = Compute("my_compute_id", protocol="https", host="example.com", port=84, controller=MagicMock(), name=None)
assert c.name == "https://example.com:84"
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
c = Compute("local", protocol="https", host="example.com", port=84, controller=MagicMock(), name=None)
assert c.name == "local"
c = Compute("world", protocol="https", host="example.com", port=84, controller=MagicMock(), name="hello")
assert c.name == "hello"
def test_compute_local(compute): def test_compute_local(compute):
""" """
If the compute is local but the compute id is local If the compute is local but the compute id is local
@ -174,6 +184,7 @@ def test_json(compute):
compute.user = "test" compute.user = "test"
assert compute.__json__() == { assert compute.__json__() == {
"compute_id": "my_compute_id", "compute_id": "my_compute_id",
"name": compute.name,
"protocol": "https", "protocol": "https",
"host": "example.com", "host": "example.com",
"port": 84, "port": 84,

View File

@ -63,7 +63,8 @@ def test_load(controller, controller_config_path, async_run):
"host": "localhost", "host": "localhost",
"port": 8000, "port": 8000,
"protocol": "http", "protocol": "http",
"user": "admin" "user": "admin",
"name": "http://localhost:8000"
} }

View File

@ -62,7 +62,8 @@ def test_compute_list(http_controller, controller):
"host": "example.com", "host": "example.com",
"port": 84, "port": 84,
"user": "julien", "user": "julien",
"password": "secure" "password": "secure",
"name": "My super server"
} }
response = http_controller.post("/computes", params) response = http_controller.post("/computes", params)
assert response.status == 201 assert response.status == 201
@ -78,6 +79,8 @@ def test_compute_list(http_controller, controller):
'host': 'example.com', 'host': 'example.com',
'port': 84, 'port': 84,
'protocol': 'http', 'protocol': 'http',
'user': 'julien' 'user': 'julien',
'name': 'My super server'
} }
] ]