1 import { HttpClient } from '@angular/common/http';
2 import { Injectable } from '@angular/core';
4 import { BehaviorSubject, Observable, Subscription } from 'rxjs';
5 import { filter } from 'rxjs/operators';
7 import { cdEncode, cdEncodeNot } from '../decorators/cd-encode';
8 import { MirroringSummary } from '../models/mirroring-summary';
9 import { TimerService } from '../services/timer.service';
15 export class RbdMirroringService {
16 readonly REFRESH_INTERVAL = 30000;
18 private summaryDataSource = new BehaviorSubject<MirroringSummary>(null);
20 summaryData$ = this.summaryDataSource.asObservable();
22 constructor(private http: HttpClient, private timerService: TimerService) {}
24 startPolling(): Subscription {
25 return this.timerService
26 .get(() => this.retrieveSummaryObservable(), this.REFRESH_INTERVAL)
27 .subscribe(this.retrieveSummaryObserver());
30 refresh(): Subscription {
31 return this.retrieveSummaryObservable().subscribe(this.retrieveSummaryObserver());
34 private retrieveSummaryObservable(): Observable<MirroringSummary> {
35 return this.http.get('api/block/mirroring/summary');
38 private retrieveSummaryObserver(): (data: MirroringSummary) => void {
39 return (data: any) => {
40 this.summaryDataSource.next(data);
45 * Subscribes to the summaryData,
46 * which is updated periodically or when a new task is created.
49 next: (summary: MirroringSummary) => void,
50 error?: (error: any) => void
52 return this.summaryData$.pipe(filter((value) => !!value)).subscribe(next, error);
55 getPool(poolName: string) {
56 return this.http.get(`api/block/mirroring/pool/${poolName}`);
59 updatePool(poolName: string, request: any) {
60 return this.http.put(`api/block/mirroring/pool/${poolName}`, request, { observe: 'response' });
64 return this.http.get(`api/block/mirroring/site_name`);
67 setSiteName(@cdEncodeNot siteName: string) {
69 `api/block/mirroring/site_name`,
70 { site_name: siteName },
71 { observe: 'response' }
75 createBootstrapToken(poolName: string) {
76 return this.http.post(`api/block/mirroring/pool/${poolName}/bootstrap/token`, {});
81 @cdEncodeNot direction: string,
82 @cdEncodeNot token: string
88 return this.http.post(`api/block/mirroring/pool/${poolName}/bootstrap/peer`, request, {
93 getPeer(poolName: string, peerUUID: string) {
94 return this.http.get(`api/block/mirroring/pool/${poolName}/peer/${peerUUID}`);
97 addPeer(poolName: string, request: any) {
98 return this.http.post(`api/block/mirroring/pool/${poolName}/peer`, request, {
103 updatePeer(poolName: string, peerUUID: string, request: any) {
104 return this.http.put(`api/block/mirroring/pool/${poolName}/peer/${peerUUID}`, request, {
109 deletePeer(poolName: string, peerUUID: string) {
110 return this.http.delete(`api/block/mirroring/pool/${poolName}/peer/${peerUUID}`, {