summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.ts
blob: 4958382e27982a1a1f06aeaafbbe6a1775e28e78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

import { BehaviorSubject, Observable, Subscription } from 'rxjs';
import { filter } from 'rxjs/operators';

import { cdEncode, cdEncodeNot } from '../decorators/cd-encode';
import { MirroringSummary } from '../models/mirroring-summary';
import { TimerService } from '../services/timer.service';

@cdEncode
@Injectable({
  providedIn: 'root'
})
export class RbdMirroringService {
  readonly REFRESH_INTERVAL = 30000;
  // Observable sources
  private summaryDataSource = new BehaviorSubject<MirroringSummary>(null);
  // Observable streams
  summaryData$ = this.summaryDataSource.asObservable();

  constructor(private http: HttpClient, private timerService: TimerService) {}

  startPolling(): Subscription {
    return this.timerService
      .get(() => this.retrieveSummaryObservable(), this.REFRESH_INTERVAL)
      .subscribe(this.retrieveSummaryObserver());
  }

  refresh(): Subscription {
    return this.retrieveSummaryObservable().subscribe(this.retrieveSummaryObserver());
  }

  private retrieveSummaryObservable(): Observable<MirroringSummary> {
    return this.http.get('api/block/mirroring/summary');
  }

  private retrieveSummaryObserver(): (data: MirroringSummary) => void {
    return (data: any) => {
      this.summaryDataSource.next(data);
    };
  }

  /**
   * Subscribes to the summaryData,
   * which is updated periodically or when a new task is created.
   */
  subscribeSummary(
    next: (summary: MirroringSummary) => void,
    error?: (error: any) => void
  ): Subscription {
    return this.summaryData$.pipe(filter((value) => !!value)).subscribe(next, error);
  }

  getPool(poolName: string) {
    return this.http.get(`api/block/mirroring/pool/${poolName}`);
  }

  updatePool(poolName: string, request: any) {
    return this.http.put(`api/block/mirroring/pool/${poolName}`, request, { observe: 'response' });
  }

  getSiteName() {
    return this.http.get(`api/block/mirroring/site_name`);
  }

  setSiteName(@cdEncodeNot siteName: string) {
    return this.http.put(
      `api/block/mirroring/site_name`,
      { site_name: siteName },
      { observe: 'response' }
    );
  }

  createBootstrapToken(poolName: string) {
    return this.http.post(`api/block/mirroring/pool/${poolName}/bootstrap/token`, {});
  }

  importBootstrapToken(
    poolName: string,
    @cdEncodeNot direction: string,
    @cdEncodeNot token: string
  ) {
    const request = {
      direction: direction,
      token: token
    };
    return this.http.post(`api/block/mirroring/pool/${poolName}/bootstrap/peer`, request, {
      observe: 'response'
    });
  }

  getPeer(poolName: string, peerUUID: string) {
    return this.http.get(`api/block/mirroring/pool/${poolName}/peer/${peerUUID}`);
  }

  addPeer(poolName: string, request: any) {
    return this.http.post(`api/block/mirroring/pool/${poolName}/peer`, request, {
      observe: 'response'
    });
  }

  updatePeer(poolName: string, peerUUID: string, request: any) {
    return this.http.put(`api/block/mirroring/pool/${poolName}/peer/${peerUUID}`, request, {
      observe: 'response'
    });
  }

  deletePeer(poolName: string, peerUUID: string) {
    return this.http.delete(`api/block/mirroring/pool/${poolName}/peer/${peerUUID}`, {
      observe: 'response'
    });
  }
}