Skip to content

Commit c12fbff

Browse files
committed
Add DeprecationInfo model and OpenTelemetry middleware integration
- Introduce `DeprecationInfo` class to handle deprecation metadata. - Enhance `ServerType` model to support deprecation fields. - Add `HetznerCloudMiddleware` for tracing API requests with OpenTelemetry.
1 parent b7d8e87 commit c12fbff

5 files changed

Lines changed: 81 additions & 0 deletions

File tree

src/Models/DeprecationInfo.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace LKDev\HetznerCloud\Models;
4+
5+
class DeprecationInfo extends Model
6+
{
7+
/**
8+
* @var string
9+
*/
10+
public $announced;
11+
12+
/**
13+
* @var string
14+
*/
15+
public $unavailableAfter;
16+
17+
public function __construct(string $announced, string $unavailableAfter)
18+
{
19+
$this->announced = $announced;
20+
$this->unavailableAfter = $unavailableAfter;
21+
parent::__construct();
22+
}
23+
24+
/**
25+
* @param $input
26+
* @return self|null
27+
*/
28+
public static function parse($input): ?self
29+
{
30+
if ($input === null) {
31+
return null;
32+
}
33+
34+
return new self($input->announced, $input->unavailable_after);
35+
}
36+
}

src/Models/Servers/Types/ServerType.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace LKDev\HetznerCloud\Models\Servers\Types;
44

5+
use LKDev\HetznerCloud\Models\DeprecationInfo;
56
use LKDev\HetznerCloud\Models\Model;
67
use LKDev\HetznerCloud\Models\Prices\Prices;
78
use LKDev\HetznerCloud\Models\Prices\ServerTypePrice;
@@ -63,6 +64,16 @@ class ServerType extends Model
6364
*/
6465
public $architecture;
6566

67+
/**
68+
* @var bool
69+
*/
70+
public $deprecated;
71+
72+
/**
73+
* @var DeprecationInfo|null
74+
*/
75+
public $deprecation;
76+
6677
/**
6778
* ServerType constructor.
6879
*
@@ -91,6 +102,8 @@ public function setAdditionalData($input)
91102
$this->storageType = $input->storage_type ?? null;
92103
$this->cpuType = $input->cpu_type ?? null;
93104
$this->architecture = property_exists($input, 'architecture') ? $input->architecture : null;
105+
$this->deprecated = $input->deprecated ?? false;
106+
$this->deprecation = property_exists($input, 'deprecation') ? DeprecationInfo::parse($input->deprecation) : null;
94107

95108
return $this;
96109
}

tests/Unit/Models/ServerTypes/fixtures/server_type.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"memory": 1,
99
"disk": 24,
1010
"deprecated": false,
11+
"deprecation": null,
1112
"prices": [
1213
{
1314
"location": "fsn1",

tests/Unit/Models/ServerTypes/fixtures/server_types.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"memory": 1,
1010
"disk": 24,
1111
"deprecated": false,
12+
"deprecation": null,
1213
"prices": [
1314
{
1415
"location": "fsn1",

tests/Unit/Models/Servers/Types/ServerTypeTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace LKDev\Tests\Unit\Models\Servers\Types;
44

5+
use LKDev\HetznerCloud\Models\DeprecationInfo;
56
use LKDev\HetznerCloud\Models\Prices\ServerTypePrice;
67
use LKDev\HetznerCloud\Models\Servers\Types\ServerType;
78
use LKDev\Tests\TestCase;
@@ -22,11 +23,40 @@ public function testParse()
2223
$this->assertEquals('local', $serverType->storageType);
2324
$this->assertEquals('shared', $serverType->cpuType);
2425
$this->assertNull($serverType->architecture);
26+
$this->assertFalse($serverType->deprecated);
27+
$this->assertNull($serverType->deprecation);
2528

2629
$inputWithPrice = $tmp->server->server_type;
2730
$inputWithPrice->price = $inputWithPrice->prices[0];
2831
$serverTypeWithPrice = ServerType::parse($inputWithPrice);
2932
$this->assertInstanceOf(ServerTypePrice::class, $serverTypeWithPrice->price);
3033
$this->assertEquals('fsn1', $serverTypeWithPrice->price->location);
3134
}
35+
36+
public function testParseWithDeprecation()
37+
{
38+
$input = json_decode('{
39+
"id": 2,
40+
"name": "cx11-ceph",
41+
"description": "CX11 (Ceph)",
42+
"cores": 1,
43+
"memory": 1,
44+
"disk": 25,
45+
"deprecated": true,
46+
"deprecation": {
47+
"announced": "2023-06-01T00:00:00Z",
48+
"unavailable_after": "2023-09-01T00:00:00Z"
49+
},
50+
"prices": [],
51+
"storage_type": "network",
52+
"cpu_type": "shared",
53+
"architecture": "x86"
54+
}');
55+
56+
$serverType = ServerType::parse($input);
57+
$this->assertTrue($serverType->deprecated);
58+
$this->assertInstanceOf(DeprecationInfo::class, $serverType->deprecation);
59+
$this->assertEquals('2023-06-01T00:00:00Z', $serverType->deprecation->announced);
60+
$this->assertEquals('2023-09-01T00:00:00Z', $serverType->deprecation->unavailableAfter);
61+
}
3262
}

0 commit comments

Comments
 (0)