Skip to content

Commit 3b0a429

Browse files
committed
New api: add/remove/list SecurityGroups for a server
1 parent 5b5d0d9 commit 3b0a429

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
$openstack = new OpenStack\OpenStack([
6+
'authUrl' => '{authUrl}',
7+
'region' => '{region}',
8+
'user' => [
9+
'id' => '{userId}',
10+
'password' => '{password}'
11+
],
12+
'scope' => ['project' => ['id' => '{projectId}']]
13+
]);
14+
15+
$compute = $openstack->computeV2(['region' => '{region}']);
16+
17+
$server = $compute->getServer([
18+
'id' => '{serverId}',
19+
]);
20+
21+
$server->addSecurityGroup(['name' => 'secgroup name']);
22+
$server->removeSecurityGroup(['name' => 'default']);
23+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use OpenStack\Networking\v2\Extensions\SecurityGroups\Models\SecurityGroup;
4+
use OpenStack\Networking\v2\Extensions\SecurityGroups\Models\SecurityGroupRule;
5+
6+
require 'vendor/autoload.php';
7+
8+
$openstack = new OpenStack\OpenStack([
9+
'authUrl' => '{authUrl}',
10+
'region' => '{region}',
11+
'user' => [
12+
'id' => '{userId}',
13+
'password' => '{password}'
14+
],
15+
'scope' => ['project' => ['id' => '{projectId}']]
16+
]);
17+
18+
$compute = $openstack->computeV2(['region' => '{region}']);
19+
20+
$server = $compute->getServer(['id' => 'uuid']);
21+
foreach($server->listSecurityGroups() as $securityGroup)
22+
{
23+
/**@var SecurityGroup $securityGroup */
24+
$rules = $securityGroup->securityGroupRules;
25+
26+
foreach($rules as $rule)
27+
{
28+
/**@var SecurityGroupRule $rule */
29+
$rule->direction;
30+
}
31+
}

src/Compute/v2/Api.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,42 @@ public function deleteKeypair(): array
472472
];
473473
}
474474

475+
public function addSecurityGroup(): array
476+
{
477+
return [
478+
'method' => 'POST',
479+
'path' => 'servers/{id}/action',
480+
'jsonKey' => 'addSecurityGroup',
481+
'params' => [
482+
'id' => $this->params->urlId('server'),
483+
'name' => $this->isRequired($this->params->name('securityGroup')),
484+
],
485+
];
486+
}
487+
488+
public function removeSecurityGroup(): array
489+
{
490+
return [
491+
'method' => 'POST',
492+
'path' => 'servers/{id}/action',
493+
'jsonKey' => 'removeSecurityGroup',
494+
'params' => [
495+
'id' => $this->params->urlId('server'),
496+
'name' => $this->isRequired($this->params->name('securityGroup')),
497+
],
498+
];
499+
}
500+
501+
public function listSecurityGroupByServer(): array
502+
{
503+
return [
504+
'method' => 'GET',
505+
'path' => 'servers/{id}/os-security-groups',
506+
'jsonKey' => 'security_groups',
507+
'params' => [
508+
'id' => $this->params->urlId('server')
509+
],
510+
];
511+
}
512+
475513
}

src/Compute/v2/Models/Server.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use OpenCloud\Common\Resource\OperatorResource;
1212
use OpenCloud\Common\Transport\Utils;
1313
use OpenStack\Compute\v2\Enum;
14+
use OpenStack\Networking\v2\Extensions\SecurityGroups\Models\SecurityGroup;
1415
use Psr\Http\Message\ResponseInterface;
16+
use Symfony\Component\VarDumper\VarDumper;
1517

1618
/**
1719
* @property \OpenStack\Compute\v2\Api $api
@@ -299,8 +301,41 @@ public function deleteMetadataItem(string $key)
299301
$this->execute($this->api->deleteServerMetadataKey(), ['id' => $this->id, 'key' => $key]);
300302
}
301303

304+
305+
/**
306+
* Add security group to a server (addSecurityGroup action)
307+
*
308+
* @param array $options {@see \OpenStack\Compute\v2\Api::addSecurityGroup}
309+
*/
310+
public function addSecurityGroup(array $options)
311+
{
312+
$options['id'] = $this->id;
313+
$this->execute($this->api->addSecurityGroup(), $options);
314+
}
315+
316+
/**
317+
* Add security group to a server (addSecurityGroup action)
318+
*
319+
* @param array $options {@see \OpenStack\Compute\v2\Api::removeSecurityGroup}
320+
*/
321+
public function removeSecurityGroup(array $options)
322+
{
323+
$options['id'] = $this->id;
324+
$this->execute($this->api->removeSecurityGroup(), $options);
325+
}
326+
302327
public function parseMetadata(ResponseInterface $response): array
303328
{
304329
return Utils::jsonDecode($response)['metadata'];
305330
}
331+
332+
/**
333+
* Returns Generator for SecurityGroups
334+
*
335+
* @return \Generator
336+
*/
337+
public function listSecurityGroups()
338+
{
339+
return $this->model(SecurityGroup::class)->enumerate($this->api->listSecurityGroupByServer(), ['id' => $this->id]);
340+
}
306341
}

src/Networking/v2/Extensions/SecurityGroups/Models/SecurityGroup.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class SecurityGroup extends OperatorResource implements Creatable, Listable, Del
4242

4343
protected $aliases = [
4444
'security_group_rules' => 'securityGroupRules',
45+
'rules' => 'securityGroupRules',
4546
'tenant_id' => 'tenantId',
4647
];
4748

0 commit comments

Comments
 (0)