]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: Handle unset settings
authorStephan Müller <smueller@suse.com>
Fri, 22 Mar 2019 16:47:21 +0000 (17:47 +0100)
committerStephan Müller <smueller@suse.com>
Thu, 4 Jul 2019 14:19:26 +0000 (16:19 +0200)
It's now easy to deal with unset settings.

Fixes: https://tracker.ceph.com/issues/36722
Signed-off-by: Stephan Müller <smueller@suse.com>
src/pybind/mgr/dashboard/frontend/src/app/shared/api/settings.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/api/settings.service.ts

index faae4237381b406d89af6c23037812de7f1181be..122f2691cfcd10ba6afbf4f742df4ad4413869d3 100644 (file)
@@ -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]: '' });
+  });
 });
index a722ff6baff93e888f11b50b084641d0f72090e8..9403b5769858208bd2d390ce4620cdc0f0e406c5 100644 (file)
@@ -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 || '';
   }