|
|
|
|
@ -9,7 +9,8 @@ use App\Models\CloudJob;
|
|
|
|
|
final class CloudOperationCompletionService
|
|
|
|
|
{
|
|
|
|
|
private const RESULT_ENVELOPE = 'virt_cloud_completion_event';
|
|
|
|
|
private const RESULT_ENVELOPE_VERSION = 1;
|
|
|
|
|
private const RESULT_ENVELOPE_VERSION = 2;
|
|
|
|
|
private const LEGACY_RESULT_ENVELOPE_VERSION = 1;
|
|
|
|
|
private const IDENTITY_RETRY_SECONDS = 300;
|
|
|
|
|
|
|
|
|
|
public static function accept(array $input): array
|
|
|
|
|
@ -87,6 +88,7 @@ final class CloudOperationCompletionService
|
|
|
|
|
'request_fingerprint' =>
|
|
|
|
|
$receipt['request_fingerprint'],
|
|
|
|
|
],
|
|
|
|
|
'cause_code' => $receipt['cause_code'] ?? null,
|
|
|
|
|
'result' => $receipt['result'],
|
|
|
|
|
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
|
|
|
|
|
| JSON_UNESCAPED_UNICODE);
|
|
|
|
|
@ -275,7 +277,7 @@ final class CloudOperationCompletionService
|
|
|
|
|
public static function operation(CloudAgentCompletionEvent $event): array
|
|
|
|
|
{
|
|
|
|
|
$envelope = self::resultEnvelope($event);
|
|
|
|
|
return [
|
|
|
|
|
$operation = [
|
|
|
|
|
'operation_id' => (string) $event->operation_id,
|
|
|
|
|
'status' => (string) $event->outcome,
|
|
|
|
|
'effect_may_have_applied' => (bool) $event->effect_may_have_applied,
|
|
|
|
|
@ -292,6 +294,10 @@ final class CloudOperationCompletionService
|
|
|
|
|
$envelope['identity']['request_fingerprint'],
|
|
|
|
|
'result' => $envelope['result'],
|
|
|
|
|
];
|
|
|
|
|
if ($envelope['cause_code'] !== null) {
|
|
|
|
|
$operation['cause_code'] = $envelope['cause_code'];
|
|
|
|
|
}
|
|
|
|
|
return $operation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -475,6 +481,9 @@ final class CloudOperationCompletionService
|
|
|
|
|
'error_code' => $errorCode,
|
|
|
|
|
'result' => $result,
|
|
|
|
|
];
|
|
|
|
|
if ($envelope['cause_code'] !== null) {
|
|
|
|
|
$receipt['cause_code'] = $envelope['cause_code'];
|
|
|
|
|
}
|
|
|
|
|
$calculated = hash('sha256', json_encode(
|
|
|
|
|
$receipt,
|
|
|
|
|
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
|
|
|
|
|
@ -594,7 +603,7 @@ final class CloudOperationCompletionService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array{schema:string,version:int,identity:array{action:string,resource_id:string,request_fingerprint:string},result:?array}
|
|
|
|
|
* @return array{schema:string,version:int,identity:array{action:string,resource_id:string,request_fingerprint:string},cause_code:?string,result:?array}
|
|
|
|
|
*/
|
|
|
|
|
private static function resultEnvelope(
|
|
|
|
|
CloudAgentCompletionEvent $event
|
|
|
|
|
@ -618,23 +627,37 @@ final class CloudOperationCompletionService
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$identity = is_array($value) ? ($value['identity'] ?? null) : null;
|
|
|
|
|
$version = is_array($value) ? ($value['version'] ?? null) : null;
|
|
|
|
|
$allowedKeys = $version === self::LEGACY_RESULT_ENVELOPE_VERSION
|
|
|
|
|
? ['schema', 'version', 'identity', 'result']
|
|
|
|
|
: ['schema', 'version', 'identity', 'cause_code', 'result'];
|
|
|
|
|
$causeCode = $version === self::LEGACY_RESULT_ENVELOPE_VERSION
|
|
|
|
|
? null : (is_array($value) ? ($value['cause_code'] ?? null) : null);
|
|
|
|
|
if (!is_array($value) || array_is_list($value)
|
|
|
|
|
|| array_diff(array_keys($value), [
|
|
|
|
|
'schema', 'version', 'identity', 'result',
|
|
|
|
|
]) !== []
|
|
|
|
|
|| array_diff(array_keys($value), $allowedKeys) !== []
|
|
|
|
|
|| ($value['schema'] ?? null) !== self::RESULT_ENVELOPE
|
|
|
|
|
|| ($value['version'] ?? null) !== self::RESULT_ENVELOPE_VERSION
|
|
|
|
|
|| !in_array($version, [
|
|
|
|
|
self::LEGACY_RESULT_ENVELOPE_VERSION,
|
|
|
|
|
self::RESULT_ENVELOPE_VERSION,
|
|
|
|
|
], true)
|
|
|
|
|
|| !is_array($identity) || array_is_list($identity)
|
|
|
|
|
|| array_diff(array_keys($identity), [
|
|
|
|
|
'action', 'resource_id', 'request_fingerprint',
|
|
|
|
|
]) !== []
|
|
|
|
|
|| !array_key_exists('result', $value)
|
|
|
|
|
|| ($version === self::RESULT_ENVELOPE_VERSION
|
|
|
|
|
&& !array_key_exists('cause_code', $value))
|
|
|
|
|
|| ($causeCode !== null
|
|
|
|
|
&& (!is_string($causeCode)
|
|
|
|
|
|| preg_match('/^[a-z][a-z0-9_]{0,63}$/D', $causeCode)
|
|
|
|
|
!== 1))
|
|
|
|
|
|| ($value['result'] !== null && !is_array($value['result']))) {
|
|
|
|
|
throw new CloudRecoveryOperationIdentityConflict(
|
|
|
|
|
'Agent 完成回执身份封装无效', 409
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
self::assertIdentityShape($identity);
|
|
|
|
|
$value['cause_code'] = $causeCode;
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -1179,7 +1202,8 @@ final class CloudOperationCompletionService
|
|
|
|
|
$allowed = ['type', 'id', 'agent_id', 'operation_id', 'action',
|
|
|
|
|
'resource_id', 'request_fingerprint', 'status',
|
|
|
|
|
'effect_may_have_applied', 'safe_to_resubmit', 'error_code',
|
|
|
|
|
'result'];
|
|
|
|
|
'cause_code', 'result'];
|
|
|
|
|
$causeCode = $input['cause_code'] ?? '';
|
|
|
|
|
if (array_is_list($input)
|
|
|
|
|
|| array_diff(array_keys($input), $allowed) !== []
|
|
|
|
|
|| ($input['type'] ?? null) !== 'completion'
|
|
|
|
|
@ -1211,12 +1235,16 @@ final class CloudOperationCompletionService
|
|
|
|
|
|| (($input['error_code'] ?? '') !== ''
|
|
|
|
|
&& preg_match('/^[a-z][a-z0-9_]{0,63}$/D',
|
|
|
|
|
$input['error_code']) !== 1)
|
|
|
|
|
|| !is_string($causeCode)
|
|
|
|
|
|| ($causeCode !== ''
|
|
|
|
|
&& preg_match('/^[a-z][a-z0-9_]{0,63}$/D', $causeCode) !== 1)
|
|
|
|
|
|| !array_key_exists('result', $input)
|
|
|
|
|
|| ($input['result'] !== null && !is_array($input['result']))
|
|
|
|
|
|| (($input['status'] ?? null) === 'succeeded'
|
|
|
|
|
&& (($input['effect_may_have_applied'] ?? null) !== false
|
|
|
|
|
|| ($input['safe_to_resubmit'] ?? null) !== false
|
|
|
|
|
|| ($input['error_code'] ?? null) !== ''))
|
|
|
|
|
|| ($input['error_code'] ?? null) !== ''
|
|
|
|
|
|| $causeCode !== ''))
|
|
|
|
|
|| (($input['status'] ?? null) === 'failed'
|
|
|
|
|
&& (($input['error_code'] ?? '') === ''
|
|
|
|
|
|| (($input['effect_may_have_applied'] ?? false) === true
|
|
|
|
|
@ -1224,7 +1252,7 @@ final class CloudOperationCompletionService
|
|
|
|
|
throw new CloudError('Agent 完成回执无效', 400);
|
|
|
|
|
}
|
|
|
|
|
self::assertCompletionResultSafe($input['result']);
|
|
|
|
|
return [
|
|
|
|
|
$receipt = [
|
|
|
|
|
'event_id' => $input['id'],
|
|
|
|
|
'host_id' => (int) $input['agent_id'],
|
|
|
|
|
'operation_id' => $input['operation_id'],
|
|
|
|
|
@ -1237,5 +1265,9 @@ final class CloudOperationCompletionService
|
|
|
|
|
'error_code' => $input['error_code'],
|
|
|
|
|
'result' => $input['result'],
|
|
|
|
|
];
|
|
|
|
|
if ($causeCode !== '') {
|
|
|
|
|
$receipt['cause_code'] = $causeCode;
|
|
|
|
|
}
|
|
|
|
|
return $receipt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|