From b7a5e5fcd03e6ef40d74fa04fe53068bb737578b Mon Sep 17 00:00:00 2001 From: Jan Fabry Date: Wed, 13 Jan 2016 16:50:18 +0100 Subject: [PATCH] Add support for security groups on server create --- lib/OpenCloud/Compute/Resource/Server.php | 28 +++++++++++++ .../Tests/Compute/Resource/ServerTest.php | 39 +++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/lib/OpenCloud/Compute/Resource/Server.php b/lib/OpenCloud/Compute/Resource/Server.php index b9536947e..9038c291d 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\SecurityGroup; use OpenCloud\Networking\Resource\Port; use OpenCloud\Volume\Resource\Volume; use OpenCloud\Common\Exceptions; @@ -104,6 +105,12 @@ class Server extends NovaResource implements HasPtrRecordsInterface */ public $networks = array(); + /** + * Security groups for this server. An array of either the names or SecurityGroup objects. + * @var (string|SecurityGroup)[] + */ + public $security_groups = array(); + /** * @var string The server ID. */ @@ -705,6 +712,27 @@ protected function createJson() } } + // Security groups + if (is_array($this->security_groups) && count($this->security_groups)) { + $server->security_groups = array(); + + foreach ($this->security_groups as $security_group) { + if ($security_group instanceof SecurityGroup) { + $securityGroupName = $security_group->name(); + } elseif (is_string($security_group)) { + $securityGroupName = $security_group; + } else { + throw new Exceptions\InvalidParameterError(sprintf( + 'When creating a server, the "security_groups" key must be an ' . + 'array of strings or objects of type OpenCloud\Networking\Resource\SecurityGroup;' . + 'variable passed in was a [%s]', + gettype($security_group) + )); + } + $server->security_groups[] = (object) array('name' => $securityGroupName); + } + } + // Personality files if (!empty($this->personality)) { $server->personality = array(); diff --git a/tests/OpenCloud/Tests/Compute/Resource/ServerTest.php b/tests/OpenCloud/Tests/Compute/Resource/ServerTest.php index 7d173f2cb..1c105396d 100644 --- a/tests/OpenCloud/Tests/Compute/Resource/ServerTest.php +++ b/tests/OpenCloud/Tests/Compute/Resource/ServerTest.php @@ -365,6 +365,45 @@ public function test_Create_Fails_Without_Correct_Networks() )); } + public function test_Create_With_Security_Group_Strings() + { + $new = new PublicServer($this->service); + $new->security_groups[] = 'foo'; + $obj = $new->createJson(); + + $this->assertCount(1, $obj->server->security_groups); + $this->assertEquals((object) array('name' => 'foo'), $obj->server->security_groups[0]); + } + + public function test_Create_With_Security_Group_Objects() + { + $neutronService = $this->client->networkingService(null, 'IAD'); + $securityGroup = $neutronService->securityGroup(); + $securityGroup->setName('foo'); + + $new = new PublicServer($this->service); + $new->security_groups[] = $securityGroup; + $obj = $new->createJson(); + + $this->assertCount(1, $obj->server->security_groups); + $this->assertEquals((object) array('name' => 'foo'), $obj->server->security_groups[0]); + } + + /** + * @expectedException OpenCloud\Common\Exceptions\InvalidParameterError + */ + public function test_Create_Fails_Without_Correct_Security_Groups() + { + $this->service->server()->create(array( + 'name' => 'personality test 1', + 'image' => $this->service->imageList()->first(), + 'flavor' => $this->service->flavorList()->first(), + 'security_groups' => array( + 1234 + ) + )); + } + public function test_Create_With_Bootable_Volume() { $new = new PublicServer($this->service);