Progress on alarms (details and create)

This commit is contained in:
Rob Raymond 2014-05-08 12:43:28 -06:00
parent 0579bac6b4
commit 3301b8faf3
14 changed files with 74 additions and 46 deletions

View File

@ -68,7 +68,10 @@ def alarm_delete(request, alarm_id):
def alarm_get(request, alarm_id): def alarm_get(request, alarm_id):
return monclient(request).alarms.get(alarm_id) args = AttrStore()
args.runlocal = True
args.os_tenant_id = "12345678"
return monclient(request).alarms.get(args, alarm_id=alarm_id)
def alarm_create(request, password=None, **kwargs): def alarm_create(request, password=None, **kwargs):
@ -94,11 +97,17 @@ def notification_delete(request, notification_id):
def notification_get(request, notification_id): def notification_get(request, notification_id):
return monclient(request).notifications.get(notification_id) args = AttrStore()
args.runlocal = True
args.os_tenant_id = "12345678"
return monclient(request).notifications.get(args, notification_id=notification_id)
def notification_create(request, password=None, **kwargs): def notification_create(request, **kwargs):
return monclient(request, password).notifications.create(**kwargs) args = AttrStore()
args.runlocal = True
args.os_tenant_id = "12345678"
return monclient(request).notifications.create(args, **kwargs)
def notification_update(request, notification_id, **kwargs): def notification_update(request, notification_id, **kwargs):

Binary file not shown.

View File

