]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
8ecf1eafa8c9f54dd8f1e32924072c91a0908bce
[ceph-ci.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         this.subvolumegroupForm.get('pool').disable();
127         this.subvolumegroupForm.get('uid').disable();
128         this.subvolumegroupForm.get('gid').disable();
129
130         this.subvolumegroupForm.get('subvolumegroupName').setValue(this.subvolumegroupName);
131         if (resp.bytes_quota !== 'infinite') {
132           this.subvolumegroupForm
133             .get('size')
134             .setValue(this.dimlessBinary.transform(resp.bytes_quota));
135         }
136         this.subvolumegroupForm.get('uid').setValue(resp.uid);
137         this.subvolumegroupForm.get('gid').setValue(resp.gid);
138         this.initialMode = this.octalToHumanReadable.transform(resp.mode, true);
139
140         this.loadingReady();
141       });
142   }
143
144   submit() {
145     const subvolumegroupName = this.subvolumegroupForm.getValue('subvolumegroupName');
146     const pool = this.subvolumegroupForm.getValue('pool');
147     const size = this.formatter.toBytes(this.subvolumegroupForm.getValue('size')) || 0;
148     const uid = this.subvolumegroupForm.getValue('uid');
149     const gid = this.subvolumegroupForm.getValue('gid');
150     const mode = this.formatter.toOctalPermission(this.subvolumegroupForm.getValue('mode'));
151     if (this.isEdit) {
152       const editSize = size === 0 ? 'infinite' : size;
153       this.taskWrapper
154         .wrapTaskAroundCall({
155           task: new FinishedTask('cephfs/subvolume/group/' + URLVerbs.EDIT, {
156             subvolumegroupName: subvolumegroupName
157           }),
158           call: this.cephfsSubvolumeGroupService.update(
159             this.fsName,
160             subvolumegroupName,
161             String(editSize)
162           )
163         })
164         .subscribe({
165           error: () => {
166             this.subvolumegroupForm.setErrors({ cdSubmitButton: true });
167           },
168           complete: () => {
169             this.activeModal.close();
170           }
171         });
172     } else {
173       this.taskWrapper
174         .wrapTaskAroundCall({
175           task: new FinishedTask('cephfs/subvolume/group/' + URLVerbs.CREATE, {
176             subvolumegroupName: subvolumegroupName
177           }),
178           call: this.cephfsSubvolumeGroupService.create(
179             this.fsName,
180             subvolumegroupName,
181             pool,
182             String(size),
183             uid,
184             gid,
185             mode
186           )
187         })
188         .subscribe({
189           error: () => {
190             this.subvolumegroupForm.setErrors({ cdSubmitButton: true });
191           },
192           complete: () => {
193             this.activeModal.close();
194           }
195         });
196     }
197   }
198 }