1 import { Component, OnInit } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
3 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
4 import { CephfsSubvolumeService } from '~/app/shared/api/cephfs-subvolume.service';
5 import { ActionLabelsI18n, URLVerbs } from '~/app/shared/constants/app.constants';
6 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
7 import { FinishedTask } from '~/app/shared/models/finished-task';
8 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
9 import { Pool } from '../../pool/pool';
10 import { FormatterService } from '~/app/shared/services/formatter.service';
11 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
12 import { CdValidators } from '~/app/shared/forms/cd-validators';
13 import { CephfsSubvolumeInfo } from '~/app/shared/models/cephfs-subvolume.model';
14 import { DimlessBinaryPipe } from '~/app/shared/pipes/dimless-binary.pipe';
15 import { OctalToHumanReadablePipe } from '~/app/shared/pipes/octal-to-human-readable.pipe';
16 import { CdForm } from '~/app/shared/forms/cd-form';
17 import { CephfsSubvolumeGroupService } from '~/app/shared/api/cephfs-subvolume-group.service';
18 import { CephfsSubvolumeGroup } from '~/app/shared/models/cephfs-subvolume-group.model';
19 import { Observable } from 'rxjs';
22 selector: 'cd-cephfs-subvolume-form',
23 templateUrl: './cephfs-subvolume-form.component.html',
24 styleUrls: ['./cephfs-subvolume-form.component.scss']
26 export class CephfsSubvolumeFormComponent extends CdForm implements OnInit {
28 subVolumeName: string;
29 subVolumeGroupName: string;
33 subvolumeForm: CdFormGroup;
38 subVolumeGroups$: Observable<CephfsSubvolumeGroup[]>;
39 subVolumeGroups: CephfsSubvolumeGroup[];
42 columns: CdTableColumn[];
43 scopePermissions: Array<any> = [];
45 owner: ['read', 'write', 'execute'],
46 group: ['read', 'execute'],
47 others: ['read', 'execute']
49 scopes: string[] = ['owner', 'group', 'others'];
52 public activeModal: NgbActiveModal,
53 private actionLabels: ActionLabelsI18n,
54 private taskWrapper: TaskWrapperService,
55 private cephFsSubvolumeService: CephfsSubvolumeService,
56 private cephFsSubvolumeGroupService: CephfsSubvolumeGroupService,
57 private formatter: FormatterService,
58 private dimlessBinary: DimlessBinaryPipe,
59 private octalToHumanReadable: OctalToHumanReadablePipe
62 this.resource = $localize`Subvolume`;
66 this.action = this.actionLabels.CREATE;
75 name: $localize`Read`,
77 cellClass: 'text-center'
81 name: $localize`Write`,
83 cellClass: 'text-center'
87 name: $localize`Execute`,
89 cellClass: 'text-center'
93 this.subVolumeGroups$ = this.cephFsSubvolumeGroupService.get(this.fsName);
94 this.dataPools = this.pools.filter((pool) => pool.type === 'data');
97 this.isEdit ? this.populateForm() : this.loadingReady();
101 this.subvolumeForm = new CdFormGroup({
102 volumeName: new FormControl({ value: this.fsName, disabled: true }),
103 subvolumeName: new FormControl('', {
104 validators: [Validators.required, Validators.pattern(/^[.A-Za-z0-9_-]+$/)],
107 this.cephFsSubvolumeService.exists,
108 this.cephFsSubvolumeService,
115 subvolumeGroupName: new FormControl(this.subVolumeGroupName),
116 pool: new FormControl(this.dataPools[0]?.pool, {
117 validators: [Validators.required]
119 size: new FormControl(null, {
122 uid: new FormControl(null),
123 gid: new FormControl(null),
124 mode: new FormControl({}),
125 isolatedNamespace: new FormControl(false)
130 this.action = this.actionLabels.EDIT;
131 this.cephFsSubvolumeService
132 .info(this.fsName, this.subVolumeName, this.subVolumeGroupName)
133 .subscribe((resp: CephfsSubvolumeInfo) => {
134 // Disabled these fields since its not editable
135 this.subvolumeForm.get('subvolumeName').disable();
136 this.subvolumeForm.get('subvolumeGroupName').disable();
137 this.subvolumeForm.get('pool').disable();
138 this.subvolumeForm.get('uid').disable();
139 this.subvolumeForm.get('gid').disable();
141 this.subvolumeForm.get('isolatedNamespace').disable();
142 this.subvolumeForm.get('subvolumeName').setValue(this.subVolumeName);
143 this.subvolumeForm.get('subvolumeGroupName').setValue(this.subVolumeGroupName);
144 if (resp.bytes_quota !== 'infinite') {
145 this.subvolumeForm.get('size').setValue(this.dimlessBinary.transform(resp.bytes_quota));
147 this.subvolumeForm.get('uid').setValue(resp.uid);
148 this.subvolumeForm.get('gid').setValue(resp.gid);
149 this.subvolumeForm.get('isolatedNamespace').setValue(resp.pool_namespace);
150 this.initialMode = this.octalToHumanReadable.transform(resp.mode, true);
157 const subVolumeName = this.subvolumeForm.getValue('subvolumeName');
158 const subVolumeGroupName = this.subvolumeForm.getValue('subvolumeGroupName');
159 const pool = this.subvolumeForm.getValue('pool');
160 const size = this.formatter.toBytes(this.subvolumeForm.getValue('size')) || 0;
161 const uid = this.subvolumeForm.getValue('uid');
162 const gid = this.subvolumeForm.getValue('gid');
163 const mode = this.formatter.toOctalPermission(this.subvolumeForm.getValue('mode'));
164 const isolatedNamespace = this.subvolumeForm.getValue('isolatedNamespace');
167 const editSize = size === 0 ? 'infinite' : size;
169 .wrapTaskAroundCall({
170 task: new FinishedTask('cephfs/subvolume/' + URLVerbs.EDIT, {
171 subVolumeName: subVolumeName
173 call: this.cephFsSubvolumeService.update(
182 this.subvolumeForm.setErrors({ cdSubmitButton: true });
185 this.activeModal.close();
190 .wrapTaskAroundCall({
191 task: new FinishedTask('cephfs/subvolume/' + URLVerbs.CREATE, {
192 subVolumeName: subVolumeName
194 call: this.cephFsSubvolumeService.create(
208 this.subvolumeForm.setErrors({ cdSubmitButton: true });
211 this.activeModal.close();