1 import { HttpClient } from '@angular/common/http';
2 import { Injectable } from '@angular/core';
4 import { BehaviorSubject, Observable, Subscription } from 'rxjs';
6 import { cdEncode, cdEncodeNot } from '../decorators/cd-encode';
7 import { TimerService } from '../services/timer.service';
8 import { ApiModule } from './api.module';
14 export class RbdMirroringService {
15 readonly REFRESH_INTERVAL = 30000;
17 private summaryDataSource = new BehaviorSubject(null);
19 summaryData$ = this.summaryDataSource.asObservable();
21 constructor(private http: HttpClient, private timerService: TimerService) {}
23 startPolling(): Subscription {
24 return this.timerService
25 .get(() => this.retrieveSummaryObservable(), this.REFRESH_INTERVAL)
26 .subscribe(this.retrieveSummaryObserver());
29 refresh(): Subscription {
30 return this.retrieveSummaryObservable().subscribe(this.retrieveSummaryObserver());
33 private retrieveSummaryObservable(): Observable<Object> {
34 return this.http.get('api/block/mirroring/summary');
37 private retrieveSummaryObserver(): (data: any) => void {
38 return (data: any) => {
39 this.summaryDataSource.next(data);
44 * Returns the current value of summaryData
46 getCurrentSummary(): { [key: string]: any; executing_tasks: object[] } {
47 return this.summaryDataSource.getValue();
51 * Subscribes to the summaryData,
52 * which is updated periodically or when a new task is created.
54 subscribeSummary(next: (summary: any) => void, error?: (error: any) => void): Subscription {
55 return this.summaryData$.subscribe(next, error);
58 getPool(poolName: string) {
59 return this.http.get(`api/block/mirroring/pool/${poolName}`);
62 updatePool(poolName: string, request: any) {
63 return this.http.put(`api/block/mirroring/pool/${poolName}`, request, { observe: 'response' });
67 return this.http.get(`api/block/mirroring/site_name`);
70 setSiteName(@cdEncodeNot siteName: string) {
72 `api/block/mirroring/site_name`,
73 { site_name: siteName },
74 { observe: 'response' }
78 createBootstrapToken(poolName: string) {
79 return this.http.post(`api/block/mirroring/pool/${poolName}/bootstrap/token`, {});
84 @cdEncodeNot direction: string,
85 @cdEncodeNot token: string
91 return this.http.post(`api/block/mirroring/pool/${poolName}/bootstrap/peer`, request, {
96 getPeer(poolName: string, peerUUID: string) {
97 return this.http.get(`api/block/mirroring/pool/${poolName}/peer/${peerUUID}`);
100 addPeer(poolName: string, request: any) {
101 return this.http.post(`api/block/mirroring/pool/${poolName}/peer`, request, {
106 updatePeer(poolName: string, peerUUID: string, request: any) {
107 return this.http.put(`api/block/mirroring/pool/${poolName}/peer/${peerUUID}`, request, {
112 deletePeer(poolName: string, peerUUID: string) {
113 return this.http.delete(`api/block/mirroring/pool/${poolName}/peer/${peerUUID}`, {