@@ -2,91 +2,93 @@ import { StorageConsumerKind } from '@odf/shared';
2
2
import { Patch } from '@openshift-console/dynamic-plugin-sdk' ;
3
3
import * as _ from 'lodash-es' ;
4
4
5
- export const generatePatchForDistributionOfResources = (
5
+ const getInitiallySelectedStorageClasses = (
6
+ storageConsumer : StorageConsumerKind
7
+ ) : string [ ] => {
8
+ const storageClasses = storageConsumer ?. spec ?. storageClasses || [ ] ;
9
+ return storageClasses . map ( ( sc ) => sc . name ) ;
10
+ } ;
11
+
12
+ const getInitiallySelectedVolumeSnapshotClasses = (
13
+ storageConsumer : StorageConsumerKind
14
+ ) : string [ ] => {
15
+ const volumeSnapshotClasses =
16
+ storageConsumer ?. spec ?. volumeSnapshotClasses || [ ] ;
17
+ return volumeSnapshotClasses . map ( ( vsc ) => vsc . name ) ;
18
+ } ;
19
+
20
+ const generatePatchToModifyResourcesForDistribution = (
6
21
storageConsumer : StorageConsumerKind ,
7
- storageClassNames : string [ ] ,
8
- volumeSnapshotClassNames : string [ ] ,
9
- volumeGroupSnapshotClassNames : string [ ]
22
+ resourceType : 'SC' | 'VSC' ,
23
+ resourceNames : string [ ]
10
24
) : Patch [ ] => {
11
25
const patches : Patch [ ] = [ ] ;
12
- const isStorageClassesDistributed = ! _ . isEmpty (
13
- storageConsumer ?. spec ?. storageClasses
14
- ) ;
15
- const isVolumeSnapshotClassesDistributed = ! _ . isEmpty (
16
- storageConsumer ?. spec ?. volumeSnapshotClasses
26
+ const path =
27
+ resourceType === 'SC'
28
+ ? '/spec/storageClasses'
29
+ : '/spec/volumeSnapshotClasses' ;
30
+ const currentlySelectedResources =
31
+ resourceType === 'SC'
32
+ ? getInitiallySelectedStorageClasses ( storageConsumer )
33
+ : getInitiallySelectedVolumeSnapshotClasses ( storageConsumer ) ;
34
+ const removedResources = currentlySelectedResources . filter (
35
+ ( sc ) => ! resourceNames . includes ( sc )
17
36
) ;
18
- const isVolumeGroupSnapshotClassesDistributed = ! _ . isEmpty (
19
- storageConsumer ?. spec ?. volumeGroupSnapshotClasses
37
+ const addedResources = resourceNames . filter (
38
+ ( sc ) => ! currentlySelectedResources . includes ( sc )
20
39
) ;
21
- const currentlySelectedStorageClasses =
22
- storageConsumer . spec ?. storageClasses ?. map ( ( sc ) => sc . name ) || [ ] ;
23
- const currentlySelectedVolumeSnapshotClasses =
24
- storageConsumer . spec ?. volumeSnapshotClasses ?. map ( ( vsc ) => vsc . name ) || [ ] ;
25
- const currentlySelectedVolumeGroupSnapshotClasses =
26
- storageConsumer . spec ?. volumeGroupSnapshotClasses ?. map (
27
- ( vgsc ) => vgsc . name
28
- ) || [ ] ;
29
- const isStorageClassesChanged =
30
- storageClassNames . length !== currentlySelectedStorageClasses . length ||
31
- ! storageClassNames . every ( ( res ) =>
32
- currentlySelectedStorageClasses . includes ( res )
33
- ) ;
34
- const isVolumeSnapshotClassesChanged =
35
- volumeSnapshotClassNames . length !==
36
- currentlySelectedVolumeSnapshotClasses . length ||
37
- ! volumeSnapshotClassNames ?. every ( ( res ) =>
38
- currentlySelectedVolumeSnapshotClasses ?. includes ( res )
39
- ) ;
40
- const isVolumeGroupSnapshotClassesChanged =
41
- volumeGroupSnapshotClassNames . length !==
42
- currentlySelectedVolumeGroupSnapshotClasses . length ||
43
- ! volumeGroupSnapshotClassNames ?. every ( ( res ) =>
44
- currentlySelectedVolumeGroupSnapshotClasses ?. includes ( res )
45
- ) ;
46
-
47
- if ( ! isStorageClassesDistributed && ! _ . isEmpty ( storageClassNames ) ) {
48
- patches . push ( {
49
- op : 'add' ,
50
- path : '/spec/storageClasses' ,
51
- value : storageClassNames . map ( ( name ) => ( { name } ) ) ,
52
- } ) ;
53
- } else if ( isStorageClassesChanged ) {
40
+ if ( removedResources . length > 0 ) {
54
41
patches . push ( {
55
42
op : 'replace' ,
56
- path : '/spec/storageClasses' ,
57
- value : storageClassNames . map ( ( name ) => ( { name } ) ) ,
43
+ path,
44
+ value : resourceNames . map ( ( name ) => ( { name } ) ) ,
58
45
} ) ;
59
46
}
60
-
61
- if (
62
- ! isVolumeSnapshotClassesDistributed &&
63
- ! _ . isEmpty ( volumeSnapshotClassNames )
64
- ) {
47
+ if ( addedResources . length > 0 ) {
65
48
patches . push ( {
66
49
op : 'add' ,
67
- path : '/spec/volumeSnapshotClasses' ,
68
- value : volumeSnapshotClassNames . map ( ( name ) => ( { name } ) ) ,
69
- } ) ;
70
- } else if ( isVolumeSnapshotClassesChanged ) {
71
- patches . push ( {
72
- op : 'replace' ,
73
- path : '/spec/volumeSnapshotClasses' ,
74
- value : volumeSnapshotClassNames . map ( ( name ) => ( { name } ) ) ,
75
- } ) ;
76
- }
77
- if ( ! isVolumeGroupSnapshotClassesDistributed ) {
78
- patches . push ( {
79
- op : 'add' ,
80
- path : '/spec/volumeGroupSnapshotClasses' ,
81
- value : volumeGroupSnapshotClassNames . map ( ( name ) => ( { name } ) ) ,
82
- } ) ;
83
- } else if ( isVolumeGroupSnapshotClassesChanged ) {
84
- patches . push ( {
85
- op : 'replace' ,
86
- path : '/spec/volumeGroupSnapshotClasses' ,
87
- value : volumeGroupSnapshotClassNames . map ( ( name ) => ( { name } ) ) ,
50
+ path,
51
+ value : resourceNames . map ( ( name ) => ( { name } ) ) ,
88
52
} ) ;
89
53
}
54
+ return patches ;
55
+ } ;
56
+
57
+ export const generatePatchToModifyStorageClasses = (
58
+ storageConsumer : StorageConsumerKind ,
59
+ storageClassNames : string [ ]
60
+ ) : Patch [ ] =>
61
+ generatePatchToModifyResourcesForDistribution (
62
+ storageConsumer ,
63
+ 'SC' ,
64
+ storageClassNames
65
+ ) ;
66
+
67
+ export const generatePatchToModifyVolumeSnapshotClasses = (
68
+ storageConsumer : StorageConsumerKind ,
69
+ volumeSnapshotClassNames : string [ ]
70
+ ) : Patch [ ] =>
71
+ generatePatchToModifyResourcesForDistribution (
72
+ storageConsumer ,
73
+ 'VSC' ,
74
+ volumeSnapshotClassNames
75
+ ) ;
90
76
77
+ export const generatePatchForDistributionOfResources = (
78
+ storageConsumer : StorageConsumerKind ,
79
+ storageClassNames : string [ ] ,
80
+ volumeSnapshotClassNames : string [ ]
81
+ ) : Patch [ ] => {
82
+ const patches : Patch [ ] = [ ] ;
83
+ const storageClassPatches = generatePatchToModifyStorageClasses (
84
+ storageConsumer ,
85
+ storageClassNames
86
+ ) ;
87
+ const volumeSnapshotClassPatches = generatePatchToModifyVolumeSnapshotClasses (
88
+ storageConsumer ,
89
+ volumeSnapshotClassNames
90
+ ) ;
91
+ patches . push ( ...storageClassPatches ) ;
92
+ patches . push ( ...volumeSnapshotClassPatches ) ;
91
93
return patches ;
92
94
} ;
0 commit comments