1 import { Component, OnInit } from '@angular/core';
2 import { FormArray, FormControl, Validators } from '@angular/forms';
3 import { ActivatedRoute, Router } from '@angular/router';
5 import { I18n } from '@ngx-translate/i18n-polyfill';
6 import * as _ from 'lodash';
7 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
8 import { forkJoin } from 'rxjs';
10 import { IscsiService } from '../../../shared/api/iscsi.service';
11 import { RbdService } from '../../../shared/api/rbd.service';
12 import { SelectMessages } from '../../../shared/components/select/select-messages.model';
13 import { SelectOption } from '../../../shared/components/select/select-option.model';
14 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
15 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
16 import { CdValidators } from '../../../shared/forms/cd-validators';
17 import { FinishedTask } from '../../../shared/models/finished-task';
18 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
19 import { IscsiTargetImageSettingsModalComponent } from '../iscsi-target-image-settings-modal/iscsi-target-image-settings-modal.component';
20 import { IscsiTargetIqnSettingsModalComponent } from '../iscsi-target-iqn-settings-modal/iscsi-target-iqn-settings-modal.component';
23 selector: 'cd-iscsi-target-form',
24 templateUrl: './iscsi-target-form.component.html',
25 styleUrls: ['./iscsi-target-form.component.scss']
27 export class IscsiTargetFormComponent implements OnInit {
28 targetForm: CdFormGroup;
31 target_default_controls: any;
32 disk_default_controls: any;
34 default_backstore: string;
35 unsupported_rbd_features: any;
36 required_rbd_features: any;
42 imagesSelections: SelectOption[];
43 portalsSelections: SelectOption[] = [];
45 imagesInitiatorSelections: SelectOption[][] = [];
46 groupDiskSelections: SelectOption[][] = [];
47 groupMembersSelections: SelectOption[][] = [];
49 imagesSettings: any = {};
51 portals: new SelectMessages(
52 { noOptions: this.i18n('There are no portals available.') },
55 images: new SelectMessages(
56 { noOptions: this.i18n('There are no images available.') },
59 initiatorImage: new SelectMessages(
62 'There are no images available. Please make sure you add an image to the target.'
67 groupInitiator: new SelectMessages(
70 'There are no initiators available. Please make sure you add an initiator to the target.'
77 IQN_REGEX = /^iqn\.(19|20)\d\d-(0[1-9]|1[0-2])\.\D{2,3}(\.[A-Za-z0-9-]+)+(:[A-Za-z0-9-\.]+)*$/;
78 USER_REGEX = /[\w\.:@_-]{8,64}/;
79 PASSWORD_REGEX = /[\w@\-_\/]{12,16}/;
84 private iscsiService: IscsiService,
85 private modalService: BsModalService,
86 private rbdService: RbdService,
87 private router: Router,
88 private route: ActivatedRoute,
90 private taskWrapper: TaskWrapperService,
91 public actionLabels: ActionLabelsI18n
93 this.resource = this.i18n('target');
97 const promises: any[] = [
98 this.iscsiService.listTargets(),
99 this.rbdService.list(),
100 this.iscsiService.portals(),
101 this.iscsiService.settings()
104 if (this.router.url.startsWith('/block/iscsi/targets/edit')) {
106 this.route.params.subscribe((params: { target_iqn: string }) => {
107 this.target_iqn = decodeURIComponent(params.target_iqn);
108 promises.push(this.iscsiService.getTarget(this.target_iqn));
111 this.action = this.isEdit ? this.actionLabels.EDIT : this.actionLabels.CREATE;
113 forkJoin(promises).subscribe((data: any[]) => {
114 // iscsiService.listTargets
115 const usedImages = _(data[0])
116 .filter((target) => target.target_iqn !== this.target_iqn)
117 .flatMap((target) => target.disks)
118 .map((image) => `${image.pool}/${image.image}`)
121 // iscsiService.settings()
122 this.minimum_gateways = data[3].config.minimum_gateways;
123 this.target_default_controls = data[3].target_default_controls;
124 this.disk_default_controls = data[3].disk_default_controls;
125 this.backstores = data[3].backstores;
126 this.default_backstore = data[3].default_backstore;
127 this.unsupported_rbd_features = data[3].unsupported_rbd_features;
128 this.required_rbd_features = data[3].required_rbd_features;
131 this.imagesAll = _(data[1])
132 .flatMap((pool) => pool.value)
134 const imageId = `${image.pool_name}/${image.name}`;
135 if (usedImages.indexOf(imageId) !== -1) {
138 const validBackstores = this.getValidBackstores(image);
139 if (validBackstores.length === 0) {
146 this.imagesSelections = this.imagesAll.map(
147 (image) => new SelectOption(false, `${image.pool_name}/${image.name}`, '')
150 // iscsiService.portals()
151 const portals: SelectOption[] = [];
152 data[2].forEach((portal) => {
153 portal.ip_addresses.forEach((ip) => {
154 portals.push(new SelectOption(false, portal.name + ':' + ip, ''));
157 this.portalsSelections = [...portals];
161 // iscsiService.getTarget()
163 this.resolveModel(data[4]);
169 this.targetForm = new CdFormGroup({
170 target_iqn: new FormControl('iqn.2001-07.com.ceph:' + Date.now(), {
171 validators: [Validators.required, Validators.pattern(this.IQN_REGEX)]
173 target_controls: new FormControl({}),
174 portals: new FormControl([], {
176 CdValidators.custom('minGateways', (value) => {
177 const gateways = _.uniq(value.map((elem) => elem.split(':')[0]));
178 return gateways.length < Math.max(1, this.minimum_gateways);
182 disks: new FormControl([]),
183 initiators: new FormArray([]),
184 groups: new FormArray([]),
185 acl_enabled: new FormControl(false)
190 this.targetForm.patchValue({
191 target_iqn: res.target_iqn,
192 target_controls: res.target_controls,
193 acl_enabled: res.acl_enabled
197 _.forEach(res.portals, (portal) => {
198 const id = `${portal.host}:${portal.ip}`;
201 this.targetForm.patchValue({
206 _.forEach(res.disks, (disk) => {
207 const id = `${disk.pool}/${disk.image}`;
209 this.imagesSettings[id] = {
210 backstore: disk.backstore
212 this.imagesSettings[id][disk.backstore] = disk.controls;
214 this.onImageSelection({ option: { name: id, selected: true } });
216 this.targetForm.patchValue({
220 _.forEach(res.clients, (client) => {
221 const initiator = this.addInitiator();
222 client.luns = _.map(client.luns, (lun) => `${lun.pool}/${lun.image}`);
223 initiator.patchValue(client);
224 // updatedInitiatorSelector()
227 _.forEach(res.groups, (group) => {
228 const fg = this.addGroup();
229 group.disks = _.map(group.disks, (disk) => `${disk.pool}/${disk.image}`);
230 fg.patchValue(group);
231 _.forEach(group.members, (member) => {
232 this.onGroupMemberSelection({ option: new SelectOption(true, member, '') });
237 hasAdvancedSettings(settings: any) {
238 return Object.values(settings).length > 0;
243 return this.targetForm.get('portals') as FormControl;
246 onPortalSelection() {
247 this.portals.setValue(this.portals.value);
250 removePortal(index: number, portal: string) {
251 this.portalsSelections.forEach((value) => {
252 if (value.name === portal) {
253 value.selected = false;
257 this.portals.value.splice(index, 1);
258 this.portals.setValue(this.portals.value);
264 return this.targetForm.get('disks') as FormControl;
267 removeImage(index: number, image: string) {
268 this.imagesSelections.forEach((value) => {
269 if (value.name === image) {
270 value.selected = false;
273 this.disks.value.splice(index, 1);
274 this.removeImageRefs(image);
278 removeImageRefs(name) {
279 this.initiators.controls.forEach((element) => {
280 const newImages = element.value.luns.filter((item) => item !== name);
281 element.get('luns').setValue(newImages);
284 this.groups.controls.forEach((element) => {
285 const newDisks = element.value.disks.filter((item) => item !== name);
286 element.get('disks').setValue(newDisks);
289 _.forEach(this.imagesInitiatorSelections, (selections, i) => {
290 this.imagesInitiatorSelections[i] = selections.filter((item: any) => item.name !== name);
292 _.forEach(this.groupDiskSelections, (selections, i) => {
293 this.groupDiskSelections[i] = selections.filter((item: any) => item.name !== name);
297 getDefaultBackstore(imageId) {
298 let result = this.default_backstore;
299 const image = this.getImageById(imageId);
300 if (!this.validFeatures(image, this.default_backstore)) {
301 this.backstores.forEach((backstore) => {
302 if (backstore !== this.default_backstore) {
303 if (this.validFeatures(image, backstore)) {
312 onImageSelection($event) {
313 const option = $event.option;
315 if (option.selected) {
316 if (!this.imagesSettings[option.name]) {
317 const defaultBackstore = this.getDefaultBackstore(option.name);
318 this.imagesSettings[option.name] = {
319 backstore: defaultBackstore
321 this.imagesSettings[option.name][defaultBackstore] = {};
324 _.forEach(this.imagesInitiatorSelections, (selections, i) => {
325 selections.push(new SelectOption(false, option.name, ''));
326 this.imagesInitiatorSelections[i] = [...selections];
329 _.forEach(this.groupDiskSelections, (selections, i) => {
330 selections.push(new SelectOption(false, option.name, ''));
331 this.groupDiskSelections[i] = [...selections];
334 this.removeImageRefs(option.name);
340 return this.targetForm.get('initiators') as FormArray;
344 const fg = new CdFormGroup({
345 client_iqn: new FormControl('', {
348 CdValidators.custom('notUnique', (client_iqn) => {
349 const flattened = this.initiators.controls.reduce(function(accumulator, currentValue) {
350 return accumulator.concat(currentValue.value.client_iqn);
353 return flattened.indexOf(client_iqn) !== flattened.lastIndexOf(client_iqn);
355 Validators.pattern(this.IQN_REGEX)
358 auth: new CdFormGroup({
359 user: new FormControl(''),
360 password: new FormControl(''),
361 mutual_user: new FormControl(''),
362 mutual_password: new FormControl('')
364 luns: new FormControl([]),
365 cdIsInGroup: new FormControl(false)
368 CdValidators.validateIf(
370 () => fg.getValue('password') || fg.getValue('mutual_user') || fg.getValue('mutual_password'),
371 [Validators.required],
372 [Validators.pattern(this.USER_REGEX)],
373 [fg.get('password'), fg.get('mutual_user'), fg.get('mutual_password')]
376 CdValidators.validateIf(
378 () => fg.getValue('user') || fg.getValue('mutual_user') || fg.getValue('mutual_password'),
379 [Validators.required],
380 [Validators.pattern(this.PASSWORD_REGEX)],
381 [fg.get('user'), fg.get('mutual_user'), fg.get('mutual_password')]
384 CdValidators.validateIf(
385 fg.get('mutual_user'),
386 () => fg.getValue('mutual_password'),
387 [Validators.required],
388 [Validators.pattern(this.USER_REGEX)],
389 [fg.get('user'), fg.get('password'), fg.get('mutual_password')]
392 CdValidators.validateIf(
393 fg.get('mutual_password'),
394 () => fg.getValue('mutual_user'),
395 [Validators.required],
396 [Validators.pattern(this.PASSWORD_REGEX)],
397 [fg.get('user'), fg.get('password'), fg.get('mutual_user')]
400 this.initiators.push(fg);
402 _.forEach(this.groupMembersSelections, (selections, i) => {
403 selections.push(new SelectOption(false, '', ''));
404 this.groupMembersSelections[i] = [...selections];
408 this.targetForm.getValue('disks'),
409 (disk) => new SelectOption(false, disk, '')
411 this.imagesInitiatorSelections.push(disks);
416 removeInitiator(index) {
417 const removed = this.initiators.value[index];
419 this.initiators.removeAt(index);
421 _.forEach(this.groupMembersSelections, (selections, i) => {
422 selections.splice(index, 1);
423 this.groupMembersSelections[i] = [...selections];
426 this.groups.controls.forEach((element) => {
427 const newMembers = element.value.members.filter((item) => item !== removed.client_iqn);
428 element.get('members').setValue(newMembers);
431 this.imagesInitiatorSelections.splice(index, 1);
434 updatedInitiatorSelector() {
435 // Validate all client_iqn
436 this.initiators.controls.forEach((control) => {
437 control.get('client_iqn').updateValueAndValidity({ emitEvent: false });
440 // Update Group Initiator Selector
441 _.forEach(this.groupMembersSelections, (group, group_index) => {
442 _.forEach(group, (elem, index) => {
443 const oldName = elem.name;
444 elem.name = this.initiators.controls[index].value.client_iqn;
446 this.groups.controls.forEach((element) => {
447 const members = element.value.members;
448 const i = members.indexOf(oldName);
451 members[i] = elem.name;
453 element.get('members').setValue(members);
456 this.groupMembersSelections[group_index] = [...this.groupMembersSelections[group_index]];
460 removeInitiatorImage(initiator: any, lun_index: number, initiator_index: string, image: string) {
461 const luns = initiator.getValue('luns');
462 luns.splice(lun_index, 1);
463 initiator.patchValue({ luns: luns });
465 this.imagesInitiatorSelections[initiator_index].forEach((value) => {
466 if (value.name === image) {
467 value.selected = false;
476 return this.targetForm.get('groups') as FormArray;
480 const fg = new CdFormGroup({
481 group_id: new FormControl('', { validators: [Validators.required] }),
482 members: new FormControl([]),
483 disks: new FormControl([])
486 this.groups.push(fg);
489 this.targetForm.getValue('disks'),
490 (disk) => new SelectOption(false, disk, '')
492 this.groupDiskSelections.push(disks);
494 const initiators = _.map(
495 this.initiators.value,
496 (initiator) => new SelectOption(false, initiator.client_iqn, '', !initiator.cdIsInGroup)
498 this.groupMembersSelections.push(initiators);
504 this.groups.removeAt(index);
505 this.groupDiskSelections.splice(index, 1);
508 onGroupMemberSelection($event) {
509 const option = $event.option;
511 let initiator_index: number;
512 this.initiators.controls.forEach((element, index) => {
513 if (element.value.client_iqn === option.name) {
514 element.patchValue({ luns: [] });
515 element.get('cdIsInGroup').setValue(option.selected);
516 initiator_index = index;
520 // Members can only be at one group at a time, so when a member is selected
521 // in one group we need to disable its selection in other groups
522 _.forEach(this.groupMembersSelections, (group) => {
523 group[initiator_index].enabled = !option.selected;
527 removeGroupInitiator(group, member_index, group_index) {
528 const name = group.getValue('members')[member_index];
529 group.getValue('members').splice(member_index, 1);
531 this.groupMembersSelections[group_index].forEach((value) => {
532 if (value.name === name) {
533 value.selected = false;
536 this.groupMembersSelections[group_index] = [...this.groupMembersSelections[group_index]];
538 this.onGroupMemberSelection({ option: new SelectOption(false, name, '') });
541 removeGroupDisk(group, disk_index, group_index) {
542 const name = group.getValue('disks')[disk_index];
543 group.getValue('disks').splice(disk_index, 1);
545 this.groupDiskSelections[group_index].forEach((value) => {
546 if (value.name === name) {
547 value.selected = false;
550 this.groupDiskSelections[group_index] = [...this.groupDiskSelections[group_index]];
554 const formValue = _.cloneDeep(this.targetForm.value);
557 target_iqn: this.targetForm.getValue('target_iqn'),
558 target_controls: this.targetForm.getValue('target_controls'),
559 acl_enabled: this.targetForm.getValue('acl_enabled'),
567 formValue.disks.forEach((disk) => {
568 const imageSplit = disk.split('/');
569 const backstore = this.imagesSettings[disk].backstore;
572 image: imageSplit[1],
573 backstore: backstore,
574 controls: this.imagesSettings[disk][backstore]
579 formValue.portals.forEach((portal) => {
580 const index = portal.indexOf(':');
581 request.portals.push({
582 host: portal.substring(0, index),
583 ip: portal.substring(index + 1)
588 if (request.acl_enabled) {
589 formValue.initiators.forEach((initiator) => {
590 if (!initiator.auth.user) {
591 initiator.auth.user = '';
593 if (!initiator.auth.password) {
594 initiator.auth.password = '';
596 if (!initiator.auth.mutual_user) {
597 initiator.auth.mutual_user = '';
599 if (!initiator.auth.mutual_password) {
600 initiator.auth.mutual_password = '';
602 delete initiator.cdIsInGroup;
605 initiator.luns.forEach((lun) => {
606 const imageSplit = lun.split('/');
613 initiator.luns = newLuns;
615 request.clients = formValue.initiators;
619 if (request.acl_enabled) {
620 formValue.groups.forEach((group) => {
622 group.disks.forEach((disk) => {
623 const imageSplit = disk.split('/');
630 group.disks = newDisks;
632 request.groups = formValue.groups;
637 request['new_target_iqn'] = request.target_iqn;
638 request.target_iqn = this.target_iqn;
639 wrapTask = this.taskWrapper.wrapTaskAroundCall({
640 task: new FinishedTask('iscsi/target/edit', {
641 target_iqn: request.target_iqn
643 call: this.iscsiService.updateTarget(this.target_iqn, request)
646 wrapTask = this.taskWrapper.wrapTaskAroundCall({
647 task: new FinishedTask('iscsi/target/create', {
648 target_iqn: request.target_iqn
650 call: this.iscsiService.createTarget(request)
657 this.targetForm.setErrors({ cdSubmitButton: true });
659 () => this.router.navigate(['/block/iscsi/targets'])
663 targetSettingsModal() {
664 const initialState = {
665 target_controls: this.targetForm.get('target_controls'),
666 target_default_controls: this.target_default_controls
669 this.modalRef = this.modalService.show(IscsiTargetIqnSettingsModalComponent, { initialState });
672 imageSettingsModal(image) {
673 const initialState = {
674 imagesSettings: this.imagesSettings,
676 disk_default_controls: this.disk_default_controls,
677 backstores: this.getValidBackstores(this.getImageById(image))
680 this.modalRef = this.modalService.show(IscsiTargetImageSettingsModalComponent, {
685 validFeatures(image, backstore) {
686 const imageFeatures = image.features;
687 const requiredFeatures = this.required_rbd_features[backstore];
688 const unsupportedFeatures = this.unsupported_rbd_features[backstore];
689 // tslint:disable-next-line:no-bitwise
690 const validRequiredFeatures = (imageFeatures & requiredFeatures) === requiredFeatures;
691 // tslint:disable-next-line:no-bitwise
692 const validSupportedFeatures = (imageFeatures & unsupportedFeatures) === 0;
693 return validRequiredFeatures && validSupportedFeatures;
696 getImageById(imageId) {
697 return this.imagesAll.find((image) => imageId === `${image.pool_name}/${image.name}`);
700 getValidBackstores(image) {
701 return this.backstores.filter((backstore) => this.validFeatures(image, backstore));