Skip to content

Commit b7a5e5f

Browse files
committed
Add support for security groups on server create
1 parent 755d9ad commit b7a5e5f

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

lib/OpenCloud/Compute/Resource/Server.php

+28
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use OpenCloud\DNS\Resource\HasPtrRecordsInterface;
2222
use OpenCloud\Image\Resource\ImageInterface;
2323
use OpenCloud\Networking\Resource\NetworkInterface;
24+
use OpenCloud\Networking\Resource\SecurityGroup;
2425
use OpenCloud\Networking\Resource\Port;
2526
use OpenCloud\Volume\Resource\Volume;
2627
use OpenCloud\Common\Exceptions;
@@ -104,6 +105,12 @@ class Server extends NovaResource implements HasPtrRecordsInterface
104105
*/
105106
public $networks = array();
106107

108+
/**
109+
* Security groups for this server. An array of either the names or SecurityGroup objects.
110+
* @var (string|SecurityGroup)[]
111+
*/
112+
public $security_groups = array();
113+
107114
/**
108115
* @var string The server ID.
109116
*/
@@ -705,6 +712,27 @@ protected function createJson()
705712
}
706713
}
707714

715+
// Security groups
716+
if (is_array($this->security_groups) && count($this->security_groups)) {
717+
$server->security_groups = array();
718+
719+
foreach ($this->security_groups as $security_group) {
720+
if ($security_group instanceof SecurityGroup) {
721+
$securityGroupName = $security_group->name();
722+
} elseif (is_string($security_group)) {
723+
$securityGroupName = $security_group;
724+
} else {
725+
throw new Exceptions\InvalidParameterError(sprintf(
726+
'When creating a server, the "security_groups" key must be an ' .
727+
'array of strings or objects of type OpenCloud\Networking\Resource\SecurityGroup;' .
728+
'variable passed in was a [%s]',
729+
gettype($security_group)
730+
));
731+
}
732+
$server->security_groups[] = (object) array('name' => $securityGroupName);
733+
}
734+
}
735+
708736
// Personality files
709737
if (!empty($this->personality)) {
710738
$server->personality = array();

tests/OpenCloud/Tests/Compute/Resource/ServerTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,45 @@ public function test_Create_Fails_Without_Correct_Networks()
365365
));
366366
}
367367

368+
public function test_Create_With_Security_Group_Strings()
369+
{
370+
$new = new PublicServer($this->service);
371+
$new->security_groups[] = 'foo';
372+
$obj = $new->createJson();
373+
374+
$this->assertCount(1, $obj->server->security_groups);
375+
$this->assertEquals((object) array('name' => 'foo'), $obj->server->security_groups[0]);
376+
}
377+
378+
public function test_Create_With_Security_Group_Objects()
379+
{
380+
$neutronService = $this->client->networkingService(null, 'IAD');
381+
$securityGroup = $neutronService->securityGroup();
382+
$securityGroup->setName('foo');
383+
384+
$new = new PublicServer($this->service);
385+
$new->security_groups[] = $securityGroup;
386+
$obj = $new->createJson();
387+
388+
$this->assertCount(1, $obj->server->security_groups);
389+
$this->assertEquals((object) array('name' => 'foo'), $obj->server->security_groups[0]);
390+
}
391+
392+
/**
393+
* @expectedException OpenCloud\Common\Exceptions\InvalidParameterError
394+
*/
395+
public function test_Create_Fails_Without_Correct_Security_Groups()
396+
{
397+
$this->service->server()->create(array(
398+
'name' => 'personality test 1',
399+
'image' => $this->service->imageList()->first(),
400+
'flavor' => $this->service->flavorList()->first(),
401+
'security_groups' => array(
402+
1234
403+
)
404+
));
405+
}
406+
368407
public function test_Create_With_Bootable_Volume()
369408
{
370409
$new = new PublicServer($this->service);

0 commit comments

Comments
 (0)