Added Supplier Domain importing - using dreamscape API

This commit is contained in:
Deon George
2022-08-10 15:18:56 +10:00
parent 53c665787e
commit e4c1305da5
8 changed files with 268 additions and 45 deletions

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\{Site,Supplier};
use App\Jobs\SupplierDomainSync as Job;
class SupplierDomainSync extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'supplier:domain:sync'
.' {siteid : Site ID}'
.' {supplier : Supplier Name}'
.' {--f|forceprod : Force Prod API on dev environment}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Sync domains from a supplier';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$so = Supplier::where('name',$this->argument('supplier'))->singleOrFail();
Job::dispatchSync(Site::findOrFail($this->argument('siteid')),$so,$this->option('forceprod'));
}
}

View File

@@ -0,0 +1,81 @@
<?php
namespace App\Jobs;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use App\Models\{Site,Supplier,TLD};
class SupplierDomainSync implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private const LOGKEY = 'JSD';
protected Site $site;
protected Supplier $supplier;
protected bool $forceprod;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Site $site,Supplier $supplier,bool $forceprod=FALSE)
{
$this->site = $site;
$this->supplier = $supplier;
$this->forceprod = $forceprod;
Config::set('site',$site);
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$registrar_id = ($x=$this->supplier->registrar()) ? $x->id : NULL;
foreach ($this->supplier->API($this->forceprod)->getDomains(['fetchall'=>true]) as $domain) {
// @todo See if we can find this domain by its ID
// Find this domain by it's name
if (! $to=TLD::domaintld($domain->domain_name)) {
Log::alert(sprintf('%s:Domain [%s] from (%s) is not in a TLD that we manage',self::LOGKEY,$this->supplier->name,$domain->domain_name));
} elseif (($domainpart=strtolower($to->domain_part($domain->domain_name))) && (($x=$to->domains->where('domain_name',$domainpart))->count() === 1)) {
$o = $x->pop();
$o->registrar_auth_password = $domain->auth_key;
$o->expire_at = Carbon::create($domain->expiry_date);
$o->registrar_account = $domain->account;
$o->registrar_username = '';
$o->registrar_ns = Supplier\Domain::nameserver_name($domain->nameservers());
if ($registrar_id)
$o->domain_registrar_id = $registrar_id;
if ($o->getDirty()) {
Log::info(sprintf('%s:Updating Domain [%s] from (%s)',self::LOGKEY,$domain->domain_name,$this->supplier->name));
$o->save();
} else {
Log::info(sprintf('%s:No Change to Domain [%s] from (%s)',self::LOGKEY,$domain->domain_name,$this->supplier->name));
}
// Alert an unmanaged name.
} else {
Log::alert(sprintf('%s:Domain [%s] from (%s) is not one managed in OSB',self::LOGKEY,$this->supplier->name,$domain->domain_name));
}
}
}
}

View File

@@ -165,6 +165,16 @@ class Supplier extends Model
return $this->api_class() && (collect(['api_key','api_secret'])->diff($this->detail->connections->keys())->count() === 0);
}
/**
* If this supplier has a domain registrar, return it.
*
* @return DomainRegistrar|null
*/
public function registrar(): ?DomainRegistrar
{
return ($x=config('services.supplier.'.strtolower($this->name).'.registrar')) ? DomainRegistrar::where('name',$x)->single() : NULL;
}
/**
* Return the traffic records, that were not matched to a service.
*

View File

@@ -2,6 +2,8 @@
namespace App\Models\Supplier;
use Illuminate\Support\Collection;
use App\Interfaces\SupplierItem;
use App\Models\Product\Domain as ProductDomain;
use App\Models\TLD;
@@ -29,6 +31,22 @@ final class Domain extends Type implements SupplierItem
return $this->hasMany(ProductDomain::class,'supplier_item_id','id');
}
/* STATIC */
/**
* Determine the owner of the name servers used for the domain
*
*/
public static function nameserver_name(Collection $nameservers): string
{
foreach (config('nameservers') as $key => $ns) {
if ($nameservers->diff($ns)->count() < count($ns))
return $key;
}
return 'custom';
}
/* RELATIONS */
public function tld()

View File

@@ -3,15 +3,65 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use App\Models\Service\Domain;
class TLD extends Model
{
protected $table = 'tlds';
/* RELATIONS */
public function domains()
{
return $this->hasMany(Domain::class,'tld_id');
}
/* STATIC */
/**
* Given a domain name, return the TLD component
*
* @param string $domainname
* @return TLD|null
*/
public static function domaintld(string $domainname): ?TLD
{
$tld = NULL;
foreach (self::get() as $o) {
// Find the most specific match
if (preg_match('/'.$o->name.'$/i',$domainname) && (! $tld || (strlen($o->name) > strlen($tld->name))))
$tld = $o;
}
return $tld;
}
/* ATTRIBUTES */
public function getNameAttribute($value): string
{
return strtoupper($value);
}
/* METHOD */
/**
* Given a domain name, return the domain part
*
* @param string $domainname
* @return string|null
*/
public function domain_part(string $domainname): ?string
{
$domainname = strtoupper($domainname);
// Quick check that this domain is part of this model
if (! Str::endsWith($domainname,'.'.$this->name))
return NULL;
return Str::replaceLast('.'.$this->name,'',$domainname);
}
}