Add alarm edit
This commit is contained in:
parent
e1a01ecf82
commit
83f3f8862b
@ -16,8 +16,6 @@ import logging
|
||||
|
||||
from django.conf import settings
|
||||
from monclient import client as mon_client
|
||||
from horizon.utils import functions as utils
|
||||
from openstack_dashboard.api import base
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
@ -69,8 +67,8 @@ def alarm_create(request, password=None, **kwargs):
|
||||
return monclient(request, password).alarms.create(**kwargs)
|
||||
|
||||
|
||||
def alarm_update(request, alarm_id, **kwargs):
|
||||
return monclient(request).alarms.update(alarm_id, **kwargs)
|
||||
def alarm_update(request, **kwargs):
|
||||
return monclient(request).alarms.update(**kwargs)
|
||||
|
||||
|
||||
def notification_list(request, marker=None, paginate=False):
|
||||
|
@ -172,12 +172,7 @@ class BaseAlarmForm(forms.SelfHandlingForm):
|
||||
required=False,
|
||||
widget=notificationWidget)
|
||||
|
||||
|
||||
class CreateAlarmForm(BaseAlarmForm):
|
||||
def __init__(self, request, *args, **kwargs):
|
||||
super(CreateAlarmForm, self).__init__(request, *args, **kwargs)
|
||||
super(CreateAlarmForm, self)._init_fields(readOnly=False, create=True)
|
||||
|
||||
def set_notification_choices(self, request):
|
||||
try:
|
||||
notifications = api.monitor.notification_list(request)
|
||||
except Exception as e:
|
||||
@ -194,9 +189,16 @@ class CreateAlarmForm(BaseAlarmForm):
|
||||
notification_choices.insert(
|
||||
0, ("", unicode(_("No notifications available."))))
|
||||
|
||||
self.fields.pop('state')
|
||||
self.fields['notifications'].choices = notification_choices
|
||||
|
||||
|
||||
class CreateAlarmForm(BaseAlarmForm):
|
||||
def __init__(self, request, *args, **kwargs):
|
||||
super(CreateAlarmForm, self).__init__(request, *args, **kwargs)
|
||||
super(CreateAlarmForm, self)._init_fields(readOnly=False, create=True)
|
||||
super(CreateAlarmForm, self).set_notification_choices(request)
|
||||
self.fields.pop('state')
|
||||
|
||||
def clean_notifications(self):
|
||||
notifications = self.cleaned_data["notifications"]
|
||||
if len(notifications) == 0:
|
||||
@ -231,6 +233,37 @@ class DetailAlarmForm(BaseAlarmForm):
|
||||
return True
|
||||
|
||||
|
||||
class EditAlarmForm(BaseAlarmForm):
|
||||
def __init__(self, request, *args, **kwargs):
|
||||
super(EditAlarmForm, self).__init__(request, *args, **kwargs)
|
||||
super(EditAlarmForm, self)._init_fields(readOnly=False)
|
||||
super(EditAlarmForm, self).set_notification_choices(request)
|
||||
self.fields.pop('state')
|
||||
|
||||
def handle(self, request, data):
|
||||
try:
|
||||
alarm_actions = []
|
||||
if data['notifications']:
|
||||
alarm_actions = [notification.get('notification_id')
|
||||
for notification in data['notifications']]
|
||||
api.monitor.alarm_update(
|
||||
request,
|
||||
alarm_id=self.initial['id'],
|
||||
actions_enabled=self.initial['actions_enabled'],
|
||||
state=self.initial['state'],
|
||||
name=data['name'],
|
||||
expression=data['expression'],
|
||||
description=data['description'],
|
||||
alarm_actions=alarm_actions,
|
||||
)
|
||||
messages.success(request,
|
||||
_('Alarm has been edited successfully.'))
|
||||
except Exception as e:
|
||||
exceptions.handle(request, _('Unable to edit the alarm: %s') % e)
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
READONLY_TEXTINPUT = forms.TextInput(attrs={'readonly': 'readonly'})
|
||||
|
||||
|
||||
|
@ -80,6 +80,16 @@ class CreateAlarm(tables.LinkAction):
|
||||
return True
|
||||
|
||||
|
||||
class EditAlarm(tables.LinkAction):
|
||||
name = "edit_alarm"
|
||||
verbose_name = _("Edit Alarm")
|
||||
classes = ("ajax-modal", "btn-create")
|
||||
url = constants.URL_PREFIX + 'alarm_edit'
|
||||
|
||||
def allowed(self, request, datum=None):
|
||||
return True
|
||||
|
||||
|
||||
class DeleteAlarm(tables.DeleteAction):
|
||||
name = "delete_alarm"
|
||||
verbose_name = _("Delete Alarm")
|
||||
@ -138,7 +148,7 @@ class RealAlarmsTable(tables.DataTable):
|
||||
class Meta:
|
||||
name = "alarms"
|
||||
verbose_name = _("Alarms")
|
||||
row_actions = (ShowAlarmHistory, ShowAlarmMeters, DeleteAlarm, )
|
||||
row_actions = (ShowAlarmHistory, ShowAlarmMeters, DeleteAlarm, EditAlarm, )
|
||||
table_actions = (CreateNotification, CreateAlarm, )
|
||||
status_columns = ['state']
|
||||
|
||||
|
@ -36,5 +36,6 @@ urlpatterns = patterns(
|
||||
name='meters'),
|
||||
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_edit/$', views.AlarmEditView.as_view(), name='alarm_edit'),
|
||||
url(r'^notification_create$', views.NotificationCreateView.as_view(), name='notification_create'),
|
||||
)
|
||||
|
@ -106,6 +106,10 @@ SAMPLE = [{'name': _('Platform Services'),
|
||||
'class': 'alert-success',
|
||||
'icon': '/static/monitoring/img/ok-icon.png',
|
||||
'display': _('Horizon')},
|
||||
{'name': 'monitoring',
|
||||
'class': 'alert-success',
|
||||
'icon': '/static/monitoring/img/ok-icon.png',
|
||||
'display': _('Monitoring')},
|
||||
]},
|
||||
{'name': _('Network Services'),
|
||||
'services': [{'name': 'dhcp',
|
||||
@ -278,6 +282,54 @@ class AlarmDetailView(forms.ModalFormView):
|
||||
return context
|
||||
|
||||
|
||||
class AlarmEditView(forms.ModalFormView):
|
||||
form_class = alarm_forms.EditAlarmForm
|
||||
template_name = constants.TEMPLATE_PREFIX + 'alarms/edit.html'
|
||||
success_url = reverse_lazy(constants.URL_PREFIX + 'alarm')
|
||||
|
||||
def get_object(self):
|
||||
id = self.kwargs['id']
|
||||
try:
|
||||
if hasattr(self, "_object"):
|
||||
return self._object
|
||||
self._object = None
|
||||
self._object = api.monitor.alarm_get(self.request, id)
|
||||
notifications = []
|
||||
# Fetch the notification object for each alarm_actions
|
||||
for notif_id in self._object["alarm_actions"]:
|
||||
try:
|
||||
notification = api.monitor.notification_get(
|
||||
self.request,
|
||||
notif_id)
|
||||
notifications.append(notification)
|
||||
except exceptions.NOT_FOUND:
|
||||
msg = _("Notification %s has already been deleted.") % \
|
||||
notif_id
|
||||
notifications.append({"id": notif_id,
|
||||
"name": unicode(msg),
|
||||
"type": "",
|
||||
"address": ""})
|
||||
self._object["notifications"] = notifications
|
||||
return self._object
|
||||
except Exception:
|
||||
redirect = reverse(constants.URL_PREFIX + 'alarm')
|
||||
exceptions.handle(self.request,
|
||||
_('Unable to retrieve alarm details.'),
|
||||
redirect=redirect)
|
||||
return None
|
||||
|
||||
def get_initial(self):
|
||||
self.alarm = self.get_object()
|
||||
return transform_alarm_data(self.alarm)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(AlarmEditView, self).get_context_data(**kwargs)
|
||||
id = self.kwargs['id']
|
||||
context["cancel_url"] = self.success_url
|
||||
context["action_url"] = reverse(constants.URL_PREFIX + 'alarm_edit', args=(id,))
|
||||
return context
|
||||
|
||||
|
||||
class AlarmHistoryView(tables.DataTableView):
|
||||
table_class = AlarmHistoryTable
|
||||
template_name = constants.TEMPLATE_PREFIX + 'alarm_history.html'
|
||||
|
Loading…
x
Reference in New Issue
Block a user