Skip to content

Commit

Permalink
Merge pull request #638 from jamiehannaford/server-create-ports
Browse files Browse the repository at this point in the history
Add ability to use ports when creating server
  • Loading branch information
Jamie Hannaford committed Oct 26, 2015
2 parents 9d423ba + 3c3557b commit 22031ae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
19 changes: 8 additions & 11 deletions lib/OpenCloud/Compute/Resource/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use OpenCloud\DNS\Resource\HasPtrRecordsInterface;
use OpenCloud\Image\Resource\ImageInterface;
use OpenCloud\Networking\Resource\NetworkInterface;
use OpenCloud\Networking\Resource\Port;
use OpenCloud\Volume\Resource\Volume;
use OpenCloud\Common\Exceptions;
use OpenCloud\Common\Http\Message\Formatter;
Expand Down Expand Up @@ -689,22 +690,18 @@ protected function createJson()
$server->networks = array();

foreach ($this->networks as $network) {
if (!$network instanceof NetworkInterface) {
if ($network instanceof NetworkInterface) {
$server->networks[] = (object) array('uuid' => $network->getId());
} elseif ($network instanceof Port) {
$server->networks[] = (object) array('port' => $network->getId());
} else {
throw new Exceptions\InvalidParameterError(sprintf(
'When creating a server, the "networks" key must be an ' .
'array of objects which implement OpenCloud\Networking\Resource\NetworkInterface;' .
'variable passed in was a [%s]',
'array of objects which implement either OpenCloud\Networking\Resource\NetworkInterface ' .
'or OpenCloud\Networking\Resource\Port. The variable you passed in was a [%s]',
gettype($network)
));
}
if (!($networkId = $network->getId())) {
$this->getLogger()->warning('When creating a server, the '
. 'network objects passed in must have an ID'
);
continue;
}
// Stock networks array
$server->networks[] = (object) array('uuid' => $networkId);
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/OpenCloud/Tests/Compute/Resource/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,20 @@ public function test_Create_With_Networks()
));
}

public function test_Create_With_Ports()
{
$neutronService = $this->client->networkingService(null, 'IAD');
$port = $neutronService->port();
$port->setId('foo');

$this->service->server()->create(array(
'name' => 'port test',
'image' => $this->service->imageList()->first(),
'flavor' => $this->service->flavorList()->first(),
'networks' => [$port]
));
}

/**
* @expectedException OpenCloud\Common\Exceptions\InvalidParameterError
*/
Expand Down

0 comments on commit 22031ae

Please sign in to comment.