Skip to content

Commit

Permalink
Docker support update
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-duponchelle committed Dec 16, 2015
1 parent 1f740a3 commit d3c06ea
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
28 changes: 28 additions & 0 deletions gns3server/handlers/api/docker_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from ...schemas.docker import (
DOCKER_CREATE_SCHEMA,
DOCKER_OBJECT_SCHEMA,
DOCKER_UPDATE_SCHEMA,
DOCKER_LIST_IMAGES_SCHEMA
)
from ...schemas.nio import NIO_SCHEMA
Expand Down Expand Up @@ -247,3 +248,30 @@ def delete_nio(request, response):
yield from container.adapter_remove_nio_binding(
int(request.match_info["adapter_number"]))
response.set_status(204)

@classmethod
@Route.put(
r"/projects/{project_id}/docker/vms/{vm_id}",
parameters={
"project_id": "UUID for the project",
"vm_id": "UUID for the instance"
},
status_codes={
200: "Instance updated",
400: "Invalid request",
404: "Instance doesn't exist",
409: "Conflict"
},
description="Update a Docker instance",
input=DOCKER_UPDATE_SCHEMA,
output=DOCKER_OBJECT_SCHEMA)
def update(request, response):

docker_manager = Docker.instance()
vm = docker_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"])
vm.name = request.json.get("name", vm.name)
vm.console = request.json.get("console", vm.console)
vm.start_command = request.json.get("start_command", vm.start_command)
response.json(vm)


11 changes: 10 additions & 1 deletion gns3server/modules/docker/docker_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,18 @@ def __json__(self):
"project_id": self._project.id,
"image": self._image,
"adapters": self.adapters,
"console": self.console
"console": self.console,
"start_command": self.start_command
}

@property
def start_command(self):
return self._start_command

@start_command.setter
def start_command(self, command):
self._start_command = command

@asyncio.coroutine
def _get_container_state(self):
"""Returns the container state (e.g. running, paused etc.)
Expand Down
31 changes: 31 additions & 0 deletions gns3server/schemas/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,32 @@
"additionalProperties": False,
}


DOCKER_UPDATE_SCHEMA = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Request validation to create a new Docker container",
"type": "object",
"properties": {
"name": {
"description": "Docker container name",
"type": "string",
"minLength": 1,
},
"console": {
"description": "console TCP port",
"minimum": 1,
"maximum": 65535,
"type": ["integer", "null"]
},
"start_command": {
"description": "Docker CMD entry",
"type": "string",
"minLength": 0,
}
},
"additionalProperties": False,
}

DOCKER_OBJECT_SCHEMA = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Docker instance",
Expand Down Expand Up @@ -106,6 +132,11 @@
"type": ["integer", "null"],
"minimum": 0,
"maximum": 32,
},
"start_command": {
"description": "Docker CMD entry",
"type": "string",
"minLength": 0,
}
},
"additionalProperties": False,
Expand Down
13 changes: 13 additions & 0 deletions tests/handlers/api/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def base_params():
def vm(server, project, base_params):
with asyncio_patch("gns3server.modules.docker.Docker.query", return_value={"Id": "8bd8153ea8f5"}) as mock:
response = server.post("/projects/{project_id}/docker/vms".format(project_id=project.id), base_params)
if response.status != 201:
print(response.body)
assert response.status == 201
return response.json

Expand Down Expand Up @@ -103,3 +105,14 @@ def test_docker_delete_nio(server, vm):
response = server.delete("/projects/{project_id}/docker/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 204
assert response.route == "/projects/{project_id}/docker/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"


def test_docker_update(server, vm, tmpdir, free_console_port):
response = server.put("/projects/{project_id}/docker/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"name": "test",
"console": free_console_port,
"start_command": "yes"},
example=True)
assert response.status == 200
assert response.json["name"] == "test"
assert response.json["console"] == free_console_port
assert response.json["start_command"] == "yes"
3 changes: 2 additions & 1 deletion tests/modules/docker/test_docker_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def test_json(vm, project):
'project_id': project.id,
'vm_id': vm.id,
'adapters': 1,
'console': vm.console
'console': vm.console,
'start_command': vm.start_command
}


Expand Down

0 comments on commit d3c06ea

Please sign in to comment.