1 import { HttpClient, HttpParams } from '@angular/common/http';
2 import { Injectable } from '@angular/core';
4 import _ from 'lodash';
5 import { Observable, of as observableOf } from 'rxjs';
6 import { map, mergeMap, toArray } from 'rxjs/operators';
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';
20 export class HostService extends ApiClient {
22 baseUIURL = 'ui-api/host';
24 predefinedLabels = ['mon', 'mgr', 'osd', 'mds', 'rgw', 'nfs', 'iscsi', 'rbd', 'grafana'];
26 constructor(private http: HttpClient, private deviceService: DeviceService) {
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 }
37 create(hostname: string, addr: string, labels: string[], status: string) {
38 return this.http.post(
40 { hostname: hostname, addr: addr, labels: labels, status: status },
41 { observe: 'response', headers: { Accept: CdHelperClass.cdVersionHeader('0', '1') } }
45 delete(hostname: string) {
46 return this.http.delete(`${this.baseURL}/${hostname}`, { observe: 'response' });
49 getDevices(hostname: string): Observable<CdDevice[]> {
51 .get<CdDevice[]>(`${this.baseURL}/${hostname}/devices`)
52 .pipe(map((devices) => devices.map((device) => this.deviceService.prepareDevice(device))));
55 getSmartData(hostname: string) {
56 return this.http.get<SmartDataResponseV1>(`${this.baseURL}/${hostname}/smart`);
59 getDaemons(hostname: string): Observable<Daemon[]> {
60 return this.http.get<Daemon[]>(`${this.baseURL}/${hostname}/daemons`);
63 getLabels(): Observable<string[]> {
64 return this.http.get<string[]>(`${this.baseUIURL}/labels`);
70 labels: string[] = [],
76 `${this.baseURL}/${hostname}`,
78 update_labels: updateLabels,
80 maintenance: maintenance,
84 { headers: { Accept: this.getVersionHeaderValue(0, 1) } }
88 identifyDevice(hostname: string, device: string, duration: number) {
89 return this.http.post(`${this.baseURL}/${hostname}/identify_device`, {
95 private getInventoryParams(refresh?: boolean): HttpParams {
96 let params = new HttpParams();
98 params = params.append('refresh', _.toString(refresh));
104 * Get inventory of a host.
106 * @param hostname the host query.
107 * @param refresh true to ask the Orchestrator to refresh inventory.
109 getInventory(hostname: string, refresh?: boolean): Observable<InventoryHost> {
110 const params = this.getInventoryParams(refresh);
111 return this.http.get<InventoryHost>(`${this.baseURL}/${hostname}/inventory`, {
117 * Get inventories of all hosts.
119 * @param refresh true to ask the Orchestrator to refresh inventory.
121 inventoryList(refresh?: boolean): Observable<InventoryHost[]> {
122 const params = this.getInventoryParams(refresh);
123 return this.http.get<InventoryHost[]>(`${this.baseUIURL}/inventory`, { params: params });
127 * Get device list via host inventories.
129 * @param hostname the host to query. undefined for all hosts.
130 * @param refresh true to ask the Orchestrator to refresh inventory.
132 inventoryDeviceList(hostname?: string, refresh?: boolean): Observable<InventoryDevice[]> {
135 observable = this.getInventory(hostname, refresh).pipe(toArray());
137 observable = this.inventoryList(refresh);
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 ? device.device_id : `${device.hostname}-${device.path}`;
148 return observableOf(devices);