]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/services/service-form/service-form.component.ts
import ceph quincy 17.2.6
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / cluster / services / service-form / service-form.component.ts
index 4d964575eec1811aa0ad9a574c0448eec95aad0a..3b5cc0c71869a358725437dc1a4032c1ef83c931 100644 (file)
@@ -1,3 +1,4 @@
+import { HttpParams } from '@angular/common/http';
 import { Component, Input, OnInit, ViewChild } from '@angular/core';
 import { AbstractControl, Validators } from '@angular/forms';
 import { ActivatedRoute, Router } from '@angular/router';
@@ -28,8 +29,10 @@ import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
 })
 export class ServiceFormComponent extends CdForm implements OnInit {
   readonly RGW_SVC_ID_PATTERN = /^([^.]+)(\.([^.]+)\.([^.]+))?$/;
+  readonly MDS_SVC_ID_PATTERN = /^[a-zA-Z_.-][a-zA-Z0-9_.-]*$/;
   readonly SNMP_DESTINATION_PATTERN = /^[^\:]+:[0-9]/;
   readonly SNMP_ENGINE_ID_PATTERN = /^[0-9A-Fa-f]{10,64}/g;
+  readonly INGRESS_SUPPORTED_SERVICE_TYPES = ['rgw', 'nfs'];
   @ViewChild(NgbTypeahead, { static: false })
   typeahead: NgbTypeahead;
 
@@ -45,6 +48,7 @@ export class ServiceFormComponent extends CdForm implements OnInit {
   action: string;
   resource: string;
   serviceTypes: string[] = [];
+  serviceIds: string[] = [];
   hosts: any;
   labels: string[];
   labelClick = new Subject<string>();
@@ -52,6 +56,7 @@ export class ServiceFormComponent extends CdForm implements OnInit {
   pools: Array<object>;
   services: Array<CephServiceSpec> = [];
   pageURL: string;
+  serviceList: CephServiceSpec[];
 
   constructor(
     public actionLabels: ActionLabelsI18n,
@@ -83,9 +88,20 @@ export class ServiceFormComponent extends CdForm implements OnInit {
       service_id: [
         null,
         [
-          CdValidators.requiredIf({
-            service_type: 'mds'
-          }),
+          CdValidators.composeIf(
+            {
+              service_type: 'mds'
+            },
+            [
+              Validators.required,
+              CdValidators.custom('mdsPattern', (value: string) => {
+                if (_.isEmpty(value)) {
+                  return false;
+                }
+                return !this.MDS_SVC_ID_PATTERN.test(value);
+              })
+            ]
+          ),
           CdValidators.requiredIf({
             service_type: 'nfs'
           }),
@@ -108,7 +124,10 @@ export class ServiceFormComponent extends CdForm implements OnInit {
                 return !this.RGW_SVC_ID_PATTERN.test(value);
               })
             ]
-          )
+          ),
+          CdValidators.custom('uniqueName', (service_id: string) => {
+            return this.serviceIds && this.serviceIds.includes(service_id);
+          })
         ]
       ],
       placement: ['hosts'],
@@ -129,8 +148,7 @@ export class ServiceFormComponent extends CdForm implements OnInit {
         null,
         [
           CdValidators.requiredIf({
-            service_type: 'iscsi',
-            unmanaged: false
+            service_type: 'iscsi'
           })
         ]
       ],
@@ -162,8 +180,7 @@ export class ServiceFormComponent extends CdForm implements OnInit {
         null,
         [
           CdValidators.requiredIf({
-            service_type: 'ingress',
-            unmanaged: false
+            service_type: 'ingress'
           })
         ]
       ],
@@ -171,13 +188,28 @@ export class ServiceFormComponent extends CdForm implements OnInit {
         null,
         [
           CdValidators.requiredIf({
-            service_type: 'ingress',
-            unmanaged: false
+            service_type: 'ingress'
+          })
+        ]
+      ],
+      frontend_port: [
+        null,
+        [
+          CdValidators.number(false),
+          CdValidators.requiredIf({
+            service_type: 'ingress'
+          })
+        ]
+      ],
+      monitor_port: [
+        null,
+        [
+          CdValidators.number(false),
+          CdValidators.requiredIf({
+            service_type: 'ingress'
           })
         ]
       ],
-      frontend_port: [null, [CdValidators.number(false)]],
-      monitor_port: [null, [CdValidators.number(false)]],
       virtual_interface_networks: [null],
       // RGW, Ingress & iSCSI
       ssl: [false],
@@ -318,6 +350,16 @@ export class ServiceFormComponent extends CdForm implements OnInit {
         this.serviceType = params.type;
       });
     }
