2021-04-26 15:34:17 +03:00
|
|
|
import { Component, OnInit, OnDestroy, OnChanges, Input, Output, EventEmitter, SimpleChanges } from '@angular/core';
|
2021-04-07 14:26:28 +03:00
|
|
|
import { ToastrService } from 'ngx-toastr';
|
|
|
|
import { CatalogService } from '../../catalog/helpers/catalog.service';
|
2021-04-26 15:34:17 +03:00
|
|
|
import { empty, forkJoin, Observable, of, Subject, ReplaySubject } from 'rxjs';
|
2021-04-07 14:26:28 +03:00
|
|
|
import { delay, first, map, switchMap, takeUntil, tap } from 'rxjs/operators';
|
|
|
|
import { InstancesService } from '../helpers/instances.service';
|
|
|
|
import { BsModalService } from 'ngx-bootstrap/modal';
|
|
|
|
import { Instance } from '../models/instance';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-instance-info',
|
|
|
|
templateUrl: './instance-info.component.html',
|
|
|
|
styleUrls: ['./instance-info.component.scss']
|
|
|
|
})
|
2021-04-26 15:34:17 +03:00
|
|
|
export class InstanceInfoComponent implements OnInit, OnDestroy, OnChanges
|
2021-04-07 14:26:28 +03:00
|
|
|
{
|
|
|
|
@Input()
|
|
|
|
instance: Instance;
|
|
|
|
|
|
|
|
@Input()
|
2021-04-26 15:34:17 +03:00
|
|
|
loadInfo: boolean;
|
2021-04-07 14:26:28 +03:00
|
|
|
|
|
|
|
@Output()
|
2021-04-26 15:34:17 +03:00
|
|
|
processing = new EventEmitter();
|
|
|
|
|
|
|
|
@Output()
|
|
|
|
finishedProcessing = new EventEmitter();
|
2021-04-07 14:26:28 +03:00
|
|
|
|
|
|
|
@Output()
|
|
|
|
load = new EventEmitter();
|
|
|
|
|
|
|
|
loading: boolean;
|
|
|
|
|
|
|
|
private finishedLoading: boolean;
|
|
|
|
private destroy$ = new Subject();
|
2021-04-26 15:34:17 +03:00
|
|
|
private onChanges$ = new ReplaySubject();
|
2021-04-07 14:26:28 +03:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------------------------------
|
|
|
|
constructor(private readonly instancesService: InstancesService,
|
|
|
|
private readonly catalogService: CatalogService,
|
|
|
|
private readonly modalService: BsModalService,
|
|
|
|
private readonly toastr: ToastrService)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------------------------------
|
|
|
|
toggleDeletionProtection(event, instance: Instance)
|
|
|
|
{
|
2021-04-26 15:34:17 +03:00
|
|
|
this.processing.emit();
|
2021-04-07 14:26:28 +03:00
|
|
|
|
|
|
|
this.instancesService.toggleDeletionProtection(instance.id, event.target.checked)
|
|
|
|
.subscribe(() =>
|
|
|
|
{
|
|
|
|
this.toastr.info(`The deletion protection for machine "${instance.name}" is now ${event.target.checked ? 'enabled' : 'disabled'}`);
|
2021-04-26 15:34:17 +03:00
|
|
|
this.finishedProcessing.emit();
|
2021-04-07 14:26:28 +03:00
|
|
|
},
|
|
|
|
err =>
|
|
|
|
{
|
|
|
|
this.toastr.error(`Machine "${instance.name}" error: ${err.error.message}`);
|
2021-04-26 15:34:17 +03:00
|
|
|
this.finishedProcessing.emit();
|
2021-04-07 14:26:28 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------------------------------
|
|
|
|
dnsCopied(event)
|
|
|
|
{
|
|
|
|
this.toastr.info('The DNS address has been copied to the clipboard');
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------------------------------
|
|
|
|
private getInfo()
|
|
|
|
{
|
|
|
|
if (this.finishedLoading) return;
|
|
|
|
|
|
|
|
this.loading = true;
|
|
|
|
|
|
|
|
this.instancesService.getById(this.instance.id)
|
|
|
|
.subscribe(x =>
|
|
|
|
{
|
|
|
|
const dnsList = {};
|
|
|
|
for (const dns of x.dns_names.sort((a, b) => b.localeCompare(a)))
|
|
|
|
dnsList[dns] = this.getParsedDns(dns);
|
|
|
|
|
|
|
|
this.instance.dnsList = dnsList;
|
|
|
|
|
|
|
|
this.loading = false;
|
|
|
|
this.finishedLoading = true;
|
2021-04-26 15:34:17 +03:00
|
|
|
this.load.emit(dnsList);
|
2021-04-07 14:26:28 +03:00
|
|
|
},
|
|
|
|
err =>
|
|
|
|
{
|
|
|
|
const errorDetails = err.error?.message ? `(${err.error.message})` : '';
|
|
|
|
this.toastr.error(`Couldn't load details for machine "${this.instance.name}" ${errorDetails}`);
|
|
|
|
this.loading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------------------------------
|
|
|
|
private getParsedDns(dnsName: string): string[]
|
|
|
|
{
|
|
|
|
const dns = dnsName.toLowerCase();
|
|
|
|
|
|
|
|
const a = dns.split('.on.spearhead.cloud');
|
|
|
|
const b = a[0].split('.inst.');
|
|
|
|
const c = b[0].split('.');
|
|
|
|
|
|
|
|
return [c[0], c.length > 1 ? c[1] : '', `inst.${b[1]}.on.spearhead.cloud`];
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------------------------------
|
|
|
|
ngOnInit(): void
|
|
|
|
{
|
2021-04-26 15:34:17 +03:00
|
|
|
this.onChanges$.pipe(takeUntil(this.destroy$)).subscribe(() =>
|
|
|
|
{
|
|
|
|
if (!this.finishedLoading && this.loadInfo && !this.instance?.infoLoaded)
|
|
|
|
this.getInfo();
|
|
|
|
});
|
2021-04-07 14:26:28 +03:00
|
|
|
}
|
|
|
|
|
2021-04-26 15:34:17 +03:00
|
|
|
// ----------------------------------------------------------------------------------------------------------------
|
|
|
|
ngOnChanges(changes: SimpleChanges): void
|
|
|
|
{
|
|
|
|
// Since we can't control if ngOnChanges is executed before ngOnInit, we need this trick
|
|
|
|
this.onChanges$.next(changes);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-07 14:26:28 +03:00
|
|
|
// ----------------------------------------------------------------------------------------------------------------
|
|
|
|
ngOnDestroy()
|
|
|
|
{
|
|
|
|
this.destroy$.next();
|
|
|
|
}
|
|
|
|
}
|