All files / src/app/ceph/block/iscsi-target-form iscsi-target-form.component.ts

71.75% Statements 221/308
63.41% Branches 26/41
58.06% Functions 54/93
71.58% Lines 204/285

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 6973x 3x 3x   3x 3x 3x 3x   3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x             3x     13x               13x         13x   13x 13x 13x   13x 13x                                                     13x 13x 13x         13x 13x 13x 13x 13x 13x 13x 13x   13x     13x                             13x   13x   13x 13x 13x 13x       13x 13x 13x 13x 13x 13x 13x     13x 26x   26x   13x   13x       13x       13x 13x       13x 13x 26x 39x     13x   13x                 13x 13x               16x 16x                     3x                                                                                                 3x           3x     3x       3x                                 3x                     3x 1x         1x 1x 1x     1x     1x 1x       9x 9x 9x                   9x     10x 10x       9x 9x     9x     9x         9x 4x 4x     1x           60x     7x 7x         20x 13x     20x                             7x               7x               7x               7x               7x   7x 4x 4x     7x   7x   7x   7x     3x 1x   1x   1x 1x 1x     1x 1x 1x     1x     5x   5x 5x       5x 5x 5x 5x   5x 5x 5x         5x     5x       3x                               23x     3x 9x           9x   9x   8x   9x   9x   4x   9x   9x     3x         3x 1x   1x   1x 1x         3x                           3x                       3x 3x                         3x 3x 3x 3x                 3x 6x 6x               2x   2x     2x     2x     2x   2x   2x 2x               2x   2x         2x 2x 2x 2x 2x           2x   2x         1x 1x 1x             2x               3x                 3x                 3x                         3x 35x 35x 35x   35x   35x 35x     3x 9x     13x 26x   3x  
import { Component, OnInit } from '@angular/core';
import { FormArray, FormControl, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
 
import { I18n } from '@ngx-translate/i18n-polyfill';
import * as _ from 'lodash';
import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
import { forkJoin } from 'rxjs';
 
import { IscsiService } from '../../../shared/api/iscsi.service';
import { RbdService } from '../../../shared/api/rbd.service';
import { SelectMessages } from '../../../shared/components/select/select-messages.model';
import { SelectOption } from '../../../shared/components/select/select-option.model';
import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
import { CdFormGroup } from '../../../shared/forms/cd-form-group';
import { CdValidators } from '../../../shared/forms/cd-validators';
import { FinishedTask } from '../../../shared/models/finished-task';
import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
import { IscsiTargetImageSettingsModalComponent } from '../iscsi-target-image-settings-modal/iscsi-target-image-settings-modal.component';
import { IscsiTargetIqnSettingsModalComponent } from '../iscsi-target-iqn-settings-modal/iscsi-target-iqn-settings-modal.component';
 
@Component({
  selector: 'cd-iscsi-target-form',
  template: require('./iscsi-target-form.component.html'),
  styles: []
})
export class IscsiTargetFormComponent implements OnInit {
  targetForm: CdFormGroup;
  modalRef: BsModalRef;
  minimum_gateways = 1;
  target_default_controls: any;
  disk_default_controls: any;
  backstores: string[];
  default_backstore: string;
  supported_rbd_features: any;
  required_rbd_features: any;
 
  isEdit = false;
  target_iqn: string;
 
  imagesAll: any[];
  imagesSelections: SelectOption[];
  portalsSelections: SelectOption[] = [];
 
  imagesInitiatorSelections: SelectOption[][] = [];
  groupDiskSelections: SelectOption[][] = [];
  groupMembersSelections: SelectOption[][] = [];
 
  imagesSettings: any = {};
  messages = {
    portals: new SelectMessages(
      { noOptions: this.i18n('There are no portals available.') },
      this.i18n
    ),
    images: new SelectMessages(
      { noOptions: this.i18n('There are no images available.') },
      this.i18n
    ),
    initiatorImage: new SelectMessages(
      {
        noOptions: this.i18n(
          'There are no images available. Please make sure you add an image to the target.'
        )
      },
      this.i18n
    ),
    groupInitiator: new SelectMessages(
      {
        noOptions: this.i18n(
          'There are no initiators available. Please make sure you add an initiator to the target.'
        )
      },
      this.i18n
    )
  };
 
  IQN_REGEX = /^iqn\.(19|20)\d\d-(0[1-9]|1[0-2])\.\D{2,3}(\.[A-Za-z0-9-]+)+(:[A-Za-z0-9-\.]+)*$/;
  USER_REGEX = /[\w\.:@_-]{8,64}/;
  PASSWORD_REGEX = /[\w@\-_\/]{12,16}/;
  action: string;
  resource: string;
 
  constructor(
    private iscsiService: IscsiService,
    private modalService: BsModalService,
    private rbdService: RbdService,
    private router: Router,
    private route: ActivatedRoute,
    private i18n: I18n,
    private taskWrapper: TaskWrapperService,
    public actionLabels: ActionLabelsI18n
  ) {
    this.resource = this.i18n('target');
  }
 
  ngOnInit() {
    const promises: any[] = [
      this.iscsiService.listTargets(),
      this.rbdService.list(),
      this.iscsiService.portals(),
      this.iscsiService.settings()
    ];
 
    if (this.router.url.startsWith('/block/iscsi/targets/edit')) {
      this.isEdit = true;
      this.route.params.subscribe((params: { target_iqn: string }) => {
        this.target_iqn = decodeURIComponent(params.target_iqn);
        promises.push(this.iscsiService.getTarget(this.target_iqn));
      });
    }
    this.action = this.isEdit ? this.actionLabels.EDIT : this.actionLabels.CREATE;
 
    forkJoin(promises).subscribe((data: any[]) => {
      // iscsiService.listTargets
      const usedImages = _(data[0])
        .filter((target) => target.target_iqn !== this.target_iqn)
        .flatMap((target) => target.disks)
        .map((image) => `${image.pool}/${image.image}`)
        .value();
 
      // iscsiService.settings()
      this.minimum_gateways = data[3].config.minimum_gateways;
      this.target_default_controls = data[3].target_default_controls;
      this.disk_default_controls = data[3].disk_default_controls;
      this.backstores = data[3].backstores;
      this.default_backstore = data[3].default_backstore;
      this.supported_rbd_features = data[3].supported_rbd_features;
      this.required_rbd_features = data[3].required_rbd_features;
 
      // rbdService.list()
      this.imagesAll = _(data[1])
        .flatMap((pool) => pool.value)
        .filter((image) => {
          const imageId = `${image.pool_name}/${image.name}`;
          if (usedImages.indexOf(imageId) !== -1) {
            return false;
          }
          const validBackstores = this.getValidBackstores(image);
          if (validBackstores.length === 0) {
            return false;
          }
          return true;
        })
        .value();
 
      this.imagesSelections = this.imagesAll.map(
        (image) => new SelectOption(false, `${image.pool_name}/${image.name}`, '')
      );
 
      // iscsiService.portals()
      const portals: SelectOption[] = [];
      data[2].forEach((portal) => {
        portal.ip_addresses.forEach((ip) => {
          portals.push(new SelectOption(false, portal.name + ':' + ip, ''));
        });
      });
      this.portalsSelections = [...portals];
 
      this.createForm();
 
      // iscsiService.getTarget()
      if (data[4]) {
        this.resolveModel(data[4]);
      }
    });
  }
 
  createForm() {
    this.targetForm = new CdFormGroup({
      target_iqn: new FormControl('iqn.2001-07.com.ceph:' + Date.now(), {
        validators: [Validators.required, Validators.pattern(this.IQN_REGEX)]
      }),
      target_controls: new FormControl({}),
      portals: new FormControl([], {
        validators: [
          CdValidators.custom('minGateways', (value) => {
            const gateways = _.uniq(value.map((elem) => elem.split(':')[0]));
            return gateways.length < Math.max(1, this.minimum_gateways);
          })
        ]
      }),
      disks: new FormControl([]),
      initiators: new FormArray([]),
      groups: new FormArray([]),
      acl_enabled: new FormControl(false)
    });
  }
 
  resolveModel(res) {
    this.targetForm.patchValue({
      target_iqn: res.target_iqn,
      target_controls: res.target_controls,
      acl_enabled: res.acl_enabled
    });
 
    const portals = [];
    _.forEach(res.portals, (portal) => {
      const id = `${portal.host}:${portal.ip}`;
      portals.push(id);
    });
    this.targetForm.patchValue({
      portals: portals
    });
 
    const disks = [];
    _.forEach(res.disks, (disk) => {
      const id = `${disk.pool}/${disk.image}`;
      disks.push(id);
      this.imagesSettings[id] = {
        backstore: disk.backstore
      };
      this.imagesSettings[id][disk.backstore] = disk.controls;
 
      this.onImageSelection({ option: { name: id, selected: true } });
    });
    this.targetForm.patchValue({
      disks: disks
    });
 
    _.forEach(res.clients, (client) => {
      const initiator = this.addInitiator();
      client.luns = _.map(client.luns, (lun) => `${lun.pool}/${lun.image}`);
      initiator.patchValue(client);
      // updatedInitiatorSelector()
    });
 
    _.forEach(res.groups, (group) => {
      const fg = this.addGroup();
      console.log(group);
      group.disks = _.map(group.disks, (disk) => `${disk.pool}/${disk.image}`);
      fg.patchValue(group);
      _.forEach(group.members, (member) => {
        this.onGroupMemberSelection({ option: new SelectOption(true, member, '') });
      });
    });
  }
 
  hasAdvancedSettings(settings: any) {
    return Object.values(settings).length > 0;
  }
 
  // Portals
  get portals() {
    return this.targetForm.get('portals') as FormControl;
  }
 
  onPortalSelection() {
    this.portals.setValue(this.portals.value);
  }
 
  removePortal(index: number, portal: string) {
    this.portalsSelections.forEach((value) => {
      if (value.name === portal) {
        value.selected = false;
      }
    });
 
    this.portals.value.splice(index, 1);
    this.portals.setValue(this.portals.value);
    return false;
  }
 
  // Images
  get disks() {
    return this.targetForm.get('disks') as FormControl;
  }
 
  removeImage(index: number, image: string) {
    this.imagesSelections.forEach((value) => {
      if (value.name === image) {
        value.selected = false;
      }
    });
    this.disks.value.splice(index, 1);
    this.removeImageRefs(image);
    return false;
  }
 
  removeImageRefs(name) {
    this.initiators.controls.forEach((element) => {
      const newImages = element.value.luns.filter((item) => item !== name);
      element.get('luns').setValue(newImages);
    });
 
    this.groups.controls.forEach((element) => {
      const newDisks = element.value.disks.filter((item) => item !== name);
      element.get('disks').setValue(newDisks);
    });
 
    _.forEach(this.imagesInitiatorSelections, (selections, i) => {
      this.imagesInitiatorSelections[i] = selections.filter((item: any) => item.name !== name);
    });
    _.forEach(this.groupDiskSelections, (selections, i) => {
      this.groupDiskSelections[i] = selections.filter((item: any) => item.name !== name);
    });
  }
 
  getDefaultBackstore(imageId) {
    let result = this.default_backstore;
    const image = this.getImageById(imageId);
    if (!this.validFeatures(image, this.default_backstore)) {
      this.backstores.forEach((backstore) => {
        if (backstore !== this.default_backstore) {
          if (this.validFeatures(image, backstore)) {
            result = backstore;
          }
        }
      });
    }
    return result;
  }
 
  onImageSelection($event) {
    const option = $event.option;
 
    if (option.selected) {
      if (!this.imagesSettings[option.name]) {
        const defaultBackstore = this.getDefaultBackstore(option.name);
        this.imagesSettings[option.name] = {
          backstore: defaultBackstore
        };
        this.imagesSettings[option.name][defaultBackstore] = {};
      }
 
      _.forEach(this.imagesInitiatorSelections, (selections, i) => {
        selections.push(new SelectOption(false, option.name, ''));
        this.imagesInitiatorSelections[i] = [...selections];
      });
 
      _.forEach(this.groupDiskSelections, (selections, i) => {
        selections.push(new SelectOption(false, option.name, ''));
        this.groupDiskSelections[i] = [...selections];
      });
    } else {
      this.removeImageRefs(option.name);
    }
  }
 
  // Initiators
  get initiators() {
    return this.targetForm.get('initiators') as FormArray;
  }
 
  addInitiator() {
    const fg = new CdFormGroup({
      client_iqn: new FormControl('', {
        validators: [
          Validators.required,
          CdValidators.custom('notUnique', (client_iqn) => {
            const flattened = this.initiators.controls.reduce(function(accumulator, currentValue) {
              return accumulator.concat(currentValue.value.client_iqn);
            }, []);
 
            return flattened.indexOf(client_iqn) !== flattened.lastIndexOf(client_iqn);
          }),
          Validators.pattern(this.IQN_REGEX)
        ]
      }),
      auth: new CdFormGroup({
        user: new FormControl(''),
        password: new FormControl(''),
        mutual_user: new FormControl(''),
        mutual_password: new FormControl('')
      }),
      luns: new FormControl([]),
      cdIsInGroup: new FormControl(false)
    });
 
    CdValidators.validateIf(
      fg.get('user'),
      () => fg.getValue('password') || fg.getValue('mutual_user') || fg.getValue('mutual_password'),
      [Validators.required],
      [Validators.pattern(this.USER_REGEX)],
      [fg.get('password'), fg.get('mutual_user'), fg.get('mutual_password')]
    );
 
    CdValidators.validateIf(
      fg.get('password'),
      () => fg.getValue('user') || fg.getValue('mutual_user') || fg.getValue('mutual_password'),
      [Validators.required],
      [Validators.pattern(this.PASSWORD_REGEX)],
      [fg.get('user'), fg.get('mutual_user'), fg.get('mutual_password')]
    );
 
    CdValidators.validateIf(
      fg.get('mutual_user'),
      () => fg.getValue('mutual_password'),
      [Validators.required],
      [Validators.pattern(this.USER_REGEX)],
      [fg.get('user'), fg.get('password'), fg.get('mutual_password')]
    );
 
    CdValidators.validateIf(
      fg.get('mutual_password'),
      () => fg.getValue('mutual_user'),
      [Validators.required],
      [Validators.pattern(this.PASSWORD_REGEX)],
      [fg.get('user'), fg.get('password'), fg.get('mutual_user')]
    );
 
    this.initiators.push(fg);
 
    _.forEach(this.groupMembersSelections, (selections, i) => {
      selections.push(new SelectOption(false, '', ''));
      this.groupMembersSelections[i] = [...selections];
    });
 
    const disks = _.map(
      this.targetForm.getValue('disks'),
      (disk) => new SelectOption(false, disk, '')
    );
    this.imagesInitiatorSelections.push(disks);
 
    return fg;
  }
 
  removeInitiator(index) {
    const removed = this.initiators.value[index];
 
    this.initiators.removeAt(index);
 
    _.forEach(this.groupMembersSelections, (selections, i) => {
      selections.splice(index, 1);
      this.groupMembersSelections[i] = [...selections];
    });
 
    this.groups.controls.forEach((element) => {
      const newMembers = element.value.members.filter((item) => item !== removed.client_iqn);
      element.get('members').setValue(newMembers);
    });
 
    this.imagesInitiatorSelections.splice(index, 1);
  }
 
  updatedInitiatorSelector() {
    // Validate all client_iqn
    this.initiators.controls.forEach((control) => {
      control.get('client_iqn').updateValueAndValidity({ emitEvent: false });
    });
 
    // Update Group Initiator Selector
    _.forEach(this.groupMembersSelections, (group, group_index) => {
      _.forEach(group, (elem, index) => {
        const oldName = elem.name;
        elem.name = this.initiators.controls[index].value.client_iqn;
 
        this.groups.controls.forEach((element) => {
          const members = element.value.members;
          const i = members.indexOf(oldName);
 
          if (i !== -1) {
            members[i] = elem.name;
          }
          element.get('members').setValue(members);
        });
      });
      this.groupMembersSelections[group_index] = [...this.groupMembersSelections[group_index]];
    });
  }
 
  removeInitiatorImage(initiator: any, lun_index: number, initiator_index: string, image: string) {
    const luns = initiator.getValue('luns');
    luns.splice(lun_index, 1);
    initiator.patchValue({ luns: luns });
 
    this.imagesInitiatorSelections[initiator_index].forEach((value) => {
      if (value.name === image) {
        value.selected = false;
      }
    });
 
    return false;
  }
 
  // Groups
  get groups() {
    return this.targetForm.get('groups') as FormArray;
  }
 
  addGroup() {
    const fg = new CdFormGroup({
      group_id: new FormControl('', { validators: [Validators.required] }),
      members: new FormControl([]),
      disks: new FormControl([])
    });
 
    this.groups.push(fg);
 
    const disks = _.map(
      this.targetForm.getValue('disks'),
      (disk) => new SelectOption(false, disk, '')
    );
    this.groupDiskSelections.push(disks);
 
    const initiators = _.map(
      this.initiators.value,
      (initiator) => new SelectOption(false, initiator.client_iqn, '')
    );
    this.groupMembersSelections.push(initiators);
 
    return fg;
  }
 
  removeGroup(index) {
    this.groups.removeAt(index);
    this.groupDiskSelections.splice(index, 1);
  }
 
  onGroupMemberSelection($event) {
    const option = $event.option;
 
    this.initiators.controls.forEach((element) => {
      if (element.value.client_iqn === option.name) {
        element.patchValue({ luns: [] });
        element.get('cdIsInGroup').setValue(option.selected);
      }
    });
  }
 
  removeGroupInitiator(group, member_index, group_index) {
    const name = group.getValue('members')[member_index];
    group.getValue('members').splice(member_index, 1);
 
    this.groupMembersSelections[group_index].forEach((value) => {
      if (value.name === name) {
        value.selected = false;
      }
    });
    this.groupMembersSelections[group_index] = [...this.groupMembersSelections[group_index]];
 
    this.onGroupMemberSelection({ option: new SelectOption(false, name, '') });
  }
 
  removeGroupDisk(group, disk_index, group_index) {
    const name = group.getValue('disks')[disk_index];
    group.getValue('disks').splice(disk_index, 1);
 
    this.groupDiskSelections[group_index].forEach((value) => {
      if (value.name === name) {
        value.selected = false;
      }
    });
    this.groupDiskSelections[group_index] = [...this.groupDiskSelections[group_index]];
  }
 
  submit() {
    const formValue = _.cloneDeep(this.targetForm.value);
 
    const request = {
      target_iqn: this.targetForm.getValue('target_iqn'),
      target_controls: this.targetForm.getValue('target_controls'),
      acl_enabled: this.targetForm.getValue('acl_enabled'),
      portals: [],
      disks: [],
      clients: [],
      groups: []
    };
 
    // Disks
    formValue.disks.forEach((disk) => {
      const imageSplit = disk.split('/');
      const backstore = this.imagesSettings[disk].backstore;
      request.disks.push({
        pool: imageSplit[0],
        image: imageSplit[1],
        backstore: backstore,
        controls: this.imagesSettings[disk][backstore]
      });
    });
 
    // Portals
    formValue.portals.forEach((portal) => {
      const portalSplit = portal.split(':');
      request.portals.push({
        host: portalSplit[0],
        ip: portalSplit[1]
      });
    });
 
    // Clients
    if (request.acl_enabled) {
      formValue.initiators.forEach((initiator) => {
        if (!initiator.auth.user) {
          initiator.auth.user = '';
        }
        if (!initiator.auth.password) {
          initiator.auth.password = '';
        }
        if (!initiator.auth.mutual_user) {
          initiator.auth.mutual_user = '';
        }
        if (!initiator.auth.mutual_password) {
          initiator.auth.mutual_password = '';
        }
        delete initiator.cdIsInGroup;
 
        const newLuns = [];
        initiator.luns.forEach((lun) => {
          const imageSplit = lun.split('/');
          newLuns.push({
            pool: imageSplit[0],
            image: imageSplit[1]
          });
        });
 
        initiator.luns = newLuns;
      });
      request.clients = formValue.initiators;
    }
 
    // Groups
    if (request.acl_enabled) {
      formValue.groups.forEach((group) => {
        const newDisks = [];
        group.disks.forEach((disk) => {
          const imageSplit = disk.split('/');
          newDisks.push({
            pool: imageSplit[0],
            image: imageSplit[1]
          });
        });
 
        group.disks = newDisks;
      });
      request.groups = formValue.groups;
    }
 
    let wrapTask;
    if (this.isEdit) {
      request['new_target_iqn'] = request.target_iqn;
      request.target_iqn = this.target_iqn;
      wrapTask = this.taskWrapper.wrapTaskAroundCall({
        task: new FinishedTask('iscsi/target/edit', {
          target_iqn: request.target_iqn
        }),
        call: this.iscsiService.updateTarget(this.target_iqn, request)
      });
    } else {
      wrapTask = this.taskWrapper.wrapTaskAroundCall({
        task: new FinishedTask('iscsi/target/create', {
          target_iqn: request.target_iqn
        }),
        call: this.iscsiService.createTarget(request)
      });
    }
 
    wrapTask.subscribe(
      undefined,
      () => {
        this.targetForm.setErrors({ cdSubmitButton: true });
      },
      () => this.router.navigate(['/block/iscsi/targets'])
    );
  }
 
  targetSettingsModal() {
    const initialState = {
      target_controls: this.targetForm.get('target_controls'),
      target_default_controls: this.target_default_controls
    };
 
    this.modalRef = this.modalService.show(IscsiTargetIqnSettingsModalComponent, { initialState });
  }
 
  imageSettingsModal(image) {
    const initialState = {
      imagesSettings: this.imagesSettings,
      image: image,
      disk_default_controls: this.disk_default_controls,
      backstores: this.getValidBackstores(this.getImageById(image))
    };
 
    this.modalRef = this.modalService.show(IscsiTargetImageSettingsModalComponent, {
      initialState
    });
  }
 
  validFeatures(image, backstore) {
    const imageFeatures = image.features;
    const requiredFeatures = this.required_rbd_features[backstore];
    const supportedFeatures = this.supported_rbd_features[backstore];
    // tslint:disable-next-line:no-bitwise
    const validRequiredFeatures = (imageFeatures & requiredFeatures) === requiredFeatures;
    // tslint:disable-next-line:no-bitwise
    const validSupportedFeatures = (imageFeatures & supportedFeatures) === imageFeatures;
    return validRequiredFeatures && validSupportedFeatures;
  }
 
  getImageById(imageId) {
    return this.imagesAll.find((image) => imageId === `${image.pool_name}/${image.name}`);
  }
 
  getValidBackstores(image) {
    return this.backstores.filter((backstore) => this.validFeatures(image, backstore));
  }
}