]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/settings.service.ts
update ceph source to reef 18.2.1
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / api / settings.service.ts
1 import { HttpClient } from '@angular/common/http';
2 import { Injectable } from '@angular/core';
3
4 import _ from 'lodash';
5 import { Observable } from 'rxjs';
6 import { map } from 'rxjs/operators';
7
8 class SettingResponse {
9 name: string;
10 default: any;
11 type: string;
12 value: any;
13 }
14
15 @Injectable({
16 providedIn: 'root'
17 })
18 export class SettingsService {
19 constructor(private http: HttpClient) {}
20
21 private settings: { [url: string]: string } = {};
22
23 getValues(names: string | string[]): Observable<{ [key: string]: any }> {
24 if (_.isArray(names)) {
25 names = names.join(',');
26 }
27 return this.http.get(`api/settings?names=${names}`).pipe(
28 map((resp: SettingResponse[]) => {
29 const result = {};
30 _.forEach(resp, (option: SettingResponse) => {
31 _.set(result, option.name, option.value);
32 });
33 return result;
34 })
35 );
36 }
37
38 ifSettingConfigured(url: string, fn: (value?: string) => void, elseFn?: () => void): void {
39 const setting = this.settings[url];
40 if (setting === undefined) {
41 this.http.get(url).subscribe(
42 (data: any) => {
43 this.settings[url] = this.getSettingsValue(data);
44 this.ifSettingConfigured(url, fn, elseFn);
45 },
46 (resp) => {
47 if (resp.status !== 401) {
48 this.settings[url] = '';
49 }
50 }
51 );
52 } else if (setting !== '') {
53 fn(setting);
54 } else {
55 if (elseFn) {
56 elseFn();
57 }
58 }
59 }
60
61 // Easiest way to stop reloading external content that can't be reached
62 disableSetting(url: string) {
63 this.settings[url] = '';
64 }
65
66 private getSettingsValue(data: any): string {
67 return data.value || data.instance || '';
68 }
69
70 validateGrafanaDashboardUrl(uid: string) {
71 return this.http.get(`api/grafana/validation/${uid}`);
72 }
73
74 getStandardSettings(): Observable<{ [key: string]: any }> {
75 return this.http.get('ui-api/standard_settings');
76 }
77 }