2017-02-01 10:30:14 +00:00
|
|
|
#!/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/>.
|
|
|
|
|
2017-06-28 07:37:19 +00:00
|
|
|
import copy
|
2017-02-01 10:30:14 +00:00
|
|
|
import uuid
|
|
|
|
|
2017-04-12 12:35:49 +00:00
|
|
|
ID_TO_CATEGORY = {
|
|
|
|
3: "firewall",
|
|
|
|
2: "guest",
|
|
|
|
1: "switch",
|
|
|
|
0: "router"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-01 10:30:14 +00:00
|
|
|
class Appliance:
|
|
|
|
|
2018-11-11 12:13:58 +00:00
|
|
|
def __init__(self, appliance_id, settings, builtin=False):
|
2018-03-15 07:17:39 +00:00
|
|
|
|
2017-02-01 10:30:14 +00:00
|
|
|
if appliance_id is None:
|
|
|
|
self._id = str(uuid.uuid4())
|
2017-06-19 06:47:50 +00:00
|
|
|
elif isinstance(appliance_id, uuid.UUID):
|
|
|
|
self._id = str(appliance_id)
|
2017-02-01 10:30:14 +00:00
|
|
|
else:
|
|
|
|
self._id = appliance_id
|
2018-11-11 12:13:58 +00:00
|
|
|
|
|
|
|
self._settings = copy.deepcopy(settings)
|
2017-05-16 08:38:58 +00:00
|
|
|
|
|
|
|
# Version of the gui before 2.1 use linked_base
|
|
|
|
# and the server linked_clone
|
2018-11-11 12:13:58 +00:00
|
|
|
if "linked_base" in self.settings:
|
|
|
|
linked_base = self._settings.pop("linked_base")
|
|
|
|
if "linked_clone" not in self._settings:
|
|
|
|
self._settings["linked_clone"] = linked_base
|
|
|
|
|
|
|
|
# Convert old GUI category to text category
|
|
|
|
try:
|
|
|
|
self._settings["category"] = ID_TO_CATEGORY[self._settings["category"]]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# The "server" setting has been replaced by "compute_id" setting in version 2.2
|
|
|
|
if "server" in self._settings:
|
|
|
|
self._settings["compute_id"] = self._settings.pop("server")
|
|
|
|
|
2018-11-13 06:59:18 +00:00
|
|
|
# The "node_type" setting has been replaced by "appliance_type" setting in version 2.2
|
|
|
|
if "node_type" in self._settings:
|
|
|
|
self._settings["appliance_type"] = self._settings.pop("node_type")
|
|
|
|
|
2018-11-11 12:13:58 +00:00
|
|
|
# Remove an old IOU setting
|
2018-11-13 06:59:18 +00:00
|
|
|
if self._settings["appliance_type"] == "iou" and "image" in self._settings:
|
2018-11-11 12:13:58 +00:00
|
|
|
del self._settings["image"]
|
|
|
|
|
2017-04-12 12:35:49 +00:00
|
|
|
self._builtin = builtin
|
2017-02-01 10:30:14 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
return self._id
|
|
|
|
|
2017-04-12 12:35:49 +00:00
|
|
|
@property
|
2018-11-11 12:13:58 +00:00
|
|
|
def settings(self):
|
|
|
|
return self._settings
|
|
|
|
|
|
|
|
@settings.setter
|
|
|
|
def settings(self, settings):
|
|
|
|
self._settings.update(settings)
|
2017-04-12 12:35:49 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2018-11-11 12:13:58 +00:00
|
|
|
return self._settings["name"]
|
2017-04-12 12:35:49 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def compute_id(self):
|
2018-11-11 12:13:58 +00:00
|
|
|
return self._settings["compute_id"]
|
|
|
|
|
|
|
|
@property
|
2018-11-13 06:59:18 +00:00
|
|
|
def appliance_type(self):
|
|
|
|
return self._settings["appliance_type"]
|
2017-04-12 12:35:49 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def builtin(self):
|
|
|
|
return self._builtin
|
|
|
|
|
2018-11-13 06:59:18 +00:00
|
|
|
def update(self, **kwargs):
|
2018-11-13 07:40:18 +00:00
|
|
|
|
|
|
|
#TODO: do not update appliance_id, builtin or appliance_type
|
2018-11-13 06:59:18 +00:00
|
|
|
self._settings.update(kwargs)
|
|
|
|
|
2017-02-01 10:30:14 +00:00
|
|
|
def __json__(self):
|
|
|
|
"""
|
2018-11-11 12:13:58 +00:00
|
|
|
Appliance settings.
|
2017-02-01 10:30:14 +00:00
|
|
|
"""
|
2018-11-13 06:59:18 +00:00
|
|
|
|
2018-11-11 12:13:58 +00:00
|
|
|
settings = self._settings
|
|
|
|
settings.update({"appliance_id": self._id,
|
|
|
|
"default_name_format": settings.get("default_name_format", "{name}-{0}"),
|
|
|
|
"symbol": settings.get("symbol", ":/symbols/computer.svg"),
|
|
|
|
"builtin": self.builtin})
|
|
|
|
|
|
|
|
if not self.builtin:
|
|
|
|
settings["compute_id"] = self.compute_id
|
|
|
|
|
|
|
|
return settings
|