Skip to content

Commit 660b0b6

Browse files
committed
fixing various null issues
1 parent c09e050 commit 660b0b6

5 files changed

Lines changed: 34 additions & 8 deletions

File tree

subdomains/database/migrations/005_add_srv_target_to_nodes.php

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

33
use Illuminate\Database\Migrations\Migration;
44
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\DB;
56
use Illuminate\Support\Facades\Schema;
67

78
return new class extends Migration

subdomains/lang/en/strings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
'cloudflare_missing_srv_port' => 'SRV port is missing for :server.',
4141

4242
'cloudflare_missing_srv_target_title' => 'Cloudflare: Missing SRV Target',
43-
'cloudflare_missing_srv_target' => 'SRV target is missing from :node. ',
43+
'cloudflare_missing_srv_target' => 'SRV target is missing from :node.',
4444

4545
'cloudflare_record_created_title' => 'Cloudflare: Record Created',
4646
'cloudflare_record_created' => 'Successfully created :subdomain record',

subdomains/src/Filament/Components/Actions/SetSrvTargetAction.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ protected function setUp(): void
2828
->label(fn () => trans('subdomains::strings.srv_target'))
2929
->default(fn () => $node->srv_target) // @phpstan-ignore property.notFound
3030
->placeholder('play.example.com OR IPv4/IPv6 address')
31-
->helperText(trans('subdomains::strings.srv_target_confirmation'))
32-
->rules(['nullable', 'string', 'regex:/^(?=.{1,253}$)(?!-)[A-Za-z0-9-]{1,63}(?<!-)(?:\.(?!-)[A-Za-z0-9-]{1,63}(?<!-))*$/']),
31+
->helperText(trans('subdomains::strings.srv_target_confirmation')),
3332
];
3433
});
3534

subdomains/src/Filament/Server/Resources/Subdomains/SubdomainResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public static function form(Schema $schema): Schema
116116
TextInput::make('name')
117117
->label(trans('subdomains::strings.name'))
118118
->required()
119-
->suffix(fn (callable $get) => CloudflareDomain::find($get('domain_id'))->name)
119+
->suffix(fn (callable $get) => CloudflareDomain::find($get('domain_id'))?->name)
120120
->unique(),
121121
Select::make('domain_id')
122122
->label(trans_choice('subdomains::strings.domain', 1))

subdomains/src/Models/Subdomain.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function setSrvRecordAttribute(bool $isSrvRecord): void
120120
if ($isSrvRecord) {
121121
$this->attributes['record_type'] = 'SRV';
122122
} else {
123-
$ip = $this->server->allocation?->ip;
123+
$ip = $this->server?->allocation?->ip;
124124
if (!empty($ip) && filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
125125
$this->attributes['record_type'] = 'AAAA';
126126
} else {
@@ -136,9 +136,19 @@ protected function upsertOnCloudflare(): bool
136136
$zoneId = $this->domain->cloudflare_id;
137137
$domainName = $this->domain->name;
138138

139+
if ($this->server || $this->server->node) {
140+
Log::warning('Subdomain server/node relation is null', ['subdomain_id' => $this->id]);
141+
Notification::make()
142+
->danger()
143+
->title(trans('subdomains::strings.notifications.cloudflare_upsert_failed_title'))
144+
->body(trans('subdomains::strings.notifications.cloudflare_upsert_failed', ['subdomain' => $this->getLabel() ?? 'unknown', 'errors' => 'Server/node relation is null']))
145+
->send();
146+
147+
return false;
148+
}
149+
139150
if (empty($zoneId) || empty($domainName)) {
140151
Log::warning('Cloudflare zone id or name missing for domain', ['domain_id' => $this->domain_id]);
141-
142152
Notification::make()
143153
->danger()
144154
->title(trans('subdomains::strings.notifications.cloudflare_missing_zone_title'))
@@ -192,7 +202,6 @@ protected function upsertOnCloudflare(): bool
192202
if ($this->exists) {
193203
$this->updateQuietly(['cloudflare_id' => $result['id']]);
194204
} else {
195-
// Model is being created, set attribute so it gets persisted with the insert
196205
$this->cloudflare_id = $result['id'];
197206
}
198207
}
@@ -225,7 +234,13 @@ protected function upsertOnCloudflare(): bool
225234
$result = $registrar->upsertDnsRecord($zoneId, $domainName, $this->name, $this->record_type, $ip, $this->cloudflare_id, null, null);
226235

227236
if ($result['success'] && !empty($result['id'])) {
228-
$this->cloudflare_id = $result['id'];
237+
if ($this->cloudflare_id !== $result['id']) {
238+
if ($this->exists) {
239+
$this->updateQuietly(['cloudflare_id' => $result['id']]);
240+
} else {
241+
$this->cloudflare_id = $result['id'];
242+
}
243+
}
229244

230245
return true;
231246
}
@@ -248,6 +263,17 @@ protected function deleteOnCloudflare(): bool
248263
return true;
249264
}
250265

266+
if ($this->domain || empty($this->domain->cloudflare_id)) {
267+
Log::warning('Cloudflare zone missing for subdomain during subdomain delete', ['domain_id' => $this->domain_id]);
268+
Notification::make()
269+
->danger()
270+
->title(trans('subdomains::strings.notifications.cloudflare_delete_failed_title'))
271+
->body(trans('subdomains::strings.notifications.cloudflare_delete_failed', ['errors' => 'Cloudflare zone missing for domain']))
272+
->send();
273+
274+
return false;
275+
}
276+
251277
$registrar = app(CloudflareService::class);
252278

253279
$result = $registrar->deleteDnsRecord($this->domain->cloudflare_id, $this->cloudflare_id);

0 commit comments

Comments
 (0)