Commit 9e0ce948 authored by Roman Ondráček's avatar Roman Ondráček

Network: check exit code when reading network connection, add network connection deletion

Signed-off-by: Roman Ondráček's avatarRoman Ondráček <ondracek.roman@centrum.cz>
parent 6337b633
Pipeline #2326 passed with stages
in 14 minutes and 20 seconds
......@@ -250,7 +250,7 @@ class EthernetFormFactory {
}
try {
$this->manager->set($this->connection, $form->getValues());
$this->manager->up($this->connection);
$this->manager->up($this->connection->getUuid());
$this->presenter->flashSuccess('network.ethernet.form.messages.success');
$this->presenter->redirect('Ethernet:default');
} catch (NetworkManagerException $e) {
......
......@@ -45,14 +45,30 @@ class ConnectionManager {
$this->commandManager = $commandManager;
}
/**
* Deletes the network connection
* @param UuidInterface $uuid Network connection UUID
* @throws NetworkManagerException
*/
public function delete(UuidInterface $uuid): void {
$output = $this->commandManager->run('nmcli -t connection delete ' . $uuid->toString(), true);
if ($output->getExitCode() !== 0) {
throw new NetworkManagerException($output->getStderr());
}
}
/**
* Returns the detailed network connection entity
* @param UuidInterface $uuid Network connection UUID
* @return ConnectionDetail Detailed network connection entity
* @throws NetworkManagerException
*/
public function get(UuidInterface $uuid): ConnectionDetail {
$output = $this->commandManager->run('nmcli -t connection show ' . $uuid->toString(), true)->getStdout();
return ConnectionDetail::fromNmCli($output);
$output = $this->commandManager->run('nmcli -t connection show ' . $uuid->toString(), true);
if ($output->getExitCode() !== 0) {
throw new NetworkManagerException($output->getStderr());
}
return ConnectionDetail::fromNmCli($output->getStdout());
}
/**
......@@ -90,12 +106,11 @@ class ConnectionManager {
/**
* Activate a connection on the interface
* @param ConnectionDetail $connection Detailed network connection entity
* @param UuidInterface $uuid Network connection UUID
* @throws NetworkManagerException
*/
public function up(ConnectionDetail $connection): void {
$uuid = $connection->getUuid()->toString();
$command = sprintf('nmcli -t connection up %s', $uuid);
public function up(UuidInterface $uuid): void {
$command = sprintf('nmcli -t connection up %s', $uuid->toString());
$output = $this->commandManager->run($command, true);
if ($output->getExitCode() !== 0) {
throw new NetworkManagerException($output->getStderr());
......
......@@ -84,6 +84,18 @@ class ConnectionManagerTest extends CommandTestCase {
return new IPv6Connection($method, $addresses, $dns);
}
/**
* Tests the function to delete network connection
*/
public function testDelete(): void {
$uuid = '25ab1b06-2a86-40a9-950f-1c576ddcd35a';
$command = 'nmcli -t connection delete ' . $uuid;
$this->receiveCommand($command, true, '');
Assert::noError(function () use ($uuid): void {
$this->manager->delete(Uuid::fromString($uuid));
});
}
/**
* Tests the function to get detailed network connection entity
*/
......@@ -141,7 +153,7 @@ class ConnectionManagerTest extends CommandTestCase {
$command = 'nmcli -t connection up 25ab1b06-2a86-40a9-950f-1c576ddcd35a';
$this->receiveCommand($command, true);
Assert::noError(function () use ($connection): void {
$this->manager->up($connection);
$this->manager->up($connection->getUuid());
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment