Skip to content

Commit

Permalink
extend FloatingIP with description and subnet_id
Browse files Browse the repository at this point in the history
The OpenStack API for Floating IPs supports:

- an optional description of the floating IP
- specifying a specific subnet_id upon creation

  The subnet ID is not returned on GET.
  • Loading branch information
antondollmaier committed Jan 5, 2025
1 parent 842981a commit 3eb10b8
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 1 deletion.
3 changes: 2 additions & 1 deletion samples/Networking/v2/floatingIPs/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
$floatingIp = $networking->createFloatingIp([
"floatingNetworkId" => "{networkId}",
"portId" => "{portId}",
'fixedIpAddress' => '{fixedIpAddress}',
"fixedIpAddress" => "{fixedIpAddress}",
"description" => "{description}",
]);
2 changes: 2 additions & 0 deletions src/Networking/v2/Extensions/Layer3/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function postFloatingIps(): array
'path' => $this->pathPrefix.'/floatingips',
'jsonKey' => 'floatingip',
'params' => [
'description' => $this->params->descriptionJson(),
'tenantId' => $this->params->tenantIdJson(),
'floatingNetworkId' => $this->params->floatingNetworkIdJson(),
'fixedIpAddress' => $this->params->fixedIpAddressJson(),
Expand Down Expand Up @@ -53,6 +54,7 @@ public function putFloatingIp(): array
'jsonKey' => 'floatingip',
'params' => [
'id' => $this->params->idPath(),
'description' => $this->notRequired($this->params->descriptionJson()),
'floatingNetworkId' => $this->notRequired($this->params->floatingNetworkIdJson()),
'fixedIpAddress' => $this->params->fixedIpAddressJson(),
'floatingIpAddress' => $this->params->floatingIpAddressJson(),
Expand Down
3 changes: 3 additions & 0 deletions src/Networking/v2/Extensions/Layer3/ApiTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public function postFloatingIps(): array
'jsonKey' => 'floatingip',
'params' => [
'tenantId' => $this->params->tenantIdJson(),
'description' => $this->notRequired($this->params->descriptionJson()),
'floatingNetworkId' => $this->params->floatingNetworkIdJson(),
'subnetId' => $this->notRequired($this->params->subnetIdJson()),
'fixedIpAddress' => $this->params->fixedIpAddressJson(),
'floatingIpAddress' => $this->params->floatingIpAddressJson(),
'portId' => $this->params->portIdJson(),
Expand All @@ -47,6 +49,7 @@ public function putFloatingIp(): array
'jsonKey' => 'floatingip',
'params' => [
'id' => $this->params->idPath(),
'description' => $this->notRequired($this->params->descriptionJson()),
'floatingNetworkId' => $this->notRequired($this->params->floatingNetworkIdJson()),
'fixedIpAddress' => $this->params->fixedIpAddressJson(),
'floatingIpAddress' => $this->params->floatingIpAddressJson(),
Expand Down
7 changes: 7 additions & 0 deletions src/Networking/v2/Extensions/Layer3/Models/FloatingIp.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ class FloatingIp extends OperatorResource implements Listable, Creatable, Retrie
/** @var string */
public $status;

/** @var string */
public $description;

/** @var string */
public $floatingNetworkId;

/** @var string */
public $subnetId;

/** @var string */
public $routerId;

Expand All @@ -41,6 +47,7 @@ class FloatingIp extends OperatorResource implements Listable, Creatable, Retrie

protected $aliases = [
'floating_network_id' => 'floatingNetworkId',
'subnet_id' => 'subnetId',
'router_id' => 'routerId',
'fixed_ip_address' => 'fixedIpAddress',
'floating_ip_address' => 'floatingIpAddress',
Expand Down
9 changes: 9 additions & 0 deletions src/Networking/v2/Extensions/Layer3/Params.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
*/
class Params extends \OpenStack\Networking\v2\Params
{
public function descriptionJson(): array
{
return [
'type' => self::STRING_TYPE,
'description' => 'The description of the floating IP.',
'sentAs' => 'description',
];
}

public function tenantIdJson(): array
{
return [
Expand Down
18 changes: 18 additions & 0 deletions src/Networking/v2/Extensions/Layer3/ParamsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
*/
trait ParamsTrait
{
public function descriptionJson(): array
{
return [
'type' => self::STRING_TYPE,
'description' => 'The description of the floating IP.',
'sentAs' => 'description',
];
}

public function floatingNetworkIdJson(): array
{
return [
Expand Down Expand Up @@ -37,6 +46,15 @@ public function floatingIpAddressJson(): array
];
}

public function subnetIdJson(): array
{
return [
'type' => self::STRING_TYPE,
'description' => 'The UUID of the subnet of the floating Network associated with the floating IP.',
'sentAs' => 'subnet',
];
}

public function portIdJson(): array
{
return [
Expand Down
4 changes: 4 additions & 0 deletions tests/sample/Networking/v2/FloatingIpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,19 @@ public function testCreate(): FloatingIpData
'name' => $this->randomStr(),
]);
$fixedIp = $this->findSubnetIp($data->port, $data->internalSubnet);
$description = $this->randomStr();

/** @var FloatingIp $floatingIp */
require_once $this->sampleFile('floatingIPs/create.php', [
'{networkId}' => $data->externalNetwork->id,
'{portId}' => $data->port->id,
'{fixedIpAddress}' => $fixedIp,
'description' => $description,
]);
$this->assertInstanceOf(FloatingIp::class, $floatingIp);
$this->assertEquals($data->externalNetwork->id, $floatingIp->floatingNetworkId);
$this->assertEquals($data->port->id, $floatingIp->portId);
$this->assertEquals($description, $floatingIp->description);

$data->floatingIp = $floatingIp;

Expand Down Expand Up @@ -123,6 +126,7 @@ public function testGet(FloatingIpData $data)
$this->assertInstanceOf(FloatingIp::class, $floatingIp);
$this->assertEquals($data->floatingIp->id, $floatingIp->id);
$this->assertEmpty($floatingIp->portId);
$this->assertEmpty($floatingIp->description);

$floatingIp->retrieve();
$this->assertEquals($data->floatingIp->portId, $floatingIp->portId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Content-Type: application/json
"fixed_ip_address": "10.0.0.3",
"floating_ip_address": "172.24.4.228",
"tenant_id": "4969c491a3c74ee4af974e6d800c62de",
"description": "some-floating-ip",
"status": "ACTIVE",
"port_id": "ce705c24-c1ef-408a-bda3-7bbd946164ab",
"id": "2f245a7b-796b-4f26-9cf9-9e82d248fda7"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Content-Type: application/json
"floating_ip_address": "172.24.4.228",
"port_id": "ce705c24-c1ef-408a-bda3-7bbd946164ab",
"id": "2f245a7b-796b-4f26-9cf9-9e82d248fda7",
"description": "some-floating-ip",
"status": "ACTIVE"
},
{
Expand All @@ -21,6 +22,7 @@ Content-Type: application/json
"floating_ip_address": "172.24.4.227",
"port_id": null,
"id": "61cea855-49cb-4846-997d-801b70c71bdd",
"description": "some-floating-ip",
"status": "DOWN"
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ public function test_it_updates()
$expectedJson = ['floatingip' => [
"floating_network_id" => "376da547-b977-4cfe-9cba-275c80debf57",
"port_id" => "ce705c24-c1ef-408a-bda3-7bbd946164ab",
"description" => "some-floating-ip",
]];

$this->mockRequest('PUT', 'v2.0/floatingips/id', new Response(202), $expectedJson, []);

$this->floatingIp->floatingNetworkId = "376da547-b977-4cfe-9cba-275c80debf57";
$this->floatingIp->portId = "ce705c24-c1ef-408a-bda3-7bbd946164ab";
$this->floatingIp->description = "some-floating-ip";
$this->floatingIp->update();
}

Expand All @@ -49,6 +51,10 @@ public function test_it_retrieves()

$this->floatingIp->retrieve();

self::assertEquals(
'some-floating-ip',
$this->floatingIp->description
);
self::assertEquals(
'376da547-b977-4cfe-9cba-275c80debf57',
$this->floatingIp->floatingNetworkId
Expand Down

0 comments on commit 3eb10b8

Please sign in to comment.