]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
accbb61b7eeeb4ed64b4a85695a16425674ccc2b
[ceph.git] /
1 import { Component, OnInit } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
3 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
4 import { CephfsSubvolumeGroupService } from '~/app/shared/api/cephfs-subvolume-group.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 _ from 'lodash';
13 import { CdValidators } from '~/app/shared/forms/cd-validators';
14 import { CdForm } from '~/app/shared/forms/cd-form';
15 import { DimlessBinaryPipe } from '~/app/shared/pipes/dimless-binary.pipe';
16 import { OctalToHumanReadablePipe } from '~/app/shared/pipes/octal-to-human-readable.pipe';
17
18 @Component({
19   selector: 'cd-cephfs-subvolumegroup-form',
20   templateUrl: './cephfs-subvolumegroup-form.component.html',
21   styleUrls: ['./cephfs-subvolumegroup-form.component.scss']
22 })
23 export class CephfsSubvolumegroupFormComponent extends CdForm implements OnInit {
24   fsName: string;
25   subvolumegroupName: string;
26   pools: Pool[];
27   isEdit: boolean = false;
28
29   subvolumegroupForm: CdFormGroup;
30
31   action: string;
32   resource: string;
33
34   dataPools: Pool[];
35
36   columns: CdTableColumn[];
37   scopePermissions: Array<any> = [];
38   initialMode = {
39     owner: ['read', 'write', 'execute'],
40     group: ['read', 'execute'],
41     others: ['read', 'execute']
42   };
43   scopes: string[] = ['owner', 'group', 'others'];
44
45   constructor(
46     public activeModal: NgbActiveModal,
47     private actionLabels: ActionLabelsI18n,
48     private taskWrapper: TaskWrapperService,
49     private cephfsSubvolumeGroupService: CephfsSubvolumeGroupService,
50     private formatter: FormatterService,
51     private dimlessBinary: DimlessBinaryPipe,
52     private octalToHumanReadable: OctalToHumanReadablePipe
53   ) {
54     super();
55     this.resource = $localize`subvolume group`;
56   }
57
58   ngOnInit(): void {
59     this.action = this.actionLabels.CREATE;
60     this.columns = [
61       {
62         prop: 'scope',
63         name: $localize`All`,
64         flexGrow: 0.5
65       },
66       {
67         prop: 'read',
68         name: $localize`Read`,
69         flexGrow: 0.5,
70         cellClass: 'text-center'
71       },
72       {
73         prop: 'write',
74         name: $localize`Write`,
75         flexGrow: 0.5,
76         cellClass: 'text-center'
77       },
78       {
79         prop: 'execute',
80         name: $localize`Execute`,
81         flexGrow: 0.5,
82         cellClass: 'text-center'
83       }
84     ];
85
86     this.dataPools = this.pools.filter((pool) => pool.type === 'data');
87     this.createForm();
88
89     this.isEdit ? this.populateForm() : this.loadingReady();
90   }
91
92   createForm() {
93     this.subvolumegroupForm = new CdFormGroup({
94       volumeName: new FormControl({ value: this.fsName, disabled: true }),
95       subvolumegroupName: new FormControl('', {
96         validators: [Validators.required, Validators.pattern(/^[.A-Za-z0-9_-]+$/)],
97         asyncValidators: [
98           CdValidators.unique(
99             this.cephfsSubvolumeGroupService.exists,
100             this.cephfsSubvolumeGroupService,
101             null,
102             null,
103             this.fsName
104           )
105         ]
106       }),
107       pool: new FormControl(this.dataPools[0]?.pool, {
108         validators: [Validators.required]
109       }),
110       size: new FormControl(null, {
111         updateOn: 'blur'
112       }),
113       uid: new FormControl(null),
114       gid: new FormControl(null),
115       mode: new FormControl({})
116     });
117   }
118
119   populateForm() {
120     this.action = this.actionLabels.EDIT;
121     this.cephfsSubvolumeGroupService
122       .info(this.fsName, this.subvolumegroupName)
123       .subscribe((resp: any) => {
124         // Disabled these fields since its not editable
125         this.subvolumegroupForm.get('subvolumegroupName').disable();
126
127         this.subvolumegroupForm.get('subvolumegroupName').setValue(this.subvolumegroupName);
128         if (resp.bytes_quota !== 'infinite') {
129           this.subvolumegroupForm
130             .get('size')
131             .setValue(this.dimlessBinary.transform(resp.bytes_quota));
132         }
133         this.subvolumegroupForm.get('uid').setValue(resp.uid);
134         this.subvolumegroupForm.get('gid').setValue(resp.gid);
135         this.initialMode = this.octalToHumanReadable.transform(resp.mode, true);
136
137         this.loadingReady();
138       });
139   }
140
141   submit() {
142     const subvolumegroupName = this.subvolumegroupForm.getValue('subvolumegroupName');
143     const pool = this.subvolumegroupForm.getValue('pool');
144     const size = this.formatter.toBytes(this.subvolumegroupForm.getValue('size')) || 0;
145     const uid = this.subvolumegroupForm.getValue('uid');
146     const gid = this.subvolumegroupForm.getValue('gid');
147     const mode = this.formatter.toOctalPermission(this.subvolumegroupForm.getValue('mode'));
148     if (this.isEdit) {
149       const editSize = size === 0 ? 'infinite' : size;
150       this.taskWrapper
151         .wrapTaskAroundCall({
152           task: new FinishedTask('cephfs/subvolume/group/' + URLVerbs.EDIT, {
153             subvolumegroupName: subvolumegroupName
154           }),
155           call: this.cephfsSubvolumeGroupService.create(
156             this.fsName,
157             subvolumegroupName,
158             pool,
159             String(editSize),
160             uid,
161             gid,
162             mode
163           )
164         })
165         .subscribe({
166           error: () => {
167             this.subvolumegroupForm.setErrors({ cdSubmitButton: true });
168           },
169           complete: () => {
170             this.activeModal.close();
171           }
172         });
173     } else {
174       this.taskWrapper
175         .wrapTaskAroundCall({
176           task: new FinishedTask('cephfs/subvolume/group/' + URLVerbs.CREATE, {
177             subvolumegroupName: subvolumegroupName
178           }),
179           call: this.cephfsSubvolumeGroupService.create(
180             this.fsName,
181             subvolumegroupName,
182             pool,
183             String(size),
184             uid,
185             gid,
186             mode
187           )
188         })
189         .subscribe({
190           error: () => {
191             this.subvolumegroupForm.setErrors({ cdSubmitButton: true });
192           },
193           complete: () => {
194             this.activeModal.close();
195           }
196         });
197     }
198   }
199 }