]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-daemon.service.ts
import ceph quincy 17.2.4
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / api / rgw-daemon.service.ts
1 import { HttpClient, HttpParams } from '@angular/common/http';
2 import { Injectable } from '@angular/core';
3
4 import _ from 'lodash';
5 import { BehaviorSubject, Observable, of, throwError } from 'rxjs';
6 import { mergeMap, take, tap } from 'rxjs/operators';
7
8 import { RgwDaemon } from '~/app/ceph/rgw/models/rgw-daemon';
9 import { cdEncode } from '~/app/shared/decorators/cd-encode';
10
11 @cdEncode
12 @Injectable({
13 providedIn: 'root'
14 })
15 export class RgwDaemonService {
16 private url = 'api/rgw/daemon';
17 private daemons = new BehaviorSubject<RgwDaemon[]>([]);
18 daemons$ = this.daemons.asObservable();
19 private selectedDaemon = new BehaviorSubject<RgwDaemon>(null);
20 selectedDaemon$ = this.selectedDaemon.asObservable();
21
22 constructor(private http: HttpClient) {}
23
24 list(): Observable<RgwDaemon[]> {
25 return this.http.get<RgwDaemon[]>(this.url).pipe(
26 tap((daemons: RgwDaemon[]) => {
27 this.daemons.next(daemons);
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 })) {
32 this.selectDefaultDaemon(daemons);
33 }
34 })
35 );
36 }
37
38 get(id: string) {
39 return this.http.get(`${this.url}/${id}`);
40 }
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) =>
65 // If there is no selected daemon, retrieve daemon list so default daemon will be selected.
66 _.isEmpty(daemon)
67 ? this.list().pipe(
68 mergeMap((daemons) =>
69 _.isEmpty(daemons) ? throwError('No RGW daemons found!') : this.selectedDaemon$
70 )
71 )
72 : of(daemon)
73 ),
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 }
82 }