Move the primary allocation when it is removed from a server
This commit is contained in:
1 parent
72779c7ea6
commit
ddaef93768
6 files changed
+82
-4
No files matched your search
+2
-2
@@ -117,7 +117,7 @@ class AllocationsRelationManager extends RelationManager
|
||||
->after(function (Allocation $allocation) {
|
||||
$allocation->update(Allocation::RELEASE_ATTRIBUTES);
|
||||
|
||||
if (!$this->getOwnerRecord()->allocation_id) {
|
||||
if (!$this->getOwnerRecord()->allocation_id || $this->getOwnerRecord()->allocation_id === $allocation->id) {
|
||||
$this->getOwnerRecord()->update(['allocation_id' => $this->getOwnerRecord()->allocations()->first()?->id]);
|
||||
}
|
||||
}),
|
||||
@@ -128,7 +128,7 @@ class AllocationsRelationManager extends RelationManager
|
||||
->after(function (Collection $records) {
|
||||
Allocation::whereIn('id', $records->pluck('id'))->update(Allocation::RELEASE_ATTRIBUTES);
|
||||
|
||||
if (!$this->getOwnerRecord()->allocation_id) {
|
||||
if (!$this->getOwnerRecord()->allocation_id || $records->contains('id', $this->getOwnerRecord()->allocation_id)) {
|
||||
$this->getOwnerRecord()->update(['allocation_id' => $this->getOwnerRecord()->allocations()->first()?->id]);
|
||||
}
|
||||
}),
|
||||
|
||||
@@ -139,6 +139,10 @@ class NetworkAllocationController extends ClientApiController
|
||||
|
||||
Allocation::query()->where('id', $allocation->id)->update(Allocation::RELEASE_ATTRIBUTES);
|
||||
|
||||
if ($allocation->id === $server->allocation_id) {
|
||||
$server->update(['allocation_id' => $server->allocations()->first()?->id]);
|
||||
}
|
||||
|
||||
Activity::event('server:allocation.delete')
|
||||
->subject($allocation)
|
||||
->property('allocation', $allocation->address)
|
||||
|
||||
@@ -231,11 +231,14 @@ class Server extends Model implements HasAvatar, Validatable
|
||||
*/
|
||||
public function getAllocationMappings(): array
|
||||
{
|
||||
if (!$this->allocation) {
|
||||
$allocations = $this->allocations->where('node_id', $this->node_id);
|
||||
|
||||
// Daemon expects a map here, and an empty array would be encoded as a JSON list.
|
||||
if (!$this->allocation || $allocations->isEmpty()) {
|
||||
return ['' => []];
|
||||
}
|
||||
|
||||
return $this->allocations->where('node_id', $this->node_id)->groupBy('ip')->map(function ($item) {
|
||||
return $allocations->groupBy('ip')->map(function ($item) {
|
||||
return $item->pluck('port');
|
||||
})->toArray();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user