]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-daemon.service.ts
import ceph 16.2.6
[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';
6import { mergeMap, retryWhen, take, tap } from 'rxjs/operators';
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);
28 if (_.isEmpty(this.selectedDaemon.getValue())) {
29 this.selectDefaultDaemon(daemons);
30 }
31 })
32 );
11fdf7f2
TL
33 }
34
35 get(id: string) {
36 return this.http.get(`${this.url}/${id}`);
37 }
f67539c2
TL
38
39 selectDaemon(daemon: RgwDaemon) {
40 this.selectedDaemon.next(daemon);
41 }
42
43 private selectDefaultDaemon(daemons: RgwDaemon[]): RgwDaemon {
44 if (daemons.length === 0) {
45 return null;
46 }
47
48 for (const daemon of daemons) {
49 if (daemon.default) {
50 this.selectDaemon(daemon);
51 return daemon;
52 }
53 }
54
55 this.selectDaemon(daemons[0]);
56 return daemons[0];
57 }
58
59 request(next: (params: HttpParams) => Observable<any>) {
60 return this.selectedDaemon.pipe(
61 mergeMap((daemon: RgwDaemon) =>
62 // If there is no selected daemon, retrieve daemon list (default daemon will be selected)
63 // and try again if daemon list is not empty.
64 _.isEmpty(daemon)
65 ? this.list().pipe(mergeMap((daemons) => throwError(!_.isEmpty(daemons))))
66 : of(daemon)
67 ),
68 retryWhen((error) =>
69 error.pipe(
70 mergeMap((hasToRetry) => (hasToRetry ? error : throwError('No RGW daemons found!')))
71 )
72 ),
73 take(1),
74 mergeMap((daemon: RgwDaemon) => {
75 let params = new HttpParams();
76 params = params.append('daemon_name', daemon.id);
77 return next(params);
78 })
79 );
80 }
11fdf7f2 81}