xref: /openbmc/webui-vue/src/store/modules/Settings/SnmpAlertsStore.js (revision de23ea23d88451a2fa2774ec72053772603c23ae)
17c1cfe7eSKonstantin Aladyshevimport api, { getResponseCount } from '@/store/api';
27c1cfe7eSKonstantin Aladyshevimport i18n from '@/i18n';
37c1cfe7eSKonstantin Aladyshev
47c1cfe7eSKonstantin Aladyshevconst SnmpAlertsStore = {
57c1cfe7eSKonstantin Aladyshev  namespaced: true,
67c1cfe7eSKonstantin Aladyshev  state: {
77c1cfe7eSKonstantin Aladyshev    allSnmpDetails: [],
87c1cfe7eSKonstantin Aladyshev  },
97c1cfe7eSKonstantin Aladyshev  getters: {
107c1cfe7eSKonstantin Aladyshev    allSnmpDetails(state) {
117c1cfe7eSKonstantin Aladyshev      return state.allSnmpDetails;
127c1cfe7eSKonstantin Aladyshev    },
137c1cfe7eSKonstantin Aladyshev  },
147c1cfe7eSKonstantin Aladyshev  mutations: {
157c1cfe7eSKonstantin Aladyshev    setSnmpDetails(state, allSnmpDetails) {
167c1cfe7eSKonstantin Aladyshev      state.allSnmpDetails = allSnmpDetails;
177c1cfe7eSKonstantin Aladyshev    },
187c1cfe7eSKonstantin Aladyshev  },
197c1cfe7eSKonstantin Aladyshev  actions: {
207c1cfe7eSKonstantin Aladyshev    async getSnmpAlertUrl() {
217c1cfe7eSKonstantin Aladyshev      return await api
227c1cfe7eSKonstantin Aladyshev        .get('/redfish/v1/')
237c1cfe7eSKonstantin Aladyshev        .then((response) => api.get(response.data.EventService['@odata.id']))
247c1cfe7eSKonstantin Aladyshev        .then((response) => api.get(response.data.Subscriptions['@odata.id']))
257c1cfe7eSKonstantin Aladyshev        .then((response) => response.data['@odata.id'])
267c1cfe7eSKonstantin Aladyshev        .catch((error) => console.log('Error', error));
277c1cfe7eSKonstantin Aladyshev    },
287c1cfe7eSKonstantin Aladyshev    async getSnmpDetails({ commit, dispatch }) {
297c1cfe7eSKonstantin Aladyshev      const snmpAlertUrl = await dispatch('getSnmpAlertUrl');
307c1cfe7eSKonstantin Aladyshev      return await api
317c1cfe7eSKonstantin Aladyshev        .get(snmpAlertUrl)
327c1cfe7eSKonstantin Aladyshev        .then((response) =>
338132399cSEd Tanous          response.data.Members.map((user) => user['@odata.id']),
347c1cfe7eSKonstantin Aladyshev        )
357c1cfe7eSKonstantin Aladyshev        .then((userIds) => api.all(userIds.map((user) => api.get(user))))
367c1cfe7eSKonstantin Aladyshev        .then((users) => {
377c1cfe7eSKonstantin Aladyshev          const snmpDetailsData = users.map((user) => user.data);
387c1cfe7eSKonstantin Aladyshev          commit('setSnmpDetails', snmpDetailsData);
397c1cfe7eSKonstantin Aladyshev        })
407c1cfe7eSKonstantin Aladyshev        .catch((error) => {
417c1cfe7eSKonstantin Aladyshev          console.log(error);
42*de23ea23SSurya V          const message = i18n.global.t(
43*de23ea23SSurya V            'pageSnmpAlerts.toast.errorLoadSnmpDetails',
44*de23ea23SSurya V          );
457c1cfe7eSKonstantin Aladyshev          throw new Error(message);
467c1cfe7eSKonstantin Aladyshev        });
477c1cfe7eSKonstantin Aladyshev    },
487c1cfe7eSKonstantin Aladyshev    async deleteDestination({ dispatch }, id) {
497c1cfe7eSKonstantin Aladyshev      const snmpAlertUrl = await dispatch('getSnmpAlertUrl');
507c1cfe7eSKonstantin Aladyshev      return await api
517c1cfe7eSKonstantin Aladyshev        .delete(`${snmpAlertUrl}/${id}`)
527c1cfe7eSKonstantin Aladyshev        .then(() => dispatch('getSnmpDetails'))
537c1cfe7eSKonstantin Aladyshev        .then(() =>
54*de23ea23SSurya V          i18n.global.t('pageSnmpAlerts.toast.successDeleteDestination', {
557c1cfe7eSKonstantin Aladyshev            id,
568132399cSEd Tanous          }),
577c1cfe7eSKonstantin Aladyshev        )
587c1cfe7eSKonstantin Aladyshev        .catch((error) => {
597c1cfe7eSKonstantin Aladyshev          console.log(error);
60*de23ea23SSurya V          const message = i18n.global.t(
617c1cfe7eSKonstantin Aladyshev            'pageSnmpAlerts.toast.errorDeleteDestination',
627c1cfe7eSKonstantin Aladyshev            {
637c1cfe7eSKonstantin Aladyshev              id,
648132399cSEd Tanous            },
657c1cfe7eSKonstantin Aladyshev          );
667c1cfe7eSKonstantin Aladyshev          throw new Error(message);
677c1cfe7eSKonstantin Aladyshev        });
687c1cfe7eSKonstantin Aladyshev    },
697c1cfe7eSKonstantin Aladyshev    async deleteMultipleDestinations({ dispatch }, destination) {
707c1cfe7eSKonstantin Aladyshev      const snmpAlertUrl = await dispatch('getSnmpAlertUrl');
717c1cfe7eSKonstantin Aladyshev      const promises = destination.map(({ id }) => {
727c1cfe7eSKonstantin Aladyshev        return api.delete(`${snmpAlertUrl}/${id}`).catch((error) => {
737c1cfe7eSKonstantin Aladyshev          console.log(error);
747c1cfe7eSKonstantin Aladyshev          return error;
757c1cfe7eSKonstantin Aladyshev        });
767c1cfe7eSKonstantin Aladyshev      });
777c1cfe7eSKonstantin Aladyshev      return await api
787c1cfe7eSKonstantin Aladyshev        .all(promises)
797c1cfe7eSKonstantin Aladyshev        .then((response) => {
807c1cfe7eSKonstantin Aladyshev          dispatch('getSnmpDetails');
817c1cfe7eSKonstantin Aladyshev          return response;
827c1cfe7eSKonstantin Aladyshev        })
837c1cfe7eSKonstantin Aladyshev        .then(
847c1cfe7eSKonstantin Aladyshev          api.spread((...responses) => {
857c1cfe7eSKonstantin Aladyshev            const { successCount, errorCount } = getResponseCount(responses);
867c1cfe7eSKonstantin Aladyshev            let toastMessages = [];
877c1cfe7eSKonstantin Aladyshev
887c1cfe7eSKonstantin Aladyshev            if (successCount) {
89*de23ea23SSurya V              const message = i18n.global.t(
907c1cfe7eSKonstantin Aladyshev                'pageSnmpAlerts.toast.successBatchDelete',
918132399cSEd Tanous                successCount,
927c1cfe7eSKonstantin Aladyshev              );
937c1cfe7eSKonstantin Aladyshev              toastMessages.push({ type: 'success', message });
947c1cfe7eSKonstantin Aladyshev            }
957c1cfe7eSKonstantin Aladyshev
967c1cfe7eSKonstantin Aladyshev            if (errorCount) {
97*de23ea23SSurya V              const message = i18n.global.t(
987c1cfe7eSKonstantin Aladyshev                'pageSnmpAlerts.toast.errorBatchDelete',
998132399cSEd Tanous                errorCount,
1007c1cfe7eSKonstantin Aladyshev              );
1017c1cfe7eSKonstantin Aladyshev              toastMessages.push({ type: 'error', message });
1027c1cfe7eSKonstantin Aladyshev            }
1037c1cfe7eSKonstantin Aladyshev
1047c1cfe7eSKonstantin Aladyshev            return toastMessages;
1058132399cSEd Tanous          }),
1067c1cfe7eSKonstantin Aladyshev        );
1077c1cfe7eSKonstantin Aladyshev    },
1087c1cfe7eSKonstantin Aladyshev    async addDestination({ dispatch }, { data }) {
1097c1cfe7eSKonstantin Aladyshev      const snmpAlertUrl = await dispatch('getSnmpAlertUrl');
1107c1cfe7eSKonstantin Aladyshev      return await api
1117c1cfe7eSKonstantin Aladyshev        .post(snmpAlertUrl, data)
1127c1cfe7eSKonstantin Aladyshev        .then(() => dispatch('getSnmpDetails'))
113*de23ea23SSurya V        .then(() => i18n.global.t('pageSnmpAlerts.toast.successAddDestination'))
1147c1cfe7eSKonstantin Aladyshev        .catch((error) => {
1157c1cfe7eSKonstantin Aladyshev          console.log(error);
116*de23ea23SSurya V          const message = i18n.global.t(
117*de23ea23SSurya V            'pageSnmpAlerts.toast.errorAddDestination',
118*de23ea23SSurya V          );
1197c1cfe7eSKonstantin Aladyshev          throw new Error(message);
1207c1cfe7eSKonstantin Aladyshev        });
1217c1cfe7eSKonstantin Aladyshev    },
1227c1cfe7eSKonstantin Aladyshev  },
1237c1cfe7eSKonstantin Aladyshev};
1247c1cfe7eSKonstantin Aladyshev
1257c1cfe7eSKonstantin Aladyshevexport default SnmpAlertsStore;
126