]>
Commit | Line | Data |
---|---|---|
f67539c2 | 1 | import { HttpClient, HttpParams } from '@angular/common/http'; |
11fdf7f2 TL |
2 | import { Injectable } from '@angular/core'; |
3 | ||
f67539c2 TL |
4 | import _ from 'lodash'; |
5 | import { Observable, of as observableOf } from 'rxjs'; | |
6 | import { map, mergeMap, toArray } from 'rxjs/operators'; | |
9f95a23c | 7 | |
f67539c2 TL |
8 | import { InventoryDevice } from '~/app/ceph/cluster/inventory/inventory-devices/inventory-device.model'; |
9 | import { InventoryHost } from '~/app/ceph/cluster/inventory/inventory-host.model'; | |
9f95a23c TL |
10 | import { Daemon } from '../models/daemon.interface'; |
11 | import { CdDevice } from '../models/devices'; | |
12 | import { SmartDataResponseV1 } from '../models/smart'; | |
13 | import { DeviceService } from '../services/device.service'; | |
11fdf7f2 TL |
14 | |
15 | @Injectable({ | |
f67539c2 | 16 | providedIn: 'root' |
11fdf7f2 TL |
17 | }) |
18 | export class HostService { | |
9f95a23c | 19 | baseURL = 'api/host'; |
f67539c2 | 20 | baseUIURL = 'ui-api/host'; |
9f95a23c TL |
21 | |
22 | constructor(private http: HttpClient, private deviceService: DeviceService) {} | |
11fdf7f2 | 23 | |
adb31ebb TL |
24 | list(): Observable<object[]> { |
25 | return this.http.get<object[]>(this.baseURL); | |
9f95a23c TL |
26 | } |
27 | ||
b3b6e05e TL |
28 | create(hostname: string, addr: string, labels: string[], status: string) { |
29 | return this.http.post( | |
30 | this.baseURL, | |
31 | { hostname: hostname, addr: addr, labels: labels, status: status }, | |
32 | { observe: 'response', headers: { Accept: 'application/vnd.ceph.api.v0.1+json' } } | |
33 | ); | |
9f95a23c TL |
34 | } |
35 | ||
36 | delete(hostname: string) { | |
37 | return this.http.delete(`${this.baseURL}/${hostname}`, { observe: 'response' }); | |
38 | } | |
39 | ||
40 | getDevices(hostname: string): Observable<CdDevice[]> { | |
11fdf7f2 | 41 | return this.http |
9f95a23c TL |
42 | .get<CdDevice[]>(`${this.baseURL}/${hostname}/devices`) |
43 | .pipe(map((devices) => devices.map((device) => this.deviceService.prepareDevice(device)))); | |
44 | } | |
45 | ||
46 | getSmartData(hostname: string) { | |
47 | return this.http.get<SmartDataResponseV1>(`${this.baseURL}/${hostname}/smart`); | |
48 | } | |
49 | ||
50 | getDaemons(hostname: string): Observable<Daemon[]> { | |
51 | return this.http.get<Daemon[]>(`${this.baseURL}/${hostname}/daemons`); | |
11fdf7f2 | 52 | } |
f6b5b4d7 TL |
53 | |
54 | getLabels(): Observable<string[]> { | |
f67539c2 | 55 | return this.http.get<string[]>(`${this.baseUIURL}/labels`); |
f6b5b4d7 TL |
56 | } |
57 | ||
f67539c2 TL |
58 | update( |
59 | hostname: string, | |
60 | updateLabels = false, | |
61 | labels: string[] = [], | |
62 | maintenance = false, | |
63 | force = false | |
64 | ) { | |
b3b6e05e TL |
65 | return this.http.put( |
66 | `${this.baseURL}/${hostname}`, | |
67 | { | |
68 | update_labels: updateLabels, | |
69 | labels: labels, | |
70 | maintenance: maintenance, | |
71 | force: force | |
72 | }, | |
73 | { headers: { Accept: 'application/vnd.ceph.api.v0.1+json' } } | |
74 | ); | |
f67539c2 TL |
75 | } |
76 | ||
77 | identifyDevice(hostname: string, device: string, duration: number) { | |
78 | return this.http.post(`${this.baseURL}/${hostname}/identify_device`, { | |
79 | device, | |
80 | duration | |
81 | }); | |
82 | } | |
83 | ||
84 | private getInventoryParams(refresh?: boolean): HttpParams { | |
85 | let params = new HttpParams(); | |
86 | if (refresh) { | |
87 | params = params.append('refresh', _.toString(refresh)); | |
88 | } | |
89 | return params; | |
90 | } | |
91 | ||
92 | /** | |
93 | * Get inventory of a host. | |
94 | * | |
95 | * @param hostname the host query. | |
96 | * @param refresh true to ask the Orchestrator to refresh inventory. | |
97 | */ | |
98 | getInventory(hostname: string, refresh?: boolean): Observable<InventoryHost> { | |
99 | const params = this.getInventoryParams(refresh); | |
100 | return this.http.get<InventoryHost>(`${this.baseURL}/${hostname}/inventory`, { | |
101 | params: params | |
102 | }); | |
103 | } | |
104 | ||
105 | /** | |
106 | * Get inventories of all hosts. | |
107 | * | |
108 | * @param refresh true to ask the Orchestrator to refresh inventory. | |
109 | */ | |
110 | inventoryList(refresh?: boolean): Observable<InventoryHost[]> { | |
111 | const params = this.getInventoryParams(refresh); | |
112 | return this.http.get<InventoryHost[]>(`${this.baseUIURL}/inventory`, { params: params }); | |
113 | } | |
114 | ||
115 | /** | |
116 | * Get device list via host inventories. | |
117 | * | |
118 | * @param hostname the host to query. undefined for all hosts. | |
119 | * @param refresh true to ask the Orchestrator to refresh inventory. | |
120 | */ | |
121 | inventoryDeviceList(hostname?: string, refresh?: boolean): Observable<InventoryDevice[]> { | |
122 | let observable; | |
123 | if (hostname) { | |
124 | observable = this.getInventory(hostname, refresh).pipe(toArray()); | |
125 | } else { | |
126 | observable = this.inventoryList(refresh); | |
127 | } | |
128 | return observable.pipe( | |
129 | mergeMap((hosts: InventoryHost[]) => { | |
130 | const devices = _.flatMap(hosts, (host) => { | |
131 | return host.devices.map((device) => { | |
132 | device.hostname = host.name; | |
133 | device.uid = device.device_id ? device.device_id : `${device.hostname}-${device.path}`; | |
134 | return device; | |
135 | }); | |
136 | }); | |
137 | return observableOf(devices); | |
138 | }) | |
139 | ); | |
f6b5b4d7 | 140 | } |
11fdf7f2 | 141 | } |