Merge "Hide Graph Metric action of alarm when Grafana is not available"

This commit is contained in:
Zuul 2019-09-18 08:45:25 +00:00 committed by Gerrit Code Review
commit 9fa2be83ca
2 changed files with 20 additions and 3 deletions

View File

@ -15,6 +15,7 @@
import json
from django.conf import settings
from django import template
from django.urls import reverse
from django.urls import reverse_lazy
@ -25,7 +26,6 @@ from horizon import tables
from monitoring.alarms import constants
from monitoring import api
from monitoring.config import local_settings
from monitoring.overview import constants as ov_constants
@ -159,7 +159,7 @@ class GraphMetric(tables.LinkAction):
self.attrs['target'] = '_blank'
try:
region = self.table.request.user.services_region
grafana_url = getattr(local_settings, 'GRAFANA_URL').get(region, '')
grafana_url = getattr(settings, 'GRAFANA_URL').get(region, '')
url = grafana_url + \
'/dashboard/script/drilldown.js'
metric = datum['metrics'][0]['name']
@ -179,7 +179,8 @@ class GraphMetric(tables.LinkAction):
return url + query
def allowed(self, request, datum=None):
return datum['metrics']
return (getattr(settings, 'GRAFANA_URL', None) is not None and
datum['metrics'])
class ShowAlarmDefinition(tables.LinkAction):

View File

@ -15,6 +15,7 @@
from __future__ import absolute_import
from django.test import TestCase
from django.test.utils import override_settings
from mock import Mock
import monitoring.alarms.tables
@ -46,3 +47,18 @@ class GraphMetricLinkActionTests(TestCase):
r'&threshold=[{"name": "metric1"}, {"name": "metric \u2461"}]'
r'&api=http://foo/api/'
)
def test_allowed_grafana_links(self):
allowed = self.get_allowed()
self.assertEqual(allowed, False)
@override_settings(GRAFANA_URL={'CustomRegion': '/grafana'})
def test_allowed_grafana_links_nonempty(self):
allowed = self.get_allowed()
self.assertEqual(allowed, 'metrics')
def get_allowed(self):
table_mock = Mock()
table_mock.request.build_absolute_uri.return_value = u"http://foo/api/"
link_action = monitoring.alarms.tables.GraphMetric(table=table_mock)
return link_action.allowed(table_mock.request, {'metrics': 'metrics'})