1 import { HttpClient, HttpParams } from '@angular/common/http';
2 import { Injectable } from '@angular/core';
4 import _ from 'lodash';
5 import { BehaviorSubject, Observable, of, throwError } from 'rxjs';
6 import { mergeMap, take, tap } from 'rxjs/operators';
8 import { RgwDaemon } from '~/app/ceph/rgw/models/rgw-daemon';
9 import { cdEncode } from '~/app/shared/decorators/cd-encode';
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();
22 constructor(private http: HttpClient) {}
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);
39 return this.http.get(`${this.url}/${id}`);
42 selectDaemon(daemon: RgwDaemon) {
43 this.selectedDaemon.next(daemon);
46 private selectDefaultDaemon(daemons: RgwDaemon[]): RgwDaemon {
47 if (daemons.length === 0) {
51 for (const daemon of daemons) {
53 this.selectDaemon(daemon);
58 this.selectDaemon(daemons[0]);
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.
69 _.isEmpty(daemons) ? throwError('No RGW daemons found!') : this.selectedDaemon$
75 mergeMap((daemon: RgwDaemon) => {
76 let params = new HttpParams();
77 params = params.append('daemon_name', daemon.id);