List rules for library policies

This commit adds support list rules for specific library policy.
Reference screenshot of GUI: https://imagebin.ca/v/3fkr2eDbDS9i

Partially-Implements blueprint add-policy-library-gui
Change-Id: Icd51bd408a295241d763e8369049b9aff5db5504
This commit is contained in:
Anusha Ramineni 2017-10-30 14:30:39 +05:30
parent f5e46c0b0a
commit b2d848640b
7 changed files with 126 additions and 20 deletions

View File

@ -28,6 +28,30 @@ TABLE_SEPARATOR = ':'
LOG = logging.getLogger(__name__)
def format_rule(rule):
"""Make rule's text more human readable."""
head_body = rule.split(RULE_SEPARATOR)
if len(head_body) < 2:
return rule
head = head_body[0]
body = head_body[1]
body_literals = body.split(LITERALS_SEPARATOR)
result = []
for lit in body_literals:
# First remove extra newlines in the literals
lit = lit.strip()
result.append(lit)
# Add newline after each literal in the body.
literals_break = LITERALS_SEPARATOR + '\n'
new_body = literals_break.join(result)
# Add newline after the head.
rules_break = RULE_SEPARATOR + '\n'
return rules_break.join([head, new_body])
def _set_id_as_name_if_empty(apidict, length=0):
try:
if not apidict._apidict.get('name'):
@ -349,3 +373,14 @@ def list_policies_from_library(request):
except Exception:
LOG.exception("List library policies failed")
raise
def show_library_policy(request, name):
client = congressclient(request)
try:
policy = client.show_library_policy(name)
rules = [PolicyRule(r) for r in policy['rules']]
return policy, rules
except Exception:
LOG.exception("unable to get library policy '%s' details", name)
raise

View File

@ -12,14 +12,23 @@
# License for the specific language governing permissions and limitations
# under the License.
from django.core.urlresolvers import reverse
from django.template.defaultfilters import linebreaksbr
from django.utils.translation import ugettext_lazy as _
from horizon import tables
from congress_dashboard.api import congress
def get_policy_link(datum):
return reverse('horizon:admin:library:detail', args=(datum['id'],))
class LibraryTable(tables.DataTable):
id = tables.Column("id", verbose_name=_("Policy ID"), hidden=True,
sortable=False)
name = tables.Column("name", verbose_name=_("Policy Name"))
name = tables.Column("name", verbose_name=_("Policy Name"),
link=get_policy_link)
desc = tables.WrappingColumn("description", verbose_name=_("Description"),
sortable=False)
@ -27,3 +36,16 @@ class LibraryTable(tables.DataTable):
name = "policy_library"
verbose_name = _("Policy Library")
hidden_title = True
class LibraryPolicyRulesTable(tables.DataTable):
name = tables.Column("name", verbose_name=_("Rule Name"),
classes=('nowrap-col',))
rule = tables.Column("rule", verbose_name=_("Rule"),
filters=(congress.format_rule, linebreaksbr,))
comment = tables.WrappingColumn("comment", verbose_name=_("Comment"))
class Meta(object):
name = "policy_library_rules"
verbose_name = _("Policy Rules")
hidden_title = False

View File

@ -0,0 +1,16 @@
{% load i18n %}
<h3>{% trans "Policy Overview" %}</h3>
<div class="detail">
<dl class="dl-horizontal">
<dt>{% trans "Policy Name" %}</dt>
<dd>{{ policy.name|default:policy.id }}</dd>
<dt>{% trans "Policy ID" %}</dt>
<dd>{{ policy.id }}</dd>
<dt>{% trans "Description" %}</dt>
<dd>{{ policy.description }}</dd>
<dt>{% trans "Kind" %}</dt>
<dd>{{ policy.kind }}</dd>
</dl>
</div>

View File

@ -0,0 +1,15 @@
{% extends 'base.html' %}
{% load i18n %}
{% block title %}{% trans "Policy Details" %}{% endblock %}
{% block page_header %}
{% include "horizon/common/_page_header.html" with title=_("Policy Details: ")|add:policy.name %}
{% endblock page_header %}
{% block main %}
{% include "admin/library/_detail_overview.html" %}
<hr>
<div id="policy_library_rules">
{{ policy_library_rules_table.render }}
</div>
{% endblock %}

View File

@ -16,7 +16,9 @@ from django.conf.urls import url
from congress_dashboard.library import views
POLICY = r'^(?P<policy_name>[^/]+)/%s$'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(POLICY % 'detail', views.DetailView.as_view(), name='detail'),
]

View File

@ -38,3 +38,37 @@ class IndexView(tables.DataTableView):
LOG.exception(msg)
messages.error(self.request, msg)
return []
class DetailView(tables.DataTableView):
"""List details about and rules in a policy."""
table_class = library_tables.LibraryPolicyRulesTable
template_name = 'admin/library/detail.html'
def get_data(self):
try:
policy_id = self.kwargs['policy_name']
policy, rules = congress.show_library_policy(self.request,
policy_id)
for r in rules:
head = r['rule'].split(congress.RULE_SEPARATOR)[0]
name = (head.split('(')[0]).replace('_', ' ').title()
name = name.split('[')[0]
r.set_value('name', name)
r.set_id_if_empty(name)
return rules
except Exception as e:
msg = _('Unable to list rules of library policy: %s') % str(e)
LOG.exception(msg)
messages.error(self.request, msg)
return []
def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)
try:
policy_id = self.kwargs['policy_name']
policy, _ = congress.show_library_policy(self.request, policy_id)
context['policy'] = policy
return context
except Exception:
raise

View File

@ -92,30 +92,12 @@ class DeleteRule(policy.PolicyTargetMixin, tables.DeleteAction):
raise exceptions.Http302(redirect)
def _format_rule(rule):
"""Make rule's text more human readable."""
head_body = rule.split(congress.RULE_SEPARATOR)
if len(head_body) < 2:
return rule
head = head_body[0]
body = head_body[1]
# Add newline after each literal in the body.
body_literals = body.split(congress.LITERALS_SEPARATOR)
literals_break = congress.LITERALS_SEPARATOR + '\n'
new_body = literals_break.join(body_literals)
# Add newline after the head.
rules_break = congress.RULE_SEPARATOR + '\n'
return rules_break.join([head, new_body])
class PolicyRulesTable(tables.DataTable):
id = tables.Column("id", verbose_name=_("Rule ID"))
name = tables.Column("name", verbose_name=_("Name"))
comment = tables.Column("comment", verbose_name=_("Comment"))
rule = tables.Column("rule", verbose_name=_("Rule"),
filters=(_format_rule, linebreaksbr,))
filters=(congress.format_rule, linebreaksbr,))
class Meta(object):
name = "policy_rules"