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(params: any, facts: string): Observable<object[]> {
31 params = params.set('facts', facts);
33 .get<object[]>(this.baseURL, {
34 headers: { Accept: this.getVersionHeaderValue(1, 2) },
39 map((response: any) => {
40 return response['body'].map((host: any) => {
41 host['headers'] = response.headers;
48 create(hostname: string, addr: string, labels: string[], status: string) {
49 return this.http.post(
51 { hostname: hostname, addr: addr, labels: labels, status: status },
52 { observe: 'response', headers: { Accept: CdHelperClass.cdVersionHeader('0', '1') } }
56 delete(hostname: string) {
57 return this.http.delete(`${this.baseURL}/${hostname}`, { observe: 'response' });
60 getDevices(hostname: string): Observable<CdDevice[]> {
62 .get<CdDevice[]>(`${this.baseURL}/${hostname}/devices`)
63 .pipe(map((devices) => devices.map((device) => this.deviceService.prepareDevice(device))));
66 getSmartData(hostname: string) {
67 return this.http.get<SmartDataResponseV1>(`${this.baseURL}/${hostname}/smart`);
70 getDaemons(hostname: string): Observable<Daemon[]> {
71 return this.http.get<Daemon[]>(`${this.baseURL}/${hostname}/daemons`);
74 getLabels(): Observable<string[]> {
75 return this.http.get<string[]>(`${this.baseUIURL}/labels`);
81 labels: string[] = [],
87 `${this.baseURL}/${hostname}`,
89 update_labels: updateLabels,
91 maintenance: maintenance,
95 { headers: { Accept: this.getVersionHeaderValue(0, 1) } }
99 identifyDevice(hostname: string, device: string, duration: number) {
100 return this.http.post(`${this.baseURL}/${hostname}/identify_device`, {
106 private getInventoryParams(refresh?: boolean): HttpParams {
107 let params = new HttpParams();
109 params = params.append('refresh', _.toString(refresh));
115 * Get inventory of a host.
117 * @param hostname the host query.
118 * @param refresh true to ask the Orchestrator to refresh inventory.
120 getInventory(hostname: string, refresh?: boolean): Observable<InventoryHost> {
121 const params = this.getInventoryParams(refresh);
122 return this.http.get<InventoryHost>(`${this.baseURL}/${hostname}/inventory`, {
128 * Get inventories of all hosts.
130 * @param refresh true to ask the Orchestrator to refresh inventory.
132 inventoryList(refresh?: boolean): Observable<InventoryHost[]> {
133 const params = this.getInventoryParams(refresh);
134 return this.http.get<InventoryHost[]>(`${this.baseUIURL}/inventory`, { params: params });
138 * Get device list via host inventories.
140 * @param hostname the host to query. undefined for all hosts.
141 * @param refresh true to ask the Orchestrator to refresh inventory.
143 inventoryDeviceList(hostname?: string, refresh?: boolean): Observable<InventoryDevice[]> {
146 observable = this.getInventory(hostname, refresh).pipe(toArray());
148 observable = this.inventoryList(refresh);
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}`;
161 return observableOf(devices);