Disable dashboard when Masakari is not available

Change-Id: I59d91249fe4b2036c12ecca089a61937571be201
This commit is contained in:
Pavlo Shchelokovskyy 2021-02-23 20:19:13 +00:00 committed by Pavlo Shchelokovskyy
parent 4c423195e8
commit 1a6d07c683
2 changed files with 32 additions and 1 deletions

View File

@ -13,12 +13,17 @@
# License for the specific language governing permissions and limitations
# under the License.
from oslo_log import log as logging
from django.utils.translation import ugettext_lazy as _
import horizon
from masakaridashboard.api import api
from masakaridashboard.default import panel
LOG = logging.getLogger(__name__)
class MasakariDashboard(horizon.Dashboard):
slug = "masakaridashboard"
@ -27,6 +32,23 @@ class MasakariDashboard(horizon.Dashboard):
default_panel = 'default'
policy_rules = (('instance-ha', 'context_is_admin'),)
def allowed(self, context):
# disable whole dashboard if masakari
# is not present in the service catalog
try:
# NOTE(pas-ha) this method tries to construct keystoneauth.Adapter
# for the Instance-HA service,
# which will fail if the service is absent
api.openstack_connection(context['request'])
except Exception as e:
# catch all errors and log them,
# no need to totally fail on e.g. HTTP connect failure
LOG.warning(f"Failed to find suitable endpoint for Instance HA "
f"service, Masakari Dashboard will not be displayed. "
f"Error was: {e}")
return False
return super().allowed(context)
horizon.register(MasakariDashboard)
MasakariDashboard.register(panel.Default)

View File

@ -13,6 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
from unittest import mock
from openstack_dashboard.test import helpers
from masakaridashboard.test.test_data import utils
@ -25,4 +27,11 @@ class MasakariTestsMixin(object):
class TestCase(MasakariTestsMixin, helpers.TestCase):
pass
def setUp(self):
allowed_patch = mock.patch(
"masakaridashboard.dashboard.MasakariDashboard.allowed",
return_value=True)
allowed_patch.start()
self.addCleanup(mock.patch.stopall)
super().setUp()