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