Skip to content

Commit

Permalink
core: frontend: Add checks prior to delete IPs
Browse files Browse the repository at this point in the history
* Add a check layer prior to deleting IPs on interfaces avoiding to
  delete the last IP or curently being useed IP by mistake
  • Loading branch information
JoaoMario109 committed Mar 13, 2024
1 parent 8f5e935 commit 76e2741
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
65 changes: 65 additions & 0 deletions core/frontend/src/components/ethernet/AddressDeletionDialog.vue
@@ -0,0 +1,65 @@
<template>
<v-dialog
width="350"
:value="show"
@input="resolve"
>
<v-card>
<v-card-title>{{ title }}</v-card-title>

<v-card-subtitle class="mb-3 mt-3">
{{ message }}
</v-card-subtitle>

<v-card-text class="d-flex flex-row justify-space-around">
<v-btn
color="grey"
class="ma-2 elevation-2"
text
@click="resolve(false)"
>
Abort
</v-btn>
<v-btn
color="primary"
class="ma-2 elevation-2"
text
@click="resolve(true)"
>
Confirm
</v-btn>
</v-card-text>
</v-card>
</v-dialog>
</template>

<script lang="ts">
import Vue from 'vue'
export default Vue.extend({
name: 'AddressDeletionDialog',
model: {
prop: 'show',
event: 'change',
},
props: {
show: {
type: Boolean,
default: false,
},
},
data() {
return {
title: '',
message: '',
resolveCallback: undefined as ((state: boolean) => void) | undefined,
}
},
methods: {
resolve(state: boolean) {
this.resolveCallback?.(state)
this.$emit('change', state)
},
},
})
</script>
65 changes: 65 additions & 0 deletions core/frontend/src/components/ethernet/InterfaceCard.vue
Expand Up @@ -47,6 +47,10 @@
v-model="show_creation_dialog"
:interface-name="adapter.name"
/>
<address-deletion-dialog
ref="deletionDialog"
v-model="show_deletion_dialog"
/>
<v-btn
small
class="ma-2 px-2 py-5 elevation-1"
Expand Down Expand Up @@ -85,12 +89,14 @@
import Vue, { PropType } from 'vue'
import Notifier from '@/libs/notifier'
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 AddressDeletionDialog from './AddressDeletionDialog.vue'
import DHCPServerDialog from './DHCPServerDialog.vue'
const notifier = new Notifier(ethernet_service)
Expand All @@ -99,6 +105,7 @@ export default Vue.extend({
name: 'InterfaceCard',
components: {
AddressCreationDialog,
AddressDeletionDialog,
'dhcp-server-dialog': DHCPServerDialog,
},
props: {
Expand All @@ -111,6 +118,7 @@ export default Vue.extend({
data() {
return {
show_creation_dialog: false,
show_deletion_dialog: false,
show_dhcp_server_dialog: false,
}
},
Expand All @@ -127,8 +135,58 @@ export default Vue.extend({
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 dialog = this.$refs.deletionDialog as InstanceType<typeof AddressDeletionDialog>
dialog.title = 'Last IP Address'
dialog.message = 'This is the last IP address on the interface. Are you sure you want to proceed?'
this.show_deletion_dialog = true
return new Promise((resolve) => {
dialog.resolveCallback = resolve
})
}
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 dialog = this.$refs.deletionDialog as InstanceType<typeof AddressDeletionDialog>
dialog.title = 'IP Address in Use'
dialog.message = 'The IP address is currently being used to access BlueOS. Are you sure you want to proceed?'
this.show_deletion_dialog = true
return new Promise((resolve) => {
dialog.resolveCallback = resolve
})
}
return true
},
showable_mode_name(mode: AddressMode): string {
switch (mode) {
case AddressMode.client: return 'Dynamic IP'
Expand All @@ -141,6 +199,13 @@ export default Vue.extend({
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 = confirmed_ip_used && await this.confirm_last_interface_ip()
if (!confirmed_ip_used || !confirmed_last_ip) {
return
}
ethernet.setUpdatingInterfaces(true)
await back_axios({
Expand Down

0 comments on commit 76e2741

Please sign in to comment.