Paillasse/portail/app/Services/Labs/DefinitionLab.php

52 lines
1.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Services\Labs;
use Illuminate\Support\Facades\File;
use RuntimeException;
use Symfony\Component\Yaml\Yaml;
/**
* Lit le lab.yaml d'un lab (voir paillasse-contenus/README.md pour le
* format), pour que GenerateurTemplateHeat puisse en faire une stack.
*/
class DefinitionLab
{
public readonly string $identifiant;
/** @var array<int, array{nom: string, cidr: string}> */
public readonly array $reseaux;
/** @var array<int, array{nom: string, image: string, flavor: string, reseaux: array<int, string>, preparation: ?string}> */
public readonly array $machines;
public static function depuis(string $identifiantLab): self
{
$chemin = config('labs.dossier')."/{$identifiantLab}/lab.yaml";
if (! File::exists($chemin)) {
throw new RuntimeException("Lab introuvable : {$chemin}");
}
$donnees = Yaml::parseFile($chemin);
return new self($donnees);
}
private function __construct(array $donnees)
{
$this->identifiant = $donnees['id'];
$this->reseaux = $donnees['reseaux'];
$this->machines = array_map(
fn (array $m) => [
'nom' => $m['nom'],
'image' => $m['image'],
'flavor' => $m['flavor'],
'reseaux' => $m['reseaux'],
'preparation' => $m['preparation'] ?? null,
],
$donnees['machines'],
);
}
}