Skip to content

Commit

Permalink
forward restore api call to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Glatthard committed Aug 12, 2015
1 parent d6ba160 commit ff0c6bb
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 45 deletions.
2 changes: 1 addition & 1 deletion ipynbsrv/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
url(r'^containers/(?P<pk>[0-9]+)/snapshots/?$', views.ContainerSnapshotsList.as_view(), name="container_snapshots"),
url(r'^containers/(?P<pk>[0-9]+)/commit$', views.container_commit, name="container_commit"),
url(r'^containers/(?P<pk>[0-9]+)/create_snapshot$', views.container_create_snapshot, name="container_create_snapshot"),
url(r'^containers/(?P<pk>[0-9]+)/restore_snapshot$', views.container_restore_snapshot, name="container_restore_snapshot"),
url(r'^containers/(?P<pk>[0-9]+)/restart$', views.container_restart, name="container_restart"),
url(r'^containers/(?P<pk>[0-9]+)/resume$', views.container_resume, name="container_resume"),
url(r'^containers/(?P<pk>[0-9]+)/start$', views.container_start, name="container_start"),
Expand All @@ -50,6 +49,7 @@
# /api/container/snapshots(/)...
url(r'^containers/snapshots/?$', views.ContainerSnapshotList.as_view(), name="snapshot"),
url(r'^containers/snapshots/(?P<pk>[0-9]+)$', views.ContainerSnapshotDetail.as_view(), name="snapshot_detail"),
url(r'^containers/snapshots/(?P<pk>[0-9]+)/restore$', views.container_snapshot_restore, name="container_snapshot_restore"),

# /api/servers(/)...
url(r'^servers/?$', views.ServerList.as_view(), name="servers"),
Expand Down
51 changes: 20 additions & 31 deletions ipynbsrv/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,37 +589,6 @@ def container_create_snapshot(request, pk):
return Response({"error": "Container not found!", "pk": pk})


@api_view(['POST'])
def container_restore_snapshot(request, pk):
"""
Restore a snapshot of the container.
Todo: show params on OPTIONS call.
:param pk pk of the container that needs to be cloned
"""
params = {}

data = request.data

if not data.get('id'):
return Response({"error": "please provide name for the clone: {\"name\" : \"some name \"}"})

params['id'] = data.get('id')

container = get_container(pk)

# validate permissions
validate_object_permission(ContainerDetailPermission, request, container)

snapshots = ContainerSnapshot.objects.filter(id=params.get('id'))
if container and snapshots:
s = snapshots.first()
s.restore()
serializer = ContainerSerializer(container)
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response({"error": "Container or Snapshot not found!", "pk": pk})


@api_view(['GET'])
def container_clones(request, pk):
container = get_container(pk)
Expand Down Expand Up @@ -799,6 +768,26 @@ class ContainerSnapshotDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = ContainerSnapshot.objects.all()


@api_view(['POST'])
def container_snapshot_restore(request, pk):
"""
Restore a snapshot of the container.
Todo: show params on OPTIONS call.
:param pk pk of the container that needs to be cloned
"""
snapshots = ContainerSnapshot.objects.filter(id=pk)
if snapshots:
s = snapshots.first()
container = s.container
# validate permissions
validate_object_permission(ContainerDetailPermission, request, container)
s.restore()
serializer = ContainerSerializer(container)
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response({"error": "Snapshot not found!", "pk": pk})


class ServerList(generics.ListCreateAPIView):
"""
Get a list of all the servers.
Expand Down
3 changes: 2 additions & 1 deletion ipynbsrv/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,8 @@ def restore(self):
"""
TODO: write doc.
"""
pass
from ipynbsrv.core.signals.signals import container_snapshot_restored
container_snapshot_restored.send(sender=self, snapshot=self)

def save(self, *args, **kwargs):
"""
Expand Down
8 changes: 7 additions & 1 deletion ipynbsrv/core/signals/container_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ def restore_on_server(sender, snapshot, **kwargs):
"""
Restore the restored snapshot on the container backend.
"""
pass
if snapshot is not None:
backend = snapshot.container.server.get_container_backend()
try:
backend.restore_container_snapshot(snapshot.container.backend_pk, snapshot.backend_pk)
except ContainerBackendError as ex:
# XXX: restore?
raise ex


@receiver(post_delete, sender=ContainerSnapshot)
Expand Down
1 change: 0 additions & 1 deletion ipynbsrv/web/templates/web/container_snapshots/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ <h1>Container Snapshots</h1>
<form action="{% url 'container_restore_snapshot' %}" method="POST" class="form-action" role="form">
{% csrf_token %}
<input type="hidden" name="id" value="{{ snapshot.id }}">
<input type="hidden" name="ct_id" value="{{ container.id }}">
<button type="submit" class="btn btn-sm btn-warning" title="Restore this image?" data-toggle="confirmation" data-placement="left" disabled>
<i class="glyphicon glyphicon-open"></i>
</button>
Expand Down
23 changes: 13 additions & 10 deletions ipynbsrv/web/views/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,24 +374,27 @@ def restore_snapshot(request):
if request.method != "POST":
messages.error(request, "Invalid request method.")
return redirect('containers')
if 'id' not in request.POST or 'ct_id' not in request.POST:
if 'id' not in request.POST:
messages.error(request, "Invalid POST request.")
return redirect('containers')

ct_id = int(request.POST.get('ct_id'))
params = {}
params['id'] = int(request.POST.get('id'))
id = int(request.POST.get('id'))

client = get_httpclient_instance(request)

# create snapshot
try:
client.containers(ct_id).restore_snapshot.post(params)
messages.success(request, "Sucessfully restored snapshot `{}`.".format(params.get('name')))
except Exception as e:
messages.error(request, api_error_message(e, params))
snapshot = client.containers.snapshots(id).get()
if snapshot:
# restore snapshot
try:
client.containers.snapshots(id).restore.post()
messages.success(request, "Sucessfully restored snapshot `{}`.".format(snapshot.name)
except Exception as e:
messages.error(request, api_error_message(e, ""))
else:
messages.error(request, "Snapshot not found")

return redirect('container_snapshots', ct_id)
return redirect('container_snapshots', snapshot.container.id)


@user_passes_test(login_allowed)
Expand Down

0 comments on commit ff0c6bb

Please sign in to comment.