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/Services/Cloud/CloudOperationCatalog.php

180 lines
6.5 KiB
PHP

<?php
namespace App\Services\Cloud;
/**
* Canonical labels for panel-visible cloud jobs and power actions.
*
* The Agent keeps its own protocol allow-list. This catalog is deliberately
* limited to the values persisted by the panel so filters and every panel
* consumer render the same task vocabulary.
*/
final class CloudOperationCatalog
{
private const KINDS = [
'create' => '创建实例',
'action' => '电源操作',
'reinstall' => '重装系统',
'rescue' => '救援操作',
'delete' => '删除实例',
'delete_force' => '强制删除实例',
'network_change' => '更换公网 IP',
'public_ip_pool_replacement_batch' => '批量迁移公网地址池',
'instance_upgrade' => '升级实例',
'instance_settings' => '实例设置',
'service_compensation' => '实例可用时间补偿',
'snapshot_create' => '创建快照',
'snapshot_restore' => '恢复快照',
'snapshot_delete' => '删除快照',
'backup_create' => '创建可移植备份',
'backup_workspace_cleanup' => '清理备份工作区',
'backup_restore' => '从备份创建实例',
'backup_recover' => '从云备份恢复实例',
'instance_migration_prepare' => '准备维护迁移源实例',
'instance_migration_transfer' => '传输维护迁移数据',
'instance_migration_target_clean' => '清理维护迁移目标残留',
'instance_migration_cleanup' => '清理维护迁移源副本',
'instance_migration_rollback' => '恢复维护迁移源实例',
'instance_migration_activate' => '恢复维护迁移目标状态',
'firewall_apply' => '应用防火墙',
'vpc_host_apply' => '同步 VPC 宿主机路由',
'vpc_host_remove' => '清理 VPC 宿主机路由',
'vpc_port_attach' => '连接 VPC 私网',
'vpc_port_detach' => '断开 VPC 私网',
'vpc_membership_batch' => '批量调整 VPC 实例',
'service_suspend' => '到期停用',
'network_suspend' => '限制网络',
'network_resume' => '恢复网络',
'expiry_delete' => '到期清理',
'payment_create' => '创建支付',
'template_import' => '导入云镜像',
'template_import_replica' => '复制云镜像',
'template_metadata_sync' => '同步云镜像元数据',
'template_metadata_replica' => '同步云镜像元数据副本',
'template_delete' => '删除云镜像',
'template_delete_replica' => '删除云镜像副本',
'rescue_catalog_validate_replica' => '校验救援 ISO',
'rescue_catalog_import_replica' => '复制救援 ISO',
'rescue_catalog_delete_replica' => '清理救援 ISO 副本',
'user_iso_import' => '导入用户 ISO',
'user_iso_import_replica' => '复制用户 ISO',
'user_iso_delete' => '删除用户 ISO',
'user_iso_delete_replica' => '删除用户 ISO 副本',
'host_storage_configure' => '配置宿主机存储',
];
private const POWER_ACTIONS = [
'start' => '开机',
'shutdown' => '关机',
'reboot' => '重启',
'poweroff' => '强制断电',
'reset' => '强制重置',
];
/** Jobs shown in the operation log on an instance detail page. */
private const INSTANCE_LOG_KINDS = [
'action',
'reinstall',
'instance_settings',
'service_compensation',
'network_change',
'instance_upgrade',
'firewall_apply',
'snapshot_create',
'snapshot_restore',
'snapshot_delete',
'backup_create',
'backup_workspace_cleanup',
'backup_restore',
'backup_recover',
'instance_migration_prepare',
'instance_migration_transfer',
'instance_migration_target_clean',
'instance_migration_cleanup',
'instance_migration_rollback',
'instance_migration_activate',
'vpc_host_apply',
'vpc_host_remove',
'vpc_port_attach',
'vpc_port_detach',
'rescue',
];
/** Safe server-derived detail codes retained with instance-setting jobs. */
private const INSTANCE_SETTING_FIELDS = [
'hostname' => '修改主机名',
'password' => '重置 root 密码',
'ssh_key' => '更新 SSH 公钥',
];
/** Return the canonical persisted job kinds and their labels. */
public static function kinds(): array
{
return self::KINDS;
}
/** Return options suitable for an administrator kind filter. */
public static function kindOptions(): array
{
$options = [];
foreach (self::KINDS as $value => $label) {
$options[] = ['value' => $value, 'label' => $label];
}
return $options;
}
public static function isPowerAction(string $action): bool
{
return array_key_exists($action, self::POWER_ACTIONS);
}
/** Return every user-visible operation retained in instance history. */
public static function instanceLogKinds(): array
{
return self::INSTANCE_LOG_KINDS;
}
public static function kindLabel(?string $kind, ?string $action = null,
array $settingsFields = []): string
{
$kind = (string) $kind;
if ($kind === 'action' && $action !== null && self::isPowerAction($action)) {
return self::POWER_ACTIONS[$action];
}
if ($kind === 'instance_settings') {
$labels = [];
foreach (self::INSTANCE_SETTING_FIELDS as $field => $label) {
if (in_array($field, $settingsFields, true)) {
$labels[] = $label;
}
}
if ($labels !== []) {
return implode('、', $labels);
}
}
return self::KINDS[$kind] ?? '其他任务';
}
public static function actionLabel(?string $action): ?string
{
if ($action === null || !self::isPowerAction($action)) {
return null;
}
return self::POWER_ACTIONS[$action];
}
/**
* Return both raw-action metadata and the final display label. The raw
* action remains available for clients that need protocol details while
* kind_label is the one value every panel list should render.
*/
public static function labels(?string $kind, ?string $action = null,
array $settingsFields = []): array
{
return [
'kind_label' => self::kindLabel($kind, $action, $settingsFields),
'action_label' => self::actionLabel($action),
];
}
}