2017-06-27 08:09:21 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2017 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/>.
|
|
|
|
|
2020-11-05 08:17:59 +00:00
|
|
|
from gns3server.controller.controller_error import ControllerError
|
2017-06-27 08:09:21 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-10-27 09:11:24 +00:00
|
|
|
def get_next_application_id(projects, computes):
|
2017-06-27 08:09:21 +00:00
|
|
|
"""
|
|
|
|
Calculates free application_id from given nodes
|
2018-03-12 06:38:50 +00:00
|
|
|
|
2020-02-10 07:20:49 +00:00
|
|
|
:param projects: all projects managed by controller
|
2020-10-27 09:11:24 +00:00
|
|
|
:param computes: all computes used by the project
|
2020-02-10 07:20:49 +00:00
|
|
|
:raises HTTPConflict when exceeds number
|
2017-06-27 08:09:21 +00:00
|
|
|
:return: integer first free id
|
|
|
|
"""
|
2018-03-15 07:17:39 +00:00
|
|
|
|
2020-02-10 07:20:49 +00:00
|
|
|
nodes = []
|
|
|
|
|
2020-10-27 09:11:24 +00:00
|
|
|
# look for application id for in all nodes across all opened projects that share the same computes
|
2020-02-10 07:20:49 +00:00
|
|
|
for project in projects.values():
|
2020-10-27 09:11:24 +00:00
|
|
|
if project.status == "opened":
|
2020-02-10 07:20:49 +00:00
|
|
|
nodes.extend(list(project.nodes.values()))
|
|
|
|
|
2020-10-27 09:11:24 +00:00
|
|
|
used = set([n.properties["application_id"] for n in nodes if n.node_type == "iou" and n.compute.id in computes])
|
2017-06-27 08:09:21 +00:00
|
|
|
pool = set(range(1, 512))
|
|
|
|
try:
|
2020-10-27 09:11:24 +00:00
|
|
|
application_id = (pool - used).pop()
|
|
|
|
return application_id
|
2017-06-27 08:09:21 +00:00
|
|
|
except KeyError:
|
2020-11-05 08:17:59 +00:00
|
|
|
raise ControllerError("Cannot create a new IOU node (limit of 512 nodes across all opened projects using the same computes)")
|