create protection plan basic function

It includes the python and html features.

Change-Id: Ic28d5462f35e2774e4de8058a131ca0d9d6972c3
Closes-Bug: #1591726
This commit is contained in:
zhangshuai
2016-06-12 19:28:51 +08:00
parent dd53ba37b9
commit e7e79b2865
6 changed files with 205 additions and 1 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -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<plan_id>[^/]+)/scheduleprotect/$',
views.ScheduleProtectView.as_view(), name='scheduleprotect'),
]

View File

@@ -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")

View File

@@ -0,0 +1,64 @@
{% extends "horizon/common/_modal_form.html" %}
{% load i18n %}
{% load static %}
{% load compress %}
{% block modal-body %}
{% block css %}
{% compress css %}
<link rel="stylesheet" href="{% static 'smaugdashboard/css/jquery.treetable.css' %}">
{% endcompress %}
{% endblock %}
<div class="left" style="width:100%">
<fieldset>
{% include "horizon/common/_form_fields.html" %}
</fieldset>
<div class="table_wrapper">
<table id="protectionplanCreateResource" class="{% block table_css_classes %}table table-striped datatable {{ table.css_classes }}{% endblock %}">
<thead>
<tr class="table_column_header">
<th {{ column.attr_string|safe }}>
Resource Name
</th>
<th {{ column.attr_string|safe }}>
Resource Type
</th>
<th {{ column.attr_string|safe }}>
Actions
</th>
</tr>
</thead>
<tbody>
{% for instance in instances %}
<tr data-tt-id="{{ instance.showid }}"
resource-id="{{ instance.id }}"
{% if instance.showparentid != None %}
data-tt-parent-id="{{ instance.showparentid }}"
{% endif %}>
<td>
<span class="spanresource"><input type="checkbox" class="cbresource"/></span>
<span class="logoresource"></span>
<span class="spanresource">{{instance.name}}</span>
</td>
<td>
<span class="spanresource">{{instance.type}}</span>
</td>
<td>
<input type="button" class="btn btn-default editparameters" value="Edit Parameters" resourcetype="{{instance.type}}">
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="dialog_wrapper"></div>
</div>
{% endblock %}
{% block modal-footer %}
<input class="btn btn-primary create" type="submit" value='{% trans "Create Protection Plan" %}' />
<input class="btn btn-primary schedule" type="submit" value='{% trans "Schedule Protect" %}' />
<input class="btn btn-primary now" type="submit" value='{% trans "Protect Now" %}' />
{% endblock %}

View File

@@ -0,0 +1,10 @@
{% extends 'base.html' %}
{% load i18n %}
{% block title %}
{% trans "Create Protection Plan" %}
{% endblock %}
{% block main %}
{% include 'protectionplans/_create.html' %}
{% endblock %}