blob: b95e7d397b934beee6221690dcd31e8f252755f2 [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>
Dixsie Wolmersb34349d2021-11-02 22:06:35 -050030 <b-col class="text-right">
31 <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
40 :fields="ipv4TableFields"
41 :items="form.ipv4TableItems"
42 :empty-text="$t('global.table.emptyMessage')"
43 class="mb-0"
44 show-empty
45 >
Dixsie Wolmersb34349d2021-11-02 22:06:35 -050046 <template #cell(actions)="{ item, index }">
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050047 <table-row-action
Sivaprabu Ganesan20983592023-09-25 18:43:10 +053048 v-for="(action, actionIndex) in filteredActions(item)"
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050049 :key="actionIndex"
50 :value="action.value"
51 :title="action.title"
52 :enabled="action.enabled"
53 @click-table-action="onIpv4TableAction(action, $event, index)"
54 >
55 <template #icon>
56 <icon-edit v-if="action.value === 'edit'" />
57 <icon-trashcan v-if="action.value === 'delete'" />
58 </template>
59 </table-row-action>
60 </template>
61 </b-table>
62 </page-section>
63</template>
64
65<script>
66import BVToastMixin from '@/components/Mixins/BVToastMixin';
Dixsie Wolmersb34349d2021-11-02 22:06:35 -050067import IconAdd from '@carbon/icons-vue/es/add--alt/20';
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050068import IconEdit from '@carbon/icons-vue/es/edit/20';
69import IconTrashcan from '@carbon/icons-vue/es/trash-can/20';
Dixsie Wolmersb34349d2021-11-02 22:06:35 -050070import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050071import PageSection from '@/components/Global/PageSection';
72import TableRowAction from '@/components/Global/TableRowAction';
73import { mapState } from 'vuex';
Surya Vde23ea22024-07-11 15:19:46 +053074import { useI18n } from 'vue-i18n';
75import i18n from '@/i18n';
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050076
77export default {
78 name: 'Ipv4Table',
79 components: {
Dixsie Wolmersb34349d2021-11-02 22:06:35 -050080 IconAdd,
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050081 IconEdit,
82 IconTrashcan,
83 PageSection,
84 TableRowAction,
85 },
Dixsie Wolmersb34349d2021-11-02 22:06:35 -050086 mixins: [BVToastMixin, LoadingBarMixin],
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050087 props: {
88 tabIndex: {
89 type: Number,
90 default: 0,
91 },
92 },
93 data() {
94 return {
Surya Vde23ea22024-07-11 15:19:46 +053095 $t: useI18n().t,
Dixsie Wolmersc4b87572021-10-07 16:15:50 -050096 form: {
97 ipv4TableItems: [],
98 },
99 actions: [
100 {
101 value: 'edit',
Surya Vde23ea22024-07-11 15:19:46 +0530102 title: i18n.global.t('global.action.edit'),
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500103 },
104 {
105 value: 'delete',
Surya Vde23ea22024-07-11 15:19:46 +0530106 title: i18n.global.t('global.action.delete'),
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500107 },
108 ],
109 ipv4TableFields: [
110 {
111 key: 'Address',
Surya Vde23ea22024-07-11 15:19:46 +0530112 label: i18n.global.t('pageNetwork.table.ipAddress'),
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500113 },
114 {
115 key: 'Gateway',
Surya Vde23ea22024-07-11 15:19:46 +0530116 label: i18n.global.t('pageNetwork.table.gateway'),
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500117 },
118 {
119 key: 'SubnetMask',
Surya Vde23ea22024-07-11 15:19:46 +0530120 label: i18n.global.t('pageNetwork.table.subnet'),
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500121 },
122 {
123 key: 'AddressOrigin',
Surya Vde23ea22024-07-11 15:19:46 +0530124 label: i18n.global.t('pageNetwork.table.addressOrigin'),
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500125 },
126 { key: 'actions', label: '', tdClass: 'text-right' },
127 ],
128 };
129 },
130 computed: {
131 ...mapState('network', ['ethernetData']),
Nikhil Ashokae8cb2c62023-01-04 15:45:20 +0530132 selectedInterface() {
133 return this.$store.getters['network/selectedInterfaceIndex'];
134 },
135 dhcpEnabledState: {
136 get() {
137 return this.$store.getters['network/globalNetworkSettings'][
138 this.selectedInterface
139 ].dhcpEnabled;
140 },
141 set(newValue) {
142 return newValue;
143 },
144 },
Sivaprabu Ganesan20983592023-09-25 18:43:10 +0530145 filteredActions() {
146 return (item) => {
147 if (item.AddressOrigin === 'DHCP') {
148 return item.actions.filter((action) => action.value !== 'delete');
149 } else {
150 return item.actions;
151 }
152 };
153 },
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500154 },
155 watch: {
156 // Watch for change in tab index
157 tabIndex() {
158 this.getIpv4TableItems();
159 },
Dixsie Wolmersb34349d2021-11-02 22:06:35 -0500160 ethernetData() {
161 this.getIpv4TableItems();
162 },
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500163 },
164 created() {
165 this.getIpv4TableItems();
166 this.$store.dispatch('network/getEthernetData').finally(() => {
167 // Emit initial data fetch complete to parent component
168 this.$root.$emit('network-table-ipv4-complete');
169 });
170 },
171 methods: {
172 getIpv4TableItems() {
173 const index = this.tabIndex;
174 const addresses = this.ethernetData[index].IPv4Addresses || [];
175 this.form.ipv4TableItems = addresses.map((ipv4) => {
176 return {
177 Address: ipv4.Address,
178 SubnetMask: ipv4.SubnetMask,
179 Gateway: ipv4.Gateway,
180 AddressOrigin: ipv4.AddressOrigin,
181 actions: [
182 {
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500183 value: 'delete',
Surya Vde23ea22024-07-11 15:19:46 +0530184 title: i18n.global.t('pageNetwork.table.deleteIpv4'),
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500185 },
186 ],
187 };
188 });
189 },
Dixsie Wolmersb34349d2021-11-02 22:06:35 -0500190 onIpv4TableAction(action, $event, index) {
191 if ($event === 'delete') {
192 this.deleteIpv4TableRow(index);
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500193 }
194 },
Dixsie Wolmersb34349d2021-11-02 22:06:35 -0500195 deleteIpv4TableRow(index) {
196 this.form.ipv4TableItems.splice(index, 1);
197 const newIpv4Array = this.form.ipv4TableItems.map((ipv4) => {
198 const { Address, SubnetMask, Gateway } = ipv4;
199 return {
200 Address,
201 SubnetMask,
202 Gateway,
203 };
204 });
205 this.$store
206 .dispatch('network/editIpv4Address', newIpv4Array)
207 .then((message) => this.successToast(message))
208 .catch(({ message }) => this.errorToast(message));
209 },
210 initAddIpv4Address() {
211 this.$bvModal.show('modal-add-ipv4');
212 },
Nikhil Ashokae8cb2c62023-01-04 15:45:20 +0530213 changeDhcpEnabledState(state) {
214 this.$bvModal
215 .msgBoxConfirm(
216 state
Surya Vde23ea22024-07-11 15:19:46 +0530217 ? i18n.global.t('pageNetwork.modal.confirmEnableDhcp')
218 : i18n.global.t('pageNetwork.modal.confirmDisableDhcp'),
Nikhil Ashokae8cb2c62023-01-04 15:45:20 +0530219 {
Surya Vde23ea22024-07-11 15:19:46 +0530220 title: i18n.global.t('pageNetwork.modal.dhcpConfirmTitle', {
Nikhil Ashokae8cb2c62023-01-04 15:45:20 +0530221 dhcpState: state
Surya Vde23ea22024-07-11 15:19:46 +0530222 ? i18n.global.t('global.action.enable')
223 : i18n.global.t('global.action.disable'),
Nikhil Ashokae8cb2c62023-01-04 15:45:20 +0530224 }),
225 okTitle: state
Surya Vde23ea22024-07-11 15:19:46 +0530226 ? i18n.global.t('global.action.enable')
227 : i18n.global.t('global.action.disable'),
Nikhil Ashokae8cb2c62023-01-04 15:45:20 +0530228 okVariant: 'danger',
Surya Vde23ea22024-07-11 15:19:46 +0530229 cancelTitle: i18n.global.t('global.action.cancel'),
Paul Fertserd1ef18e2024-04-06 08:04:14 +0000230 autoFocusButton: 'cancel',
Ed Tanous81323992024-02-27 11:26:24 -0800231 },
Nikhil Ashokae8cb2c62023-01-04 15:45:20 +0530232 )
233 .then((dhcpEnableConfirmed) => {
234 if (dhcpEnableConfirmed) {
235 this.$store
236 .dispatch('network/saveDhcpEnabledState', state)
237 .then((message) => this.successToast(message))
238 .catch(({ message }) => this.errorToast(message));
239 } else {
240 let onDhcpCancel = document.getElementById('dhcpSwitch');
241 onDhcpCancel.checked = !state;
242 }
243 });
244 },
Dixsie Wolmersc4b87572021-10-07 16:15:50 -0500245 },
246};
247</script>