Skip to content

Commit

Permalink
Merge pull request #54 from utopia-php/add-network-exists
Browse files Browse the repository at this point in the history
Add network exists
  • Loading branch information
christyjacob4 authored Aug 29, 2024
2 parents 9d2ff89 + be8eeee commit 331b831
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 22 deletions.
44 changes: 22 additions & 22 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Orchestration/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ abstract public function networkConnect(string $container, string $network): boo
*/
abstract public function networkDisconnect(string $container, string $network, bool $force = false): bool;

/**
* Check if a network exists
*/
abstract public function networkExists(string $name): bool;

/**
* List Networks
*
Expand Down
10 changes: 10 additions & 0 deletions src/Orchestration/Adapter/DockerAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ public function networkDisconnect(string $container, string $network, bool $forc
return $result['code'] === 200;
}

/**
* Check if a network exists
*/
public function networkExists(string $name): bool
{
$result = $this->call('http://localhost/networks/'.$name, 'GET');

return $result['code'] === 200;
}

/**
* Get usage stats of containers
*
Expand Down
12 changes: 12 additions & 0 deletions src/Orchestration/Adapter/DockerCLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ public function networkDisconnect(string $container, string $network, bool $forc
return $result === 0;
}

/**
* Check if a network exists
*/
public function networkExists(string $name): bool
{
$output = '';

$result = Console::execute('docker network inspect '.$name.' --format "{{.Name}}"', '', $output);

return $result === 0 && trim($output) === $name;
}

/**
* Get usage stats of containers
*
Expand Down
8 changes: 8 additions & 0 deletions src/Orchestration/Orchestration.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ public function networkDisconnect(string $container, string $network, bool $forc
return $this->adapter->networkDisconnect($container, $network, $force);
}

/**
* Check if a network exists
*/
public function networkExists(string $name): bool
{
return $this->adapter->networkExists($name);
}

/**
* Pull Image
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/Orchestration/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -716,4 +716,24 @@ public function testUsageStats(): void

$stats = static::getOrchestration()->getStats('IDontExist');
}

public function testNetworkExists(): void
{
$networkName = 'test_network_'.uniqid();

// Test non-existent network
$this->assertFalse(static::getOrchestration()->networkExists($networkName));

// Create network and test it exists
$response = static::getOrchestration()->createNetwork($networkName);
$this->assertTrue($response);
$this->assertTrue(static::getOrchestration()->networkExists($networkName));

// Remove network
$response = static::getOrchestration()->removeNetwork($networkName);
$this->assertTrue($response);

// Test removed network
$this->assertFalse(static::getOrchestration()->networkExists($networkName));
}
}

0 comments on commit 331b831

Please sign in to comment.