]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-ci.git/blob
aa46f503b5a501e9024fa3100854186e0ef08b9d
[ceph-ci.git] /
1 import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
2 import { ActivatedRoute } from '@angular/router';
3 import { forkJoin, of } from 'rxjs';
4 import { catchError, map, switchMap } from 'rxjs/operators';
5 import { NvmeofService } from '~/app/shared/api/nvmeof.service';
6 import {
7   NvmeofSubsystem,
8   NvmeofSubsystemData,
9   NvmeofSubsystemInitiator
10 } from '~/app/shared/models/nvmeof';
11 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
12 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
13
14 import { ICON_TYPE } from '~/app/shared/enum/icons.enum';
15 import { NvmeofSubsystemAuthType } from '~/app/shared/enum/nvmeof.enum';
16
17 @Component({
18   selector: 'cd-nvmeof-gateway-subsystem',
19   templateUrl: './nvmeof-gateway-subsystem.component.html',
20   styleUrls: ['./nvmeof-gateway-subsystem.component.scss'],
21   standalone: false
22 })
23 export class NvmeofGatewaySubsystemComponent implements OnInit {
24   @ViewChild('authTpl', { static: true })
25   authTpl!: TemplateRef<any>;
26
27   groupName!: string;
28
29   columns: CdTableColumn[] = [];
30
31   subsystems: NvmeofSubsystemData[] = [];
32   selection = new CdTableSelection();
33
34   iconType = ICON_TYPE;
35   authType = NvmeofSubsystemAuthType;
36
37   constructor(private nvmeofService: NvmeofService, private route: ActivatedRoute) {}
38
39   ngOnInit(): void {
40     this.columns = [
41       {
42         name: $localize`Subsystem NQN`,
43         prop: 'nqn',
44         flexGrow: 2
45       },
46       {
47         name: $localize`Authentication`,
48         prop: 'auth',
49         flexGrow: 1.5,
50         cellTemplate: this.authTpl
51       },
52       {
53         name: $localize`Hosts (Initiators)`,
54         prop: 'hosts',
55         flexGrow: 1
56       }
57     ];
58
59     this.route.parent?.params.subscribe((params) => {
60       if (params['group']) {
61         this.groupName = params['group'];
62         this.getSubsystemsData();
63       }
64     });
65   }
66
67   getSubsystemsData() {
68     this.nvmeofService
69       .listSubsystems(this.groupName)
70       .pipe(
71         switchMap((subsystems: NvmeofSubsystem[] | NvmeofSubsystem) => {
72           const subs = Array.isArray(subsystems) ? subsystems : [subsystems];
73           if (subs.length === 0) return of([]);
74
75           return forkJoin(
76             subs.map((sub) =>
77               this.nvmeofService.getInitiators(sub.nqn, this.groupName).pipe(
78                 catchError(() => of([])),
79                 map(
80                   (
81                     initiators: NvmeofSubsystemInitiator[] | { hosts?: NvmeofSubsystemInitiator[] }
82                   ) => {
83                     let count = 0;
84                     if (Array.isArray(initiators)) count = initiators.length;
85                     else if (initiators?.hosts && Array.isArray(initiators.hosts)) {
86                       count = initiators.hosts.length;
87                     }
88
89                     let authStatus = NvmeofSubsystemAuthType.NO_AUTH;
90                     if (sub.psk) {
91                       authStatus = NvmeofSubsystemAuthType.BIDIRECTIONAL;
92                     } else if (
93                       initiators &&
94                       'hosts' in initiators &&
95                       Array.isArray(initiators.hosts)
96                     ) {
97                       const hasDhchapKey = initiators.hosts.some(
98                         (host: NvmeofSubsystemInitiator) => !!host.dhchap_key
99                       );
100                       if (hasDhchapKey) {
101                         authStatus = NvmeofSubsystemAuthType.UNIDIRECTIONAL;
102                       }
103                     } else if (Array.isArray(initiators)) {
104                       // Fallback for unexpected structure, though getInitiators usually returns {hosts: []}
105                       const hasDhchapKey = (initiators as NvmeofSubsystemInitiator[]).some(
106                         (host: NvmeofSubsystemInitiator) => !!host.dhchap_key
107                       );
108                       if (hasDhchapKey) {
109                         authStatus = NvmeofSubsystemAuthType.UNIDIRECTIONAL;
110                       }
111                     }
112
113                     return {
114                       ...sub,
115                       auth: authStatus,
116                       hosts: count
117                     };
118                   }
119                 )
120               )
121             )
122           );
123         })
124       )
125       .subscribe({
126         next: (subsystems: NvmeofSubsystemData[]) => {
127           this.subsystems = subsystems;
128         },
129         error: () => {
130           this.subsystems = [];
131         }
132       });
133   }
134
135   updateSelection(selection: CdTableSelection): void {
136     this.selection = selection;
137   }
138 }