Method for reloading a topology (not bind to an api handler)

Ref https://github.com/GNS3/gns3-gui/issues/1243
This commit is contained in:
Julien Duponchelle committed 2016-06-15 16:26:16 +02:00
1 parent 3aea16c527
commit 742243e9df
4 files changed
+159 -4

No files matched your search

+24
View File
@@ -26,6 +26,7 @@ from .project import Project
from .compute import Compute
from .notification import Notification
from ..version import __version__
from .topology import load_topology
import logging
log = logging.getLogger(__name__)
@@ -185,6 +186,29 @@ class Controller:
def remove_project(self, project):
del self._projects[project.id]
@asyncio.coroutine
def load_project(self, path):
"""
Load a project from a .gns3
:param path: Path of the .gns3
"""
topo_data = load_topology(path)
topology = topo_data.pop("topology")
topo_data.pop("version")
topo_data.pop("revision")
topo_data.pop("type")
project = yield from self.add_project(path=os.path.dirname(path), **topo_data)
for compute in topology["computes"]:
yield from self.add_compute(**compute)
for node in topology["nodes"]:
compute = self.get_compute(node.pop("compute_id"))
name = node.pop("name")
node_id = node.pop("node_id")
yield from project.add_node(compute, name, node_id, **node)
@property
def projects(self):
"""
+20 -2
View File
@@ -15,8 +15,12 @@
# 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 json
import aiohttp
from ..version import __version__
GNS3_FILE_FORMAT_REVISION = 5
def project_to_topology(project):
"""
@@ -31,7 +35,7 @@ def project_to_topology(project):
"computes": []
},
"type": "topology",
"revision": 5,
"revision": GNS3_FILE_FORMAT_REVISION,
"version": __version__
}
@@ -44,6 +48,20 @@ def project_to_topology(project):
for compute in computes:
if hasattr(compute, "__json__"):
data["topology"]["computes"].append(compute.__json__())
print(data)
#TODO: check JSON schema
return data
def load_topology(path):
"""
Open a topology file, patch it for last GNS3 release and return it
"""
try:
with open(path) as f:
topo = json.load(f)
except OSError as e:
raise aiohttp.web.HTTPConflict(text="Could not load topology {}: {}".format(path, str(e)))
#TODO: Check JSON schema
if topo["revision"] < GNS3_FILE_FORMAT_REVISION:
raise aiohttp.web.HTTPConflict(text="Old GNS3 project are not yet supported")
return topo