From 5243190dfb066b63fa8d2306ba9253408cdaf471 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Stephan=20M=C3=BCller?= Date: Fri, 22 Mar 2019 17:47:21 +0100 Subject: [PATCH] mgr/dashboard: Handle unset settings MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit It's now easy to deal with unset settings. Fixes: https://tracker.ceph.com/issues/36722 Signed-off-by: Stephan Müller --- .../app/shared/api/settings.service.spec.ts | 27 ++++++++++++++----- .../src/app/shared/api/settings.service.ts | 26 ++++++++++++++---- 2 files changed, 41 insertions(+), 12 deletions(-) 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 faae4237381b4..122f2691cfcd1 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 a722ff6baff93..9403b57698582 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 || ''; } -- 2.39.5