import { Component, Input, OnInit, EventEmitter, Output, ViewChild } from '@angular/core'; import { of } from 'rxjs'; import { catchError, map, take } from 'rxjs/operators'; import { ProbeService } from '../service/probe.service'; import { Interface } from '@overflow/model/net/nic'; import { SelectItem } from 'primeng/primeng'; import { Zone } from '@overflow/model/discovery'; import { toMetaIPType, MetaIPTypeEnum } from '@overflow/model/meta'; import { DropdownPanelComponent } from '@overflow/commons/ui/component/primeng'; @Component({ selector: 'app-nic-dropdown', templateUrl: './nic-dropdown.component.html', styleUrls: ['./nic-dropdown.component.scss'], }) export class NicDropdownComponent implements OnInit { @Input() blockTarget: any; @Output() select = new EventEmitter(); @ViewChild('panel') panel: DropdownPanelComponent; nics: SelectItem[] = []; selected; constructor( private probeService: ProbeService, ) { } ngOnInit(): void { this.probeService.call('MachineService.Interfaces').pipe( map((ifaces: Interface[]) => { ifaces.forEach(iface => { console.log(iface); iface.addresses.forEach(address => { if (address.metaIPType.key != toMetaIPType(MetaIPTypeEnum.V4).key) { return; } this.nics.push({ label: iface.iface + ' (' + address.address + ')', value: { name: iface.iface, metaIPType: address.metaIPType, mac: iface.mac, address: address.address, }, disabled: address.metaIPType.key != toMetaIPType(MetaIPTypeEnum.V4).key ? true : false, }); }); }); if (this.nics.length > 0) { this.nicSelected(this.nics[0].value); } }), catchError(error => { console.log(error); return of(); }), take(1), ).subscribe(); } nicSelected(nic: SelectItem) { this.selected = nic; var zone: Zone = { network: this.selected.address, iface: this.selected.name, metaIPType: toMetaIPType(MetaIPTypeEnum.V4), address: this.selected.address.split('/')[0], mac: this.selected.mac, } this.select.emit(zone); this.panel.hide(); } }