Skip to content
This repository was archived by the owner on Nov 4, 2025. It is now read-only.

Commit 7543d00

Browse files
author
Niek Brekelmans
authored
Nameserver records
* create nameserver records * leave old function as it is * unused import * remove unused github actions
1 parent 8268dc0 commit 7543d00

File tree

7 files changed

+36
-124
lines changed

7 files changed

+36
-124
lines changed

.github/workflows/php-cs-fixer.yml

Lines changed: 0 additions & 23 deletions
This file was deleted.

.github/workflows/phpstan.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

.github/workflows/run-tests.yml

Lines changed: 0 additions & 47 deletions
This file was deleted.

.github/workflows/update-changelog.yml

Lines changed: 0 additions & 28 deletions
This file was deleted.

config/hetzner-dns.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@
33
return [
44
'api_token' => env('HETZNER_DNS_API_TOKEN'),
55
'default_ttl' => env('HETZNER_DNS_DEFAULT_TTL', 300),
6+
'nameservers' => [
7+
env('HETZNER_NAMESERVER_1', 'hydrogen.ns.hetzner.com.'),
8+
env('HETZNER_NAMESERVER_2', 'oxygen.ns.hetzner.com.'),
9+
env('HETZNER_NAMESERVER_3', 'helium.ns.hetzner.de.'),
10+
]
611
];

src/Objects/Record.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ class Record extends BaseRecord
1212
public string $id;
1313
public Carbon $created;
1414
public Carbon $modified;
15+
public int $ttl;
1516
}

src/RequestCollections/RecordCollection.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ public function create(string $zone_id, RecordType $type, string $name, string $
2828
return $this->connector->request(new CreateRecord(zone_id: $zone_id, type: $type, name: $name, value: $value, ttl: $ttl))->send()->dto();
2929
}
3030

31+
public function createNameserverRecords(string $zone_id)
32+
{
33+
$records = [];
34+
$nameservers = array_filter(config('hetzner-dns.nameservers', []));
35+
foreach ($nameservers as $nameserver) {
36+
$records[] = $this->create($zone_id, RecordType::NS(), '@', $nameserver);
37+
}
38+
39+
return new Records(records: $records);
40+
}
41+
3142
public function createIfNotExists(string $zone_id, RecordType $type, string $name, string $value, ?int $ttl = null): Record
3243
{
3344
$records = $this->all(zone_id: $zone_id);
@@ -43,6 +54,25 @@ public function createIfNotExists(string $zone_id, RecordType $type, string $nam
4354
return $this->create($zone_id, $type, $name, $value, $ttl);
4455
}
4556

57+
public function createOrUpdate(string $zone_id, RecordType $type, string $name, string $value, ?int $ttl = null): Record
58+
{
59+
$records = $this->all(zone_id: $zone_id);
60+
$record = collect($records->records)
61+
->where('name', $name)
62+
->filter(fn (Record $record) => $record->type->value === $type->value)
63+
->first();
64+
65+
if (! is_null($record)) {
66+
// already exists, update if changed
67+
if ($value !== $record->value || $ttl !== $record->ttl) {
68+
$record = $this->update($record->id, $zone_id, $type, $name, $value, $ttl);
69+
}
70+
return $record;
71+
}
72+
73+
return $this->create($zone_id, $type, $name, $value, $ttl);
74+
}
75+
4676
public function get(string $record_id): Record
4777
{
4878
return $this->connector->request(new GetRecord(record_id: $record_id))->send()->dto();

0 commit comments

Comments
 (0)