Files
karbor-dashboard/smaug_dashboard/checkpoints/forms.py
chenpengzi 69d6c79834 restore checkpoint basic function
It includes the python and html features.

Change-Id: I24a0d62251413b164f07fbe387f3b8d368612a9f
Closes-Bug:#1591789
2016-07-07 09:21:33 +08:00

60 lines
2.4 KiB
Python

# Copyright (c) 2016 Huawei, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from django import forms
from django.utils.translation import ugettext_lazy as _
from horizon import exceptions
from horizon import forms as horizon_forms
from horizon import messages
import json
from smaug_dashboard.api import smaug as smaugclient
class RestoreCheckpointForm(horizon_forms.SelfHandlingForm):
provider_id = forms.CharField(label=_("Provider ID"),
widget=forms.HiddenInput(),
required=False)
checkpoint_id = forms.CharField(label=_("Checkpoint ID"),
widget=forms.HiddenInput(),
required=False)
restore_target = forms.CharField(label=_("Restore Target"))
provider = forms.CharField(
widget=forms.HiddenInput(attrs={"class": "provider"}))
parameters = forms.CharField(
widget=forms.HiddenInput(attrs={"class": "parameters"}))
failure_url = 'horizon:smaug:checkpoints:index'
def __init__(self, request, *args, **kwargs):
super(RestoreCheckpointForm, self).\
__init__(request, *args, **kwargs)
provider_id = str(kwargs["initial"]["provider_id"])
provider = smaugclient.provider_get(request, provider_id)
self.fields['provider'].initial = json.dumps(provider._info)
def handle(self, request, data):
try:
new_restore = smaugclient.restore_create(
request,
provider_id=data["provider_id"],
checkpoint_id=data["checkpoint_id"],
restore_target=data["restore_target"],
parameters=json.loads(data["parameters"]))
messages.success(request, _("Checkpoint restored successfully."))
return new_restore
except Exception:
exceptions.handle(request, _('Unable to restore checkpoint.'))