]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/host.service.ts
import ceph 16.2.7
[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 ) {
74 return this.http.put(
75 `${this.baseURL}/${hostname}`,
76 {
77 update_labels: updateLabels,
78 labels: labels,
79 maintenance: maintenance,
80 force: force
81 },
82 { headers: { Accept: this.getVersionHeaderValue(0, 1) } }
83 );
84 }
85
86 identifyDevice(hostname: string, device: string, duration: number) {
87 return this.http.post(`${this.baseURL}/${hostname}/identify_device`, {
88 device,
89 duration
90 });
91 }
92
93 private getInventoryParams(refresh?: boolean): HttpParams {
94 let params = new HttpParams();
95 if (refresh) {
96 params = params.append('refresh', _.toString(refresh));
97 }
98 return params;
99 }
100
101 /**
102 * Get inventory of a host.
103 *
104 * @param hostname the host query.
105 * @param refresh true to ask the Orchestrator to refresh inventory.
106 */
107 getInventory(hostname: string, refresh?: boolean): Observable<InventoryHost> {
108 const params = this.getInventoryParams(refresh);
109 return this.http.get<InventoryHost>(`${this.baseURL}/${hostname}/inventory`, {
110 params: params
111 });
112 }
113
114 /**
115 * Get inventories of all hosts.
116 *
117 * @param refresh true to ask the Orchestrator to refresh inventory.
118 */
119 inventoryList(refresh?: boolean): Observable<InventoryHost[]> {
120 const params = this.getInventoryParams(refresh);
121 return this.http.get<InventoryHost[]>(`${this.baseUIURL}/inventory`, { params: params });
122 }
123
124 /**
125 * Get device list via host inventories.
126 *
127 * @param hostname the host to query. undefined for all hosts.
128 * @param refresh true to ask the Orchestrator to refresh inventory.
129 */
130 inventoryDeviceList(hostname?: string, refresh?: boolean): Observable<InventoryDevice[]> {
131 let observable;
132 if (hostname) {
133 observable = this.getInventory(hostname, refresh).pipe(toArray());
134 } else {
135 observable = this.inventoryList(refresh);
136 }
137 return observable.pipe(
138 mergeMap((hosts: InventoryHost[]) => {
139 const devices = _.flatMap(hosts, (host) => {
140 return host.devices.map((device) => {
141 device.hostname = host.name;
142 device.uid = device.device_id ? device.device_id : `${device.hostname}-${device.path}`;
143 return device;
144 });
145 });
146 return observableOf(devices);
147 })
148 );
149 }
150 }