Skip to content

Commit 7cef83f

Browse files
authored
Merge pull request clue#57 from tjoussen/feature/network-api
Add network api
2 parents 2b50bca + 18a023c commit 7cef83f

File tree

3 files changed

+265
-0
lines changed

3 files changed

+265
-0
lines changed

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 ? json_encode($endpointConfig) : null
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' => $this->boolArg($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();

tests/FunctionalClientTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,4 +419,65 @@ public function testContainerList()
419419

420420
$this->loop->run();
421421
}
422+
423+
/**
424+
* @depends testImageInspectCheckIfBusyboxExists
425+
*/
426+
public function testCreateConnectDisconnectAndRemoveNetwork()
427+
{
428+
$containerConfig = array(
429+
'Image' => 'busybox',
430+
'Cmd' => array('echo', 'test')
431+
);
432+
$networkName = uniqid('reactphp-docker');
433+
434+
$promise = $this->client->containerCreate($containerConfig);
435+
$container = Block\await($promise, $this->loop);
436+
437+
$promise = $this->client->containerStart($container['Id']);
438+
$ret = Block\await($promise, $this->loop);
439+
440+
$start = microtime(true);
441+
442+
$promise = $this->client->networkCreate($networkName);
443+
$network = Block\await($promise, $this->loop);
444+
445+
$this->assertNotNull($network['Id']);
446+
$this->assertEquals('', $network['Warning']);
447+
448+
$promise = $this->client->networkConnect($network['Id'], $container['Id']);
449+
$ret = Block\await($promise, $this->loop);
450+
451+
$this->assertEquals('', $ret);
452+
453+
$promise = $this->client->networkDisconnect($network['Id'], $container['Id'], false);
454+
$ret = Block\await($promise, $this->loop);
455+
456+
$this->assertEquals('', $ret);
457+
458+
$promise = $this->client->networkRemove($network['Id']);
459+
$ret = Block\await($promise, $this->loop);
460+
461+
$this->assertEquals('', $ret);
462+
463+
$end = microtime(true);
464+
465+
$promise = $this->client->containerStop($container['Id']);
466+
$ret = Block\await($promise, $this->loop);
467+
468+
$promise = $this->client->containerRemove($container['Id']);
469+
$ret = Block\await($promise, $this->loop);
470+
471+
// get all events between starting and removing for this container
472+
$promise = $this->client->events($start, $end, array('network' => array($network['Id'])));
473+
$ret = Block\await($promise, $this->loop);
474+
475+
// expects "create", "connect", "disconnect", "destroy" events
476+
//$this->assertEquals(4, count($ret));
477+
$this->assertEquals(3, count($ret));
478+
$this->assertEquals('create', $ret[0]['Action']);
479+
//$this->assertEquals('connect', $ret[1]['Action']);
480+
$this->assertEquals('disconnect', $ret[1]['Action']);
481+
$this->assertEquals('destroy', $ret[2]['Action']);
482+
}
422483
}

0 commit comments

Comments
 (0)