Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add network exists #54

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ Once you have initialised your Orchestration object the following methods can be
This method creates a new network and returns a boolean value indicating if the network was created successfully.

```php
$orchestration->createNetwork('name', false);
$orchestration->networkCreate('name', false);
```

<details>
Expand All @@ -305,7 +305,7 @@ Once you have initialised your Orchestration object the following methods can be
This method removes a network and returns a boolean value indicating if the network was removed successfully.

```php
$orchestration->removeNetwork('network_id');
$orchestration->networkRemove('network_id');
```

<details>
Expand Down
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.

9 changes: 7 additions & 2 deletions src/Orchestration/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public function filterEnvKey(string $string): string
/**
* Create Network
*/
abstract public function createNetwork(string $name, bool $internal = false): bool;
abstract public function networkCreate(string $name, bool $internal = false): bool;

/**
* Remove Network
*/
abstract public function removeNetwork(string $name): bool;
abstract public function networkRemove(string $name): bool;

/**
* Connect a container to a network
Expand All @@ -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
14 changes: 12 additions & 2 deletions src/Orchestration/Adapter/DockerAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ protected function streamCall(string $url, int $timeout = -1): array
/**
* Create Network
*/
public function createNetwork(string $name, bool $internal = false): bool
public function networkCreate(string $name, bool $internal = false): bool
{
$body = \json_encode([
'Name' => $name,
Expand All @@ -225,7 +225,7 @@ public function createNetwork(string $name, bool $internal = false): bool
/**
* Remove Network
*/
public function removeNetwork(string $name): bool
public function networkRemove(string $name): bool
{
$result = $this->call('http://localhost/networks/'.$name, 'DELETE');

Expand Down 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
16 changes: 14 additions & 2 deletions src/Orchestration/Adapter/DockerCLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(?string $username = null, ?string $password = null)
/**
* Create Network
*/
public function createNetwork(string $name, bool $internal = false): bool
public function networkCreate(string $name, bool $internal = false): bool
{
$output = '';

Expand All @@ -43,7 +43,7 @@ public function createNetwork(string $name, bool $internal = false): bool
/**
* Remove Network
*/
public function removeNetwork(string $name): bool
public function networkRemove(string $name): bool
{
$output = '';

Expand Down 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
16 changes: 12 additions & 4 deletions src/Orchestration/Orchestration.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ public function parseCommandString(string $command): array
/**
* Create Network
*/
public function createNetwork(string $name, bool $internal = false): bool
public function networkCreate(string $name, bool $internal = false): bool
{
return $this->adapter->createNetwork($name, $internal);
return $this->adapter->networkCreate($name, $internal);
}

/**
* Remove Network
*/
public function removeNetwork(string $name): bool
public function networkRemove(string $name): bool
{
return $this->adapter->removeNetwork($name);
return $this->adapter->networkRemove($name);
}

/**
Expand Down 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
32 changes: 26 additions & 6 deletions tests/Orchestration/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,15 @@ public function testCreateContainer(): void
/**
* @depends testCreateContainer
*/
public function testCreateNetwork(): void
public function testNetworkCreate(): void
{
$response = static::getOrchestration()->createNetwork('TestNetwork');
$response = static::getOrchestration()->networkCreate('TestNetwork');

$this->assertEquals(true, $response);
}

/**
* @depends testCreateNetwork
* @depends testNetworkCreate
*/
public function testListNetworks(): void
{
Expand All @@ -211,7 +211,7 @@ public function testListNetworks(): void
}

/**
* @depends testCreateNetwork
* @depends testNetworkCreate
*/
public function testNetworkConnect(): void
{
Expand Down Expand Up @@ -260,9 +260,9 @@ public function testNetworkDisconnect(): void
/**
* @depends testNetworkDisconnect
*/
public function testRemoveNetwork(): void
public function testNetworkRemove(): void
{
$response = static::getOrchestration()->removeNetwork('TestNetwork');
$response = static::getOrchestration()->networkRemove('TestNetwork');

$this->assertEquals(true, $response);
}
Expand Down 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()->networkCreate($networkName);
$this->assertTrue($response);
$this->assertTrue(static::getOrchestration()->networkExists($networkName));

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

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