Skip to content

Commit 22f665f

Browse files
committed
Add network api
1 parent 2b50bca commit 22f665f

2 files changed

Lines changed: 204 additions & 0 deletions

File tree

src/Client.php

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,152 @@ public function execInspect($exec)
12731273
)->then(array($this->parser, 'expectJson'));
12741274
}
12751275

1276+
/**
1277+
* List networks.
1278+
*
1279+
* @return PromiseInterface Promise<array>
1280+
* @link https://docs.docker.com/engine/api/v1.40/#operation/NetworkList
1281+
*/
1282+
public function networkList()
1283+
{
1284+
return $this->browser->get(
1285+
$this->uri->expand(
1286+
'/networks',
1287+
array()
1288+
)
1289+
)->then(array($this->parser, 'expectJson'));
1290+
}
1291+
1292+
/**
1293+
* Inspect network.
1294+
*
1295+
* @param string $network The network id or name
1296+
*
1297+
* @return PromiseInterface Promise<array>
1298+
* @link https://docs.docker.com/engine/api/v1.40/#operation/NetworkInspect
1299+
*/
1300+
public function networkInspect($network)
1301+
{
1302+
return $this->browser->get(
1303+
$this->uri->expand(
1304+
'/networks/{network}',
1305+
array(
1306+
'network' => $network
1307+
)
1308+
)
1309+
)->then(array($this->parser, 'expectJson'));
1310+
}
1311+
1312+
/**
1313+
* Remove network.
1314+
*
1315+
* @param string $network The network id or name
1316+
*
1317+
* @return PromiseInterface Promise<null>
1318+
* @link https://docs.docker.com/engine/api/v1.40/#operation/NetworkRemove
1319+
*/
1320+
public function networkRemove($network)
1321+
{
1322+
return $this->browser->delete(
1323+
$this->uri->expand(
1324+
'/networks/{network}',
1325+
array(
1326+
'network' => $network
1327+
)
1328+
)
1329+
)->then(array($this->parser, 'expectEmpty'));
1330+
}
1331+
1332+
/**
1333+
* Create network.
1334+
*
1335+
* @param string $name The network name
1336+
* @param array $config (optional) The network configuration
1337+
*
1338+
* @return PromiseInterface Promise<array>
1339+
* @link https://docs.docker.com/engine/api/v1.40/#operation/NetworkCreate
1340+
*/
1341+
public function networkCreate($name, $config = array())
1342+
{
1343+
$config['Name'] = $name;
1344+
1345+
return $this->postJson(
1346+
$this->uri->expand(
1347+
'/networks/create'
1348+
),
1349+
$config
1350+
)->then(array($this->parser, 'expectJson'));
1351+
}
1352+
1353+
/**
1354+
* Connect container to network
1355+
*
1356+
* @param string $network The network id or name
1357+
* @param string $container The id or name of the container to connect to network
1358+
* @param array $endpointConfig (optional) Configuration for a network endpoint
1359+
*
1360+
* @return PromiseInterface Promise<array>
1361+
* @link https://docs.docker.com/engine/api/v1.40/#operation/NetworkConnect
1362+
*/
1363+
public function networkConnect($network, $container, $endpointConfig = array())
1364+
{
1365+
return $this->postJson(
1366+
$this->uri->expand(
1367+
'/networks/{network}/connect',
1368+
array(
1369+
'network' => $network
1370+
)
1371+
),
1372+
array(
1373+
'Container' => $container,
1374+
'EndpointConfig' => $endpointConfig
1375+
)
1376+
)->then(array($this->parser, 'expectJson'));
1377+
}
1378+
1379+
/**
1380+
* Disconnect container from network.
1381+
*
1382+
* @param string $network The id or name of network
1383+
* @param string $container The id or name of container to disconnect
1384+
* @param bool $force (optional) Force the disconnect
1385+
*
1386+
* @return PromiseInterface Promise<null>
1387+
* @link https://docs.docker.com/engine/api/v1.40/#operation/NetworkDisconnect
1388+
*/
1389+
public function networkDisconnect($network, $container, $force = false)
1390+
{
1391+
return $this->postJson(
1392+
$this->uri->expand(
1393+
'/networks/{network}/disconnect',
1394+
array(
1395+
'network' => $network
1396+
)
1397+
),
1398+
array(
1399+
'Container' => $container,
1400+
'Force' => $force
1401+
)
1402+
)->then(array($this->parser, 'expectEmpty'));
1403+
}
1404+
1405+
/**
1406+
* Remove all unused networks.
1407+
*
1408+
* @return PromiseInterface Promise<array>
1409+
* @link https://docs.docker.com/engine/api/v1.40/#operation/NetworkPrune
1410+
*/
1411+
public function networkPrune()
1412+
{
1413+
return $this->postJson(
1414+
$this->uri->expand(
1415+
'/networks/prune',
1416+
array()
1417+
),
1418+
array()
1419+
)->then(array($this->parser, 'expectJson'));
1420+
}
1421+
12761422
private function postJson($url, $data)
12771423
{
12781424
$body = $this->json($data);

tests/ClientTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,64 @@ public function testExecInspect()
650650
$this->expectPromiseResolveWith($json, $this->client->execInspect(123));
651651
}
652652

653+
public function testNetworkList()
654+
{
655+
$json = array();
656+
$this->expectRequestFlow('get', '/networks', $this->createResponseJson($json), 'expectJson');
657+
658+
$this->expectPromiseResolveWith($json, $this->client->networkList());
659+
}
660+
661+
public function testNetworkInspect()
662+
{
663+
$json = array();
664+
$this->expectRequestFlow('get', '/networks/123', $this->createResponseJson($json), 'expectJson');
665+
666+
$this->expectPromiseResolveWith($json, $this->client->networkInspect(123));
667+
}
668+
669+
public function testNetworkRemove()
670+
{
671+
$json = array();
672+
$this->expectRequestFlow('delete', '/networks/123', $this->createResponse(), 'expectEmpty');
673+
674+
$this->expectPromiseResolveWith('', $this->client->networkRemove(123));
675+
}
676+
677+
public function testNetworkCreate()
678+
{
679+
$json = array();
680+
$config = array();
681+
$this->expectRequestFlow('post', '/networks/create', $this->createResponseJson($json), 'expectJson');
682+
683+
$this->expectPromiseResolveWith($json, $this->client->networkCreate($config));
684+
}
685+
686+
public function testNetworkConnect()
687+
{
688+
$json = array();
689+
$config = array();
690+
$this->expectRequestFlow('post', '/networks/123/connect', $this->createResponseJson($json), 'expectJson');
691+
692+
$this->expectPromiseResolveWith($json, $this->client->networkConnect(123, $config));
693+
}
694+
695+
public function testNetworkDisconnect()
696+
{
697+
$json = array();
698+
$this->expectRequestFlow('post', '/networks/123/disconnect', $this->createResponse(), 'expectEmpty');
699+
700+
$this->expectPromiseResolveWith('', $this->client->networkDisconnect(123, 'abc'));
701+
}
702+
703+
public function testNetworkPrune()
704+
{
705+
$json = array();
706+
$this->expectRequestFlow('post', '/networks/prune', $this->createResponseJson($json), 'expectJson');
707+
708+
$this->expectPromiseResolveWith($json, $this->client->networkPrune());
709+
}
710+
653711
private function expectRequestFlow($method, $url, ResponseInterface $response, $parser)
654712
{
655713
$return = (string)$response->getBody();

0 commit comments

Comments
 (0)