File

projects/cobbler-frontend/src/app/items/network-interface/overview/network-interface-overview.component.ts

Implements

OnInit OnDestroy

Metadata

Index

Properties
Methods

Constructor

constructor(route: ActivatedRoute, userService: UserService, cobblerApiService: CobblerApiService, router: Router, _snackBar: MatSnackBar, dialog: MatDialog)
Parameters :
Name Type Optional
route ActivatedRoute No
userService UserService No
cobblerApiService CobblerApiService No
router Router No
_snackBar MatSnackBar No
dialog MatDialog No

Methods

addNetworkInterface
addNetworkInterface()
Returns : void
deleteInterface
deleteInterface(interfaceName: string)
Parameters :
Name Type Optional
interfaceName string No
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void
renameInterface
renameInterface(name: string)
Parameters :
Name Type Optional
name string No
Returns : void
Private retrieveInterfaces
retrieveInterfaces()
Returns : void
showInterface
showInterface(name: string)
Parameters :
Name Type Optional
name string No
Returns : void

Properties

dataSource
Type : Array<NetworkInterfacePair>
Default value : []
displayedColumns
Type : string[]
Default value : [ 'name', 'mac_address', 'ipv4_address', 'ipv6_address', 'actions', ]
Private ngUnsubscribe
Default value : new Subject<void>()
systemName
Type : string
table
Type : MatTable<System>
Decorators :
@ViewChild(MatTable)
import { Component, Inject, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { MatButtonModule } from '@angular/material/button';
import { MAT_DIALOG_DATA, MatDialog } from '@angular/material/dialog';
import { MatIconModule } from '@angular/material/icon';
import { MatMenuModule } from '@angular/material/menu';
import { MatSnackBar } from '@angular/material/snack-bar';
import { MatTable, MatTableModule } from '@angular/material/table';
import { MatTooltip } from '@angular/material/tooltip';
import { ActivatedRoute, Router } from '@angular/router';
import { CobblerApiService, NetworkInterface, System } from 'cobbler-api';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { DialogConfirmCancelData } from '../../../common/dialog-box-confirm-cancel-edit/dialog-box-confirm-cancel-edit.component';
import { DialogItemRenameComponent } from '../../../common/dialog-item-rename/dialog-item-rename.component';
import { UserService } from '../../../services/user.service';
import Utils from '../../../utils';
import { TemplateCreateComponent } from '../../template/create/template-create.component';
import { NetworkInterfaceCreateComponent } from '../create/network-interface-create.component';

interface NetworkInterfacePair {
  interfaceName: string;
  networkInterface: NetworkInterface;
}

@Component({
  selector: 'cobbler-network-interface-overview',
  standalone: true,
  imports: [
    MatTableModule,
    MatMenuModule,
    MatIconModule,
    MatButtonModule,
    MatTooltip,
  ],
  templateUrl: './network-interface-overview.component.html',
  styleUrl: './network-interface-overview.component.scss',
})
export class NetworkInterfaceOverviewComponent implements OnInit, OnDestroy {
  // Unsubscribe
  private ngUnsubscribe = new Subject<void>();

  // Table
  displayedColumns: string[] = [
    'name',
    'mac_address',
    'ipv4_address',
    'ipv6_address',
    'actions',
  ];
  dataSource: Array<NetworkInterfacePair> = [];
  @ViewChild(MatTable) table: MatTable<System>;
  systemName: string;

  constructor(
    private route: ActivatedRoute,
    private userService: UserService,
    private cobblerApiService: CobblerApiService,
    private router: Router,
    private _snackBar: MatSnackBar,
    @Inject(MatDialog) readonly dialog: MatDialog,
  ) {
    this.systemName = this.route.snapshot.paramMap.get('name');
  }

  ngOnInit(): void {
    this.retrieveInterfaces();
  }

  ngOnDestroy(): void {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }

  private retrieveInterfaces(): void {
    this.cobblerApiService
      .get_system(this.systemName, false, false, this.userService.token)
      .pipe(takeUntil(this.ngUnsubscribe))
      .subscribe((cobblerSystem) => {
        const result = new Array<NetworkInterfacePair>();
        cobblerSystem.interfaces.forEach(
          (networkInterfaceMap, networkInterfaceName) => {
            const networkInterfaceObject = Object.fromEntries(
              networkInterfaceMap,
            ) as NetworkInterface;
            result.push({
              interfaceName: networkInterfaceName,
              networkInterface: networkInterfaceObject,
            });
          },
        );
        this.dataSource = result;
      });
  }

  addNetworkInterface(): void {
    const dialogRef = this.dialog.open(NetworkInterfaceCreateComponent, {
      width: '40%',
      data: { systemName: this.systemName },
    });
    dialogRef.afterClosed().subscribe((result) => {
      if (typeof result === 'string') {
        this.router.navigate([
          '/items',
          'system',
          this.systemName,
          'interface',
          result,
        ]);
      }
    });
  }

  showInterface(name: string): void {
    this.router.navigate([
      '/items',
      'system',
      this.systemName,
      'interface',
      name,
    ]);
  }

  renameInterface(name: string): void {
    const dialogRef = this.dialog.open(DialogItemRenameComponent, {
      data: {
        itemType: 'NetworkInterface',
        itemName: name,
        itemUid: '',
      },
    });

    dialogRef.afterClosed().subscribe((newItemName) => {
      if (newItemName === undefined) {
        // Cancel means we don't need to rename the system
        return;
      }
      this.cobblerApiService
        .get_system_handle(this.systemName, this.userService.token)
        .pipe(takeUntil(this.ngUnsubscribe))
        .subscribe({
          next: (systemHandle) => {
            const interfaceMap = new Map<string, string>();
            interfaceMap.set('interface', name);
            interfaceMap.set('rename_interface', newItemName);
            this.cobblerApiService
              .modify_system(
                systemHandle,
                'rename_interface',
                interfaceMap,
                this.userService.token,
              )
              .pipe(takeUntil(this.ngUnsubscribe))
              .subscribe({
                next: (value) => {
                  this.cobblerApiService
                    .save_system(systemHandle, this.userService.token)
                    .pipe(takeUntil(this.ngUnsubscribe))
                    .subscribe({
                      next: () => {
                        this.retrieveInterfaces();
                      },
                      error: (error) => {
                        // HTML encode the error message since it originates from XML
                        this._snackBar.open(
                          Utils.toHTML(error.message),
                          'Close',
                        );
                      },
                    });
                },
                error: (error) => {
                  // HTML encode the error message since it originates from XML
                  this._snackBar.open(Utils.toHTML(error.message), 'Close');
                },
              });
          },
          error: (error) => {
            // HTML encode the error message since it originates from XML
            this._snackBar.open(Utils.toHTML(error.message), 'Close');
          },
        });
    });
  }

  deleteInterface(interfaceName: string): void {
    this.cobblerApiService
      .get_system_handle(this.systemName, this.userService.token)
      .pipe(takeUntil(this.ngUnsubscribe))
      .subscribe({
        next: (systemHandle) => {
          this.cobblerApiService
            .modify_system(
              systemHandle,
              'delete_interface',
              interfaceName,
              this.userService.token,
            )
            .pipe(takeUntil(this.ngUnsubscribe))
            .subscribe({
              next: (value) => {
                if (value) {
                  this.cobblerApiService
                    .save_system(systemHandle, this.userService.token)
                    .pipe(takeUntil(this.ngUnsubscribe))
                    .subscribe({
                      next: () => {
                        this.retrieveInterfaces();
                      },
                      error: (error) => {
                        // HTML encode the error message since it originates from XML
                        this._snackBar.open(
                          Utils.toHTML(error.message),
                          'Close',
                        );
                      },
                    });
                } else {
                  this._snackBar.open(
                    'Delete failed! Check server logs for more information.',
                    'Close',
                  );
                }
              },
              error: (err) => {
                // HTML encode the error message since it originates from XML
                this._snackBar.open(Utils.toHTML(err.message), 'Close');
              },
            });
        },
        error: (err) => {
          // HTML encode the error message since it originates from XML
          this._snackBar.open(Utils.toHTML(err.message), 'Close');
        },
      });
  }
}
<div class="title-table">
  <div class="title-row">
    <h1 class="title title-cell-text">
      INTERFACES FOR SYSTEM - {{ systemName }}
    </h1>
    <span class="title-cell-button">
      <button
        mat-icon-button
        (click)="this.addNetworkInterface()"
        matTooltip="Add Network Interface"
      >
        <mat-icon>add</mat-icon>
      </button></span
    >
  </div>
</div>

<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
  <!-- Name Column -->
  <ng-container matColumnDef="name">
    <th mat-header-cell *matHeaderCellDef>Name</th>
    <td mat-cell *matCellDef="let element">{{ element.interfaceName }}</td>
  </ng-container>

  <!-- MAC Column -->
  <ng-container matColumnDef="mac_address">
    <th mat-header-cell *matHeaderCellDef>MAC Address</th>
    <td mat-cell *matCellDef="let element">
      {{ element.networkInterface.mac_address }}
    </td>
  </ng-container>

  <!-- IPv4 Address Column -->
  <ng-container matColumnDef="ipv4_address">
    <th mat-header-cell *matHeaderCellDef>IPv4 Address</th>
    <td mat-cell *matCellDef="let element">
      {{ element.networkInterface.ip }}
    </td>
  </ng-container>

  <!-- IPv6 Address Column -->
  <ng-container matColumnDef="ipv6_address">
    <th mat-header-cell *matHeaderCellDef>IPv6 Address</th>
    <td mat-cell *matCellDef="let element">
      {{ element.networkInterface.ipv6_address }}
    </td>
  </ng-container>

  <ng-container matColumnDef="actions">
    <th mat-header-cell *matHeaderCellDef></th>
    <td mat-cell *matCellDef="let element">
      <button mat-icon-button [matMenuTriggerFor]="menu">
        <mat-icon>more_vert</mat-icon>
      </button>
      <mat-menu #menu="matMenu">
        <button mat-menu-item (click)="showInterface(element.interfaceName)">
          <mat-icon>visibility</mat-icon>
          <span>Show details</span>
        </button>
        <button mat-menu-item (click)="renameInterface(element.interfaceName)">
          <mat-icon>edit</mat-icon>
          <span>Rename</span>
        </button>
        <button mat-menu-item (click)="deleteInterface(element.interfaceName)">
          <mat-icon>delete</mat-icon>
          <span>Delete</span>
        </button>
      </mat-menu>
    </td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
</table>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""