Merge "Add support to enter rule in policy language"

This commit is contained in:
Jenkins 2017-07-20 09:56:40 +00:00 committed by Gerrit Code Review
commit 86e9da918c
7 changed files with 148 additions and 2 deletions

View File

@ -0,0 +1,67 @@
# Copyright 2017 NEC, Corp
#
# 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.
import logging
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from horizon import exceptions
from horizon import forms
from horizon import messages
from congress_dashboard.api import congress
LOG = logging.getLogger(__name__)
class CreateRawRule(forms.SelfHandlingForm):
rule = forms.CharField(label=_("Rule"), required=True,
widget=forms.Textarea(attrs={'rows': 5}))
rule_name = forms.CharField(max_length=255, label=_("Rule Name"),
required=False)
comment = forms.CharField(max_length=255, label=_("Description"),
required=False)
failure_url = 'horizon:admin:policies:detail'
def __init__(self, request, *args, **kwargs):
super(CreateRawRule, self).__init__(request, *args, **kwargs)
initial = kwargs.get('initial', {})
policy_name = initial.get('policy_name')
self.fields['policy_name'] = forms.CharField(widget=forms.HiddenInput,
initial=policy_name)
def handle(self, request, data):
rule_name = data['rule_name']
comment = data['comment']
rule = data['rule']
policy_name = data['policy_name']
try:
params = {
'name': rule_name,
'comment': comment,
'rule': rule,
}
rule = congress.policy_rule_create(request, policy_name,
body=params)
msg = _("Rule created with id %s") % rule['id']
LOG.info(msg)
messages.success(request, msg)
return rule
except Exception as e:
msg = _('Error creating rule : %s') % str(e)
LOG.error(msg)
messages.error(self.request, msg)
redirect = reverse(self.failure_url, args=(policy_name,))
raise exceptions.Http302(redirect)

View File

@ -31,7 +31,7 @@ LOG = logging.getLogger(__name__)
class CreateRule(tables.LinkAction):
name = 'create_rule'
verbose_name = _('Create Rule')
verbose_name = _('Construct Rule')
url = 'horizon:admin:policies:create_rule'
classes = ('ajax-modal',)
icon = 'plus'
@ -42,6 +42,19 @@ class CreateRule(tables.LinkAction):
return reverse(self.url, args=(policy_name,))
class CreateRawRule(tables.LinkAction):
name = 'create_raw_rule'
verbose_name = _('Enter Rule')
url = 'horizon:admin:policies:create_raw_rule'
classes = ('ajax-modal',)
icon = 'plus'
policy_rules = (('policy', 'create_raw_rule'),)
def get_link_url(self, datum=None):
policy_name = self.table.kwargs['policy_name']
return reverse(self.url, args=(policy_name,))
class DeleteRule(policy.PolicyTargetMixin, tables.DeleteAction):
@staticmethod
def action_present(count):
@ -107,7 +120,7 @@ class PolicyRulesTable(tables.DataTable):
class Meta(object):
name = "policy_rules"
verbose_name = _("Rules")
table_actions = (CreateRule, DeleteRule,)
table_actions = (CreateRule, CreateRawRule, DeleteRule,)
row_actions = (DeleteRule,)
hidden_title = False

View File

@ -13,8 +13,10 @@
# under the License.
from django.core.urlresolvers import reverse
from horizon import forms
from horizon import workflows
from congress_dashboard.policies.rules import forms as rule_forms
from congress_dashboard.policies.rules import workflows as rule_workflows
@ -29,3 +31,23 @@ class CreateView(workflows.WorkflowView):
def get_initial(self):
return {'policy_name': self.kwargs['policy_name']}
class CreateRawView(forms.ModalFormView):
form_class = rule_forms.CreateRawRule
template_name = 'admin/policies/rules/create_raw.html'
success_url = 'horizon:admin:policies:detail'
def get_context_data(self, **kwargs):
context = super(CreateRawView, self).get_context_data(**kwargs)
context["policy_name"] = self.kwargs['policy_name']
return context
def get_initial(self):
initial = super(CreateRawView, self).get_initial()
initial.update({'policy_name': self.kwargs['policy_name']})
return initial
def get_success_url(self):
return reverse(self.success_url,
args=(self.kwargs['policy_name'],))

View File

@ -0,0 +1,26 @@
{% extends "horizon/common/_modal_form.html" %}
{% load i18n %}
{% load url from future %}
{% block form_id %}create_raw_rule_form{% endblock %}
{% block form_action %}{% url 'horizon:admin:policies:create_raw_rule' policy_name %}{% endblock %}
{% block modal_id %}create_raw_rule_modal{% endblock %}
{% block modal-header %}{% trans "Enter Rule" %}{% endblock %}
{% block modal-body %}
<div class="left">
<fieldset>
{% include "horizon/common/_form_fields.html" %}
</fieldset>
</div>
<div class="right">
<p>{% trans "Enter rule in policy rule language."%}</p>
<p>{% trans "For Example: " %}</p>
<p>{% trans "flavors_in_use(flavor_id, flavor_name) :- nova:flavors(id=flavor_id, name=flavor_name), nova:servers(flavor_id=flavor_id)."%}</p>
</div>
{% endblock %}
{% block modal-footer %}
<input class="btn btn-primary pull-right" type="submit" value="{% trans "Add" %}" />
<a href="{% url 'horizon:admin:policies:detail' policy_name%}" class="btn btn-default secondary cancel close">{% trans "Cancel" %}</a>
{% endblock %}

View File

@ -0,0 +1,11 @@
{% extends 'base.html' %}
{% load i18n %}
{% block title %}{% trans "Enter Rule" %}{% endblock %}
{% block page_header %}
{% include "horizon/common/_page_header.html" with title=_("Enter Rule") %}
{% endblock page_header %}
{% block main %}
{% include "admin/policies/rules/_create_raw.html" %}
{% endblock %}

View File

@ -29,6 +29,8 @@ urlpatterns = [
url(POLICY % 'detail', views.DetailView.as_view(), name='detail'),
url(POLICYTABLE % 'detail', data_views.DetailView.as_view(),
name='policy_table_detail'),
url(POLICY % 'rules/create_raw',
rule_views.CreateRawView.as_view(), name='create_raw_rule'),
url(POLICY % 'rules/create',
rule_views.CreateView.as_view(), name='create_rule'),
]

View File

@ -0,0 +1,5 @@
---
features:
- Added support to enter rules in policy language directlt using
congress_dashboard if the user doesn't need navigation support to create
rules.