+
+    this.cephServiceService
+      .list(new HttpParams({ fromObject: { limit: -1, offset: 0 } }))
+      .observable.subscribe((services: CephServiceSpec[]) => {
+        this.serviceList = services;
+        this.services = services.filter((service: any) =>
+          this.INGRESS_SUPPORTED_SERVICE_TYPES.includes(service.service_type)
+        );
+      });
+
     this.cephServiceService.getKnownTypes().subscribe((resp: Array<string>) => {
       // Remove service types:
       // osd       - This is deployed a different way.
@@ -342,106 +384,113 @@ export class ServiceFormComponent extends CdForm implements OnInit {
     this.poolService.getList().subscribe((resp: Array<object>) => {
       this.pools = resp;
     });
-    this.cephServiceService.list().subscribe((services: CephServiceSpec[]) => {
-      this.services = services.filter((service: any) => service.service_type === 'rgw');
-    });
 
     if (this.editing) {
       this.action = this.actionLabels.EDIT;
       this.disableForEditing(this.serviceType);
-      this.cephServiceService.list(this.serviceName).subscribe((response: CephServiceSpec[]) => {
-        const formKeys = ['service_type', 'service_id', 'unmanaged'];
-        formKeys.forEach((keys) => {
-          this.serviceForm.get(keys).setValue(response[0][keys]);
-        });
-        if (!response[0]['unmanaged']) {
-          const placementKey = Object.keys(response[0]['placement'])[0];
-          let placementValue: string;
-          ['hosts', 'label'].indexOf(placementKey) >= 0
-            ? (placementValue = placementKey)
-            : (placementValue = 'hosts');
-          this.serviceForm.get('placement').setValue(placementValue);
-          this.serviceForm.get('count').setValue(response[0]['placement']['count']);
-          if (response[0]?.placement[placementValue]) {
-            this.serviceForm.get(placementValue).setValue(response[0]?.placement[placementValue]);
-          }
-        }
-        switch (this.serviceType) {
-          case 'iscsi':
-            const specKeys = ['pool', 'api_password', 'api_user', 'trusted_ip_list', 'api_port'];
-            specKeys.forEach((key) => {
-              this.serviceForm.get(key).setValue(response[0].spec[key]);
-            });
-            this.serviceForm.get('ssl').setValue(response[0].spec?.api_secure);
-            if (response[0].spec?.api_secure) {
-              this.serviceForm.get('ssl_cert').setValue(response[0].spec?.ssl_cert);
-              this.serviceForm.get('ssl_key').setValue(response[0].spec?.ssl_key);
+      this.cephServiceService
+        .list(new HttpParams({ fromObject: { limit: -1, offset: 0 } }), this.serviceName)
+        .observable.subscribe((response: CephServiceSpec[]) => {
+          const formKeys = ['service_type', 'service_id', 'unmanaged'];
+          formKeys.forEach((keys) => {
+            this.serviceForm.get(keys).setValue(response[0][keys]);
+          });
+          if (!response[0]['unmanaged']) {
+            const placementKey = Object.keys(response[0]['placement'])[0];
+            let placementValue: string;
+            ['hosts', 'label'].indexOf(placementKey) >= 0
+              ? (placementValue = placementKey)
+              : (placementValue = 'hosts');
+            this.serviceForm.get('placement').setValue(placementValue);
+            this.serviceForm.get('count').setValue(response[0]['placement']['count']);
+            if (response[0]?.placement[placementValue]) {
+              this.serviceForm.get(placementValue).setValue(response[0]?.placement[placementValue]);
             }
-            break;
-          case 'rgw':
-            this.serviceForm.get('rgw_frontend_port').setValue(response[0].spec?.rgw_frontend_port);
-            this.serviceForm.get('ssl').setValue(response[0].spec?.ssl);
-            if (response[0].spec?.ssl) {
+          }
+          switch (this.serviceType) {
+            case 'iscsi':
+              const specKeys = ['pool', 'api_password', 'api_user', 'trusted_ip_list', 'api_port'];
+              specKeys.forEach((key) => {
+                this.serviceForm.get(key).setValue(response[0].spec[key]);
+              });
+              this.serviceForm.get('ssl').setValue(response[0].spec?.api_secure);
+              if (response[0].spec?.api_secure) {
+                this.serviceForm.get('ssl_cert').setValue(response[0].spec?.ssl_cert);
+                this.serviceForm.get('ssl_key').setValue(response[0].spec?.ssl_key);
+              }
+              break;
+            case 'rgw':
               this.serviceForm
-                .get('ssl_cert')
-                .setValue(response[0].spec?.rgw_frontend_ssl_certificate);
-            }
-            break;
-          case 'ingress':
-            const ingressSpecKeys = [
-              'backend_service',
-              'virtual_ip',
-              'frontend_port',
-              'monitor_port',
-              'virtual_interface_networks',
-              'ssl'
-            ];
-            ingressSpecKeys.forEach((key) => {
-              this.serviceForm.get(key).setValue(response[0].spec[key]);
-            });
-            if (response[0].spec?.ssl) {
-              this.serviceForm.get('ssl_cert').setValue(response[0].spec?.ssl_cert);
-              this.serviceForm.get('ssl_key').setValue(response[0].spec?.ssl_key);
-            }
-            break;
-          case 'snmp-gateway':
-            const snmpCommonSpecKeys = ['snmp_version', 'snmp_destination'];
-            snmpCommonSpecKeys.forEach((key) => {
-              this.serviceForm.get(key).setValue(response[0].spec[key]);
-            });
-            if (this.serviceForm.getValue('snmp_version') === 'V3') {
-              const snmpV3SpecKeys = [
-                'engine_id',
-                'auth_protocol',
-                'privacy_protocol',
-                'snmp_v3_auth_username',
-                'snmp_v3_auth_password',
-                'snmp_v3_priv_password'
+                .get('rgw_frontend_port')
+                .setValue(response[0].spec?.rgw_frontend_port);
+              this.serviceForm.get('ssl').setValue(response[0].spec?.ssl);
+              if (response[0].spec?.ssl) {
+                this.serviceForm
+                  .get('ssl_cert')
+                  .setValue(response[0].spec?.rgw_frontend_ssl_certificate);
+              }
+              break;
+            case 'ingress':
+              const ingressSpecKeys = [
+                'backend_service',
+                'virtual_ip',
+                'frontend_port',
+                'monitor_port',
+                'virtual_interface_networks',
+                'ssl'
               ];
-              snmpV3SpecKeys.forEach((key) => {
-                if (key !== null) {
-                  if (
-                    key === 'snmp_v3_auth_username' ||
-                    key === 'snmp_v3_auth_password' ||
-                    key === 'snmp_v3_priv_password'
-                  ) {
-                    this.serviceForm.get(key).setValue(response[0].spec['credentials'][key]);
-                  } else {
-                    this.serviceForm.get(key).setValue(response[0].spec[key]);
-                  }
-                }
+              ingressSpecKeys.forEach((key) => {
+                this.serviceForm.get(key).setValue(response[0].spec[key]);
               });
-            } else {
-              this.serviceForm
-                .get('snmp_community')
-                .setValue(response[0].spec['credentials']['snmp_community']);
-            }
-            break;
-        }
-      });
+              if (response[0].spec?.ssl) {
+                this.serviceForm.get('ssl_cert').setValue(response[0].spec?.ssl_cert);
+                this.serviceForm.get('ssl_key').setValue(response[0].spec?.ssl_key);
+              }
+              break;
+            case 'snmp-gateway':
+              const snmpCommonSpecKeys = ['snmp_version', 'snmp_destination'];
+              snmpCommonSpecKeys.forEach((key) => {
+                this.serviceForm.get(key).setValue(response[0].spec[key]);
+              });
+              if (this.serviceForm.getValue('snmp_version') === 'V3') {
+                const snmpV3SpecKeys = [
+                  'engine_id',
+                  'auth_protocol',
+                  'privacy_protocol',
+                  'snmp_v3_auth_username',
+                  'snmp_v3_auth_password',
+                  'snmp_v3_priv_password'
+                ];
+                snmpV3SpecKeys.forEach((key) => {
+                  if (key !== null) {
+                    if (
+                      key === 'snmp_v3_auth_username' ||
+                      key === 'snmp_v3_auth_password' ||
+                      key === 'snmp_v3_priv_password'
+                    ) {
+                      this.serviceForm.get(key).setValue(response[0].spec['credentials'][key]);
+                    } else {
+                      this.serviceForm.get(key).setValue(response[0].spec[key]);
+                    }
+                  }
+                });
+              } else {
+                this.serviceForm
+                  .get('snmp_community')
+                  .setValue(response[0].spec['credentials']['snmp_community']);
+              }
+              break;
+          }
+        });
     }
   }
 
+  getServiceIds(selectedServiceType: string) {
+    this.serviceIds = this.serviceList
+      ?.filter((service) => service['service_type'] === selectedServiceType)
+      .map((service) => service['service_id']);
+  }
+
   disableForEditing(serviceType: string) {
     const disableForEditKeys = ['service_type', 'service_id'];
     disableForEditKeys.forEach((key) => {
@@ -517,6 +566,47 @@ export class ServiceFormComponent extends CdForm implements OnInit {
       serviceName = `${serviceType}.${serviceId}`;
       serviceSpec['service_id'] = serviceId;
     }
+
+    // These services has some fields to be
+    // filled out even if unmanaged is true
+    switch (serviceType) {
+      case 'ingress':
+        serviceSpec['backend_service'] = values['backend_service'];
+        serviceSpec['service_id'] = values['backend_service'];
+        if (_.isNumber(values['frontend_port']) && values['frontend_port'] > 0) {
+          serviceSpec['frontend_port'] = values['frontend_port'];
+        }
+        if (_.isString(values['virtual_ip']) && !_.isEmpty(values['virtual_ip'])) {
+          serviceSpec['virtual_ip'] = values['virtual_ip'].trim();
+        }
+        if (_.isNumber(values['monitor_port']) && values['monitor_port'] > 0) {
+          serviceSpec['monitor_port'] = values['monitor_port'];
+        }
+        break;
+
+      case 'iscsi':
+        serviceSpec['pool'] = values['pool'];
+        break;
+
+      case 'snmp-gateway':
+        serviceSpec['credentials'] = {};
+        serviceSpec['snmp_version'] = values['snmp_version'];
+        serviceSpec['snmp_destination'] = values['snmp_destination'];
+        if (values['snmp_version'] === 'V3') {
+          serviceSpec['engine_id'] = values['engine_id'];
+          serviceSpec['auth_protocol'] = values['auth_protocol'];
+          serviceSpec['credentials']['snmp_v3_auth_username'] = values['snmp_v3_auth_username'];
+          serviceSpec['credentials']['snmp_v3_auth_password'] = values['snmp_v3_auth_password'];
+          if (values['privacy_protocol'] !== null) {
+            serviceSpec['privacy_protocol'] = values['privacy_protocol'];
+            serviceSpec['credentials']['snmp_v3_priv_password'] = values['snmp_v3_priv_password'];
+          }
+        } else {
+          serviceSpec['credentials']['snmp_community'] = values['snmp_community'];
+        }
+        break;
+    }
+
     if (!values['unmanaged']) {
       switch (values['placement']) {
         case 'hosts':
@@ -542,7 +632,6 @@ export class ServiceFormComponent extends CdForm implements OnInit {
           }
           break;
         case 'iscsi':
-          serviceSpec['pool'] = values['pool'];
           if (_.isString(values['trusted_ip_list']) && !_.isEmpty(values['trusted_ip_list'])) {
             serviceSpec['trusted_ip_list'] = values['trusted_ip_list'].trim();
           }
@@ -558,17 +647,6 @@ export class ServiceFormComponent extends CdForm implements OnInit {
           }
           break;
         case 'ingress':
-          serviceSpec['backend_service'] = values['backend_service'];
-          serviceSpec['service_id'] = values['backend_service'];
-          if (_.isString(values['virtual_ip']) && !_.isEmpty(values['virtual_ip'])) {
-            serviceSpec['virtual_ip'] = values['virtual_ip'].trim();
-          }
-          if (_.isNumber(values['frontend_port']) && values['frontend_port'] > 0) {
-            serviceSpec['frontend_port'] = values['frontend_port'];
-          }
-          if (_.isNumber(values['monitor_port']) && values['monitor_port'] > 0) {
-            serviceSpec['monitor_port'] = values['monitor_port'];
-          }
           serviceSpec['ssl'] = values['ssl'];
           if (values['ssl']) {
             serviceSpec['ssl_cert'] = values['ssl_cert']?.trim();
@@ -576,23 +654,6 @@ export class ServiceFormComponent extends CdForm implements OnInit {
           }
           serviceSpec['virtual_interface_networks'] = values['virtual_interface_networks'];
           break;
-        case 'snmp-gateway':
-          serviceSpec['credentials'] = {};
-          serviceSpec['snmp_version'] = values['snmp_version'];
-          serviceSpec['snmp_destination'] = values['snmp_destination'];
-          if (values['snmp_version'] === 'V3') {
-            serviceSpec['engine_id'] = values['engine_id'];
-            serviceSpec['auth_protocol'] = values['auth_protocol'];
-            serviceSpec['credentials']['snmp_v3_auth_username'] = values['snmp_v3_auth_username'];
-            serviceSpec['credentials']['snmp_v3_auth_password'] = values['snmp_v3_auth_password'];
-            if (values['privacy_protocol'] !== null) {
-              serviceSpec['privacy_protocol'] = values['privacy_protocol'];
-              serviceSpec['credentials']['snmp_v3_priv_password'] = values['snmp_v3_priv_password'];
-            }
-          } else {
-            serviceSpec['credentials']['snmp_community'] = values['snmp_community'];
-          }
-          break;
       }
     }
 
@@ -601,7 +662,9 @@ export class ServiceFormComponent extends CdForm implements OnInit {
         task: new FinishedTask(taskUrl, {
           service_name: serviceName
         }),
-        call: this.cephServiceService.create(serviceSpec)
+        call: this.editing
+          ? this.cephServiceService.update(serviceSpec)
+          : this.cephServiceService.create(serviceSpec)
       })
       .subscribe({
         error() {