From f5e46c0b0af12f22c6b3904b85fddbb33bb8c7cf Mon Sep 17 00:00:00 2001 From: Anusha Ramineni Date: Tue, 17 Oct 2017 10:40:06 +0530 Subject: [PATCH] dashboard: View library policies This commit is to list library policies through congress-dashboard TODO: show rules activate policy Partially-Implements blueprint add-policy-library-gui Change-Id: Iff59145001af7df9afb3c654346fada3c451e966 --- congress_dashboard/api/congress.py | 14 +++++++ congress_dashboard/enabled/_80_library.py | 5 +++ congress_dashboard/library/__init__.py | 0 congress_dashboard/library/panel.py | 25 ++++++++++++ congress_dashboard/library/tables.py | 29 ++++++++++++++ .../library/templates/library/index.html | 12 ++++++ congress_dashboard/library/urls.py | 22 ++++++++++ congress_dashboard/library/views.py | 40 +++++++++++++++++++ 8 files changed, 147 insertions(+) create mode 100644 congress_dashboard/enabled/_80_library.py create mode 100644 congress_dashboard/library/__init__.py create mode 100644 congress_dashboard/library/panel.py create mode 100644 congress_dashboard/library/tables.py create mode 100644 congress_dashboard/library/templates/library/index.html create mode 100644 congress_dashboard/library/urls.py create mode 100644 congress_dashboard/library/views.py diff --git a/congress_dashboard/api/congress.py b/congress_dashboard/api/congress.py index 6f63c3e..fb4bad9 100644 --- a/congress_dashboard/api/congress.py +++ b/congress_dashboard/api/congress.py @@ -335,3 +335,17 @@ def delete_datasource(request, datasource_name): except Exception: LOG.exception("deleting datasource %s failed", datasource_name) raise + + +def list_policies_from_library(request): + client = congressclient(request) + try: + results = client.list_library_policy()['results'] + policies = [] + for p in results: + policy = PolicyAPIDictWrapper(p) + policies.append(policy) + return policies + except Exception: + LOG.exception("List library policies failed") + raise diff --git a/congress_dashboard/enabled/_80_library.py b/congress_dashboard/enabled/_80_library.py new file mode 100644 index 0000000..39be6e3 --- /dev/null +++ b/congress_dashboard/enabled/_80_library.py @@ -0,0 +1,5 @@ +PANEL = 'library' +PANEL_DASHBOARD = 'admin' +PANEL_GROUP = 'policy' +ADD_PANEL = 'congress_dashboard.library.panel.PolicyLibrary' +AUTO_DISCOVER_STATIC_FILES = True diff --git a/congress_dashboard/library/__init__.py b/congress_dashboard/library/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/congress_dashboard/library/panel.py b/congress_dashboard/library/panel.py new file mode 100644 index 0000000..f343d60 --- /dev/null +++ b/congress_dashboard/library/panel.py @@ -0,0 +1,25 @@ +# 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. + +from django.utils.translation import ugettext_lazy as _ +import horizon +from openstack_dashboard.dashboards.admin import dashboard + + +class PolicyLibrary(horizon.Panel): + name = _("Library") + slug = "library" + permissions = ('openstack.roles.admin',) + +dashboard.Admin.register(PolicyLibrary) diff --git a/congress_dashboard/library/tables.py b/congress_dashboard/library/tables.py new file mode 100644 index 0000000..a10dff5 --- /dev/null +++ b/congress_dashboard/library/tables.py @@ -0,0 +1,29 @@ +# 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. + +from django.utils.translation import ugettext_lazy as _ +from horizon import tables + + +class LibraryTable(tables.DataTable): + id = tables.Column("id", verbose_name=_("Policy ID"), hidden=True, + sortable=False) + name = tables.Column("name", verbose_name=_("Policy Name")) + desc = tables.WrappingColumn("description", verbose_name=_("Description"), + sortable=False) + + class Meta(object): + name = "policy_library" + verbose_name = _("Policy Library") + hidden_title = True diff --git a/congress_dashboard/library/templates/library/index.html b/congress_dashboard/library/templates/library/index.html new file mode 100644 index 0000000..35482bf --- /dev/null +++ b/congress_dashboard/library/templates/library/index.html @@ -0,0 +1,12 @@ +{% extends 'base.html' %} +{% load i18n %} +{% block title %}{% trans "Policies" %}{% endblock %} + +{% block page_header %} + {% include "horizon/common/_page_header.html" with title=_("Policy Library") %} +{% endblock page_header %} + +{% block main %} +
+ {{policy_library_table.render}} +{% endblock %} diff --git a/congress_dashboard/library/urls.py b/congress_dashboard/library/urls.py new file mode 100644 index 0000000..a1bcbf6 --- /dev/null +++ b/congress_dashboard/library/urls.py @@ -0,0 +1,22 @@ +# 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. + +from django.conf.urls import url + +from congress_dashboard.library import views + + +urlpatterns = [ + url(r'^$', views.IndexView.as_view(), name='index'), +] diff --git a/congress_dashboard/library/views.py b/congress_dashboard/library/views.py new file mode 100644 index 0000000..58b6674 --- /dev/null +++ b/congress_dashboard/library/views.py @@ -0,0 +1,40 @@ +# 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.utils.translation import ugettext_lazy as _ +from horizon import messages +from horizon import tables + +from congress_dashboard.api import congress +from congress_dashboard.library import tables as library_tables + +LOG = logging.getLogger(__name__) + + +class IndexView(tables.DataTableView): + """List policies from library.""" + table_class = library_tables.LibraryTable + template_name = 'admin/library/index.html' + + def get_data(self): + try: + policies = congress.list_policies_from_library(self.request) + return policies + except Exception as e: + msg = _('Unable to list library policies: %s') % str(e) + LOG.exception(msg) + messages.error(self.request, msg) + return []