@ -39,3 +39,6 @@ PHONE_VALIDATOR = validators.RegexValidator(
EMAIL_VALIDATOR = validators.EmailValidator( EMAIL_VALIDATOR = validators.EmailValidator(
message=_("Address must contain a valid email address.")) message=_("Address must contain a valid email address."))
URL_PREFIX = 'horizon:admin:monitoring:'
TEMPLATE_PREFIX = 'admin/monitoring/'

View File

@ -178,8 +178,8 @@ class CreateAlarmForm(BaseAlarmForm):
notifications = [] notifications = []
exceptions.handle(request, exceptions.handle(request,
_('Unable to retrieve notifications: %s') % e) _('Unable to retrieve notifications: %s') % e)
notification_choices = [(notification.id, notification.name) notification_choices = [(notification['id'], notification['name'])
for notification in notifications] for notification in notifications]
if notification_choices: if notification_choices:
if len(notification_choices) > 1: if len(notification_choices) > 1:
notification_choices.insert( notification_choices.insert(
@ -283,9 +283,9 @@ class CreateMethodForm(BaseNotificationMethodForm):
try: try:
api.monitoring.notification_create( api.monitoring.notification_create(
request, request,
data['name'], name=data['name'],
data['type'], type=data['type'],
data['address']) address=data['address'])
messages.success(request, messages.success(request,
_('Notification method has been created ' _('Notification method has been created '
'successfully.')) 'successfully.'))

View File

@ -21,6 +21,8 @@ from django.utils.translation import ugettext_lazy as _
from horizon import tables from horizon import tables
from . import constants
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -56,14 +58,14 @@ def show_status(data):
class ShowAlarmHistory(tables.LinkAction): class ShowAlarmHistory(tables.LinkAction):
name = 'history' name = 'history'
verbose_name = _('Show History') verbose_name = _('Show History')
url = 'horizon:admin:monitoring:history' url = constants.URL_PREFIX + 'history'
classes = ('btn-edit',) classes = ('btn-edit',)
class ShowAlarmMeters(tables.LinkAction): class ShowAlarmMeters(tables.LinkAction):
name = 'meters' name = 'meters'
verbose_name = _('Show Meters') verbose_name = _('Show Meters')
url = 'horizon:admin:monitoring:meters' url = constants.URL_PREFIX + 'meters'
classes = ('btn-edit',) classes = ('btn-edit',)
@ -71,7 +73,7 @@ class CreateAlarm(tables.LinkAction):
name = "create_alarm" name = "create_alarm"
verbose_name = _("Create Alarm") verbose_name = _("Create Alarm")
classes = ("ajax-modal", "btn-create") classes = ("ajax-modal", "btn-create")
url = 'horizon:admin:monitoring:alarm_create' url = constants.URL_PREFIX + 'alarm_create'
def allowed(self, request, datum=None): def allowed(self, request, datum=None):
return True return True
@ -101,12 +103,14 @@ class AlarmsTable(tables.DataTable):
class RealAlarmsTable(tables.DataTable): class RealAlarmsTable(tables.DataTable):
state = tables.Column('state', verbose_name=_('State')) state = tables.Column('state', verbose_name=_('State'))
target = tables.Column('name', verbose_name=_('Name')) target = tables.Column('name', verbose_name=_('Name'),
link=constants.URL_PREFIX + 'alarm_detail',
link_classes=('ajax-modal',))
name = tables.Column('description', verbose_name=_('Description')) name = tables.Column('description', verbose_name=_('Description'))
expression = tables.Column('expression', verbose_name=_('Expression')) expression = tables.Column('expression', verbose_name=_('Expression'))
def get_object_id(self, obj): def get_object_id(self, obj):
return obj['name'] return obj['id']
class Meta: class Meta:
name = "alarms" name = "alarms"

View File

@ -3,7 +3,7 @@
{% load url from future %} {% load url from future %}
{% block form_id %}create_alarm_form{% endblock %} {% block form_id %}create_alarm_form{% endblock %}
{% block form_action %}{% url 'horizon:admin:monitoring:alarm_create' %}{% endblock %} {% block form_action %}{{ action_url }}{% endblock %}
{% block modal-header %}{% trans "Create Alarm" %}{% endblock %} {% block modal-header %}{% trans "Create Alarm" %}{% endblock %}
@ -40,5 +40,5 @@ $('#add_notification_button').click(function(){
{% block modal-footer %} {% block modal-footer %}
<input class="btn btn-primary pull-right" type="submit" value="{% trans "Create Alarm" %}" /> <input class="btn btn-primary pull-right" type="submit" value="{% trans "Create Alarm" %}" />
<a href="{% url 'horizon:admin:monitoring:alarm' %}" class="btn secondary cancel close">{% trans "Cancel" %}</a> <a href="{{ cancel_url }}" class="btn secondary cancel close">{% trans "Cancel" %}</a>
{% endblock %} {% endblock %}

View File

@ -3,7 +3,6 @@
{% load url from future %} {% load url from future %}
{% block form_id %}detail_alarm_form{% endblock %} {% block form_id %}detail_alarm_form{% endblock %}
{% block form_action %}{% url 'horizon:project:monitoring_alarms:detail' alarm.id %}{% endblock %}
{% block modal-header %}{% trans "Alarm Details" %}{% endblock %} {% block modal-header %}{% trans "Alarm Details" %}{% endblock %}
@ -14,5 +13,5 @@
{% endblock %} {% endblock %}
{% block modal-footer %} {% block modal-footer %}
<a href="{% url 'horizon:project:monitoring_alarms:index' %}" class="btn secondary cancel close">{% trans "Close" %}</a> <a href="{{ cancel_url }}" class="btn secondary cancel close">{% trans "Close" %}</a>
{% endblock %} {% endblock %}

View File

@ -7,5 +7,5 @@
{% endblock page_header %} {% endblock page_header %}
{% block main %} {% block main %}
{% include 'project/monitoring_alarms/_detail.html' %} {% include 'admin/monitoring/alarms/_detail.html' %}
{% endblock %} {% endblock %}

View File

@ -7,5 +7,5 @@
{% endblock page_header %} {% endblock page_header %}
{% load url from future %} {% load url from future %}
{% block main %} {% block main %}
{% include 'admin/monitoring/newmonitor.html' %} {% include 'admin/monitoring/monitor.html' %}
{% endblock %} {% endblock %}

View File

@ -3,7 +3,7 @@
{% load url from future %} {% load url from future %}
{% block form_id %}create_notif_method_form{% endblock %} {% block form_id %}create_notif_method_form{% endblock %}
{% block form_action %}{% url 'horizon:project:monitoring_notifications:methods:create' %}{% endblock %} {% block form_action %}{% url 'horizon:admin:monitoring:notification_create' %}{% endblock %}
{% block modal-header %}{% trans "Create Notification Method" %}{% endblock %} {% block modal-header %}{% trans "Create Notification Method" %}{% endblock %}
@ -30,5 +30,5 @@
{% block modal-footer %} {% block modal-footer %}
<input class="btn btn-primary pull-right" type="submit" value="{% trans "Create Notification Method" %}" /> <input class="btn btn-primary pull-right" type="submit" value="{% trans "Create Notification Method" %}" />
<a href="{% url 'horizon:project:monitoring_notifications:index' %}" class="btn secondary cancel close">{% trans "Cancel" %}</a> <a href="{% url 'horizon:admin:monitoring:index' %}" class="btn secondary cancel close">{% trans "Cancel" %}</a>
{% endblock %} {% endblock %}

View File

@ -7,5 +7,5 @@
{% endblock page_header %} {% endblock page_header %}
{% block main %} {% block main %}
{% include 'project/monitoring_notifications/methods/_create.html' %} {% include 'admin/monitoring/notifications/_create.html' %}
{% endblock %} {% endblock %}

View File

@ -7,5 +7,5 @@
{% endblock page_header %} {% endblock page_header %}
{% block main %} {% block main %}
{% include 'project/monitoring_notifications/methods/_detail.html' %} {% include 'admin/monitoring/notifications/_detail.html' %}
{% endblock %} {% endblock %}

View File

@ -36,4 +36,5 @@ urlpatterns = patterns(
name='meters'), name='meters'),
url(r'^alarm_create$', views.AlarmCreateView.as_view(), name='alarm_create'), url(r'^alarm_create$', views.AlarmCreateView.as_view(), name='alarm_create'),
url(r'^(?P<id>[^/]+)/alarm_detail/$', views.AlarmDetailView.as_view(), name='alarm_detail'), url(r'^(?P<id>[^/]+)/alarm_detail/$', views.AlarmDetailView.as_view(), name='alarm_detail'),
url(r'^notification_create$', views.NotificationCreateView.as_view(), name='notification_create'),
) )

View File

@ -19,14 +19,15 @@ import logging
import json import json
import random import random
from django.core.urlresolvers import reverse_lazy # noqa from django.core.urlresolvers import reverse_lazy, reverse # noqa
from django.template import defaultfilters as filters from django.template import defaultfilters as filters
from django.http import HttpResponse # noqa from django.http import HttpResponse # noqa
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.views.generic import TemplateView from django.views.generic import TemplateView
from horizon import tables from horizon import exceptions
from horizon import forms from horizon import forms
from horizon import tables
from monitoring.api import monitoring from monitoring.api import monitoring
@ -34,6 +35,7 @@ from .tables import AlarmsTable
from .tables import RealAlarmsTable from .tables import RealAlarmsTable
from .tables import AlarmHistoryTable from .tables import AlarmHistoryTable
from . import forms as alarm_forms from . import forms as alarm_forms
from . import constants
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -131,7 +133,7 @@ SAMPLE = [{'name': _('Platform Services'),
] ]
class IndexView(TemplateView): class IndexView(TemplateView):
template_name = 'admin/monitoring/index.html' template_name = constants.TEMPLATE_PREFIX + 'index.html'
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs) context = super(IndexView, self).get_context_data(**kwargs)
@ -142,7 +144,7 @@ class IndexView(TemplateView):
class StatusView(TemplateView): class StatusView(TemplateView):
template_name = "admin/metering/samples.csv" template_name = ""
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
services = ['MaaS', services = ['MaaS',
@ -211,11 +213,18 @@ class AlarmView(tables.DataTableView):
class AlarmCreateView(forms.ModalFormView): class AlarmCreateView(forms.ModalFormView):
form_class = alarm_forms.CreateAlarmForm form_class = alarm_forms.CreateAlarmForm
template_name = 'admin/monitoring/alarms/create.html' template_name = constants.TEMPLATE_PREFIX + 'alarms/create.html'
success_url = reverse_lazy('horizon:admin:monitoring:alarm') success_url = reverse_lazy(constants.URL_PREFIX + 'alarm')
def get_context_data(self, **kwargs):
context = super(AlarmCreateView, self).get_context_data(**kwargs)
context["cancel_url"] = self.success_url
context["action_url"] = reverse(constants.URL_PREFIX + 'alarm_create')
return context
def transform_alarm_data(obj): def transform_alarm_data(obj):
return obj
return {'id': getattr(obj, 'id', None), return {'id': getattr(obj, 'id', None),
'name': getattr(obj, 'name', None), 'name': getattr(obj, 'name', None),
'expression': getattr(obj, 'expression', None), 'expression': getattr(obj, 'expression', None),
@ -225,8 +234,8 @@ def transform_alarm_data(obj):
class AlarmDetailView(forms.ModalFormView): class AlarmDetailView(forms.ModalFormView):
form_class = alarm_forms.DetailAlarmForm form_class = alarm_forms.DetailAlarmForm
template_name = 'admin/monitoring/alarms/detail.html' template_name = constants.TEMPLATE_PREFIX + 'alarms/detail.html'
success_url = reverse_lazy('horizon:admin:monitoring:alarm') success_url = reverse_lazy(constants.URL_PREFIX + 'alarm')
def get_object(self): def get_object(self):
id = self.kwargs['id'] id = self.kwargs['id']
@ -234,19 +243,15 @@ class AlarmDetailView(forms.ModalFormView):
if hasattr(self, "_object"): if hasattr(self, "_object"):
return self._object return self._object
self._object = None self._object = None
self._object = api.hp_monitoring.alarm_get(self.request, id) self._object = monitoring.alarm_get(self.request, id)
alarm_actions = getattr(self._object, 'alarm_actions', [])
notifications = [] notifications = []
# Fetch the notification object for each alarm_actions # Fetch the notification object for each alarm_actions
for notif_id in alarm_actions: for notif_id in self._object["alarm_actions"]:
try: try:
notification = api.hp_monitoring.notification_method_get( notification = monitoring.notification_get(
self.request, self.request,
notif_id) notif_id)
notifications.append({"id": notification.id, notifications.append(notification)
"name": notification.name,
"type": notification.type,
"address": notification.address})
except exceptions.NOT_FOUND: except exceptions.NOT_FOUND:
msg = _("Notification %s has already been deleted.") % \ msg = _("Notification %s has already been deleted.") % \
notif_id notif_id
@ -254,10 +259,10 @@ class AlarmDetailView(forms.ModalFormView):
"name": unicode(msg), "name": unicode(msg),
"type": "", "type": "",
"address": ""}) "address": ""})
self._object.alarm_actions = notifications self._object["notifications"] = notifications
return self._object return self._object
except Exception: except Exception:
redirect = reverse(constants.ALARMS_INDEX_URL) redirect = reverse(constants.URL_PREFIX + 'alarm')
exceptions.handle(self.request, exceptions.handle(self.request,
_('Unable to retrieve alarm details.'), _('Unable to retrieve alarm details.'),
redirect=redirect) redirect=redirect)
@ -268,14 +273,15 @@ class AlarmDetailView(forms.ModalFormView):
return transform_alarm_data(self.alarm) return transform_alarm_data(self.alarm)
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs) context = super(AlarmDetailView, self).get_context_data(**kwargs)
context["alarm"] = self.alarm context["alarm"] = self.alarm
context["cancel_url"] = self.success_url
return context return context
class AlarmHistoryView(tables.DataTableView): class AlarmHistoryView(tables.DataTableView):
table_class = AlarmHistoryTable table_class = AlarmHistoryTable
template_name = 'admin/monitoring/alarm_history.html' template_name = constants.TEMPLATE_PREFIX + 'alarm_history.html'
def dispatch(self, *args, **kwargs): def dispatch(self, *args, **kwargs):
return super(AlarmHistoryView, self).dispatch(*args, **kwargs) return super(AlarmHistoryView, self).dispatch(*args, **kwargs)
@ -295,7 +301,7 @@ class AlarmHistoryView(tables.DataTableView):
class AlarmMeterView(TemplateView): class AlarmMeterView(TemplateView):
template_name = 'admin/monitoring/alarm_meter.html' template_name = constants.TEMPLATE_PREFIX + 'alarm_meter.html'
def get_random_status(): def get_random_status():
@ -311,4 +317,10 @@ def get_random_status():
if num < dist["prob"]: if num < dist["prob"]:
return dist["value"] return dist["value"]
num = num - dist["prob"] num = num - dist["prob"]
return distribution[len(distribution) - 1]["value"] return distribution[len(distribution) - 1]["value"]
class NotificationCreateView(forms.ModalFormView):
form_class = alarm_forms.CreateMethodForm
template_name = constants.TEMPLATE_PREFIX + 'notifications/create.html'
success_url = reverse_lazy(constants.URL_PREFIX + 'alarm')