From e7e79b286527f58f918395985ae4ed849648c4a0 Mon Sep 17 00:00:00 2001 From: zhangshuai <446077695@qq.com> Date: Sun, 12 Jun 2016 19:28:51 +0800 Subject: [PATCH] create protection plan basic function It includes the python and html features. Change-Id: Ic28d5462f35e2774e4de8058a131ca0d9d6972c3 Closes-Bug: #1591726 --- smaug_dashboard/protectionplans/forms.py | 61 ++++++++++++++++++ smaug_dashboard/protectionplans/tables.py | 13 +++- smaug_dashboard/protectionplans/urls.py | 1 + smaug_dashboard/protectionplans/views.py | 57 +++++++++++++++++ .../templates/protectionplans/_create.html | 64 +++++++++++++++++++ .../templates/protectionplans/create.html | 10 +++ 6 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 smaug_dashboard/templates/protectionplans/_create.html create mode 100644 smaug_dashboard/templates/protectionplans/create.html diff --git a/smaug_dashboard/protectionplans/forms.py b/smaug_dashboard/protectionplans/forms.py index 6f02d1f..ef56005 100644 --- a/smaug_dashboard/protectionplans/forms.py +++ b/smaug_dashboard/protectionplans/forms.py @@ -19,9 +19,70 @@ 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 CreateProtectionPlanForm(horizon_forms.SelfHandlingForm): + name = forms.CharField(label=_("Name"), required=True) + provider_id = forms.ChoiceField(label=_('Protection Provider'), + choices=[], + widget=forms.Select(attrs={ + 'class': 'switchable'})) + providers = forms.CharField( + widget=forms.HiddenInput(attrs={"class": "providers"})) + actionmode = forms.CharField( + widget=forms.HiddenInput(attrs={"class": "actionmode"})) + resources = forms.CharField( + widget=forms.HiddenInput(attrs={"class": "resources"})) + parameters = forms.CharField( + widget=forms.HiddenInput(attrs={"class": "parameters"})) + + def __init__(self, request, *args, **kwargs): + self.next_view = kwargs.pop('next_view') + super(CreateProtectionPlanForm, self).\ + __init__(request, *args, **kwargs) + + result = [] + providers = smaugclient.provider_list(request) + + self.fields['providers'].initial = \ + json.dumps([f._info for f in providers]) + + if providers: + result = [(e.id, e.name) for e in providers] + + self.fields['provider_id'].choices = result + + def handle(self, request, data): + try: + keys = [u'id', u'status', u'provider_id', + u'name', u'parameters', u'resources'] + new_plan = smaugclient.plan_create(request, + data["name"], + data["provider_id"], + json.loads(data["resources"]), + json.loads(data["parameters"])) + if set(keys) > set(new_plan.keys()): + smaugclient.plan_delete(request, new_plan['id']) + raise Exception() + + messages.success(request, + _("Protection Plan created successfully.")) + + if data["actionmode"] == "schedule": + request.method = 'GET' + return self.next_view.as_view()(request, + plan_id=new_plan["id"]) + elif data["actionmode"] == "now": + smaugclient.checkpoint_create(request, new_plan["provider_id"], + new_plan["id"]) + messages.success(request, _("Protect now successfully.")) + return new_plan + except Exception: + exceptions.handle(request, _('Unable to create protection plan.')) + + class ScheduleProtectForm(horizon_forms.SelfHandlingForm): id = forms.CharField(label=_("ID"), widget=forms.HiddenInput) name = forms.CharField(label=_("Name"), widget=forms.HiddenInput) diff --git a/smaug_dashboard/protectionplans/tables.py b/smaug_dashboard/protectionplans/tables.py index 1f947e0..1cf2681 100644 --- a/smaug_dashboard/protectionplans/tables.py +++ b/smaug_dashboard/protectionplans/tables.py @@ -22,6 +22,17 @@ from horizon import tables from smaug_dashboard.api import smaug as smaugclient +class CreateProtectionPlanLink(tables.LinkAction): + name = "create" + verbose_name = _("Create Protection Plan") + url = "horizon:smaug:protectionplans:create" + classes = ("ajax-modal",) + icon = "plus" + + def allowed(self, request, protectionplan): + return True + + class ScheduleProtectLink(tables.LinkAction): name = "scheduleprotect" verbose_name = _("Schedule Protect") @@ -100,5 +111,5 @@ class ProtectionPlansTable(tables.DataTable): verbose_name = _('Protection Plans') row_actions = (ScheduleProtectLink, ProtectNowLink, DeleteProtectionPlansAction) - table_actions = (ProtectionPlanFilterAction, + table_actions = (ProtectionPlanFilterAction, CreateProtectionPlanLink, DeleteProtectionPlansAction) diff --git a/smaug_dashboard/protectionplans/urls.py b/smaug_dashboard/protectionplans/urls.py index e5fe71e..5707bed 100644 --- a/smaug_dashboard/protectionplans/urls.py +++ b/smaug_dashboard/protectionplans/urls.py @@ -18,6 +18,7 @@ from smaug_dashboard.protectionplans import views urlpatterns = [ url(r'^$', views.IndexView.as_view(), name='index'), + url(r'^create/$', views.CreateView.as_view(), name='create'), url(r'^(?P[^/]+)/scheduleprotect/$', views.ScheduleProtectView.as_view(), name='scheduleprotect'), ] diff --git a/smaug_dashboard/protectionplans/views.py b/smaug_dashboard/protectionplans/views.py index 778441f..565eceb 100644 --- a/smaug_dashboard/protectionplans/views.py +++ b/smaug_dashboard/protectionplans/views.py @@ -24,6 +24,8 @@ from horizon.utils import memoized from smaug_dashboard.api import smaug as smaugclient from smaug_dashboard.protectionplans import forms from smaug_dashboard.protectionplans import tables +from smaugclient.v1 import protectables +import uuid class IndexView(horizon_tables.DataTableView): @@ -65,6 +67,61 @@ class IndexView(horizon_tables.DataTableView): return plans +class CreateView(horizon_forms.ModalFormView): + template_name = 'protectionplans/create.html' + modal_header = _("Create Protection Plan") + form_id = "create_protectionplan_form" + form_class = forms.CreateProtectionPlanForm + submit_label = _("Create Protection Plan") + submit_url = reverse_lazy("horizon:smaug:protectionplans:create") + success_url = reverse_lazy('horizon:smaug:protectionplans:index') + page_title = _("Create Protection Plan") + + def get_context_data(self, **kwargs): + context = super(CreateView, self).get_context_data(**kwargs) + context["instances"] = self.get_object() + return context + + def get_form_kwargs(self): + kwargs = super(CreateView, self).get_form_kwargs() + kwargs['next_view'] = ScheduleProtectView + return kwargs + + @memoized.memoized_method + def get_object(self): + try: + instances = smaugclient.protectable_list_instances( + self.request, "OS::Keystone::Project") + results = [] + self.get_results(instances, None, results) + return results + except Exception: + exceptions.handle( + self.request, + _('Unable to create protection plan.'), + redirect=reverse("horizon:smaug:protectionplans:index")) + + def get_results(self, instances, showparentid, results): + for instance in instances: + if instance is not None: + resource = {} + resource["id"] = instance.id + resource["type"] = instance.type + resource["name"] = instance.name + resource["showid"] = str(uuid.uuid4()) + resource["showparentid"] = showparentid + result = protectables.Instances(self, resource) + results.append(result) + + for dependent_resource in instance.dependent_resources: + if dependent_resource is not None: + dependent = smaugclient.protectable_get_instance( + self.request, + dependent_resource["type"], + dependent_resource["id"]) + self.get_results([dependent], result.showid, results) + + class ScheduleProtectView(horizon_forms.ModalFormView): template_name = 'protectionplans/scheduleprotect.html' modal_header = _("Schedule Protect") diff --git a/smaug_dashboard/templates/protectionplans/_create.html b/smaug_dashboard/templates/protectionplans/_create.html new file mode 100644 index 0000000..121954d --- /dev/null +++ b/smaug_dashboard/templates/protectionplans/_create.html @@ -0,0 +1,64 @@ +{% extends "horizon/common/_modal_form.html" %} +{% load i18n %} +{% load static %} +{% load compress %} + +{% block modal-body %} + +{% block css %} + {% compress css %} + + {% endcompress %} +{% endblock %} + +
+
+ {% include "horizon/common/_form_fields.html" %} +
+
+ + + + + + + + + + {% for instance in instances %} + + + + + + {% endfor %} + +
+ Resource Name + + Resource Type + + Actions +
+ + + {{instance.name}} + + {{instance.type}} + + +
+
+
+
+{% endblock %} + +{% block modal-footer %} + + + +{% endblock %} \ No newline at end of file diff --git a/smaug_dashboard/templates/protectionplans/create.html b/smaug_dashboard/templates/protectionplans/create.html new file mode 100644 index 0000000..e934caa --- /dev/null +++ b/smaug_dashboard/templates/protectionplans/create.html @@ -0,0 +1,10 @@ +{% extends 'base.html' %} +{% load i18n %} + +{% block title %} + {% trans "Create Protection Plan" %} +{% endblock %} + +{% block main %} + {% include 'protectionplans/_create.html' %} +{% endblock %}