@@ -20,6 +20,7 @@ import { StorageClass, ZoneGroupDetails } from '../models/rgw-storage-class.mode
2020import { CdForm } from '~/app/shared/forms/cd-form' ;
2121import { Router } from '@angular/router' ;
2222import { ActionLabelsI18n } from '~/app/shared/constants/app.constants' ;
23+ import { Lifecycle , Rule , Tag } from '../models/rgw-bucket-lifecycle' ;
2324
2425export interface Tags {
2526 tagKey : number ;
@@ -35,12 +36,12 @@ export class RgwBucketTieringFormComponent extends CdForm implements OnInit {
3536 tieringForm : CdFormGroup ;
3637 tagsToRemove : Tags [ ] = [ ] ;
3738 storageClassList : StorageClass [ ] = null ;
38- configuredLifecycle : any ;
39+ configuredLifecycle : Lifecycle ;
3940 isStorageClassFetched = false ;
4041
4142 constructor (
4243 @Inject ( 'bucket' ) public bucket : Bucket ,
43- @Optional ( ) @Inject ( 'selectedLifecycle' ) public selectedLifecycle : any ,
44+ @Optional ( ) @Inject ( 'selectedLifecycle' ) public selectedLifecycle : Rule ,
4445 @Optional ( ) @Inject ( 'editing' ) public editing = false ,
4546 public actionLabels : ActionLabelsI18n ,
4647 private rgwBucketService : RgwBucketService ,
@@ -56,11 +57,28 @@ export class RgwBucketTieringFormComponent extends CdForm implements OnInit {
5657 ngOnInit ( ) {
5758 this . rgwBucketService
5859 . getLifecycle ( this . bucket . bucket , this . bucket . owner )
59- . subscribe ( ( lifecycle ) => {
60- this . configuredLifecycle = lifecycle || { LifecycleConfiguration : { Rules : [ ] } } ;
60+ . subscribe ( ( lifecycle : Lifecycle ) => {
61+ if ( lifecycle === null ) {
62+ lifecycle = { LifecycleConfiguration : { Rule : [ ] } } ;
63+ }
64+ lifecycle . LifecycleConfiguration . Rule = lifecycle . LifecycleConfiguration . Rule . map (
65+ ( rule : Rule ) => {
66+ if ( rule ?. [ 'Filter' ] ?. [ 'Tag' ] && ! Array . isArray ( rule ?. [ 'Filter' ] ?. [ 'Tag' ] ) ) {
67+ rule [ 'Filter' ] [ 'Tag' ] = [ rule [ 'Filter' ] [ 'Tag' ] ] ;
68+ }
69+ if (
70+ rule ?. [ 'Filter' ] ?. [ 'And' ] ?. [ 'Tag' ] &&
71+ ! Array . isArray ( rule ?. [ 'Filter' ] ?. [ 'And' ] ?. [ 'Tag' ] )
72+ ) {
73+ rule [ 'Filter' ] [ 'And' ] [ 'Tag' ] = [ rule [ 'Filter' ] [ 'And' ] [ 'Tag' ] ] ;
74+ }
75+ return rule ;
76+ }
77+ ) ;
78+ this . configuredLifecycle = lifecycle ;
6179 if ( this . editing ) {
62- const ruleToEdit = this . configuredLifecycle ?. [ 'LifecycleConfiguration' ] ?. [ 'Rules ' ] . filter (
63- ( rule : any ) => rule ?. [ 'ID' ] === this . selectedLifecycle ?. [ 'ID' ]
80+ const ruleToEdit = this . configuredLifecycle ?. [ 'LifecycleConfiguration' ] ?. [ 'Rule ' ] . filter (
81+ ( rule : Rule ) => rule ?. [ 'ID' ] === this . selectedLifecycle ?. [ 'ID' ]
6482 ) [ 0 ] ;
6583 this . tieringForm . patchValue ( {
6684 name : ruleToEdit ?. [ 'ID' ] ,
@@ -89,35 +107,35 @@ export class RgwBucketTieringFormComponent extends CdForm implements OnInit {
89107 this . loadStorageClass ( ) ;
90108 }
91109
92- checkIfRuleHasFilters ( rule : any ) {
110+ checkIfRuleHasFilters ( rule : Rule ) {
93111 if (
94112 this . isValidPrefix ( rule ?. [ 'Prefix' ] ) ||
95113 this . isValidPrefix ( rule ?. [ 'Filter' ] ?. [ 'Prefix' ] ) ||
96- this . isValidArray ( rule ?. [ 'Filter' ] ?. [ 'Tags ' ] ) ||
114+ this . isValidArray ( rule ?. [ 'Filter' ] ?. [ 'Tag ' ] ) ||
97115 this . isValidPrefix ( rule ?. [ 'Filter' ] ?. [ 'And' ] ?. [ 'Prefix' ] ) ||
98- this . isValidArray ( rule ?. [ 'Filter' ] ?. [ 'And' ] ?. [ 'Tags ' ] )
116+ this . isValidArray ( rule ?. [ 'Filter' ] ?. [ 'And' ] ?. [ 'Tag ' ] )
99117 ) {
100118 return true ;
101119 }
102120 return false ;
103121 }
104122
105123 isValidPrefix ( value : string ) {
106- return value !== undefined && value !== '' ;
124+ return ! ! value ;
107125 }
108126
109- isValidArray ( value : object [ ] ) {
127+ isValidArray ( value : Tag | Tag [ ] ) {
110128 return Array . isArray ( value ) && value . length > 0 ;
111129 }
112130
113- setTags ( rule : any ) {
114- if ( rule ?. [ ' Filter' ] ?. [ 'Tags' ] ?. length > 0 ) {
115- rule ?. [ 'Filter' ] ?. [ 'Tags ' ] ?. forEach ( ( tag : { Key : string ; Value : string } ) =>
131+ setTags ( rule : Rule ) {
132+ if ( Array . isArray ( rule ?. Filter ?. Tag ) && rule ?. Filter ?. Tag ?. length > 0 ) {
133+ rule ?. [ 'Filter' ] ?. [ 'Tag ' ] ?. forEach ( ( tag : { Key : string ; Value : string } ) =>
116134 this . addTags ( tag . Key , tag . Value )
117135 ) ;
118136 }
119- if ( rule ?. [ ' Filter' ] ?. [ ' And' ] ?. [ 'Tags' ] ?. length > 0 ) {
120- rule ?. [ 'Filter' ] ?. [ 'And' ] ?. [ 'Tags ' ] ?. forEach ( ( tag : { Key : string ; Value : string } ) =>
137+ if ( Array . isArray ( rule ?. Filter ?. And ?. Tag ) && rule ?. Filter ?. And ?. Tag ?. length > 0 ) {
138+ rule ?. [ 'Filter' ] ?. [ 'And' ] ?. [ 'Tag ' ] ?. forEach ( ( tag : { Key : string ; Value : string } ) =>
121139 this . addTags ( tag . Key , tag . Value )
122140 ) ;
123141 }
@@ -130,17 +148,17 @@ export class RgwBucketTieringFormComponent extends CdForm implements OnInit {
130148 addTags ( key ?: string , value ?: string ) {
131149 this . tags . push (
132150 new FormGroup ( {
133- Key : new FormControl ( key ) ,
134- Value : new FormControl ( value )
151+ Key : new FormControl ( key || '' , Validators . required ) ,
152+ Value : new FormControl ( value || '' , Validators . required )
135153 } )
136154 ) ;
137155 this . cd . detectChanges ( ) ;
138156 }
139157
140158 duplicateConfigName ( control : AbstractControl ) : ValidationErrors | null {
141- if ( this . configuredLifecycle ?. LifecycleConfiguration ?. Rules ?. length > 0 ) {
142- const ruleIds = this . configuredLifecycle . LifecycleConfiguration . Rules . map (
143- ( rule : any ) => rule . ID
159+ if ( this . configuredLifecycle ?. LifecycleConfiguration ?. Rule ?. length > 0 ) {
160+ const ruleIds = this . configuredLifecycle . LifecycleConfiguration . Rule . map (
161+ ( rule : Rule ) => rule . ID
144162 ) ;
145163 return ruleIds . includes ( control . value ) ? { duplicate : true } : null ;
146164 }
@@ -181,15 +199,13 @@ export class RgwBucketTieringFormComponent extends CdForm implements OnInit {
181199 return ;
182200 }
183201
184- let lifecycle : any = {
202+ let lifecycle : Rule = {
185203 ID : this . tieringForm . getRawValue ( ) . name ,
186204 Status : formValue . status ,
187- Transition : [
188- {
189- Days : formValue . days ,
190- StorageClass : formValue . storageClass
191- }
192- ]
205+ Transition : {
206+ Days : formValue . days ,
207+ StorageClass : formValue . storageClass
208+ }
193209 } ;
194210 if ( formValue . hasPrefix ) {
195211 if ( this . tags . length > 0 ) {
@@ -210,11 +226,11 @@ export class RgwBucketTieringFormComponent extends CdForm implements OnInit {
210226 }
211227 } else {
212228 Object . assign ( lifecycle , {
213- Filter : { }
229+ Prefix : ''
214230 } ) ;
215231 }
216232 if ( ! this . editing ) {
217- this . configuredLifecycle . LifecycleConfiguration . Rules . push ( lifecycle ) ;
233+ this . configuredLifecycle . LifecycleConfiguration . Rule . push ( lifecycle ) ;
218234 this . rgwBucketService
219235 . setLifecycle (
220236 this . bucket . bucket ,
@@ -237,8 +253,10 @@ export class RgwBucketTieringFormComponent extends CdForm implements OnInit {
237253 }
238254 } ) ;
239255 } else {
240- const rules = this . configuredLifecycle . LifecycleConfiguration . Rules ;
241- const index = rules . findIndex ( ( rule : any ) => rule ?. [ 'ID' ] === this . selectedLifecycle ?. [ 'ID' ] ) ;
256+ const rules = this . configuredLifecycle . LifecycleConfiguration . Rule ;
257+ const index = rules . findIndex (
258+ ( rule : Rule ) => rule ?. [ 'ID' ] === this . selectedLifecycle ?. [ 'ID' ]
259+ ) ;
242260 rules . splice ( index , 1 , lifecycle ) ;
243261 this . rgwBucketService
244262 . setLifecycle (
@@ -266,5 +284,6 @@ export class RgwBucketTieringFormComponent extends CdForm implements OnInit {
266284
267285 goToCreateStorageClass ( ) {
268286 this . router . navigate ( [ 'rgw/tiering/create' ] ) ;
287+ this . closeModal ( ) ;
269288 }
270289}
0 commit comments