validate_config

This commit is contained in:
devdatt 2025-12-23 12:57:00 +05:30
parent 1af9bb484c
commit 592beb0a3c
2 changed files with 17 additions and 7 deletions

View File

@ -184,8 +184,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
} }
} }
if (validate_config($data)) {
file_put_contents('/var/www/50-cloud-init.yaml', netplan_yaml(generate_netplan($data, $iface))); file_put_contents('/var/www/50-cloud-init.yaml', netplan_yaml(generate_netplan($data, $iface)));
} }
}
} }
?> ?>

View File

@ -148,7 +148,6 @@ function build_interface(array $cfg, string $type): array
function generate_netplan(array $data, string $iface): array function generate_netplan(array $data, string $iface): array
{ {
validate_config($data);
$netplan = [ $netplan = [
'network' => [ 'network' => [
@ -201,7 +200,7 @@ function generate_netplan(array $data, string $iface): array
return $netplan; return $netplan;
} }
function validate_config(array $data): void function validate_config(array $data): bool
{ {
$p_enabled = ( $p_enabled = (
$data['primary']['mode'] !== 'disabled' || $data['primary']['mode'] !== 'disabled' ||
@ -216,14 +215,22 @@ function validate_config(array $data): void
$p_vlan = trim($data['primary']['network_primary_vlan'] ?? ''); $p_vlan = trim($data['primary']['network_primary_vlan'] ?? '');
$s_vlan = trim($data['secondary']['network_secondary_vlan'] ?? ''); $s_vlan = trim($data['secondary']['network_secondary_vlan'] ?? '');
/* If both enabled, at least one VLAN is mandatory */ /* If both enabled → at least one VLAN required */
if ($p_enabled && $s_enabled && $p_vlan === '' && $s_vlan === '') { if ($p_enabled && $s_enabled && $p_vlan === '' && $s_vlan === '') {
throw new RuntimeException( echo "<script>alert('Primary and Secondary are enabled, but no VLAN is defined.');</script>";
'Invalid configuration: Primary and Secondary are enabled but no VLAN is defined.' return false;
);
} }
/* Block duplicate VLAN IDs */
if ($p_vlan !== '' && $s_vlan !== '' && $p_vlan === $s_vlan) {
echo "<script>alert('Primary and Secondary cannot use the same VLAN ID.');</script>";
return false;
} }
return true;
}
function netplan_yaml(array $data, int $indent = 0): string function netplan_yaml(array $data, int $indent = 0): string
{ {
$out = ''; $out = '';