From: Stephan Müller Date: Fri, 22 Mar 2019 16:47:21 +0000 (+0100) Subject: mgr/dashboard: Handle unset settings X-Git-Tag: v15.1.0~2271^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=5243190dfb066b63fa8d2306ba9253408cdaf471;p=ceph.git mgr/dashboard: Handle unset settings It's now easy to deal with unset settings. Fixes: https://tracker.ceph.com/issues/36722 Signed-off-by: Stephan Müller --- diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/settings.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/settings.service.spec.ts index faae4237381b..122f2691cfcd 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/settings.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/settings.service.spec.ts @@ -8,6 +8,9 @@ describe('SettingsService', () => { let service: SettingsService; let httpTesting: HttpTestingController; + const exampleUrl = 'api/settings/something'; + const exampleValue = 'http://localhost:3000'; + configureTestBed( { providers: [SettingsService], @@ -60,15 +63,19 @@ describe('SettingsService', () => { }); describe('isSettingConfigured', () => { - const exampleUrl = 'api/settings/something'; - const exampleValue = 'http://localhost:3000'; let increment: number; const testConfig = (url, value) => { - service.ifSettingConfigured(url, (setValue) => { - expect(setValue).toBe(value); - increment++; - }); + service.ifSettingConfigured( + url, + (setValue) => { + expect(setValue).toBe(value); + increment++; + }, + () => { + increment--; + } + ); }; const expectSettingsApiCall = (url: string, value: object, isSet: string) => { @@ -77,7 +84,7 @@ describe('SettingsService', () => { expect(req.request.method).toBe('GET'); req.flush(value); tick(); - expect(increment).toBe(isSet !== '' ? 1 : 0); + expect(increment).toBe(isSet !== '' ? 1 : -1); expect(service['settings'][url]).toBe(isSet); }; @@ -112,4 +119,10 @@ describe('SettingsService', () => { expect(increment).toBe(2); })); }); + + it('should disable a set setting', () => { + service['settings'] = { [exampleUrl]: exampleValue }; + service.disableSetting(exampleUrl); + expect(service['settings']).toEqual({ [exampleUrl]: '' }); + }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/settings.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/settings.service.ts index a722ff6baff9..9403b5769858 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/settings.service.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/settings.service.ts @@ -12,18 +12,34 @@ export class SettingsService { private settings: { [url: string]: string } = {}; - ifSettingConfigured(url: string, fn: (value?: string) => void): void { + ifSettingConfigured(url: string, fn: (value?: string) => void, elseFn?: () => void): void { const setting = this.settings[url]; if (setting === undefined) { - this.http.get(url).subscribe((data: any) => { - this.settings[url] = this.getSettingsValue(data); - this.ifSettingConfigured(url, fn); - }); + this.http.get(url).subscribe( + (data: any) => { + this.settings[url] = this.getSettingsValue(data); + this.ifSettingConfigured(url, fn, elseFn); + }, + (resp) => { + if (resp.status !== 401) { + this.settings[url] = ''; + } + } + ); } else if (setting !== '') { fn(setting); + } else { + if (elseFn) { + elseFn(); + } } } + // Easiest way to stop reloading external content that can't be reached + disableSetting(url) { + this.settings[url] = ''; + } + private getSettingsValue(data: any): string { return data.value || data.instance || ''; }