-import { Component, OnInit, ViewChild } from '@angular/core';
+import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { AbstractControl, Validators } from '@angular/forms';
-import { Router } from '@angular/router';
+import { ActivatedRoute, Router } from '@angular/router';
-import { NgbTypeahead } from '@ng-bootstrap/ng-bootstrap';
+import { NgbActiveModal, NgbTypeahead } from '@ng-bootstrap/ng-bootstrap';
import _ from 'lodash';
import { merge, Observable, Subject } from 'rxjs';
import { debounceTime, distinctUntilChanged, filter, map } from 'rxjs/operators';
@ViewChild(NgbTypeahead, { static: false })
typeahead: NgbTypeahead;
+ @Input() hiddenServices: string[] = [];
+
+ @Input() editing = false;
+
+ @Input() serviceName: string;
+
+ @Input() serviceType: string;
+
serviceForm: CdFormGroup;
action: string;
resource: string;
labelFocus = new Subject<string>();
pools: Array<object>;
services: Array<CephServiceSpec> = [];
+ pageURL: string;
constructor(
public actionLabels: ActionLabelsI18n,
private hostService: HostService,
private poolService: PoolService,
private router: Router,
- private taskWrapperService: TaskWrapperService
+ private taskWrapperService: TaskWrapperService,
+ private route: ActivatedRoute,
+ public activeModal: NgbActiveModal
) {
super();
this.resource = $localize`service`;
hosts: [[]],
count: [null, [CdValidators.number(false), Validators.min(1)]],
unmanaged: [false],
- // NFS & iSCSI
+ // iSCSI
pool: [
null,
[
- CdValidators.requiredIf({
- service_type: 'nfs',
- unmanaged: false
- }),
CdValidators.requiredIf({
service_type: 'iscsi',
unmanaged: false
})
]
],
- // NFS
- namespace: [null],
// RGW
rgw_frontend_port: [
null,
ngOnInit(): void {
this.action = this.actionLabels.CREATE;
+ if (this.router.url.includes('services/(modal:create')) {
+ this.pageURL = 'services';
+ } else if (this.router.url.includes('services/(modal:edit')) {
+ this.editing = true;
+ this.pageURL = 'services';
+ this.route.params.subscribe((params: { type: string; name: string }) => {
+ this.serviceName = params.name;
+ this.serviceType = params.type;
+ });
+ }
this.cephServiceService.getKnownTypes().subscribe((resp: Array<string>) => {
// Remove service types:
// osd - This is deployed a different way.
// container - This should only be used in the CLI.
- this.serviceTypes = _.difference(resp, ['container', 'osd']).sort();
+ this.hiddenServices.push('osd', 'container');
+
+ this.serviceTypes = _.difference(resp, this.hiddenServices).sort();
});
- this.hostService.list().subscribe((resp: object[]) => {
+ this.hostService.list('false').subscribe((resp: object[]) => {
const options: SelectOption[] = [];
_.forEach(resp, (host: object) => {
if (_.get(host, 'sources.orchestrator', false)) {
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);
+ }
+ 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) {
+ 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;
+ }
+ });
+ }
}
- goToListView() {
- this.router.navigate(['/services']);
+ disableForEditing(serviceType: string) {
+ const disableForEditKeys = ['service_type', 'service_id'];
+ disableForEditKeys.forEach((key) => {
+ this.serviceForm.get(key).disable();
+ });
+ switch (serviceType) {
+ case 'ingress':
+ this.serviceForm.get('backend_service').disable();
+ }
}
searchLabels = (text$: Observable<string>) => {
onSubmit() {
const self = this;
- const values: object = this.serviceForm.value;
+ const values: object = this.serviceForm.getRawValue();
const serviceType: string = values['service_type'];
+ let taskUrl = `service/${URLVerbs.CREATE}`;
+ if (this.editing) {
+ taskUrl = `service/${URLVerbs.EDIT}`;
+ }
const serviceSpec: object = {
service_type: serviceType,
placement: {},
serviceSpec['placement']['count'] = values['count'];
}
switch (serviceType) {
- case 'nfs':
- serviceSpec['pool'] = values['pool'];
- if (_.isString(values['namespace']) && !_.isEmpty(values['namespace'])) {
- serviceSpec['namespace'] = values['namespace'];
- }
- break;
case 'rgw':
if (_.isNumber(values['rgw_frontend_port']) && values['rgw_frontend_port'] > 0) {
serviceSpec['rgw_frontend_port'] = values['rgw_frontend_port'];
}
serviceSpec['ssl'] = values['ssl'];
if (values['ssl']) {
- serviceSpec['rgw_frontend_ssl_certificate'] = values['ssl_cert'].trim();
+ serviceSpec['rgw_frontend_ssl_certificate'] = values['ssl_cert']?.trim();
}
break;
case 'iscsi':
serviceSpec['api_password'] = values['api_password'];
serviceSpec['api_secure'] = values['ssl'];
if (values['ssl']) {
- serviceSpec['ssl_cert'] = values['ssl_cert'].trim();
- serviceSpec['ssl_key'] = values['ssl_key'].trim();
+ serviceSpec['ssl_cert'] = values['ssl_cert']?.trim();
+ serviceSpec['ssl_key'] = values['ssl_key']?.trim();
}
break;
case 'ingress':
}
serviceSpec['ssl'] = values['ssl'];
if (values['ssl']) {
- serviceSpec['ssl_cert'] = values['ssl_cert'].trim();
- serviceSpec['ssl_key'] = values['ssl_key'].trim();
+ serviceSpec['ssl_cert'] = values['ssl_cert']?.trim();
+ serviceSpec['ssl_key'] = values['ssl_key']?.trim();
}
serviceSpec['virtual_interface_networks'] = values['virtual_interface_networks'];
break;
}
}
+
this.taskWrapperService
.wrapTaskAroundCall({
- task: new FinishedTask(`service/${URLVerbs.CREATE}`, {
+ task: new FinishedTask(taskUrl, {
service_name: serviceName
}),
call: this.cephServiceService.create(serviceSpec)
error() {
self.serviceForm.setErrors({ cdSubmitButton: true });
},
- complete() {
- self.goToListView();
+ complete: () => {
+ this.pageURL === 'services'
+ ? this.router.navigate([this.pageURL, { outlets: { modal: null } }])
+ : this.activeModal.close();
}
});
}