blob: 994990c2aab9d44e8d1512f55348a80a13f41426 [file] [log] [blame]
Dixsie Wolmersc4b87572021-10-07 16:15:50 -05001<template>
2 <page-section :section-title="$t('pageNetwork.ipv4')">
Nikhil Ashokae8cb2c62023-01-04 15:45:20 +05303 <b-row class="mb-4">
4 <b-col lg="2" md="6">
5 <dl>
6 <dt>{{ $t('pageNetwork.dhcp') }}</dt>
7 <dd>
8 <b-form-checkbox
9 id="dhcpSwitch"
10 v-model="dhcpEnabledState"
11 data-test-id="networkSettings-switch-dhcpEnabled"
12 switch
13 @change="changeDhcpEnabledState"
14 >
15 <span v-if="dhcpEnabledState">
16 {{ $t('global.status.enabled') }}
17 </span>
18 <span v-else>{{ $t('global.status.disabled') }}</span>
19 </b-form-checkbox>
20 </dd>
21 </dl>
22 </b-col>
23 </b-row>
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050024 <b-row>
25 <b-col>
26 <h3 class="h5">
27 {{ $t('pageNetwork.ipv4Addresses') }}
28 </h3>
29 </b-col>
jason westoverd36ac8a2025-11-03 20:58:59 -060030 <b-col class="text-end">
Dixsie Wolmersb34349d2021-11-02 22:06:35 -050031 <b-button variant="primary" @click="initAddIpv4Address()">
32 <icon-add />
33 {{ $t('pageNetwork.table.addIpv4Address') }}
34 </b-button>
35 </b-col>
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050036 </b-row>
37 <b-table
38 responsive="md"
39 hover
jason westoverd36ac8a2025-11-03 20:58:59 -060040 thead-class="table-light"
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050041 :fields="ipv4TableFields"
42 :items="form.ipv4TableItems"
43 :empty-text="$t('global.table.emptyMessage')"
44 class="mb-0"
45 show-empty
46 >
Dixsie Wolmersb34349d2021-11-02 22:06:35 -050047 <template #cell(actions)="{ item, index }">
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050048 <table-row-action
Sivaprabu Ganesan20983592023-09-25 18:43:10 +053049 v-for="(action, actionIndex) in filteredActions(item)"
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050050 :key="actionIndex"
51 :value="action.value"
52 :title="action.title"
53 :enabled="action.enabled"
54 @click-table-action="onIpv4TableAction(action, $event, index)"
55 >
56 <template #icon>
57 <icon-edit v-if="action.value === 'edit'" />
58 <icon-trashcan v-if="action.value === 'delete'" />
59 </template>
60 </table-row-action>
61 </template>
62 </b-table>
jason westoverd36ac8a2025-11-03 20:58:59 -060063 <modal-ipv4 v-model="showAddIpv4" />
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050064 </page-section>
65</template>
66
67<script>
68import BVToastMixin from '@/components/Mixins/BVToastMixin';
Dixsie Wolmersb34349d2021-11-02 22:06:35 -050069import IconAdd from '@carbon/icons-vue/es/add--alt/20';
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050070import IconEdit from '@carbon/icons-vue/es/edit/20';
71import IconTrashcan from '@carbon/icons-vue/es/trash-can/20';
Dixsie Wolmersb34349d2021-11-02 22:06:35 -050072import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050073import PageSection from '@/components/Global/PageSection';
74import TableRowAction from '@/components/Global/TableRowAction';
jason westoverd36ac8a2025-11-03 20:58:59 -060075import ModalIpv4 from './ModalIpv4.vue';
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050076import { mapState } from 'vuex';
Surya Vde23ea22024-07-11 15:19:46 +053077import { useI18n } from 'vue-i18n';
78import i18n from '@/i18n';
jason westoverd36ac8a2025-11-03 20:58:59 -060079import { useModal } from 'bootstrap-vue-next';
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050080
81export default {
82 name: 'Ipv4Table',
83 components: {
Dixsie Wolmersb34349d2021-11-02 22:06:35 -050084 IconAdd,
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050085 IconEdit,
86 IconTrashcan,
87 PageSection,
88 TableRowAction,
jason westoverd36ac8a2025-11-03 20:58:59 -060089 ModalIpv4,
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050090 },
Dixsie Wolmersb34349d2021-11-02 22:06:35 -050091 mixins: [BVToastMixin, LoadingBarMixin],
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050092 props: {
93 tabIndex: {
94 type: Number,
95 default: 0,
96 },
97 },
jason westoverd36ac8a2025-11-03 20:58:59 -060098 setup() {
99 const bvModal = useModal();
100 return { bvModal };
101 },
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500102 data() {
103 return {
Surya Vde23ea22024-07-11 15:19:46 +0530104 $t: useI18n().t,
jason westoverd36ac8a2025-11-03 20:58:59 -0600105 showAddIpv4: false,
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500106 form: {
107 ipv4TableItems: [],
108 },
109 actions: [
110 {
111 value: 'edit',
Surya Vde23ea22024-07-11 15:19:46 +0530112 title: i18n.global.t('global.action.edit'),
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500113 },
114 {
115 value: 'delete',
Surya Vde23ea22024-07-11 15:19:46 +0530116 title: i18n.global.t('global.action.delete'),
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500117 },
118 ],
119 ipv4TableFields: [
120 {
121 key: 'Address',
Surya Vde23ea22024-07-11 15:19:46 +0530122 label: i18n.global.t('pageNetwork.table.ipAddress'),
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500123 },
124 {
125 key: 'Gateway',
Surya Vde23ea22024-07-11 15:19:46 +0530126 label: i18n.global.t('pageNetwork.table.gateway'),
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500127 },
128 {
129 key: 'SubnetMask',
Surya Vde23ea22024-07-11 15:19:46 +0530130 label: i18n.global.t('pageNetwork.table.subnet'),
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500131 },
132 {
133 key: 'AddressOrigin',
Surya Vde23ea22024-07-11 15:19:46 +0530134 label: i18n.global.t('pageNetwork.table.addressOrigin'),
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500135 },
jason westoverd36ac8a2025-11-03 20:58:59 -0600136 { key: 'actions', label: '', tdClass: 'text-end' },
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500137 ],
138 };
139 },
140 computed: {
141 ...mapState('network', ['ethernetData']),
Nikhil Ashokae8cb2c62023-01-04 15:45:20 +0530142 selectedInterface() {
143 return this.$store.getters['network/selectedInterfaceIndex'];
144 },
145 dhcpEnabledState: {
146 get() {
147 return this.$store.getters['network/globalNetworkSettings'][
148 this.selectedInterface
149 ].dhcpEnabled;
150 },
151 set(newValue) {
152 return newValue;
153 },
154 },
Sivaprabu Ganesan20983592023-09-25 18:43:10 +0530155 filteredActions() {
156 return (item) => {
157 if (item.AddressOrigin === 'DHCP') {
158 return item.actions.filter((action) => action.value !== 'delete');
159 } else {
160 return item.actions;
161 }
162 };
163 },
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500164 },
165 watch: {
166 // Watch for change in tab index
167 tabIndex() {
168 this.getIpv4TableItems();
169 },
Dixsie Wolmersb34349d2021-11-02 22:06:35 -0500170 ethernetData() {
171 this.getIpv4TableItems();
172 },
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500173 },
174 created() {
175 this.getIpv4TableItems();
176 this.$store.dispatch('network/getEthernetData').finally(() => {
177 // Emit initial data fetch complete to parent component
jason westoverd36ac8a2025-11-03 20:58:59 -0600178 require('@/eventBus').default.$emit('network-table-ipv4-complete');
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500179 });
180 },
181 methods: {
182 getIpv4TableItems() {
183 const index = this.tabIndex;
184 const addresses = this.ethernetData[index].IPv4Addresses || [];
185 this.form.ipv4TableItems = addresses.map((ipv4) => {
186 return {
187 Address: ipv4.Address,
188 SubnetMask: ipv4.SubnetMask,
189 Gateway: ipv4.Gateway,
190 AddressOrigin: ipv4.AddressOrigin,
191 actions: [
192 {
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500193 value: 'delete',
Surya Vde23ea22024-07-11 15:19:46 +0530194 title: i18n.global.t('pageNetwork.table.deleteIpv4'),
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500195 },
196 ],
197 };
198 });
199 },
Dixsie Wolmersb34349d2021-11-02 22:06:35 -0500200 onIpv4TableAction(action, $event, index) {
201 if ($event === 'delete') {
202 this.deleteIpv4TableRow(index);
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500203 }
204 },
Dixsie Wolmersb34349d2021-11-02 22:06:35 -0500205 deleteIpv4TableRow(index) {
206 this.form.ipv4TableItems.splice(index, 1);
207 const newIpv4Array = this.form.ipv4TableItems.map((ipv4) => {
208 const { Address, SubnetMask, Gateway } = ipv4;
209 return {
210 Address,
211 SubnetMask,
212 Gateway,
213 };
214 });
215 this.$store
216 .dispatch('network/editIpv4Address', newIpv4Array)
217 .then((message) => this.successToast(message))
218 .catch(({ message }) => this.errorToast(message));
219 },
220 initAddIpv4Address() {
jason westoverd36ac8a2025-11-03 20:58:59 -0600221 this.showAddIpv4 = true;
Dixsie Wolmersb34349d2021-11-02 22:06:35 -0500222 },
Nikhil Ashokae8cb2c62023-01-04 15:45:20 +0530223 changeDhcpEnabledState(state) {
jason westoverd36ac8a2025-11-03 20:58:59 -0600224 const dhcpState = state
225 ? i18n.global.t('global.action.enable')
226 : i18n.global.t('global.action.disable');
227 this.confirmDialog(
228 state
229 ? i18n.global.t('pageNetwork.modal.confirmEnableDhcp')
230 : i18n.global.t('pageNetwork.modal.confirmDisableDhcp'),
231 {
232 title: i18n.global.t('pageNetwork.modal.dhcpConfirmTitle', {
233 dhcpState,
234 }),
235 okTitle: dhcpState,
236 okVariant: 'danger',
237 cancelTitle: i18n.global.t('global.action.cancel'),
238 autoFocusButton: 'cancel',
239 },
240 ).then((dhcpEnableConfirmed) => {
241 if (dhcpEnableConfirmed) {
242 this.$store
243 .dispatch('network/saveDhcpEnabledState', state)
244 .then((message) => this.successToast(message))
245 .catch(({ message }) => this.errorToast(message));
246 } else {
247 let onDhcpCancel = document.getElementById('dhcpSwitch');
248 onDhcpCancel.checked = !state;
249 }
250 });
251 },
252 confirmDialog(message, options = {}) {
253 return this.$confirm({ message, ...options });
Nikhil Ashokae8cb2c62023-01-04 15:45:20 +0530254 },
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500255 },
256};
257</script>