1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-24 09:18:08 +00:00

Accept a node name when creating a node from a template using the API. Fixes #1708

This commit is contained in:
grossmj 2020-01-08 08:19:33 +08:00
parent 688b1ac0e4
commit 0a87ee573c
3 changed files with 9 additions and 4 deletions

View File

@ -490,7 +490,7 @@ class Project:
return new_name return new_name
@open_required @open_required
async def add_node_from_template(self, template_id, x=0, y=0, compute_id=None): async def add_node_from_template(self, template_id, x=0, y=0, name=None, compute_id=None):
""" """
Create a node from a template. Create a node from a template.
""" """
@ -508,9 +508,10 @@ class Project:
compute = self.controller.get_compute(compute_id) compute = self.controller.get_compute(compute_id)
else: else:
compute = self.controller.get_compute(template.pop("compute_id", compute_id)) compute = self.controller.get_compute(template.pop("compute_id", compute_id))
name = template.pop("name") template_name = template.pop("name")
default_name_format = template.pop("default_name_format", "{name}-{0}") default_name_format = template.pop("default_name_format", "{name}-{0}")
name = default_name_format.replace("{name}", name) if name is None:
name = default_name_format.replace("{name}", template_name)
node_id = str(uuid.uuid4()) node_id = str(uuid.uuid4())
node = await self.add_node(compute, name, node_id, node_type=node_type, **template) node = await self.add_node(compute, name, node_id, node_type=node_type, **template)
return node return node

View File

@ -97,6 +97,10 @@ TEMPLATE_USAGE_SCHEMA = {
"description": "Y position", "description": "Y position",
"type": "integer" "type": "integer"
}, },
"name": {
"description": "Use this name to create a new node",
"type": ["null", "string"]
},
"compute_id": { "compute_id": {
"description": "If the template don't have a default compute use this compute", "description": "If the template don't have a default compute use this compute",
"type": ["null", "string"] "type": ["null", "string"]

View File

@ -966,6 +966,6 @@ def test_create_node_from_template(http_controller, controller, project, compute
"x": 42, "x": 42,
"y": 12 "y": 12
}) })
mock.assert_called_with(id, x=42, y=12, compute_id=None) mock.assert_called_with(id, x=42, y=12, name=None, compute_id=None)
assert response.route == "/projects/{project_id}/templates/{template_id}" assert response.route == "/projects/{project_id}/templates/{template_id}"
assert response.status == 201 assert response.status == 201