1
1
import { V1beta1Plan } from '@kubev2v/types' ;
2
2
3
3
import { PlanData , PlanPhase } from '../types' ;
4
+ import { PlanConditionType } from '../types/PlanCondition' ;
5
+
6
+ import { getConditionTypes } from './getConditionTypes' ;
4
7
5
8
export const getPlanPhase = ( data : PlanData ) : PlanPhase => {
6
9
const plan = data ?. obj ;
7
10
8
11
if ( ! plan ) return PlanPhase . Unknown ;
9
12
10
13
// Check condition type
11
- const conditions = getConditions ( plan ) ;
14
+ const conditionTypes = getConditionTypes ( plan ) ;
12
15
13
- if ( ! conditions || conditions ?. length < 1 ) {
16
+ if ( ! Object . keys ( conditionTypes ) . length ) {
14
17
return PlanPhase . Unknown ;
15
18
}
16
19
17
20
// Check for Archived
18
- if ( plan ?. spec ?. archived && ! conditions . includes ( ' Archived' ) ) {
21
+ if ( plan ?. spec ?. archived && ! conditionTypes [ PlanConditionType . Archived ] ) {
19
22
return PlanPhase . Archiving ;
20
23
}
21
24
22
- if ( conditions . includes ( ' Archived' ) ) {
25
+ if ( conditionTypes [ PlanConditionType . Archived ] ) {
23
26
return PlanPhase . Archived ;
24
27
}
25
28
26
29
// Check for Succeeded
27
- if ( conditions . includes ( ' Succeeded' ) ) {
30
+ if ( conditionTypes [ PlanConditionType . Succeeded ] ) {
28
31
return PlanPhase . Succeeded ;
29
32
}
30
33
31
34
// Check for Canceled
32
- if ( conditions . includes ( ' Canceled' ) ) {
35
+ if ( conditionTypes [ PlanConditionType . Canceled ] ) {
33
36
return PlanPhase . Canceled ;
34
37
}
35
38
36
39
// CHeck for Running
37
- if ( conditions . includes ( ' Executing' ) ) {
40
+ if ( conditionTypes [ PlanConditionType . Executing ] ) {
38
41
return PlanPhase . Running ;
39
42
}
40
43
@@ -50,7 +53,7 @@ export const getPlanPhase = (data: PlanData): PlanPhase => {
50
53
// Check for vm errors
51
54
const vmError = plan ?. status ?. migration ?. vms ?. find ( ( vm ) => vm ?. error ) ;
52
55
53
- if ( conditions . includes ( ' Failed' ) ) {
56
+ if ( conditionTypes [ PlanConditionType . Failed ] ) {
54
57
return PlanPhase . Failed ;
55
58
}
56
59
@@ -67,40 +70,40 @@ export const getPlanPhase = (data: PlanData): PlanPhase => {
67
70
return PlanPhase . Warning ;
68
71
}
69
72
70
- if ( conditions . includes ( ' Ready' ) ) {
73
+ if ( conditionTypes [ PlanConditionType . Ready ] ) {
71
74
return PlanPhase . Ready ;
72
75
}
73
76
74
77
return PlanPhase . NotReady ;
75
78
} ;
76
79
77
80
export const canPlanStart = ( plan : V1beta1Plan ) => {
78
- const conditions = getConditions ( plan ) ;
81
+ const conditionTypes = getConditionTypes ( plan ) ;
79
82
80
83
return (
81
- conditions ?. includes ( ' Ready' ) &&
82
- ! conditions ?. includes ( ' Executing' ) &&
83
- ! conditions ?. includes ( ' Succeeded' ) &&
84
+ conditionTypes [ PlanConditionType . Ready ] &&
85
+ ! conditionTypes [ PlanConditionType . Executing ] &&
86
+ ! conditionTypes [ PlanConditionType . Succeeded ] &&
84
87
! plan ?. spec ?. archived
85
88
) ;
86
89
} ;
87
90
88
91
export const canPlanReStart = ( plan : V1beta1Plan ) => {
89
- const conditions = getConditions ( plan ) ;
92
+ const conditionTypes = getConditionTypes ( plan ) ;
90
93
91
- return conditions ?. includes ( ' Failed' ) || conditions ?. includes ( ' Canceled' ) ;
94
+ return conditionTypes [ PlanConditionType . Failed ] || conditionTypes [ PlanConditionType . Canceled ] ;
92
95
} ;
93
96
94
97
export const isPlanExecuting = ( plan : V1beta1Plan ) => {
95
- const conditions = getConditions ( plan ) ;
98
+ const conditionTypes = getConditionTypes ( plan ) ;
96
99
97
- return conditions ?. includes ( ' Executing' ) ;
100
+ return conditionTypes [ PlanConditionType . Executing ] ;
98
101
} ;
99
102
100
103
export const isPlanSucceeded = ( plan : V1beta1Plan ) => {
101
- const conditions = getConditions ( plan ) ;
104
+ const conditionTypes = getConditionTypes ( plan ) ;
102
105
103
- return conditions ?. includes ( ' Succeeded' ) ;
106
+ return conditionTypes [ PlanConditionType . Succeeded ] ;
104
107
} ;
105
108
106
109
export const isPlanEditable = ( plan : V1beta1Plan ) => {
@@ -122,6 +125,3 @@ export const isPlanArchived = (plan: V1beta1Plan) => {
122
125
123
126
return planStatus === PlanPhase . Archiving || planStatus === PlanPhase . Archived ;
124
127
} ;
125
-
126
- const getConditions = ( obj : V1beta1Plan ) =>
127
- obj ?. status ?. conditions ?. filter ( ( c ) => c . status === 'True' ) . map ( ( c ) => c . type ) ;
0 commit comments