Merge branch 'feature/ipnetwork-update' into 'master'

added delete and update functions to ipnetwork

See merge request fronk/thetool!512
This commit is contained in:
Luca Haid
2024-07-25 09:42:25 +00:00
3 changed files with 123 additions and 3 deletions

View File

@@ -180,5 +180,39 @@ class IpNetworkController extends mfBaseController {
}
}
private function update(): array {
$json = json_decode(file_get_contents('php://input'), true);
try {
IpNetworkModel::updateIpNetwork($json);
return [
"status" => "success",
"message" => "IP Network updated."
];
} catch (Exception $e) {
return [
"status" => "error",
"message" => $e->getMessage()
];
}
}
private function delete(): array {
$json = json_decode(file_get_contents('php://input'), true);
try {
IpNetworkModel::deleteIpNetwork($json['id']);
return [
"status" => "success",
"message" => "IP Network deleted."
];
} catch (Exception $e) {
return [
"status" => "error",
"message" => $e->getMessage()
];
}
}
}

View File

@@ -200,6 +200,24 @@ class IpNetworkModel {
}
}
public static function updateIpNetwork($data): void {
$db = FronkDB::singleton();
$sqlSetStr = "";
$sqlSetStr .= isset($data['status']) ? "`status` = '" . $data['status'] . "', " : "";
$sqlSetStr .= isset($data['name']) ? "`name` = '" . $data['name'] . "', " : "";
$sqlSetStr .= isset($data['description']) ? "`description` = '" . $data['description'] . "', " : "";
$sqlSetStr .= isset($data['location']) ? "`location` = '" . $data['location'] . "', " : "";
$sqlSetStr .= "`edit` = UNIX_TIMESTAMP()";
$sql = "UPDATE `IpNetwork` SET $sqlSetStr WHERE `id` = " . $data['id'];
$result = $db->query($sql);
if (!$result) {
throw new Exception("Failed to update network");
}
}
public static function getById($id) {
$db = FronkDB::singleton();
@@ -209,4 +227,22 @@ class IpNetworkModel {
return $row ? new IpNetworkModel($row) : null;
}
public static function deleteIpNetwork($id) {
// delete this id and all children and children of children until no more children
$db = FronkDB::singleton();
$sql = "SELECT `id` FROM `IpNetwork` WHERE `parent_network_id` = $id";
$result = $db->query($sql);
while ($row = $result->fetch_assoc()) {
self::deleteIpNetwork($row['id']);
}
$sql = "DELETE FROM `IpNetwork` WHERE `id` = $id";
$result = $db->query($sql);
if (!$result) {
throw new Exception("Failed to delete network");
}
}
}

View File

@@ -17,7 +17,7 @@ Vue.component('IpNetwork', {
<i class="fas fa-sync-alt"></i>Go Back
</button>
<button type="button" class="btn btn-primary" @click="addModal = true">
<button type="button" class="btn btn-primary" @click="openModal(false)">
<i class="fas fa-sync-alt"></i>Add new Network Space
</button>
@@ -28,6 +28,13 @@ Vue.component('IpNetwork', {
<span style="white-space: pre;" v-if="row.description" v-text="row.description"></span>
<span v-else>No description</span>
</template>
<template v-slot:actions="{ row }">
<div style="display: flex; justify-content: space-around; align-items: center; min-width: 20px">
<a style="cursor: pointer;" @click.stop="openModal(row)"><i class="far fa-edit text-primary"
title="Editieren"></i></a>
</div>
</template>
</tt-table>
@@ -47,11 +54,12 @@ Vue.component('IpNetwork', {
<div class="form-group">
<label for="network_address">Network Address</label>
<input type="text" class="form-control" id="network_address"
:disabled="!!addModalData.id"
v-model="addModalData.network_address">
</div>
<div class="form-group">
<label for="cidr">CIDR</label>
<input type="text" class="form-control" id="cidr" v-model="addModalData.cidr">
<input type="text" class="form-control" id="cidr" :disabled="!!addModalData.id" v-model="addModalData.cidr">
</div>
<div class="form-group">
<label for="status">Status</label>
@@ -83,6 +91,7 @@ Vue.component('IpNetwork', {
</div>
<div class="modal-footer">
<button class="btn btn-primary" @click="addSubmit">Save</button>
<button class="btn btn-danger" @click="deleteSubmit" v-if="addModalData.id">Delete</button>
<button class="btn btn-secondary" @click="addModal = false">Close</button>
</div>
</div>
@@ -112,6 +121,7 @@ Vue.component('IpNetwork', {
{value: 'reserved', text: 'Reserved', icon: 'fas fa-lock text-warning'}]
},
{text: 'Children', key: 'children', filter: 'numberRange'},
{text: 'Aktionen', key: 'actions', sortable: false},
],
tableHeader: 'IPAM',
key: 'IpNetwork'
@@ -140,6 +150,34 @@ Vue.component('IpNetwork', {
},
methods: {
openModal(row = false) {
if (row) {
const data = JSON.parse(JSON.stringify(row));
this.addModalData = {
id: data.id,
network_address: data.network_address_str.split('/')[0],
cidr: data.cidr,
parent_network_id: this.currentNetworkData ? this.currentNetworkData.id : '',
status: data.status,
name: data.name,
description: data.description,
location: data.location,
}
} else {
this.addModalData = {
network_address: '',
cidr: '',
parent_network_id: this.currentNetworkData ? this.currentNetworkData.id : '',
status: 'active',
name: '',
description: '',
location: '',
};
}
this.addModal = true;
},
async switchCurrentNetwork(networkId = null) {
if (!networkId) {
this.$refs.table.$set(this.$refs.table.filters, 'parent_network_id', undefined);
@@ -159,18 +197,30 @@ Vue.component('IpNetwork', {
await this.$refs.table.fetchData();
},
async addSubmit() {
const response = await axios.post(`${this.apiUrl}?do=create`,
const response = await axios.post(`${this.apiUrl}?do=${this.addModalData.id ? 'update' : 'create'}`,
{
...this.addModalData,
parent_network_id: this.currentNetworkData ? this.currentNetworkData.id : null
});
if (response.data.status === 'success') {
this.addModal = false;
this.addModalData = {};
window.notify('success', 'Network space created successfully');
await this.$refs.table.fetchData();
} else {
window.notify('error', response.data.message);
}
},
async deleteSubmit() {
const response = await axios.post(`${this.apiUrl}?do=delete`, {id: this.addModalData.id});
if (response.data.status === 'success') {
this.addModal = false;
this.addModalData = {};
window.notify('success', 'Network space deleted successfully');
await this.$refs.table.fetchData();
} else {
window.notify('error', response.data.message);
}
}
},
})