1 import { Component, Inject, OnInit, Optional } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
3 import { CephfsSubvolumeService } from '~/app/shared/api/cephfs-subvolume.service';
4 import { ActionLabelsI18n, URLVerbs } from '~/app/shared/constants/app.constants';
5 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
6 import { FinishedTask } from '~/app/shared/models/finished-task';
7 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
8 import { Pool } from '../../pool/pool';
9 import { FormatterService } from '~/app/shared/services/formatter.service';
10 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
11 import { CdValidators } from '~/app/shared/forms/cd-validators';
12 import { CephfsSubvolumeInfo } from '~/app/shared/models/cephfs-subvolume.model';
13 import { DimlessBinaryPipe } from '~/app/shared/pipes/dimless-binary.pipe';
14 import { OctalToHumanReadablePipe } from '~/app/shared/pipes/octal-to-human-readable.pipe';
15 import { CdForm } from '~/app/shared/forms/cd-form';
16 import { CephfsSubvolumeGroupService } from '~/app/shared/api/cephfs-subvolume-group.service';
17 import { CephfsSubvolumeGroup } from '~/app/shared/models/cephfs-subvolume-group.model';
18 import { Observable } from 'rxjs';
21 selector: 'cd-cephfs-subvolume-form',
22 templateUrl: './cephfs-subvolume-form.component.html',
23 styleUrls: ['./cephfs-subvolume-form.component.scss']
25 export class CephfsSubvolumeFormComponent extends CdForm implements OnInit {
26 subvolumeForm: CdFormGroup;
31 subVolumeGroups$: Observable<CephfsSubvolumeGroup[]>;
32 subVolumeGroups: CephfsSubvolumeGroup[];
35 columns: CdTableColumn[];
36 scopePermissions: Array<any> = [];
38 owner: ['read', 'write', 'execute'],
39 group: ['read', 'execute'],
40 others: ['read', 'execute']
42 scopes: string[] = ['owner', 'group', 'others'];
45 private actionLabels: ActionLabelsI18n,
46 private taskWrapper: TaskWrapperService,
47 private cephFsSubvolumeService: CephfsSubvolumeService,
48 private cephFsSubvolumeGroupService: CephfsSubvolumeGroupService,
49 private formatter: FormatterService,
50 private dimlessBinary: DimlessBinaryPipe,
51 private octalToHumanReadable: OctalToHumanReadablePipe,
53 @Optional() @Inject('fsName') public fsName: string,
54 @Optional() @Inject('subVolumeName') public subVolumeName: string,
55 @Optional() @Inject('subVolumeGroupName') public subVolumeGroupName: string,
56 @Optional() @Inject('pools') public pools: Pool[],
57 @Optional() @Inject('isEdit') public isEdit = false
60 this.resource = $localize`Subvolume`;
64 this.action = this.actionLabels.CREATE;
73 name: $localize`Read`,
75 cellClass: 'text-center'
79 name: $localize`Write`,
81 cellClass: 'text-center'
85 name: $localize`Execute`,
87 cellClass: 'text-center'
91 this.subVolumeGroups$ = this.cephFsSubvolumeGroupService.get(this.fsName);
92 this.dataPools = this.pools.filter((pool) => pool.type === 'data');
95 this.isEdit ? this.populateForm() : this.loadingReady();
99 this.subvolumeForm = new CdFormGroup({
100 volumeName: new FormControl({ value: this.fsName, disabled: true }),
101 subvolumeName: new FormControl('', {
102 validators: [Validators.required, Validators.pattern(/^[.A-Za-z0-9_-]+$/)],
105 this.cephFsSubvolumeService.exists,
106 this.cephFsSubvolumeService,
110 this.subVolumeGroupName
114 subvolumeGroupName: new FormControl(this.subVolumeGroupName),
115 pool: new FormControl(this.dataPools[0]?.pool, {
116 validators: [Validators.required]
118 size: new FormControl(null, {
121 uid: new FormControl(null),
122 gid: new FormControl(null),
123 mode: new FormControl({}),
124 isolatedNamespace: new FormControl(false)
129 this.action = this.actionLabels.EDIT;
130 this.cephFsSubvolumeService
131 .info(this.fsName, this.subVolumeName, this.subVolumeGroupName)
132 .subscribe((resp: CephfsSubvolumeInfo) => {
133 // Disabled these fields since its not editable
134 this.subvolumeForm.get('subvolumeName').disable();
135 this.subvolumeForm.get('subvolumeGroupName').disable();
136 this.subvolumeForm.get('pool').disable();
137 this.subvolumeForm.get('uid').disable();
138 this.subvolumeForm.get('gid').disable();
140 this.subvolumeForm.get('isolatedNamespace').disable();
141 this.subvolumeForm.get('subvolumeName').setValue(this.subVolumeName);
142 this.subvolumeForm.get('subvolumeGroupName').setValue(this.subVolumeGroupName);
143 if (resp.bytes_quota !== 'infinite') {
144 this.subvolumeForm.get('size').setValue(this.dimlessBinary.transform(resp.bytes_quota));
146 this.subvolumeForm.get('uid').setValue(resp.uid);
147 this.subvolumeForm.get('gid').setValue(resp.gid);
148 this.subvolumeForm.get('isolatedNamespace').setValue(resp.pool_namespace);
149 this.initialMode = this.octalToHumanReadable.transform(resp.mode, true);
156 const subVolumeName = this.subvolumeForm.getValue('subvolumeName');
157 const subVolumeGroupName = this.subvolumeForm.getValue('subvolumeGroupName');
158 const pool = this.subvolumeForm.getValue('pool');
159 const size = this.formatter.toBytes(this.subvolumeForm.getValue('size')) || 0;
160 const uid = this.subvolumeForm.getValue('uid');
161 const gid = this.subvolumeForm.getValue('gid');
162 const mode = this.formatter.toOctalPermission(this.subvolumeForm.getValue('mode'));
163 const isolatedNamespace = this.subvolumeForm.getValue('isolatedNamespace');
166 const editSize = size === 0 ? 'infinite' : size;
168 .wrapTaskAroundCall({
169 task: new FinishedTask('cephfs/subvolume/' + URLVerbs.EDIT, {
170 subVolumeName: subVolumeName
172 call: this.cephFsSubvolumeService.update(
181 this.subvolumeForm.setErrors({ cdSubmitButton: true });
189 .wrapTaskAroundCall({
190 task: new FinishedTask('cephfs/subvolume/' + URLVerbs.CREATE, {
191 subVolumeName: subVolumeName
193 call: this.cephFsSubvolumeService.create(
207 this.subvolumeForm.setErrors({ cdSubmitButton: true });