From 384ceb887a88f384cce75368d747371735731ef9 Mon Sep 17 00:00:00 2001 From: Jamie Hannaford Date: Mon, 26 Oct 2015 12:09:00 +0100 Subject: [PATCH 1/2] add ability to use ports when creating server --- lib/OpenCloud/Compute/Resource/Server.php | 21 ++++++++----------- .../Tests/Compute/Resource/ServerTest.php | 14 +++++++++++++ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/OpenCloud/Compute/Resource/Server.php b/lib/OpenCloud/Compute/Resource/Server.php index 0d47f5dec..f26ba8bde 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]', - gettype($network) + 'array of objects which implement either %s or %s. The ' . + 'variable you passed in was a [%s]', + NetworkInterface::class, Port::class, 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 */ From 3c3557b44f1b27ac3d36c0e7bbfcd65b89f5a678 Mon Sep 17 00:00:00 2001 From: Jamie Hannaford Date: Mon, 26 Oct 2015 12:10:46 +0100 Subject: [PATCH 2/2] remove php 5.5 feature --- lib/OpenCloud/Compute/Resource/Server.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/OpenCloud/Compute/Resource/Server.php b/lib/OpenCloud/Compute/Resource/Server.php index f26ba8bde..b9536947e 100644 --- a/lib/OpenCloud/Compute/Resource/Server.php +++ b/lib/OpenCloud/Compute/Resource/Server.php @@ -697,9 +697,9 @@ protected function createJson() } else { throw new Exceptions\InvalidParameterError(sprintf( 'When creating a server, the "networks" key must be an ' . - 'array of objects which implement either %s or %s. The ' . - 'variable you passed in was a [%s]', - NetworkInterface::class, Port::class, gettype($network) + '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) )); } }