xref: /openbmc/webui-vue/src/views/Settings/SnmpAlerts/SnmpAlerts.vue (revision de23ea23d88451a2fa2774ec72053772603c23ae)
17c1cfe7eSKonstantin Aladyshev<template>
27c1cfe7eSKonstantin Aladyshev  <b-container fluid="xl">
37c1cfe7eSKonstantin Aladyshev    <page-title :description="$t('pageSnmpAlerts.pageDescription')" />
47c1cfe7eSKonstantin Aladyshev    <b-row>
57c1cfe7eSKonstantin Aladyshev      <b-col xl="9" class="text-right">
67c1cfe7eSKonstantin Aladyshev        <b-button variant="primary" @click="initModalAddDestination">
77c1cfe7eSKonstantin Aladyshev          <icon-add />
87c1cfe7eSKonstantin Aladyshev          {{ $t('pageSnmpAlerts.addDestination') }}
97c1cfe7eSKonstantin Aladyshev        </b-button>
107c1cfe7eSKonstantin Aladyshev      </b-col>
117c1cfe7eSKonstantin Aladyshev    </b-row>
127c1cfe7eSKonstantin Aladyshev    <b-row>
137c1cfe7eSKonstantin Aladyshev      <b-col xl="9">
147c1cfe7eSKonstantin Aladyshev        <table-toolbar
157c1cfe7eSKonstantin Aladyshev          ref="toolbar"
167c1cfe7eSKonstantin Aladyshev          :selected-items-count="selectedRows.length"
177c1cfe7eSKonstantin Aladyshev          :actions="tableToolbarActions"
187c1cfe7eSKonstantin Aladyshev          @clear-selected="clearSelectedRows($refs.table)"
197c1cfe7eSKonstantin Aladyshev          @batch-action="onBatchAction"
207c1cfe7eSKonstantin Aladyshev        />
217c1cfe7eSKonstantin Aladyshev        <b-table
227c1cfe7eSKonstantin Aladyshev          ref="table"
237c1cfe7eSKonstantin Aladyshev          responsive="md"
247c1cfe7eSKonstantin Aladyshev          selectable
257c1cfe7eSKonstantin Aladyshev          show-empty
267c1cfe7eSKonstantin Aladyshev          no-select-on-click
277c1cfe7eSKonstantin Aladyshev          hover
287c1cfe7eSKonstantin Aladyshev          :fields="fields"
297c1cfe7eSKonstantin Aladyshev          :items="tableItems"
307c1cfe7eSKonstantin Aladyshev          :empty-text="$t('global.table.emptyMessage')"
317c1cfe7eSKonstantin Aladyshev          @row-selected="onRowSelected($event, tableItems.length)"
327c1cfe7eSKonstantin Aladyshev        >
337c1cfe7eSKonstantin Aladyshev          <!-- Checkbox column -->
347c1cfe7eSKonstantin Aladyshev          <template #head(checkbox)>
357c1cfe7eSKonstantin Aladyshev            <b-form-checkbox
367c1cfe7eSKonstantin Aladyshev              v-model="tableHeaderCheckboxModel"
377c1cfe7eSKonstantin Aladyshev              data-test-id="snmpAlerts-checkbox-selectAll"
387c1cfe7eSKonstantin Aladyshev              :indeterminate="tableHeaderCheckboxIndeterminate"
397c1cfe7eSKonstantin Aladyshev              @change="onChangeHeaderCheckbox($refs.table)"
407c1cfe7eSKonstantin Aladyshev            >
417c1cfe7eSKonstantin Aladyshev              <span class="sr-only">{{ $t('global.table.selectAll') }}</span>
427c1cfe7eSKonstantin Aladyshev            </b-form-checkbox>
437c1cfe7eSKonstantin Aladyshev          </template>
447c1cfe7eSKonstantin Aladyshev          <template #cell(checkbox)="row">
457c1cfe7eSKonstantin Aladyshev            <b-form-checkbox
467c1cfe7eSKonstantin Aladyshev              v-model="row.rowSelected"
477c1cfe7eSKonstantin Aladyshev              :data-test-id="`snmpAlerts-checkbox-selectRow-${row.index}`"
487c1cfe7eSKonstantin Aladyshev              @change="toggleSelectRow($refs.table, row.index)"
497c1cfe7eSKonstantin Aladyshev            >
507c1cfe7eSKonstantin Aladyshev              <span class="sr-only">{{ $t('global.table.selectItem') }}</span>
517c1cfe7eSKonstantin Aladyshev            </b-form-checkbox>
527c1cfe7eSKonstantin Aladyshev          </template>
537c1cfe7eSKonstantin Aladyshev
547c1cfe7eSKonstantin Aladyshev          <!-- table actions column -->
557c1cfe7eSKonstantin Aladyshev          <template #cell(actions)="{ item }">
567c1cfe7eSKonstantin Aladyshev            <table-row-action
577c1cfe7eSKonstantin Aladyshev              v-for="(action, index) in item.actions"
587c1cfe7eSKonstantin Aladyshev              :key="index"
597c1cfe7eSKonstantin Aladyshev              :value="action.value"
607c1cfe7eSKonstantin Aladyshev              :enabled="action.enabled"
617c1cfe7eSKonstantin Aladyshev              :title="action.title"
627c1cfe7eSKonstantin Aladyshev              :data-test-id="`snmpAlerts-button-deleteRow-${item.index}`"
637c1cfe7eSKonstantin Aladyshev              @click-table-action="onTableRowAction($event, item)"
647c1cfe7eSKonstantin Aladyshev            >
657c1cfe7eSKonstantin Aladyshev              <template #icon>
667c1cfe7eSKonstantin Aladyshev                <icon-trashcan v-if="action.value === 'delete'" />
677c1cfe7eSKonstantin Aladyshev              </template>
687c1cfe7eSKonstantin Aladyshev            </table-row-action>
697c1cfe7eSKonstantin Aladyshev          </template>
707c1cfe7eSKonstantin Aladyshev        </b-table>
717c1cfe7eSKonstantin Aladyshev      </b-col>
727c1cfe7eSKonstantin Aladyshev    </b-row>
737c1cfe7eSKonstantin Aladyshev    <!-- Modals -->
747c1cfe7eSKonstantin Aladyshev    <modal-add-destination @ok="onModalOk" />
757c1cfe7eSKonstantin Aladyshev  </b-container>
767c1cfe7eSKonstantin Aladyshev</template>
777c1cfe7eSKonstantin Aladyshev
787c1cfe7eSKonstantin Aladyshev<script>
797c1cfe7eSKonstantin Aladyshevimport IconTrashcan from '@carbon/icons-vue/es/trash-can/20';
807c1cfe7eSKonstantin Aladyshevimport ModalAddDestination from './ModalAddDestination';
817c1cfe7eSKonstantin Aladyshevimport PageTitle from '@/components/Global/PageTitle';
827c1cfe7eSKonstantin Aladyshevimport IconAdd from '@carbon/icons-vue/es/add--alt/20';
837c1cfe7eSKonstantin Aladyshevimport TableToolbar from '@/components/Global/TableToolbar';
847c1cfe7eSKonstantin Aladyshevimport TableRowAction from '@/components/Global/TableRowAction';
857c1cfe7eSKonstantin Aladyshevimport LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
867c1cfe7eSKonstantin Aladyshevimport BVToastMixin from '@/components/Mixins/BVToastMixin';
877c1cfe7eSKonstantin Aladyshev
887c1cfe7eSKonstantin Aladyshevimport BVTableSelectableMixin, {
897c1cfe7eSKonstantin Aladyshev  selectedRows,
907c1cfe7eSKonstantin Aladyshev  tableHeaderCheckboxModel,
917c1cfe7eSKonstantin Aladyshev  tableHeaderCheckboxIndeterminate,
927c1cfe7eSKonstantin Aladyshev} from '@/components/Mixins/BVTableSelectableMixin';
93*de23ea23SSurya Vimport { useI18n } from 'vue-i18n';
94*de23ea23SSurya Vimport i18n from '@/i18n';
95*de23ea23SSurya V
967c1cfe7eSKonstantin Aladyshevexport default {
977c1cfe7eSKonstantin Aladyshev  name: 'SnmpAlerts',
987c1cfe7eSKonstantin Aladyshev  components: {
997c1cfe7eSKonstantin Aladyshev    PageTitle,
1007c1cfe7eSKonstantin Aladyshev    IconAdd,
1017c1cfe7eSKonstantin Aladyshev    TableToolbar,
1027c1cfe7eSKonstantin Aladyshev    IconTrashcan,
1037c1cfe7eSKonstantin Aladyshev    ModalAddDestination,
1047c1cfe7eSKonstantin Aladyshev    TableRowAction,
1057c1cfe7eSKonstantin Aladyshev  },
1067c1cfe7eSKonstantin Aladyshev  mixins: [BVTableSelectableMixin, BVToastMixin, LoadingBarMixin],
1077c1cfe7eSKonstantin Aladyshev  beforeRouteLeave(to, from, next) {
1087c1cfe7eSKonstantin Aladyshev    this.hideLoader();
1097c1cfe7eSKonstantin Aladyshev    next();
1107c1cfe7eSKonstantin Aladyshev  },
1117c1cfe7eSKonstantin Aladyshev  data() {
1127c1cfe7eSKonstantin Aladyshev    return {
113*de23ea23SSurya V      $t: useI18n().t,
1147c1cfe7eSKonstantin Aladyshev      fields: [
1157c1cfe7eSKonstantin Aladyshev        {
1167c1cfe7eSKonstantin Aladyshev          key: 'checkbox',
1177c1cfe7eSKonstantin Aladyshev        },
1187c1cfe7eSKonstantin Aladyshev        {
1197c1cfe7eSKonstantin Aladyshev          key: 'IP',
120*de23ea23SSurya V          label: i18n.global.t('pageSnmpAlerts.table.ipaddress'),
1217c1cfe7eSKonstantin Aladyshev        },
1227c1cfe7eSKonstantin Aladyshev        {
1237c1cfe7eSKonstantin Aladyshev          key: 'Port',
124*de23ea23SSurya V          label: i18n.global.t('pageSnmpAlerts.table.port'),
1257c1cfe7eSKonstantin Aladyshev        },
1267c1cfe7eSKonstantin Aladyshev        {
1277c1cfe7eSKonstantin Aladyshev          key: 'actions',
1287c1cfe7eSKonstantin Aladyshev          label: '',
1297c1cfe7eSKonstantin Aladyshev          tdClass: 'text-right text-nowrap',
1307c1cfe7eSKonstantin Aladyshev        },
1317c1cfe7eSKonstantin Aladyshev      ],
1327c1cfe7eSKonstantin Aladyshev      tableToolbarActions: [
1337c1cfe7eSKonstantin Aladyshev        {
1347c1cfe7eSKonstantin Aladyshev          value: 'delete',
135*de23ea23SSurya V          label: i18n.global.t('global.action.delete'),
1367c1cfe7eSKonstantin Aladyshev        },
1377c1cfe7eSKonstantin Aladyshev      ],
1387c1cfe7eSKonstantin Aladyshev      selectedRows: selectedRows,
1397c1cfe7eSKonstantin Aladyshev      tableHeaderCheckboxModel: tableHeaderCheckboxModel,
1407c1cfe7eSKonstantin Aladyshev      tableHeaderCheckboxIndeterminate: tableHeaderCheckboxIndeterminate,
1417c1cfe7eSKonstantin Aladyshev    };
1427c1cfe7eSKonstantin Aladyshev  },
1437c1cfe7eSKonstantin Aladyshev  computed: {
1447c1cfe7eSKonstantin Aladyshev    allSnmpDetails() {
1457c1cfe7eSKonstantin Aladyshev      return this.$store.getters['snmpAlerts/allSnmpDetails'];
1467c1cfe7eSKonstantin Aladyshev    },
1477c1cfe7eSKonstantin Aladyshev    tableItems() {
1487c1cfe7eSKonstantin Aladyshev      // transform destination data to table data
1497c1cfe7eSKonstantin Aladyshev      return this.allSnmpDetails.map((subscriptions) => {
1507c1cfe7eSKonstantin Aladyshev        const [destination, dataWithProtocol, dataWithoutProtocol] = [
1517c1cfe7eSKonstantin Aladyshev          subscriptions.Destination,
1527c1cfe7eSKonstantin Aladyshev          subscriptions.Destination.split('/')[2].split(':'),
1537c1cfe7eSKonstantin Aladyshev          subscriptions.Destination.split(':'),
1547c1cfe7eSKonstantin Aladyshev        ];
1557c1cfe7eSKonstantin Aladyshev        //condition to check if destination comes with protocol or not
1567c1cfe7eSKonstantin Aladyshev        const conditionForProtocolCheck = destination.includes('://');
1577c1cfe7eSKonstantin Aladyshev        const ip = conditionForProtocolCheck
1587c1cfe7eSKonstantin Aladyshev          ? dataWithProtocol[0]
1597c1cfe7eSKonstantin Aladyshev          : dataWithoutProtocol[0];
1607c1cfe7eSKonstantin Aladyshev        const port = conditionForProtocolCheck
1617c1cfe7eSKonstantin Aladyshev          ? dataWithProtocol[1]
1627c1cfe7eSKonstantin Aladyshev          : dataWithoutProtocol[1];
1637c1cfe7eSKonstantin Aladyshev        return {
1647c1cfe7eSKonstantin Aladyshev          IP: ip,
1657c1cfe7eSKonstantin Aladyshev          Port: port,
1667c1cfe7eSKonstantin Aladyshev          id: subscriptions.Id,
1677c1cfe7eSKonstantin Aladyshev          actions: [
1687c1cfe7eSKonstantin Aladyshev            {
1697c1cfe7eSKonstantin Aladyshev              value: 'delete',
1707c1cfe7eSKonstantin Aladyshev              enabled: true,
171*de23ea23SSurya V              title: i18n.global.t('pageSnmpAlerts.deleteDestination'),
1727c1cfe7eSKonstantin Aladyshev            },
1737c1cfe7eSKonstantin Aladyshev          ],
1747c1cfe7eSKonstantin Aladyshev          ...subscriptions,
1757c1cfe7eSKonstantin Aladyshev        };
1767c1cfe7eSKonstantin Aladyshev      });
1777c1cfe7eSKonstantin Aladyshev    },
1787c1cfe7eSKonstantin Aladyshev  },
1797c1cfe7eSKonstantin Aladyshev  created() {
1807c1cfe7eSKonstantin Aladyshev    this.startLoader();
1817c1cfe7eSKonstantin Aladyshev    this.$store
1827c1cfe7eSKonstantin Aladyshev      .dispatch('snmpAlerts/getSnmpDetails')
1837c1cfe7eSKonstantin Aladyshev      .finally(() => this.endLoader());
1847c1cfe7eSKonstantin Aladyshev  },
1857c1cfe7eSKonstantin Aladyshev  methods: {
1867c1cfe7eSKonstantin Aladyshev    onModalOk({ ipAddress, port }) {
1877c1cfe7eSKonstantin Aladyshev      const protocolIpAddress = 'snmp://' + ipAddress;
1887c1cfe7eSKonstantin Aladyshev      const destination = port
1897c1cfe7eSKonstantin Aladyshev        ? protocolIpAddress + ':' + port
1907c1cfe7eSKonstantin Aladyshev        : protocolIpAddress;
1917c1cfe7eSKonstantin Aladyshev      const data = {
1927c1cfe7eSKonstantin Aladyshev        Destination: destination,
1937c1cfe7eSKonstantin Aladyshev        SubscriptionType: 'SNMPTrap',
1947c1cfe7eSKonstantin Aladyshev        Protocol: 'SNMPv2c',
1957c1cfe7eSKonstantin Aladyshev      };
1967c1cfe7eSKonstantin Aladyshev      this.startLoader();
1977c1cfe7eSKonstantin Aladyshev      this.$store
1987c1cfe7eSKonstantin Aladyshev        .dispatch('snmpAlerts/addDestination', { data })
1997c1cfe7eSKonstantin Aladyshev        .then((success) => this.successToast(success))
2007c1cfe7eSKonstantin Aladyshev        .catch(({ message }) => this.errorToast(message))
2017c1cfe7eSKonstantin Aladyshev        .finally(() => this.endLoader());
2027c1cfe7eSKonstantin Aladyshev    },
2037c1cfe7eSKonstantin Aladyshev    initModalAddDestination() {
2047c1cfe7eSKonstantin Aladyshev      this.$bvModal.show('add-destination');
2057c1cfe7eSKonstantin Aladyshev    },
2067c1cfe7eSKonstantin Aladyshev    initModalDeleteDestination(destination) {
2077c1cfe7eSKonstantin Aladyshev      this.$bvModal
2087c1cfe7eSKonstantin Aladyshev        .msgBoxConfirm(
209*de23ea23SSurya V          i18n.global.t('pageSnmpAlerts.modal.deleteConfirmMessage', {
2107c1cfe7eSKonstantin Aladyshev            destination: destination.id,
2117c1cfe7eSKonstantin Aladyshev          }),
2127c1cfe7eSKonstantin Aladyshev          {
213*de23ea23SSurya V            title: i18n.global.t(
214*de23ea23SSurya V              'pageSnmpAlerts.modal.deleteSnmpDestinationTitle',
215*de23ea23SSurya V            ),
216*de23ea23SSurya V            okTitle: i18n.global.t('pageSnmpAlerts.deleteDestination'),
217*de23ea23SSurya V            cancelTitle: i18n.global.t('global.action.cancel'),
218d1ef18e6SPaul Fertser            autoFocusButton: 'ok',
2198132399cSEd Tanous          },
2207c1cfe7eSKonstantin Aladyshev        )
2217c1cfe7eSKonstantin Aladyshev        .then((deleteConfirmed) => {
2227c1cfe7eSKonstantin Aladyshev          if (deleteConfirmed) {
2237c1cfe7eSKonstantin Aladyshev            this.deleteDestination(destination);
2247c1cfe7eSKonstantin Aladyshev          }
2257c1cfe7eSKonstantin Aladyshev        });
2267c1cfe7eSKonstantin Aladyshev    },
2277c1cfe7eSKonstantin Aladyshev    deleteDestination({ id }) {
2287c1cfe7eSKonstantin Aladyshev      this.startLoader();
2297c1cfe7eSKonstantin Aladyshev      this.$store
2307c1cfe7eSKonstantin Aladyshev        .dispatch('snmpAlerts/deleteDestination', id)
2317c1cfe7eSKonstantin Aladyshev        .then((success) => this.successToast(success))
2327c1cfe7eSKonstantin Aladyshev        .catch(({ message }) => this.errorToast(message))
2337c1cfe7eSKonstantin Aladyshev        .finally(() => this.endLoader());
2347c1cfe7eSKonstantin Aladyshev    },
2357c1cfe7eSKonstantin Aladyshev    onBatchAction(action) {
2367c1cfe7eSKonstantin Aladyshev      if (action === 'delete') {
2377c1cfe7eSKonstantin Aladyshev        this.$bvModal
2387c1cfe7eSKonstantin Aladyshev          .msgBoxConfirm(
239*de23ea23SSurya V            i18n.global.t(
2407c1cfe7eSKonstantin Aladyshev              'pageSnmpAlerts.modal.batchDeleteConfirmMessage',
2418132399cSEd Tanous              this.selectedRows.length,
2427c1cfe7eSKonstantin Aladyshev            ),
2437c1cfe7eSKonstantin Aladyshev            {
244*de23ea23SSurya V              title: i18n.global.t(
2457c1cfe7eSKonstantin Aladyshev                'pageSnmpAlerts.modal.deleteSnmpDestinationTitle',
2468132399cSEd Tanous                this.selectedRows.length,
2477c1cfe7eSKonstantin Aladyshev              ),
248*de23ea23SSurya V              okTitle: i18n.global.t(
2497c1cfe7eSKonstantin Aladyshev                'pageSnmpAlerts.deleteDestination',
2508132399cSEd Tanous                this.selectedRows.length,
2517c1cfe7eSKonstantin Aladyshev              ),
252*de23ea23SSurya V              cancelTitle: i18n.global.t('global.action.cancel'),
253d1ef18e6SPaul Fertser              autoFocusButton: 'ok',
2548132399cSEd Tanous            },
2557c1cfe7eSKonstantin Aladyshev          )
2567c1cfe7eSKonstantin Aladyshev          .then((deleteConfirmed) => {
2577c1cfe7eSKonstantin Aladyshev            if (deleteConfirmed) {
2587c1cfe7eSKonstantin Aladyshev              this.startLoader();
2597c1cfe7eSKonstantin Aladyshev              this.$store
2607c1cfe7eSKonstantin Aladyshev                .dispatch(
2617c1cfe7eSKonstantin Aladyshev                  'snmpAlerts/deleteMultipleDestinations',
2628132399cSEd Tanous                  this.selectedRows,
2637c1cfe7eSKonstantin Aladyshev                )
2647c1cfe7eSKonstantin Aladyshev                .then((messages) => {
2657c1cfe7eSKonstantin Aladyshev                  messages.forEach(({ type, message }) => {
2667c1cfe7eSKonstantin Aladyshev                    if (type === 'success') this.successToast(message);
2677c1cfe7eSKonstantin Aladyshev                    if (type === 'error') this.errorToast(message);
2687c1cfe7eSKonstantin Aladyshev                  });
2697c1cfe7eSKonstantin Aladyshev                })
2707c1cfe7eSKonstantin Aladyshev                .finally(() => this.endLoader());
2717c1cfe7eSKonstantin Aladyshev            }
2727c1cfe7eSKonstantin Aladyshev          });
2737c1cfe7eSKonstantin Aladyshev      }
2747c1cfe7eSKonstantin Aladyshev    },
2757c1cfe7eSKonstantin Aladyshev    onTableRowAction(action, row) {
2767c1cfe7eSKonstantin Aladyshev      if (action === 'delete') {
2777c1cfe7eSKonstantin Aladyshev        this.initModalDeleteDestination(row);
2787c1cfe7eSKonstantin Aladyshev      }
2797c1cfe7eSKonstantin Aladyshev    },
2807c1cfe7eSKonstantin Aladyshev  },
2817c1cfe7eSKonstantin Aladyshev};
2827c1cfe7eSKonstantin Aladyshev</script>
283