]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/host.service.ts
d13f415275aa32f76ce858f6dc9d1d04e5ffb8b1
[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(facts: string): Observable<object[]> {
31 return this.http.get<object[]>(this.baseURL, {
32 headers: { Accept: 'application/vnd.ceph.api.v1.1+json' },
33 params: { facts: facts }
34 });
35 }
36
37 create(hostname: string, addr: string, labels: string[], status: string) {
38 return this.http.post(
39 this.baseURL,
40 { hostname: hostname, addr: addr, labels: labels, status: status },
41 { observe: 'response', headers: { Accept: CdHelperClass.cdVersionHeader('0', '1') } }
42 );
43 }
44
45 delete(hostname: string) {
46 return this.http.delete(`${this.baseURL}/${hostname}`, { observe: 'response' });
47 }
48
49 getDevices(hostname: string): Observable<CdDevice[]> {
50 return this.http
51 .get<CdDevice[]>(`${this.baseURL}/${hostname}/devices`)
52 .pipe(map((devices) => devices.map((device) => this.deviceService.prepareDevice(device))));
53 }
54
55 getSmartData(hostname: string) {
56 return this.http.get<SmartDataResponseV1>(`${this.baseURL}/${hostname}/smart`);
57 }
58
59 getDaemons(hostname: string): Observable<Daemon[]> {
60 return this.http.get<Daemon[]>(`${this.baseURL}/${hostname}/daemons`);
61 }
62
63 getLabels(): Observable<string[]> {
64 return this.http.get<string[]>(`${this.baseUIURL}/labels`);
65 }
66
67 update(
68 hostname: string,
69 updateLabels = false,
70 labels: string[] = [],
71 maintenance = false,
72 force = false,
73 drain = false
74 ) {
75 return this.http.put(
76 `${this.baseURL}/${hostname}`,
77 {
78 update_labels: updateLabels,
79 labels: labels,
80 maintenance: maintenance,
81 force: force,
82 drain: drain
83 },
84 { headers: { Accept: this.getVersionHeaderValue(0, 1) } }
85 );
86 }
87
88 identifyDevice(hostname: string, device: string, duration: number) {
89 return this.http.post(`${this.baseURL}/${hostname}/identify_device`, {
90 device,
91 duration
92 });
93 }
94
95 private getInventoryParams(refresh?: boolean): HttpParams {
96 let params = new HttpParams();
97 if (refresh) {
98 params = params.append('refresh', _.toString(refresh));
99 }
100 return params;
101 }
102
103 /**
104 * Get inventory of a host.
105 *
106 * @param hostname the host query.
107 * @param refresh true to ask the Orchestrator to refresh inventory.
108 */
109 getInventory(hostname: string, refresh?: boolean): Observable<InventoryHost> {
110 const params = this.getInventoryParams(refresh);
111 return this.http.get<InventoryHost>(`${this.baseURL}/${hostname}/inventory`, {
112 params: params
113 });
114 }
115
116 /**
117 * Get inventories of all hosts.
118 *
119 * @param refresh true to ask the Orchestrator to refresh inventory.
120 */
121 inventoryList(refresh?: boolean): Observable<InventoryHost[]> {
122 const params = this.getInventoryParams(refresh);
123 return this.http.get<InventoryHost[]>(`${this.baseUIURL}/inventory`, { params: params });
124 }
125
126 /**
127 * Get device list via host inventories.
128 *
129 * @param hostname the host to query. undefined for all hosts.
130 * @param refresh true to ask the Orchestrator to refresh inventory.
131 */
132 inventoryDeviceList(hostname?: string, refresh?: boolean): Observable<InventoryDevice[]> {
133 let observable;
134 if (hostname) {
135 observable = this.getInventory(hostname, refresh).pipe(toArray());
136 } else {
137 observable = this.inventoryList(refresh);
138 }
139 return observable.pipe(
140 mergeMap((hosts: InventoryHost[]) => {
141 const devices = _.flatMap(hosts, (host) => {
142 return host.devices.map((device) => {
143 device.hostname = host.name;
144 device.uid = device.device_id
145 ? `${device.device_id}-${device.hostname}-${device.path}`
146 : `${device.hostname}-${device.path}`;
147 return device;
148 });
149 });
150 return observableOf(devices);
151 })
152 );
153 }
154 }