@@ -14,7 +14,11 @@ import { CdForm } from '~/app/shared/forms/cd-form';
1414import { CdFormGroup } from '~/app/shared/forms/cd-form-group' ;
1515import { CdTableColumn } from '~/app/shared/models/cd-table-column' ;
1616import { FinishedTask } from '~/app/shared/models/finished-task' ;
17- import { RetentionPolicy , SnapshotScheduleFormValue } from '~/app/shared/models/snapshot-schedule' ;
17+ import {
18+ RetentionPolicy ,
19+ SnapshotSchedule ,
20+ SnapshotScheduleFormValue
21+ } from '~/app/shared/models/snapshot-schedule' ;
1822import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service' ;
1923
2024const VALIDATON_TIMER = 300 ;
@@ -27,11 +31,17 @@ const DEBOUNCE_TIMER = 300;
2731} )
2832export class CephfsSnapshotscheduleFormComponent extends CdForm implements OnInit {
2933 fsName ! : string ;
34+ path ! : string ;
35+ schedule ! : string ;
36+ retention ! : string ;
37+ start ! : string ;
38+ status ! : string ;
3039 id ! : number ;
3140 isEdit = false ;
3241 icons = Icons ;
3342 repeatFrequencies = Object . entries ( RepeatFrequency ) ;
3443 retentionFrequencies = Object . entries ( RetentionFrequency ) ;
44+ retentionPoliciesToRemove : RetentionPolicy [ ] = [ ] ;
3545
3646 currentTime ! : NgbTimeStruct ;
3747 minDate ! : NgbDateStruct ;
@@ -71,7 +81,7 @@ export class CephfsSnapshotscheduleFormComponent extends CdForm implements OnIni
7181 this . action = this . actionLabels . CREATE ;
7282 this . directoryStore . loadDirectories ( this . id , '/' , 3 ) ;
7383 this . createForm ( ) ;
74- this . loadingReady ( ) ;
84+ this . isEdit ? this . populateForm ( ) : this . loadingReady ( ) ;
7585 }
7686
7787 get retentionPolicies ( ) {
@@ -91,6 +101,50 @@ export class CephfsSnapshotscheduleFormComponent extends CdForm implements OnIni
91101 )
92102 ) ;
93103
104+ populateForm ( ) {
105+ this . action = this . actionLabels . EDIT ;
106+ this . snapScheduleService . getSnapshotSchedule ( this . path , this . fsName , false ) . subscribe ( {
107+ next : ( response : SnapshotSchedule [ ] ) => {
108+ const first = response . find ( ( x ) => x . path === this . path ) ;
109+ this . snapScheduleForm . get ( 'directory' ) . disable ( ) ;
110+ this . snapScheduleForm . get ( 'directory' ) . setValue ( first . path ) ;
111+ this . snapScheduleForm . get ( 'startDate' ) . disable ( ) ;
112+ this . snapScheduleForm . get ( 'startDate' ) . setValue ( {
113+ year : new Date ( first . start ) . getUTCFullYear ( ) ,
114+ month : new Date ( first . start ) . getUTCMonth ( ) + 1 ,
115+ day : new Date ( first . start ) . getUTCDate ( )
116+ } ) ;
117+ this . snapScheduleForm . get ( 'startTime' ) . disable ( ) ;
118+ this . snapScheduleForm . get ( 'startTime' ) . setValue ( {
119+ hour : new Date ( first . start ) . getUTCHours ( ) ,
120+ minute : new Date ( first . start ) . getUTCMinutes ( ) ,
121+ second : new Date ( first . start ) . getUTCSeconds ( )
122+ } ) ;
123+ this . snapScheduleForm . get ( 'repeatInterval' ) . disable ( ) ;
124+ this . snapScheduleForm . get ( 'repeatInterval' ) . setValue ( first . schedule . split ( '' ) ?. [ 0 ] ) ;
125+ this . snapScheduleForm . get ( 'repeatFrequency' ) . disable ( ) ;
126+ this . snapScheduleForm . get ( 'repeatFrequency' ) . setValue ( first . schedule . split ( '' ) ?. [ 1 ] ) ;
127+
128+ // retention policies
129+ first . retention &&
130+ Object . entries ( first . retention ) . forEach ( ( [ frequency , interval ] , idx ) => {
131+ const freqKey = Object . keys ( RetentionFrequency ) [
132+ Object . values ( RetentionFrequency ) . indexOf ( frequency as any )
133+ ] ;
134+ this . retentionPolicies . push (
135+ new FormGroup ( {
136+ retentionInterval : new FormControl ( interval ) ,
137+ retentionFrequency : new FormControl ( RetentionFrequency [ freqKey ] )
138+ } )
139+ ) ;
140+ this . retentionPolicies . controls [ idx ] . get ( 'retentionInterval' ) . disable ( ) ;
141+ this . retentionPolicies . controls [ idx ] . get ( 'retentionFrequency' ) . disable ( ) ;
142+ } ) ;
143+ this . loadingReady ( ) ;
144+ }
145+ } ) ;
146+ }
147+
94148 createForm ( ) {
95149 this . snapScheduleForm = new CdFormGroup (
96150 {
@@ -128,11 +182,19 @@ export class CephfsSnapshotscheduleFormComponent extends CdForm implements OnIni
128182 }
129183
130184 removeRetentionPolicy ( idx : number ) {
185+ if ( this . isEdit && this . retentionPolicies . at ( idx ) . disabled ) {
186+ const values = this . retentionPolicies . at ( idx ) . value as RetentionPolicy ;
187+ this . retentionPoliciesToRemove . push ( values ) ;
188+ }
131189 this . retentionPolicies . removeAt ( idx ) ;
190+ this . retentionPolicies . controls . forEach ( ( x ) =>
191+ x . get ( 'retentionFrequency' ) . updateValueAndValidity ( )
192+ ) ;
132193 this . cd . detectChanges ( ) ;
133194 }
134195
135196 parseDatetime ( date : NgbDateStruct , time ?: NgbTimeStruct ) : string {
197+ if ( ! date || ! time ) return null ;
136198 return `${ date . year } -${ date . month } -${ date . day } T${ time . hour || '00' } :${ time . minute || '00' } :${
137199 time . second || '00'
138200 } `;
@@ -156,40 +218,81 @@ export class CephfsSnapshotscheduleFormComponent extends CdForm implements OnIni
156218
157219 const values = this . snapScheduleForm . value as SnapshotScheduleFormValue ;
158220
159- const snapScheduleObj = {
160- fs : this . fsName ,
161- path : values . directory ,
162- snap_schedule : this . parseSchedule ( values . repeatInterval , values . repeatFrequency ) ,
163- start : this . parseDatetime ( values . startDate , values . startTime )
164- } ;
221+ if ( this . isEdit ) {
222+ const retentionPoliciesToAdd = ( this . snapScheduleForm . get (
223+ 'retentionPolicies'
224+ ) as FormArray ) . controls
225+ ?. filter (
226+ ( ctrl ) =>
227+ ! ctrl . get ( 'retentionInterval' ) . disabled && ! ctrl . get ( 'retentionFrequency' ) . disabled
228+ )
229+ . map ( ( ctrl ) => ( {
230+ retentionInterval : ctrl . get ( 'retentionInterval' ) . value ,
231+ retentionFrequency : ctrl . get ( 'retentionFrequency' ) . value
232+ } ) ) ;
165233
166- const retentionPoliciesValues = this . parseRetentionPolicies ( values ?. retentionPolicies ) ;
167- if ( retentionPoliciesValues ) {
168- snapScheduleObj [ 'retention_policy' ] = retentionPoliciesValues ;
169- }
234+ const updateObj = {
235+ fs : this . fsName ,
236+ path : this . path ,
237+ retention_to_add : this . parseRetentionPolicies ( retentionPoliciesToAdd ) || null ,
238+ retention_to_remove : this . parseRetentionPolicies ( this . retentionPoliciesToRemove ) || null
239+ } ;
170240
171- this . taskWrapper
172- . wrapTaskAroundCall ( {
173- task : new FinishedTask ( 'cephfs/snapshot/schedule/' + URLVerbs . CREATE , {
174- path : snapScheduleObj . path
175- } ) ,
176- call : this . snapScheduleService . create ( snapScheduleObj )
177- } )
178- . subscribe ( {
179- error : ( ) => {
180- this . snapScheduleForm . setErrors ( { cdSubmitButton : true } ) ;
181- } ,
182- complete : ( ) => {
183- this . activeModal . close ( ) ;
184- }
185- } ) ;
241+ this . taskWrapper
242+ . wrapTaskAroundCall ( {
243+ task : new FinishedTask ( 'cephfs/snapshot/schedule/' + URLVerbs . EDIT , {
244+ path : this . path
245+ } ) ,
246+ call : this . snapScheduleService . update ( updateObj )
247+ } )
248+ . subscribe ( {
249+ error : ( ) => {
250+ this . snapScheduleForm . setErrors ( { cdSubmitButton : true } ) ;
251+ } ,
252+ complete : ( ) => {
253+ this . activeModal . close ( ) ;
254+ }
255+ } ) ;
256+ } else {
257+ const snapScheduleObj = {
258+ fs : this . fsName ,
259+ path : values . directory ,
260+ snap_schedule : this . parseSchedule ( values ?. repeatInterval , values ?. repeatFrequency ) ,
261+ start : this . parseDatetime ( values ?. startDate , values ?. startTime )
262+ } ;
263+
264+ const retentionPoliciesValues = this . parseRetentionPolicies ( values ?. retentionPolicies ) ;
265+ if ( retentionPoliciesValues ) {
266+ snapScheduleObj [ 'retention_policy' ] = retentionPoliciesValues ;
267+ }
268+ this . taskWrapper
269+ . wrapTaskAroundCall ( {
270+ task : new FinishedTask ( 'cephfs/snapshot/schedule/' + URLVerbs . CREATE , {
271+ path : snapScheduleObj . path
272+ } ) ,
273+ call : this . snapScheduleService . create ( snapScheduleObj )
274+ } )
275+ . subscribe ( {
276+ error : ( ) => {
277+ this . snapScheduleForm . setErrors ( { cdSubmitButton : true } ) ;
278+ } ,
279+ complete : ( ) => {
280+ this . activeModal . close ( ) ;
281+ }
282+ } ) ;
283+ }
186284 }
187285
188286 validateSchedule ( ) {
189287 return ( frm : AbstractControl ) => {
190288 const directory = frm . get ( 'directory' ) ;
191289 const repeatFrequency = frm . get ( 'repeatFrequency' ) ;
192290 const repeatInterval = frm . get ( 'repeatInterval' ) ;
291+
292+ if ( this . isEdit ) {
293+ return of ( null ) ;
294+ }
295+
193296 return timer ( VALIDATON_TIMER ) . pipe (
194297 switchMap ( ( ) =>
195298 this . snapScheduleService
@@ -239,7 +342,12 @@ export class CephfsSnapshotscheduleFormComponent extends CdForm implements OnIni
239342 return null ;
240343 }
241344 return this . snapScheduleService
242- . checkRetentionPolicyExists ( frm . get ( 'directory' ) . value , this . fsName , retentionList )
345+ . checkRetentionPolicyExists (
346+ frm . get ( 'directory' ) . value ,
347+ this . fsName ,
348+ retentionList ,
349+ this . retentionPoliciesToRemove ?. map ?.( ( rp ) => rp . retentionFrequency ) || [ ]
350+ )
243351 . pipe (
244352 map ( ( { exists, errorIndex } ) => {
245353 if ( exists ) {
0 commit comments