2016-03-10 22:51:29 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2016-05-14 03:00:07 +03:00
|
|
|
from gns3server.web.route import Route
|
|
|
|
from gns3server.controller import Controller
|
|
|
|
|
|
|
|
from gns3server.schemas.node import (
|
|
|
|
NODE_OBJECT_SCHEMA,
|
|
|
|
NODE_UPDATE_SCHEMA
|
|
|
|
)
|
2016-03-10 22:51:29 +02:00
|
|
|
|
|
|
|
|
2016-05-11 20:35:36 +03:00
|
|
|
class NodeHandler:
|
2016-03-10 22:51:29 +02:00
|
|
|
"""
|
2016-05-11 20:35:36 +03:00
|
|
|
API entry point for node
|
2016-03-10 22:51:29 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
@Route.post(
|
2016-05-11 20:35:36 +03:00
|
|
|
r"/projects/{project_id}/nodes",
|
2016-03-10 22:51:29 +02:00
|
|
|
parameters={
|
2016-05-14 03:00:07 +03:00
|
|
|
"project_id": "Project UUID"
|
2016-03-10 22:51:29 +02:00
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
201: "Instance created",
|
|
|
|
400: "Invalid request"
|
|
|
|
},
|
2016-05-11 20:35:36 +03:00
|
|
|
description="Create a new node instance",
|
|
|
|
input=NODE_OBJECT_SCHEMA,
|
|
|
|
output=NODE_OBJECT_SCHEMA)
|
2016-03-10 22:51:29 +02:00
|
|
|
def create(request, response):
|
|
|
|
|
|
|
|
controller = Controller.instance()
|
2016-05-12 00:19:00 +03:00
|
|
|
compute = controller.get_compute(request.json.pop("compute_id"))
|
2016-05-11 20:35:36 +03:00
|
|
|
project = controller.get_project(request.match_info["project_id"])
|
|
|
|
node = yield from project.add_node(compute, request.json.pop("node_id", None), **request.json)
|
2016-03-10 22:51:29 +02:00
|
|
|
response.set_status(201)
|
2016-05-11 20:35:36 +03:00
|
|
|
response.json(node)
|
2016-04-12 19:02:36 +03:00
|
|
|
|
2016-04-20 15:38:14 +03:00
|
|
|
@Route.get(
|
2016-05-11 20:35:36 +03:00
|
|
|
r"/projects/{project_id}/nodes",
|
2016-04-20 15:38:14 +03:00
|
|
|
parameters={
|
2016-05-14 03:00:07 +03:00
|
|
|
"project_id": "Project UUID"
|
2016-04-20 15:38:14 +03:00
|
|
|
},
|
|
|
|
status_codes={
|
2016-05-14 03:00:07 +03:00
|
|
|
200: "List of nodes returned",
|
2016-04-20 15:38:14 +03:00
|
|
|
},
|
2016-05-11 20:35:36 +03:00
|
|
|
description="List nodes of a project")
|
|
|
|
def list_nodes(request, response):
|
2016-04-20 15:38:14 +03:00
|
|
|
|
|
|
|
controller = Controller.instance()
|
2016-05-11 20:35:36 +03:00
|
|
|
project = controller.get_project(request.match_info["project_id"])
|
2016-05-14 03:00:07 +03:00
|
|
|
response.json([v for v in project.nodes.values()])
|
2016-04-20 15:38:14 +03:00
|
|
|
|
2016-04-18 18:36:38 +03:00
|
|
|
@Route.put(
|
2016-05-11 20:35:36 +03:00
|
|
|
r"/projects/{project_id}/nodes/{node_id}",
|
2016-04-18 18:36:38 +03:00
|
|
|
status_codes={
|
2016-05-14 03:00:07 +03:00
|
|
|
200: "Instance updated",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
2016-04-18 18:36:38 +03:00
|
|
|
},
|
2016-05-11 20:35:36 +03:00
|
|
|
description="Update a node instance",
|
|
|
|
input=NODE_UPDATE_SCHEMA,
|
|
|
|
output=NODE_OBJECT_SCHEMA)
|
2016-04-18 18:36:38 +03:00
|
|
|
def update(request, response):
|
2016-05-11 20:35:36 +03:00
|
|
|
project = Controller.instance().get_project(request.match_info["project_id"])
|
|
|
|
node = project.get_node(request.match_info["node_id"])
|
2016-04-18 18:36:38 +03:00
|
|
|
|
2016-05-14 03:00:07 +03:00
|
|
|
# Ignore these because we only use them when creating a node
|
2016-05-11 20:35:36 +03:00
|
|
|
request.json.pop("node_id", None)
|
|
|
|
request.json.pop("node_type", None)
|
2016-04-18 18:36:38 +03:00
|
|
|
request.json.pop("compute_id", None)
|
|
|
|
|
2016-05-11 20:35:36 +03:00
|
|
|
yield from node.update(**request.json)
|
2016-05-14 03:00:07 +03:00
|
|
|
response.set_status(200)
|
2016-05-11 20:35:36 +03:00
|
|
|
response.json(node)
|
2016-04-18 18:36:38 +03:00
|
|
|
|
2016-05-14 04:26:50 +03:00
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/nodes/start",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "All nodes successfully started",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Start all nodes belonging to the project",
|
|
|
|
output=NODE_OBJECT_SCHEMA)
|
|
|
|
def start_all(request, response):
|
|
|
|
|
|
|
|
project = Controller.instance().get_project(request.match_info["project_id"])
|
|
|
|
for node in project.nodes.values():
|
|
|
|
yield from node.start()
|
|
|
|
response.set_status(204)
|
|
|
|
|
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/nodes/stop",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "All nodes successfully stopped",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Stop all nodes belonging to the project",
|
|
|
|
output=NODE_OBJECT_SCHEMA)
|
|
|
|
def stop_all(request, response):
|
|
|
|
|
|
|
|
project = Controller.instance().get_project(request.match_info["project_id"])
|
|
|
|
for node in project.nodes.values():
|
|
|
|
yield from node.stop()
|
|
|
|
response.set_status(204)
|
|
|
|
|
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/nodes/suspend",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "All nodes successfully suspended",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Suspend all nodes belonging to the project",
|
|
|
|
output=NODE_OBJECT_SCHEMA)
|
|
|
|
def suspend_all(request, response):
|
|
|
|
|
|
|
|
project = Controller.instance().get_project(request.match_info["project_id"])
|
|
|
|
for node in project.nodes.values():
|
|
|
|
yield from node.suspend()
|
|
|
|
response.set_status(204)
|
|
|
|
|
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/nodes/reload",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "All nodes successfully reloaded",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Reload all nodes belonging to the project",
|
|
|
|
output=NODE_OBJECT_SCHEMA)
|
|
|
|
def reload_all(request, response):
|
|
|
|
|
|
|
|
project = Controller.instance().get_project(request.match_info["project_id"])
|
|
|
|
for node in project.nodes.values():
|
|
|
|
yield from node.stop()
|
|
|
|
for node in project.nodes.values():
|
|
|
|
yield from node.start()
|
|
|
|
response.set_status(204)
|
|
|
|
|
2016-04-12 19:02:36 +03:00
|
|
|
@Route.post(
|
2016-05-11 20:35:36 +03:00
|
|
|
r"/projects/{project_id}/nodes/{node_id}/start",
|
2016-04-12 19:02:36 +03:00
|
|
|
parameters={
|
2016-05-14 03:00:07 +03:00
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
2016-04-12 19:02:36 +03:00
|
|
|
},
|
|
|
|
status_codes={
|
2016-05-14 03:00:07 +03:00
|
|
|
204: "Instance started",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
2016-04-12 19:02:36 +03:00
|
|
|
},
|
2016-05-11 20:35:36 +03:00
|
|
|
description="Start a node instance",
|
|
|
|
output=NODE_OBJECT_SCHEMA)
|
2016-04-12 19:02:36 +03:00
|
|
|
def start(request, response):
|
|
|
|
|
2016-05-11 20:35:36 +03:00
|
|
|
project = Controller.instance().get_project(request.match_info["project_id"])
|
|
|
|
node = project.get_node(request.match_info["node_id"])
|
|
|
|
yield from node.start()
|
2016-05-14 03:00:07 +03:00
|
|
|
response.set_status(204)
|
2016-04-12 19:02:36 +03:00
|
|
|
|
|
|
|
@Route.post(
|
2016-05-11 20:35:36 +03:00
|
|
|
r"/projects/{project_id}/nodes/{node_id}/stop",
|
2016-04-12 19:02:36 +03:00
|
|
|
parameters={
|
2016-05-14 03:00:07 +03:00
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
2016-04-12 19:02:36 +03:00
|
|
|
},
|
|
|
|
status_codes={
|
2016-05-14 03:00:07 +03:00
|
|
|
204: "Instance stopped",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
2016-04-12 19:02:36 +03:00
|
|
|
},
|
2016-05-14 03:00:07 +03:00
|
|
|
description="Stop a node instance",
|
2016-05-11 20:35:36 +03:00
|
|
|
output=NODE_OBJECT_SCHEMA)
|
2016-04-12 19:02:36 +03:00
|
|
|
def stop(request, response):
|
|
|
|
|
2016-05-11 20:35:36 +03:00
|
|
|
project = Controller.instance().get_project(request.match_info["project_id"])
|
|
|
|
node = project.get_node(request.match_info["node_id"])
|
|
|
|
yield from node.stop()
|
2016-05-14 03:00:07 +03:00
|
|
|
response.set_status(204)
|
2016-04-12 19:02:36 +03:00
|
|
|
|
|
|
|
@Route.post(
|
2016-05-11 20:35:36 +03:00
|
|
|
r"/projects/{project_id}/nodes/{node_id}/suspend",
|
2016-04-12 19:02:36 +03:00
|
|
|
parameters={
|
2016-05-14 03:00:07 +03:00
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
2016-04-12 19:02:36 +03:00
|
|
|
},
|
|
|
|
status_codes={
|
2016-05-14 03:00:07 +03:00
|
|
|
204: "Instance suspended",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
2016-04-12 19:02:36 +03:00
|
|
|
},
|
2016-05-14 03:00:07 +03:00
|
|
|
description="Suspend a node instance",
|
2016-05-11 20:35:36 +03:00
|
|
|
output=NODE_OBJECT_SCHEMA)
|
2016-04-12 19:02:36 +03:00
|
|
|
def suspend(request, response):
|
|
|
|
|
2016-05-11 20:35:36 +03:00
|
|
|
project = Controller.instance().get_project(request.match_info["project_id"])
|
|
|
|
node = project.get_node(request.match_info["node_id"])
|
|
|
|
yield from node.suspend()
|
2016-05-14 03:00:07 +03:00
|
|
|
response.set_status(204)
|
2016-04-18 17:57:02 +03:00
|
|
|
|
|
|
|
@Route.post(
|
2016-05-11 20:35:36 +03:00
|
|
|
r"/projects/{project_id}/nodes/{node_id}/reload",
|
2016-04-18 17:57:02 +03:00
|
|
|
parameters={
|
2016-05-14 03:00:07 +03:00
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
2016-04-18 17:57:02 +03:00
|
|
|
},
|
|
|
|
status_codes={
|
2016-05-14 03:00:07 +03:00
|
|
|
204: "Instance reloaded",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
2016-04-18 17:57:02 +03:00
|
|
|
},
|
2016-05-11 20:35:36 +03:00
|
|
|
description="Reload a node instance",
|
|
|
|
output=NODE_OBJECT_SCHEMA)
|
2016-04-18 17:57:02 +03:00
|
|
|
def reload(request, response):
|
|
|
|
|
2016-05-11 20:35:36 +03:00
|
|
|
project = Controller.instance().get_project(request.match_info["project_id"])
|
|
|
|
node = project.get_node(request.match_info["node_id"])
|
|
|
|
yield from node.reload()
|
2016-05-14 03:00:07 +03:00
|
|
|
response.set_status(204)
|
2016-04-18 18:36:38 +03:00
|
|
|
|
|
|
|
@Route.delete(
|
2016-05-11 20:35:36 +03:00
|
|
|
r"/projects/{project_id}/nodes/{node_id}",
|
2016-04-18 18:36:38 +03:00
|
|
|
parameters={
|
2016-05-14 03:00:07 +03:00
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
2016-04-18 18:36:38 +03:00
|
|
|
},
|
|
|
|
status_codes={
|
2016-05-14 03:00:07 +03:00
|
|
|
204: "Instance deleted",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
2016-04-18 18:36:38 +03:00
|
|
|
},
|
2016-05-11 20:35:36 +03:00
|
|
|
description="Delete a node instance")
|
2016-04-18 18:36:38 +03:00
|
|
|
def delete(request, response):
|
2016-05-11 20:35:36 +03:00
|
|
|
project = Controller.instance().get_project(request.match_info["project_id"])
|
2016-05-17 14:11:43 +03:00
|
|
|
yield from project.delete_node(request.match_info["node_id"])
|
2016-05-14 03:00:07 +03:00
|
|
|
response.set_status(204)
|
2016-05-19 17:21:35 +03:00
|
|
|
|
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/nodes/{node_id}/dynamips/auto_idlepc",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "Instance reloaded",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Compute the IDLE PC for a Dynamips node")
|
|
|
|
def auto_idlepc(request, response):
|
|
|
|
|
|
|
|
project = Controller.instance().get_project(request.match_info["project_id"])
|
|
|
|
node = project.get_node(request.match_info["node_id"])
|
|
|
|
idle = yield from node.dynamips_auto_idlepc()
|
|
|
|
response.json(idle)
|
|
|
|
response.set_status(200)
|
|
|
|
|
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/nodes/{node_id}/dynamips/idlepc_proposals",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "Instance reloaded",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Compute a list of potential idle PC for a node")
|
|
|
|
def idlepc_proposals(request, response):
|
|
|
|
|
|
|
|
project = Controller.instance().get_project(request.match_info["project_id"])
|
|
|
|
node = project.get_node(request.match_info["node_id"])
|
|
|
|
idle = yield from node.dynamips_idlepc_proposals()
|
|
|
|
response.json(idle)
|
|
|
|
response.set_status(200)
|