]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/host.service.ts
update ceph source to reef 18.2.1
[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 { ApiClient } from '~/app/shared/api/api-client';
11 import { CdHelperClass } from '~/app/shared/classes/cd-helper.class';
12 import { Daemon } from '../models/daemon.interface';
13 import { CdDevice } from '../models/devices';
14 import { SmartDataResponseV1 } from '../models/smart';
15 import { DeviceService } from '../services/device.service';
16
17 @Injectable({
18 providedIn: 'root'
19 })
20 export class HostService extends ApiClient {
21 baseURL = 'api/host';
22 baseUIURL = 'ui-api/host';
23
24 predefinedLabels = ['mon', 'mgr', 'osd', 'mds', 'rgw', 'nfs', 'iscsi', 'rbd', 'grafana'];
25
26 constructor(private http: HttpClient, private deviceService: DeviceService) {
27 super();
28 }
29
30 list(params: any, facts: string): Observable<object[]> {
31 params = params.set('facts', facts);
32 return this.http
33 .get<object[]>(this.baseURL, {
34 headers: { Accept: this.getVersionHeaderValue(1, 2) },
35 params: params,
36 observe: 'response'
37 })
38 .pipe(
39 map((response: any) => {
40 return response['body'].map((host: any) => {
41 host['headers'] = response.headers;
42 return host;
43 });
44 })
45 );
46 }
47
48 create(hostname: string, addr: string, labels: string[], status: string) {
49 return this.http.post(
50 this.baseURL,
51 { hostname: hostname, addr: addr, labels: labels, status: status },
52 { observe: 'response', headers: { Accept: CdHelperClass.cdVersionHeader('0', '1') } }
53 );
54 }
55
56 delete(hostname: string) {
57 return this.http.delete(`${this.baseURL}/${hostname}`, { observe: 'response' });
58 }
59
60 getDevices(hostname: string): Observable<CdDevice[]> {
61 return this.http
62 .get<CdDevice[]>(`${this.baseURL}/${hostname}/devices`)
63 .pipe(map((devices) => devices.map((device) => this.deviceService.prepareDevice(device))));
64 }
65
66 getSmartData(hostname: string) {
67 return this.http.get<SmartDataResponseV1>(`${this.baseURL}/${hostname}/smart`);
68 }
69
70 getDaemons(hostname: string): Observable<Daemon[]> {
71 return this.http.get<Daemon[]>(`${this.baseURL}/${hostname}/daemons`);
72 }
73
74 getLabels(): Observable<string[]> {
75 return this.http.get<string[]>(`${this.baseUIURL}/labels`);
76 }
77
78 update(
79 hostname: string,
80 updateLabels = false,
81 labels: string[] = [],
82 maintenance = false,
83 force = false,
84 drain = false
85 ) {
86 return this.http.put(
87 `${this.baseURL}/${hostname}`,
88 {
89 update_labels: updateLabels,
90 labels: labels,
91 maintenance: maintenance,
92 force: force,
93 drain: drain
94 },
95 { headers: { Accept: this.getVersionHeaderValue(0, 1) } }
96 );
97 }
98
99 identifyDevice(hostname: string, device: string, duration: number) {
100 return this.http.post(`${this.baseURL}/${hostname}/identify_device`, {
101 device,
102 duration
103 });
104 }
105
106 private getInventoryParams(refresh?: boolean): HttpParams {
107 let params = new HttpParams();
108 if (refresh) {
109 params = params.append('refresh', _.toString(refresh));
110 }
111 return params;
112 }
113
114 /**
115 * Get inventory of a host.
116 *
117 * @param hostname the host query.
118 * @param refresh true to ask the Orchestrator to refresh inventory.
119 */
120 getInventory(hostname: string, refresh?: boolean): Observable<InventoryHost> {
121 const params = this.getInventoryParams(refresh);
122 return this.http.get<InventoryHost>(`${this.baseURL}/${hostname}/inventory`, {
123 params: params
124 });
125 }
126
127 /**
128 * Get inventories of all hosts.
129 *
130 * @param refresh true to ask the Orchestrator to refresh inventory.
131 */
132 inventoryList(refresh?: boolean): Observable<InventoryHost[]> {
133 const params = this.getInventoryParams(refresh);
134 return this.http.get<InventoryHost[]>(`${this.baseUIURL}/inventory`, { params: params });
135 }
136
137 /**
138 * Get device list via host inventories.
139 *
140 * @param hostname the host to query. undefined for all hosts.
141 * @param refresh true to ask the Orchestrator to refresh inventory.
142 */
143 inventoryDeviceList(hostname?: string, refresh?: boolean): Observable<InventoryDevice[]> {
144 let observable;
145 if (hostname) {
146 observable = this.getInventory(hostname, refresh).pipe(toArray());
147 } else {
148 observable = this.inventoryList(refresh);
149 }
150 return observable.pipe(
151 mergeMap((hosts: InventoryHost[]) => {
152 const devices = _.flatMap(hosts, (host) => {
153 return host.devices.map((device) => {
154 device.hostname = host.name;
155 device.uid = device.device_id
156 ? `${device.device_id}-${device.hostname}-${device.path}`
157 : `${device.hostname}-${device.path}`;
158 return device;
159 });
160 });
161 return observableOf(devices);
162 })
163 );
164 }
165 }