]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-daemon.service.ts
import ceph quincy 17.2.1
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / api / rgw-daemon.service.ts
CommitLineData
f67539c2 1import { HttpClient, HttpParams } from '@angular/common/http';
11fdf7f2
TL
2import { Injectable } from '@angular/core';
3
f67539c2
TL
4import _ from 'lodash';
5import { BehaviorSubject, Observable, of, throwError } from 'rxjs';
a4b75251 6import { mergeMap, take, tap } from 'rxjs/operators';
f67539c2
TL
7
8import { RgwDaemon } from '~/app/ceph/rgw/models/rgw-daemon';
9import { cdEncode } from '~/app/shared/decorators/cd-encode';
11fdf7f2
TL
10
11@cdEncode
12@Injectable({
f67539c2 13 providedIn: 'root'
11fdf7f2
TL
14})
15export class RgwDaemonService {
16 private url = 'api/rgw/daemon';
f67539c2
TL
17 private daemons = new BehaviorSubject<RgwDaemon[]>([]);
18 daemons$ = this.daemons.asObservable();
19 private selectedDaemon = new BehaviorSubject<RgwDaemon>(null);
20 selectedDaemon$ = this.selectedDaemon.asObservable();
11fdf7f2
TL
21
22 constructor(private http: HttpClient) {}
23
f67539c2
TL
24 list(): Observable<RgwDaemon[]> {
25 return this.http.get<RgwDaemon[]>(this.url).pipe(
26 tap((daemons: RgwDaemon[]) => {
27 this.daemons.next(daemons);
33c7a0ef
TL
28 const selectedDaemon = this.selectedDaemon.getValue();
29 // Set or re-select the default daemon if the current one is not
30 // in the list anymore.
31 if (_.isEmpty(selectedDaemon) || undefined === _.find(daemons, { id: selectedDaemon.id })) {
f67539c2
TL
32 this.selectDefaultDaemon(daemons);
33 }
34 })
35 );
11fdf7f2
TL
36 }
37
38 get(id: string) {
39 return this.http.get(`${this.url}/${id}`);
40 }
f67539c2
TL
41
42 selectDaemon(daemon: RgwDaemon) {
43 this.selectedDaemon.next(daemon);
44 }
45
46 private selectDefaultDaemon(daemons: RgwDaemon[]): RgwDaemon {
47 if (daemons.length === 0) {
48 return null;
49 }
50
51 for (const daemon of daemons) {
52 if (daemon.default) {
53 this.selectDaemon(daemon);
54 return daemon;
55 }
56 }
57
58 this.selectDaemon(daemons[0]);
59 return daemons[0];
60 }
61
62 request(next: (params: HttpParams) => Observable<any>) {
63 return this.selectedDaemon.pipe(
64 mergeMap((daemon: RgwDaemon) =>
a4b75251 65 // If there is no selected daemon, retrieve daemon list so default daemon will be selected.
f67539c2 66 _.isEmpty(daemon)
a4b75251
TL
67 ? this.list().pipe(
68 mergeMap((daemons) =>
69 _.isEmpty(daemons) ? throwError('No RGW daemons found!') : this.selectedDaemon$
70 )
71 )
f67539c2
TL
72 : of(daemon)
73 ),
f67539c2
TL
74 take(1),
75 mergeMap((daemon: RgwDaemon) => {
76 let params = new HttpParams();
77 params = params.append('daemon_name', daemon.id);
78 return next(params);
79 })
80 );
81 }
11fdf7f2 82}