|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Services\Cloud\CloudAccess;
|
|
|
|
|
use App\Services\Cloud\CloudAdminInstanceService;
|
|
|
|
|
use App\Services\Cloud\CloudError;
|
|
|
|
|
use App\Services\Cloud\CloudInput;
|
|
|
|
|
use App\Services\Cloud\CloudJobService;
|
|
|
|
|
|
|
|
|
|
/** Administrator inventory snapshots plus explicitly queued VM mutations. */
|
|
|
|
|
final class CloudAdminInstanceController
|
|
|
|
|
{
|
|
|
|
|
public function index($request, $response)
|
|
|
|
|
{
|
|
|
|
|
return $response->withJson(CloudAdminInstanceService::page(
|
|
|
|
|
$request->getAttribute('cloud_actor') ?? CloudAccess::actor(false),
|
|
|
|
|
$request->getQueryParams()
|
|
|
|
|
))->withHeader('Cache-Control', 'private, no-store');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function detail($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
return $response->withJson(CloudAdminInstanceService::detail(
|
|
|
|
|
$request->getAttribute('cloud_actor') ?? CloudAccess::actor(false),
|
|
|
|
|
(string) ($args['id'] ?? '')
|
|
|
|
|
))->withHeader('Cache-Control', 'private, no-store');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Queue a controlled same-host public IPv4 replacement.
|
|
|
|
|
*
|
|
|
|
|
* The engine owns the actual address pool and performs the shutdown,
|
|
|
|
|
* cloud-init/network update, clean-traffic update and claim swap. PHP
|
|
|
|
|
* only validates the administrative boundary and records the durable job;
|
|
|
|
|
* it deliberately does not update spec_json until the worker receives a
|
|
|
|
|
* successful engine result.
|
|
|
|
|
*/
|
|
|
|
|
public function network($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
$actor = $request->getAttribute('cloud_actor') ?? CloudAccess::actor(false);
|
|
|
|
|
CloudAccess::manage($actor);
|
|
|
|
|
$instance = CloudAccess::instance((string) ($args['id'] ?? ''), $actor, true);
|
|
|
|
|
if ((string) $instance->status !== 'active') {
|
|
|
|
|
throw new CloudError('只有处于活动状态的虚拟机可以更换公网 IP', 409);
|
|
|
|
|
}
|
|
|
|
|
$body = CloudInput::object($request);
|
|
|
|
|
CloudInput::only($body, ['public_ip_cidr', 'confirm_restart']);
|
|
|
|
|
if (($body['confirm_restart'] ?? false) !== true) {
|
|
|
|
|
throw new CloudError('更换公网 IP 前必须确认虚拟机会短暂重启');
|
|
|
|
|
}
|
|
|
|
|
$cidr = CloudInput::publicIpCidr($body['public_ip_cidr'] ?? null);
|
|
|
|
|
$spec = json_decode((string) $instance->spec_json, true);
|
|
|
|
|
if (!is_array($spec) || array_is_list($spec)
|
|
|
|
|
|| ($spec['managed'] ?? false) !== true
|
|
|
|
|
|| ($spec['os_source'] ?? null) !== 'cloud_template') {
|
|
|
|
|
throw new CloudError('当前虚拟机不是支持网络配置的面板云镜像实例', 409);
|
|
|
|
|
}
|
|
|
|
|
$current = is_string($spec['public_ip_cidr'] ?? null)
|
|
|
|
|
? trim($spec['public_ip_cidr']) : '';
|
|
|
|
|
if ($current !== '' && $current === $cidr) {
|
|
|
|
|
throw new CloudError('新公网 IP 与当前配置相同', 409);
|
|
|
|
|
}
|
|
|
|
|
$job = CloudJobService::enqueue($instance, $actor, 'network_change', 'POST',
|
|
|
|
|
'/network', ['public_ip_cidr' => $cidr, 'confirm_restart' => true]);
|
|
|
|
|
return $response->withJson([
|
|
|
|
|
'job_id' => (string) $job->id,
|
|
|
|
|
'status_url' => '/api/v1/jobs/' . $job->id,
|
|
|
|
|
'public_ip_cidr' => $cidr,
|
|
|
|
|
], 202)->withHeader('Cache-Control', 'no-store');
|
|
|
|
|
}
|
|
|
|
|
}
|