Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for security groups on server create #650

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 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\SecurityGroup;
use OpenCloud\Networking\Resource\Port;
use OpenCloud\Volume\Resource\Volume;
use OpenCloud\Common\Exceptions;
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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();
Expand Down
39 changes: 39 additions & 0 deletions tests/OpenCloud/Tests/Compute/Resource/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down