diff --git a/lib/OpenCloud/Compute/Resource/Server.php b/lib/OpenCloud/Compute/Resource/Server.php index 0d47f5dec..b9536947e 100644 --- a/lib/OpenCloud/Compute/Resource/Server.php +++ b/lib/OpenCloud/Compute/Resource/Server.php @@ -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; @@ -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); } } diff --git a/tests/OpenCloud/Tests/Compute/Resource/ServerTest.php b/tests/OpenCloud/Tests/Compute/Resource/ServerTest.php index 13ed80a35..7d173f2cb 100644 --- a/tests/OpenCloud/Tests/Compute/Resource/ServerTest.php +++ b/tests/OpenCloud/Tests/Compute/Resource/ServerTest.php @@ -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 */