]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
09d52dabe3b740a81ce5d9258e87f5a36355d11e
[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 { 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 _ from 'lodash';
13 import { CdValidators } from '~/app/shared/forms/cd-validators';
14
15 @Component({
16   selector: 'cd-cephfs-subvolume-form',
17   templateUrl: './cephfs-subvolume-form.component.html',
18   styleUrls: ['./cephfs-subvolume-form.component.scss']
19 })
20 export class CephfsSubvolumeFormComponent implements OnInit {
21   fsName: string;
22   pools: Pool[];
23
24   subvolumeForm: CdFormGroup;
25
26   action: string;
27   resource: string;
28
29   dataPools: Pool[];
30
31   columns: CdTableColumn[];
32   scopePermissions: Array<any> = [];
33   scopes: string[] = ['owner', 'group', 'others'];
34
35   constructor(
36     public activeModal: NgbActiveModal,
37     private actionLabels: ActionLabelsI18n,
38     private taskWrapper: TaskWrapperService,
39     private cephFsSubvolumeService: CephfsSubvolumeService,
40     private formatter: FormatterService
41   ) {
42     this.action = this.actionLabels.CREATE;
43     this.resource = $localize`Subvolume`;
44   }
45
46   ngOnInit(): void {
47     this.columns = [
48       {
49         prop: 'scope',
50         name: $localize`All`,
51         flexGrow: 0.5
52       },
53       {
54         prop: 'read',
55         name: $localize`Read`,
56         flexGrow: 0.5,
57         cellClass: 'text-center'
58       },
59       {
60         prop: 'write',
61         name: $localize`Write`,
62         flexGrow: 0.5,
63         cellClass: 'text-center'
64       },
65       {
66         prop: 'execute',
67         name: $localize`Execute`,
68         flexGrow: 0.5,
69         cellClass: 'text-center'
70       }
71     ];
72
73     this.dataPools = this.pools.filter((pool) => pool.type === 'data');
74     this.createForm();
75   }
76
77   createForm() {
78     this.subvolumeForm = new CdFormGroup({
79       volumeName: new FormControl({ value: this.fsName, disabled: true }),
80       subvolumeName: new FormControl('', {
81         validators: [Validators.required],
82         asyncValidators: [
83           CdValidators.unique(
84             this.cephFsSubvolumeService.exists,
85             this.cephFsSubvolumeService,
86             null,
87             null,
88             this.fsName
89           )
90         ]
91       }),
92       pool: new FormControl(this.dataPools[0]?.pool, {
93         validators: [Validators.required]
94       }),
95       size: new FormControl(null, {
96         updateOn: 'blur'
97       }),
98       uid: new FormControl(null),
99       gid: new FormControl(null),
100       mode: new FormControl({}),
101       isolatedNamespace: new FormControl(false)
102     });
103   }
104
105   submit() {
106     const subVolumeName = this.subvolumeForm.getValue('subvolumeName');
107     const pool = this.subvolumeForm.getValue('pool');
108     const size = this.formatter.toBytes(this.subvolumeForm.getValue('size'));
109     const uid = this.subvolumeForm.getValue('uid');
110     const gid = this.subvolumeForm.getValue('gid');
111     const mode = this.formatter.toOctalPermission(this.subvolumeForm.getValue('mode'));
112     const isolatedNamespace = this.subvolumeForm.getValue('isolatedNamespace');
113     this.taskWrapper
114       .wrapTaskAroundCall({
115         task: new FinishedTask('cephfs/subvolume/' + URLVerbs.CREATE, {
116           subVolumeName: subVolumeName
117         }),
118         call: this.cephFsSubvolumeService.create(
119           this.fsName,
120           subVolumeName,
121           pool,
122           size,
123           uid,
124           gid,
125           mode,
126           isolatedNamespace
127         )
128       })
129       .subscribe({
130         error: () => {
131           this.subvolumeForm.setErrors({ cdSubmitButton: true });
132         },
133         complete: () => {
134           this.activeModal.close();
135         }
136       });
137   }
138 }