You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Models/CloudResourceReservation.php

106 lines
3.5 KiB
PHTML

<?php
namespace App\Models;
final class CloudResourceReservation extends Model
{
protected $connection = 'default';
protected $table = 'cloud_resource_reservations';
protected $casts = [
'id' => 'int',
'order_id' => 'int',
'backup_restore_id' => 'int',
'migration_source_debt_id' => 'int',
'offer_id' => 'int',
'resource_pool_id' => 'int',
'host_id' => 'int',
'user_id' => 'int',
'instance_id' => 'int',
'network_attachment_id' => 'int',
'public_ip_pool_id' => 'int',
'public_ip_id' => 'int',
'cpu_cores' => 'int',
'memory_mib' => 'int',
'disk_gib' => 'int',
'expires_at' => 'int',
'released_at' => 'int',
'created_at' => 'int',
'updated_at' => 'int',
];
/**
* Address ownership lives in cloud_resource_reservation_ips. The legacy
* singular columns remain schema-only until the final clean-break cleanup.
*/
public function save(array $options = [])
{
if (!$this->exists) {
$order = (int) ($this->order_id ?? 0);
$restore = (int) ($this->backup_restore_id ?? 0);
$attemptKey = (string) ($this->backup_restore_attempt_key ?? '');
$debt = (int) ($this->migration_source_debt_id ?? 0);
$offer = (int) ($this->offer_id ?? 0);
if (!(($order > 0 && $offer > 0 && $restore === 0
&& $attemptKey === '' && $debt === 0)
|| ($order === 0 && $offer === 0 && $restore > 0
&& $attemptKey === '' && $debt === 0)
|| ($order === 0 && $offer === 0 && $restore === 0
&& preg_match('/^[a-f0-9]{32}$/D', $attemptKey) === 1
&& $debt === 0)
|| ($order === 0 && $offer === 0 && $restore === 0
&& $attemptKey === '' && $debt > 0))) {
throw new \LogicException(
'New reservations require exactly one immutable source.'
);
}
}
return parent::save($options);
}
public function publicIpClaims()
{
return $this->hasMany(
CloudResourceReservationIp::class,
'reservation_id'
);
}
public function activePublicIpClaims()
{
return $this->hasMany(
CloudResourceReservationIp::class,
'reservation_id'
)->where('active_claim', 1);
}
public function backupRestoreAttemptQuery()
{
$reservationId = (int) ($this->id ?? 0);
$attemptKey = (string) ($this->backup_restore_attempt_key ?? '');
if ($reservationId < 1
|| preg_match('/^[a-f0-9]{32}$/D', $attemptKey) !== 1) {
throw new \LogicException(
'Restore attempt reservation identity is incomplete.'
);
}
return CloudBackupRestoreAttempt::query()
->where('reservation_id', $reservationId)
->where('attempt_key', $attemptKey);
}
public function migrationSourceDebtQuery()
{
$reservationId = (int) ($this->id ?? 0);
$debtId = (int) ($this->migration_source_debt_id ?? 0);
if ($reservationId < 1 || $debtId < 1) {
throw new \LogicException(
'Migration source-debt reservation identity is incomplete.'
);
}
return CloudInstanceMigrationSourceDebt::query()
->where('id', $debtId)
->where('source_reservation_id', $reservationId);
}
}