Use beginning of UUID for server uuidShort
This commit is contained in:
parent
85e75a2808
commit
36837df0a6
5 changed files with 63 additions and 4 deletions
|
@ -136,4 +136,13 @@ interface ServerRepositoryInterface extends RepositoryInterface, SearchableInter
|
|||
* @return int
|
||||
*/
|
||||
public function getServersForPowerActionCount(array $servers = [], array $nodes = []): int;
|
||||
|
||||
/**
|
||||
* Check if a given UUID and UUID-Short string are unique to a server.
|
||||
*
|
||||
* @param string $uuid
|
||||
* @param string $short
|
||||
* @return bool
|
||||
*/
|
||||
public function isUniqueUuidCombo(string $uuid, string $short): bool;
|
||||
}
|
||||
|
|
|
@ -303,6 +303,18 @@ class ServerRepository extends EloquentRepository implements ServerRepositoryInt
|
|||
return $this->getServersForPowerAction($servers, $nodes, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given UUID and UUID-Short string are unique to a server.
|
||||
*
|
||||
* @param string $uuid
|
||||
* @param string $short
|
||||
* @return bool
|
||||
*/
|
||||
public function isUniqueUuidCombo(string $uuid, string $short): bool
|
||||
{
|
||||
return ! $this->getBuilder()->where('uuid', '=', $uuid)->orWhere('uuidShort', '=', $short)->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of server IDs that a given user can access based
|
||||
* on owner and subuser permissions.
|
||||
|
|
|
@ -210,10 +210,12 @@ class ServerCreationService
|
|||
*/
|
||||
private function createModel(array $data): Server
|
||||
{
|
||||
$uuid = $this->generateUniqueUuidCombo();
|
||||
|
||||
return $this->repository->create([
|
||||
'external_id' => array_get($data, 'external_id'),
|
||||
'uuid' => Uuid::uuid4()->toString(),
|
||||
'uuidShort' => str_random(8),
|
||||
'uuid' => $uuid,
|
||||
'uuidShort' => substr($uuid, 0, 8),
|
||||
'node_id' => array_get($data, 'node_id'),
|
||||
'name' => array_get($data, 'name'),
|
||||
'description' => array_get($data, 'description') ?? '',
|
||||
|
@ -289,4 +291,20 @@ class ServerCreationService
|
|||
|
||||
return $allocation->node_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a unique UUID and UUID-Short combo for a server.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function generateUniqueUuidCombo(): string
|
||||
{
|
||||
$uuid = Uuid::uuid4()->toString();
|
||||
|
||||
if (! $this->repository->isUniqueUuidCombo($uuid, substr($uuid, 0, 8))) {
|
||||
return $this->generateUniqueUuidCombo();
|
||||
}
|
||||
|
||||
return $uuid;
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue