Auto create Domains/Zones as systems present their AKAs to us

This commit is contained in:
Deon George
2022-12-01 23:51:43 +11:00
parent 55a2a67b8d
commit d5d4a0d781
6 changed files with 112 additions and 9 deletions

View File

@@ -343,13 +343,15 @@ class Address extends Model
/**
* Find a record in the DB for a node string, eg: 10:1/1.0
*
* @param string $ftn
* @param string $address
* @param bool $create
* @param System|null $so
* @return Address|null
* @throws Exception
*/
public static function findFTN(string $ftn): ?self
public static function findFTN(string $address,bool $create=FALSE,System $so=NULL): ?self
{
$ftn = self::parseFTN($ftn);
$ftn = self::parseFTN($address);
// Are we looking for a region address
if (($ftn['f'] === 0) && $ftn['p'] === 0) {
@@ -411,6 +413,60 @@ class Address extends Model
})
->single();
if ($create) {
if (! $so)
throw new Exception(sprintf('%s:AKA create requested for [%s], but system not provided',self::LOGKEY,$address));
if (! $ftn['d']) {
Log::alert(sprintf('%s:! Refusing to create address [%s] no domain available',self::LOGKEY,$address));
return NULL;
}
Log::debug(sprintf('%s:Creating AKA [%s] for [%s]',self::LOGKEY,$address,$so->name));
// Check Domain exists
Domain::unguard();
$do = Domain::singleOrNew(['name'=>$ftn['d']]);
Domain::reguard();
if (! $do->exists) {
$do->public = TRUE;
$do->active = TRUE;
$do->notes = 'Auto created';
$do->save();
}
// Create zone
Zone::unguard();
$zo = Zone::singleOrNew(['domain_id'=>$do->id,'zone_id'=>$ftn['z']]);
Zone::reguard();
if (! $zo->exists) {
$zo->active = TRUE;
$zo->notes = 'Auto created';
$zo->system_id = System::where('name','Discovered System')->single()->id;
$do->zones()->save($zo);
}
if (! $zo->active || ! $do->active) {
Log::alert(sprintf('%s:! Refusing to create address [%s] in disabled zone or domain',self::LOGKEY,$address));
return NULL;
}
// Create Address, assigned to $so
$o = new self;
$o->active = TRUE;
$o->zone_id = $zo->id;
$o->region_id = 0;
$o->host_id = $ftn['n'];
$o->node_id = $ftn['f'];
$o->point_id = $ftn['p'];
$o->role = self::NODE_UNKNOWN;
$so->addresses()->save($o);
}
return ($o && $o->system->active) ? $o : NULL;
}