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.
68 lines
2.4 KiB
PHP
68 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use Illuminate\Database\Capsule\Manager as DB;
|
|
|
|
/** Resolves the live instance rows affected by one supported infrastructure scope. */
|
|
final class CloudAffectedInstanceResolver
|
|
{
|
|
private const SCOPE_INSTANCE = 'instance';
|
|
private const SCOPE_HOST = 'host';
|
|
private const SCOPE_PUBLIC_IP_POOL = 'public_ip_pool';
|
|
|
|
/** @return array<int, object> */
|
|
public function resolve(string $scopeType, int $scopeId): array
|
|
{
|
|
$scopeType = trim($scopeType);
|
|
if (!in_array($scopeType, [
|
|
self::SCOPE_INSTANCE,
|
|
self::SCOPE_HOST,
|
|
self::SCOPE_PUBLIC_IP_POOL,
|
|
], true) || $scopeId < 1) {
|
|
throw new CloudError('受影响实例范围无效', 422);
|
|
}
|
|
|
|
if ($scopeType === self::SCOPE_INSTANCE) {
|
|
return DB::table('cloud_instances')
|
|
->where('id', $scopeId)
|
|
->where('status', '<>', 'deleted')
|
|
->orderBy('id')->get($this->columns())->all();
|
|
}
|
|
if ($scopeType === self::SCOPE_HOST) {
|
|
return DB::table('cloud_instances')
|
|
->where('host_id', $scopeId)
|
|
->where('status', '<>', 'deleted')
|
|
->orderBy('id')->get($this->columns())->all();
|
|
}
|
|
|
|
return DB::table('cloud_resource_reservation_ips as claim')
|
|
->join('cloud_resource_reservations as reservation',
|
|
'reservation.id', '=', 'claim.reservation_id')
|
|
->join('cloud_instances as instance', static function ($join): void {
|
|
$join->on('instance.id', '=', 'reservation.instance_id')
|
|
->on('instance.reservation_id', '=', 'reservation.id');
|
|
})
|
|
->where('claim.public_ip_pool_id', $scopeId)
|
|
->where('claim.active_claim', 1)
|
|
->where('claim.state', 'committed')
|
|
->where('reservation.state', 'committed')
|
|
->where('instance.status', '<>', 'deleted')
|
|
->orderBy('instance.id')
|
|
->distinct()->get([
|
|
'instance.id', 'instance.user_id', 'instance.host_id',
|
|
'instance.reservation_id', 'instance.name',
|
|
'instance.spec_json',
|
|
])->all();
|
|
}
|
|
|
|
/** @return array<int, string> */
|
|
private function columns(): array
|
|
{
|
|
return [
|
|
'id', 'user_id', 'host_id', 'reservation_id',
|
|
'name', 'spec_json',
|
|
];
|
|
}
|
|
}
|