/
InterfaceCard.vue
244 lines (227 loc) · 7.12 KB
/
InterfaceCard.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<template>
<v-expansion-panel
flat
>
<v-expansion-panel-header color="primary">
{{ adapter.name }}
<v-spacer />
{{ status_info }}
</v-expansion-panel-header>
<v-expansion-panel-content>
<v-container class="px-0">
<v-row
v-for="(address, key) in adapter.addresses"
:key="key"
height="50"
align="center"
>
<v-col cols="5">
{{ address.ip }}
</v-col>
<v-col cols="5">
{{ showable_mode_name(address.mode) }}
</v-col>
<v-col cols="2">
<v-btn
class="text-center elevation-1"
color="error"
icon
@click.native.stop="deleteAddress(address.ip)"
>
<v-icon>
mdi-delete
</v-icon>
</v-btn>
</v-col>
</v-row>
<v-row class="d-flex align-center justify-space-between mx-0 mt-2 pa-0">
<v-btn
small
class="ma-2 px-2 py-5 elevation-1"
@click.native.stop="openAddressCreationDialog"
>
Add <br> static IP
</v-btn>
<address-creation-dialog
v-model="show_creation_dialog"
:interface-name="adapter.name"
/>
<v-btn
small
class="ma-2 px-2 py-5 elevation-1"
@click="triggerForDynamicIP"
>
Ask for <br> dynamic IP
</v-btn>
<v-btn
v-if="is_there_dhcp_server_already"
small
class="ma-2 px-2 py-5 elevation-1"
@click="removeDHCPServer"
>
Disable <br> DHCP server
</v-btn>
<v-btn
v-else
small
class="ma-2 px-2 py-5 elevation-1"
:disabled="!is_static_ip_present"
@click="openDHCPServerDialog"
>
Enable <br> DHCP server
</v-btn>
</v-row>
</v-container>
</v-expansion-panel-content>
<dhcp-server-dialog
v-model="show_dhcp_server_dialog"
:adapter="adapter"
/>
</v-expansion-panel>
</template>
<script lang="ts">
import Vue, { PropType } from 'vue'
import Notifier from '@/libs/notifier'
import Popup from '@/libs/popup'
import beacon from '@/store/beacon'
import ethernet from '@/store/ethernet'
import { AddressMode, EthernetInterface } from '@/types/ethernet'
import { ethernet_service } from '@/types/frontend_services'
import back_axios from '@/utils/api'
import AddressCreationDialog from './AddressCreationDialog.vue'
import DHCPServerDialog from './DHCPServerDialog.vue'
const notifier = new Notifier(ethernet_service)
export default Vue.extend({
name: 'InterfaceCard',
components: {
AddressCreationDialog,
'dhcp-server-dialog': DHCPServerDialog,
},
props: {
adapter: {
type: Object as PropType<EthernetInterface>,
required: true,
},
},
data() {
return {
show_creation_dialog: false,
show_dhcp_server_dialog: false,
}
},
computed: {
is_connected(): boolean {
return this.adapter.info ? this.adapter.info.connected : false
},
status_info(): string {
return this.is_connected ? 'Connected' : 'Not connected'
},
is_there_dhcp_server_already(): boolean {
return this.adapter.addresses.some((address) => address.mode === AddressMode.server)
},
is_static_ip_present(): boolean {
return this.adapter.addresses.some((address) => address.mode === AddressMode.unmanaged)
},
is_interface_last_ip_address(): boolean {
return this.adapter.addresses.length === 1
},
},
mounted() {
beacon.registerBeaconListener(this)
},
methods: {
/**
* Opens a dialog and requests the user to confirm the deletion of the IP address.
* @returns {Promise<boolean>} - Resolves to true if no confirmation is needed or
* granted and false otherwise.
*/
async confirm_last_interface_ip(): Promise<boolean> {
if (this.is_interface_last_ip_address) {
const result = await Popup.fire({
title: 'Last IP Address',
text: 'This is the last IP address on the interface. Are you sure you want to proceed?',
confirmButtonText: 'Yes',
showCancelButton: true,
})
return result.confirmed
}
return true
},
/**
* Opens a dialog and requests the user to confirm the deletion of current used IP address.
* @returns {Promise<boolean>} - Resolves to true if no confirmation is needed or
* granted and false otherwise.
*/
async confirm_ip_being_used(ip: string): Promise<boolean> {
const ip_being_used = ip === beacon.nginx_ip_address
if (ip_being_used) {
const result = await Popup.fire({
title: 'IP Address in Use',
text: 'The IP address is currently being used to access BlueOS. Are you sure you want to proceed?',
confirmButtonText: 'Yes',
showCancelButton: true,
})
return result.confirmed
}
return true
},
showable_mode_name(mode: AddressMode): string {
switch (mode) {
case AddressMode.client: return 'Dynamic IP'
case AddressMode.server: return 'DHCP Server'
case AddressMode.unmanaged: return 'Static IP'
default: return 'Undefined mode'
}
},
openAddressCreationDialog(): void {
this.show_creation_dialog = true
},
async deleteAddress(ip: string): Promise<void> {
const confirmed_ip_used = await this.confirm_ip_being_used(ip)
const confirmed_last_ip = await this.confirm_last_interface_ip()
if (!confirmed_ip_used || !confirmed_last_ip) {
return
}
ethernet.setUpdatingInterfaces(true)
await back_axios({
method: 'delete',
url: `${ethernet.API_URL}/address`,
timeout: 10000,
params: { interface_name: this.adapter.name, ip_address: ip },
})
.catch((error) => {
notifier.pushBackError('ETHERNET_ADDRESS_DELETE_FAIL', error)
})
},
async triggerForDynamicIP(): Promise<void> {
ethernet.setUpdatingInterfaces(true)
await back_axios({
method: 'post',
url: `${ethernet.API_URL}/dynamic_ip`,
timeout: 10000,
params: { interface_name: this.adapter.name },
})
.catch((error) => {
const message = `Could not trigger for dynamic IP address on '${this.adapter.name}': ${error.message}.`
notifier.pushError('DYNAMIC_IP_TRIGGER_FAIL', message)
})
},
openDHCPServerDialog(): void {
this.show_dhcp_server_dialog = true
},
async removeDHCPServer(): Promise<void> {
ethernet.setUpdatingInterfaces(true)
await back_axios({
method: 'delete',
url: `${ethernet.API_URL}/dhcp`,
timeout: 10000,
params: { interface_name: this.adapter.name },
})
.catch((error) => {
const message = `Could not remove DHCP server from interface '${this.adapter.name}': ${error.message}.`
notifier.pushError('DHCP_SERVER_REMOVE_FAIL', message)
})
},
},
})
</script>