app/src/commons/component/nic-dropdown.component.ts

94 lines
2.6 KiB
TypeScript
Raw Normal View History

2018-09-10 16:37:42 +09:00
import { Component, Input, OnInit, EventEmitter, Output, ViewChild } from '@angular/core';
2018-08-26 20:09:12 +09:00
2018-09-09 19:20:21 +09:00
import { of } from 'rxjs';
import { catchError, map, take } from 'rxjs/operators';
2018-08-26 20:09:12 +09:00
import { ProbeService } from '../service/probe.service';
2018-08-27 00:15:10 +09:00
import { Interface } from '@overflow/model/net/nic';
2018-09-10 16:37:42 +09:00
import { SelectItem } from 'primeng/primeng';
2018-09-09 19:20:21 +09:00
import { Zone } from '@overflow/model/discovery';
2018-09-13 20:01:01 +09:00
import { toMetaIPType, MetaIPType, MetaIPTypeEnum } from '@overflow/model/meta';
2018-09-10 16:37:42 +09:00
import { DropdownPanelComponent } from '@overflow/commons/ui/component/primeng';
2018-08-17 19:53:40 +09:00
2018-09-13 20:01:01 +09:00
class NICInfo {
iface: string;
friendlyName: string;
metaIPType: MetaIPType;
mac: string;
address: string;
network: string;
}
2018-08-27 10:41:27 +09:00
2018-08-17 19:53:40 +09:00
@Component({
selector: 'app-nic-dropdown',
templateUrl: './nic-dropdown.component.html',
styleUrls: ['./nic-dropdown.component.scss'],
})
2018-08-26 20:09:12 +09:00
export class NicDropdownComponent implements OnInit {
2018-08-18 10:04:59 +09:00
@Input() blockTarget: any;
2018-09-09 19:20:21 +09:00
@Output() select = new EventEmitter<Zone>();
2018-09-10 16:37:42 +09:00
@ViewChild('panel') panel: DropdownPanelComponent;
2018-08-17 19:53:40 +09:00
2018-09-10 16:37:42 +09:00
nics: SelectItem[] = [];
2018-09-13 20:01:01 +09:00
selected: NICInfo;
2018-08-27 10:41:27 +09:00
2018-08-17 19:53:40 +09:00
constructor(
2018-08-26 20:09:12 +09:00
private probeService: ProbeService,
2018-08-17 19:53:40 +09:00
) {
}
2018-08-26 20:09:12 +09:00
ngOnInit(): void {
2018-08-27 00:15:10 +09:00
this.probeService.call<Interface>('MachineService.Interfaces').pipe(
map((ifaces: Interface[]) => {
2018-08-27 10:41:27 +09:00
ifaces.forEach(iface => {
iface.addresses.forEach(address => {
2018-09-12 14:25:32 +09:00
if (address.metaIPType.key !== toMetaIPType(MetaIPTypeEnum.V4).key) {
2018-09-10 16:37:42 +09:00
return;
}
2018-09-13 20:01:01 +09:00
const nicInfo = {
iface: iface.iface,
friendlyName: iface.friendlyName,
metaIPType: address.metaIPType,
mac: iface.mac,
address: address.address,
network: address.network,
};
2018-09-10 16:37:42 +09:00
this.nics.push({
2018-09-13 12:29:12 +09:00
label: iface.friendlyName + ' (' + address.network + ')',
2018-09-13 20:01:01 +09:00
value: nicInfo,
2018-09-12 14:25:32 +09:00
disabled: address.metaIPType.key !== toMetaIPType(MetaIPTypeEnum.V4).key ? true : false,
2018-08-27 10:41:27 +09:00
});
2018-09-13 20:01:01 +09:00
if (address.gateway !== undefined && address.gateway.length > 0) {
this.nicSelected(nicInfo);
}
2018-08-27 10:41:27 +09:00
});
2018-09-13 20:01:01 +09:00
2018-08-27 10:41:27 +09:00
});
2018-08-26 20:09:12 +09:00
}),
catchError(error => {
console.log(error);
return of();
}),
take(1),
).subscribe();
2018-09-10 16:37:42 +09:00
2018-08-26 20:09:12 +09:00
}
2018-09-09 19:20:21 +09:00
2018-09-13 20:01:01 +09:00
nicSelected(nic: NICInfo) {
2018-09-10 16:37:42 +09:00
this.selected = nic;
2018-09-12 14:25:32 +09:00
const zone: Zone = {
network: this.selected.network,
2018-09-13 12:29:12 +09:00
iface: this.selected.iface,
2018-09-09 19:20:21 +09:00
metaIPType: toMetaIPType(MetaIPTypeEnum.V4),
2018-09-12 14:25:32 +09:00
address: this.selected.address,
2018-09-10 16:37:42 +09:00
mac: this.selected.mac,
2018-09-12 14:25:32 +09:00
};
2018-09-09 19:20:21 +09:00
this.select.emit(zone);
2018-09-10 16:37:42 +09:00
this.panel.hide();
2018-09-09 19:20:21 +09:00
}
2018-08-17 19:53:40 +09:00
}