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 { Daemon } from '../models/daemon.interface';
11 import { CdDevice } from '../models/devices';
12 import { SmartDataResponseV1 } from '../models/smart';
13 import { DeviceService } from '../services/device.service';
18 export class HostService {
20 baseUIURL = 'ui-api/host';
22 constructor(private http: HttpClient, private deviceService: DeviceService) {}
24 list(): Observable<object[]> {
25 return this.http.get<object[]>(this.baseURL);
28 create(hostname: string) {
29 return this.http.post(this.baseURL, { hostname: hostname }, { observe: 'response' });
32 delete(hostname: string) {
33 return this.http.delete(`${this.baseURL}/${hostname}`, { observe: 'response' });
36 getDevices(hostname: string): Observable<CdDevice[]> {
38 .get<CdDevice[]>(`${this.baseURL}/${hostname}/devices`)
39 .pipe(map((devices) => devices.map((device) => this.deviceService.prepareDevice(device))));
42 getSmartData(hostname: string) {
43 return this.http.get<SmartDataResponseV1>(`${this.baseURL}/${hostname}/smart`);
46 getDaemons(hostname: string): Observable<Daemon[]> {
47 return this.http.get<Daemon[]>(`${this.baseURL}/${hostname}/daemons`);
50 getLabels(): Observable<string[]> {
51 return this.http.get<string[]>(`${this.baseUIURL}/labels`);
57 labels: string[] = [],
61 return this.http.put(`${this.baseURL}/${hostname}`, {
62 update_labels: updateLabels,
64 maintenance: maintenance,
69 identifyDevice(hostname: string, device: string, duration: number) {
70 return this.http.post(`${this.baseURL}/${hostname}/identify_device`, {
76 private getInventoryParams(refresh?: boolean): HttpParams {
77 let params = new HttpParams();
79 params = params.append('refresh', _.toString(refresh));
85 * Get inventory of a host.
87 * @param hostname the host query.
88 * @param refresh true to ask the Orchestrator to refresh inventory.
90 getInventory(hostname: string, refresh?: boolean): Observable<InventoryHost> {
91 const params = this.getInventoryParams(refresh);
92 return this.http.get<InventoryHost>(`${this.baseURL}/${hostname}/inventory`, {
98 * Get inventories of all hosts.
100 * @param refresh true to ask the Orchestrator to refresh inventory.
102 inventoryList(refresh?: boolean): Observable<InventoryHost[]> {
103 const params = this.getInventoryParams(refresh);
104 return this.http.get<InventoryHost[]>(`${this.baseUIURL}/inventory`, { params: params });
108 * Get device list via host inventories.
110 * @param hostname the host to query. undefined for all hosts.
111 * @param refresh true to ask the Orchestrator to refresh inventory.
113 inventoryDeviceList(hostname?: string, refresh?: boolean): Observable<InventoryDevice[]> {
116 observable = this.getInventory(hostname, refresh).pipe(toArray());
118 observable = this.inventoryList(refresh);
120 return observable.pipe(
121 mergeMap((hosts: InventoryHost[]) => {
122 const devices = _.flatMap(hosts, (host) => {
123 return host.devices.map((device) => {
124 device.hostname = host.name;
125 device.uid = device.device_id ? device.device_id : `${device.hostname}-${device.path}`;
129 return observableOf(devices);