Ejemplo n.º 1
0
 public function addAllocations($id, array $allocations)
 {
     $node = Models\Node::findOrFail($id);
     DB::beginTransaction();
     try {
         foreach ($allocations as $rawIP => $ports) {
             $parsedIP = Network::parse($rawIP);
             foreach ($parsedIP as $ip) {
                 foreach ($ports as $port) {
                     if (!is_int($port) && !preg_match('/^(\\d{1,5})-(\\d{1,5})$/', $port)) {
                         throw new DisplayException('The mapping for ' . $port . ' is invalid and cannot be processed.');
                     }
                     if (preg_match('/^(\\d{1,5})-(\\d{1,5})$/', $port, $matches)) {
                         foreach (range($matches[1], $matches[2]) as $assignPort) {
                             $alloc = Models\Allocation::firstOrNew(['node' => $node->id, 'ip' => $ip, 'port' => $assignPort]);
                             if (!$alloc->exists) {
                                 $alloc->fill(['node' => $node->id, 'ip' => $ip, 'port' => $assignPort, 'assigned_to' => null]);
                                 $alloc->save();
                             }
                         }
                     } else {
                         $alloc = Models\Allocation::firstOrNew(['node' => $node->id, 'ip' => $ip, 'port' => $port]);
                         if (!$alloc->exists) {
                             $alloc->fill(['node' => $node->id, 'ip' => $ip, 'port' => $port, 'assigned_to' => null]);
                             $alloc->save();
                         }
                     }
                 }
             }
         }
         DB::commit();
         return true;
     } catch (\Exception $ex) {
         DB::rollBack();
         throw $ex;
     }
 }