1 import { Component, OnInit, ViewChild } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
3 import { Router } from '@angular/router';
5 import _ from 'lodash';
7 import { InventoryDevice } from '~/app/ceph/cluster/inventory/inventory-devices/inventory-device.model';
8 import { HostService } from '~/app/shared/api/host.service';
9 import { OrchestratorService } from '~/app/shared/api/orchestrator.service';
10 import { FormButtonPanelComponent } from '~/app/shared/components/form-button-panel/form-button-panel.component';
11 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
12 import { Icons } from '~/app/shared/enum/icons.enum';
13 import { CdForm } from '~/app/shared/forms/cd-form';
14 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
15 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
16 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
17 import { ModalService } from '~/app/shared/services/modal.service';
18 import { OsdCreationPreviewModalComponent } from '../osd-creation-preview-modal/osd-creation-preview-modal.component';
19 import { DevicesSelectionChangeEvent } from '../osd-devices-selection-groups/devices-selection-change-event.interface';
20 import { DevicesSelectionClearEvent } from '../osd-devices-selection-groups/devices-selection-clear-event.interface';
21 import { OsdDevicesSelectionGroupsComponent } from '../osd-devices-selection-groups/osd-devices-selection-groups.component';
22 import { DriveGroup } from './drive-group.model';
23 import { OsdFeature } from './osd-feature.interface';
26 selector: 'cd-osd-form',
27 templateUrl: './osd-form.component.html',
28 styleUrls: ['./osd-form.component.scss']
30 export class OsdFormComponent extends CdForm implements OnInit {
31 @ViewChild('dataDeviceSelectionGroups')
32 dataDeviceSelectionGroups: OsdDevicesSelectionGroupsComponent;
34 @ViewChild('walDeviceSelectionGroups')
35 walDeviceSelectionGroups: OsdDevicesSelectionGroupsComponent;
37 @ViewChild('dbDeviceSelectionGroups')
38 dbDeviceSelectionGroups: OsdDevicesSelectionGroupsComponent;
40 @ViewChild('previewButtonPanel')
41 previewButtonPanel: FormButtonPanelComponent;
46 columns: Array<CdTableColumn> = [];
48 allDevices: InventoryDevice[] = [];
50 availDevices: InventoryDevice[] = [];
51 dataDeviceFilters: any[] = [];
52 dbDeviceFilters: any[] = [];
53 walDeviceFilters: any[] = [];
55 driveGroup = new DriveGroup();
60 features: { [key: string]: OsdFeature };
61 featureList: OsdFeature[] = [];
63 hasOrchestrator = true;
66 public actionLabels: ActionLabelsI18n,
67 private authStorageService: AuthStorageService,
68 private orchService: OrchestratorService,
69 private hostService: HostService,
70 private router: Router,
71 private modalService: ModalService
74 this.resource = $localize`OSDs`;
75 this.action = this.actionLabels.CREATE;
79 desc: $localize`Encryption`
82 this.featureList = _.map(this.features, (o, key) => Object.assign(o, { key: key }));
87 this.orchService.status().subscribe((status) => {
88 this.hasOrchestrator = status.available;
89 if (status.available) {
90 this.getDataDevices();
96 this.form.get('walSlots').valueChanges.subscribe((value) => this.setSlots('wal', value));
97 this.form.get('dbSlots').valueChanges.subscribe((value) => this.setSlots('db', value));
98 _.each(this.features, (feature) => {
102 .valueChanges.subscribe((value) => this.featureFormUpdate(feature.key, value));
107 this.form = new CdFormGroup({
108 walSlots: new FormControl(0, {
109 validators: [Validators.min(0)]
111 dbSlots: new FormControl(0, {
112 validators: [Validators.min(0)]
114 features: new CdFormGroup(
115 this.featureList.reduce((acc: object, e) => {
116 // disable initially because no data devices are selected
117 acc[e.key] = new FormControl({ value: false, disabled: true });
125 this.hostService.inventoryDeviceList().subscribe(
126 (devices: InventoryDevice[]) => {
127 this.allDevices = _.filter(devices, 'available');
128 this.availDevices = [...this.allDevices];
132 this.allDevices = [];
133 this.availDevices = [];
139 setSlots(type: string, slots: number) {
140 if (typeof slots !== 'number') {
144 this.driveGroup.setSlots(type, slots);
148 featureFormUpdate(key: string, checked: boolean) {
149 this.driveGroup.setFeature(key, checked);
153 this.featureList.forEach((feature) => {
154 this.form.get(feature.key).enable({ emitEvent: false });
159 this.featureList.forEach((feature) => {
160 const control = this.form.get(feature.key);
161 control.disable({ emitEvent: false });
162 control.setValue(false, { emitEvent: false });
166 onDevicesSelected(event: DevicesSelectionChangeEvent) {
167 this.availDevices = event.dataOut;
169 if (event.type === 'data') {
170 // If user selects data devices for a single host, make only remaining devices on
171 // that host as available.
172 const hostnameFilter = _.find(event.filters, { prop: 'hostname' });
173 if (hostnameFilter) {
174 this.hostname = hostnameFilter.value.raw;
175 this.availDevices = event.dataOut.filter((device: InventoryDevice) => {
176 return device.hostname === this.hostname;
178 this.driveGroup.setHostPattern(this.hostname);
180 this.driveGroup.setHostPattern('*');
182 this.enableFeatures();
184 this.driveGroup.setDeviceSelection(event.type, event.filters);
187 onDevicesCleared(event: DevicesSelectionClearEvent) {
188 if (event.type === 'data') {
189 this.availDevices = [...this.allDevices];
190 this.walDeviceSelectionGroups.devices = [];
191 this.dbDeviceSelectionGroups.devices = [];
192 this.disableFeatures();
193 this.driveGroup.reset();
194 this.form.get('walSlots').setValue(0, { emitEvent: false });
195 this.form.get('dbSlots').setValue(0, { emitEvent: false });
197 this.availDevices = [...this.availDevices, ...event.clearedDevices];
198 this.driveGroup.clearDeviceSelection(event.type);
199 const slotControlName = `${event.type}Slots`;
200 this.form.get(slotControlName).setValue(0, { emitEvent: false });
205 // use user name and timestamp for drive group name
206 const user = this.authStorageService.getUsername();
207 this.driveGroup.setName(`dashboard-${user}-${_.now()}`);
208 const modalRef = this.modalService.show(OsdCreationPreviewModalComponent, {
209 driveGroups: [this.driveGroup.spec]
211 modalRef.componentInstance.submitAction.subscribe(() => {
212 this.router.navigate(['/osd']);
214 this.previewButtonPanel.submitButton.loading = false;