]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: add snmp destination validation
authorAvan Thakkar <athakkar@redhat.com>
Fri, 28 Jan 2022 16:38:56 +0000 (22:08 +0530)
committerAvan Thakkar <athakkar@redhat.com>
Thu, 10 Feb 2022 12:55:40 +0000 (18:25 +0530)
Signed-off-by: Avan Thakkar <athakkar@redhat.com>
(cherry picked from commit 81c93a21ff64e6aeeeba2c88db4f61b13352f565)

src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/services/service-form/service-form.component.html
src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/services/service-form/service-form.component.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/services/service-form/service-form.component.ts

index 34b272baea0793cf0b875e8d4fdfdf3e40d1e83c..8e4623be367f1a488742700b08f12a68b1699e33 100644 (file)
               <span class="invalid-feedback"
                     *ngIf="serviceForm.showError('snmp_destination', frm, 'required')"
                     i18n>This field is required.</span>
+              <span class="invalid-feedback"
+                    *ngIf="serviceForm.showError('snmp_destination', frm, 'snmpDestinationPattern')"
+                    i18n>The value does not match the pattern: <strong>hostname:port</strong></span>
             </div>
           </div>
           <!-- Engine id for snmp V3 -->
index 1667f6697b6e514590118ae82f825818a8972f08..3cc921403f9bc30e807f5cd81ce7326da02d9a57 100644 (file)
@@ -484,6 +484,12 @@ x4Ea7kGVgx9kWh5XjWz9wjZvY49UKIT5ppIAWPMbLl3UpfckiuNhTA==
           }
         });
       });
+      it('should submit invalid snmp destination', () => {
+        formHelper.setValue('snmp_version', 'V2c');
+        formHelper.setValue('snmp_destination', '192.168.20.1');
+        formHelper.setValue('snmp_community', 'public');
+        formHelper.expectError('snmp_destination', 'snmpDestinationPattern');
+      });
     });
 
     describe('check edit fields', () => {
index 28bae956298b0dfa6a3a3fd6aedad3288abe45fc..517f05597ad092a037c9fd5dfe68cc1ccb0d3b3c 100644 (file)
@@ -28,6 +28,7 @@ import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
 })
 export class ServiceFormComponent extends CdForm implements OnInit {
   readonly RGW_SVC_ID_PATTERN = /^([^.]+)(\.([^.]+)\.([^.]+))?$/;
+  readonly SNMP_DESTINATION_PATTERN = /^[^\:]+:[0-9]/;
   @ViewChild(NgbTypeahead, { static: false })
   typeahead: NgbTypeahead;
 
@@ -217,12 +218,20 @@ export class ServiceFormComponent extends CdForm implements OnInit {
       snmp_version: [null, [Validators.required]],
       snmp_destination: [
         null,
-        [
-          CdValidators.requiredIf({
-            service_type: 'snmp-gateway',
-            unmanaged: false
-          })
-        ]
+        {
+          validators: [
+            CdValidators.requiredIf({
+              service_type: 'snmp-gateway',
+              unmanaged: false
+            }),
+            CdValidators.custom('snmpDestinationPattern', (value: string) => {
+              if (_.isEmpty(value)) {
+                return false;
+              }
+              return !this.SNMP_DESTINATION_PATTERN.test(value);
+            })
+          ]
+        }
       ],
       engine_id: [
         null,