33import java .text .MessageFormat ;
44import java .time .Duration ;
55import java .util .List ;
6- import java .util .Map ;
76import java .util .function .LongSupplier ;
87
98import jakarta .inject .Inject ;
1211import org .cloudfoundry .multiapps .controller .client .facade .domain .CloudTask ;
1312import org .cloudfoundry .multiapps .controller .client .lib .domain .CloudApplicationExtended ;
1413import org .cloudfoundry .multiapps .controller .core .cf .CloudControllerClientFactory ;
15- import org .cloudfoundry .multiapps .controller .core .model .ApplicationColor ;
16- import org .cloudfoundry .multiapps .controller .core .model .HookPhaseProcessType ;
17- import org .cloudfoundry .multiapps .controller .core .model .SupportedParameters ;
1814import org .cloudfoundry .multiapps .controller .core .security .token .TokenService ;
1915import org .cloudfoundry .multiapps .controller .process .Messages ;
2016import org .cloudfoundry .multiapps .controller .process .util .TimeoutType ;
2117import org .cloudfoundry .multiapps .controller .process .variables .Variables ;
22- import org .cloudfoundry .multiapps .mta .model .Hook ;
2318import org .springframework .beans .factory .config .BeanDefinition ;
2419import org .springframework .context .annotation .Scope ;
2520
@@ -29,10 +24,6 @@ public class ExecuteTaskStep extends TimeoutAsyncFlowableStep {
2924
3025 protected LongSupplier currentTimeSupplier = System ::currentTimeMillis ;
3126
32- private static final String PHASE_KEY = "phase" ;
33- private static final String TARGET_APP_KEY = "target-app" ;
34- private static final String APPLICATION_PHASE_SUBSTRING = ".application." ;
35-
3627 @ Inject
3728 private CloudControllerClientFactory clientFactory ;
3829 @ Inject
@@ -44,94 +35,13 @@ protected StepPhase executeAsyncStep(ProcessContext context) {
4435 CloudTask taskToExecute = StepsUtil .getTask (context );
4536 CloudControllerClient client = context .getControllerClient ();
4637
47- String appName = resolveTargetAppName (context , app );
48-
49- getStepLogger ().info (Messages .EXECUTING_TASK_ON_APP , taskToExecute .getName (), appName );
50- CloudTask startedTask = client .runTask (appName , taskToExecute );
38+ getStepLogger ().info (Messages .EXECUTING_TASK_ON_APP , taskToExecute .getName (), app .getName ());
39+ CloudTask startedTask = client .runTask (app .getName (), taskToExecute );
5140 context .setVariable (Variables .STARTED_TASK , startedTask );
5241 context .setVariable (Variables .START_TIME , currentTimeSupplier .getAsLong ());
5342 return StepPhase .POLL ;
5443 }
5544
56- private String resolveTargetAppName (ProcessContext context , CloudApplicationExtended app ) {
57- Hook hook = context .getVariable (Variables .HOOK_FOR_EXECUTION );
58- if (hook == null ) {
59- return app .getName ();
60- }
61-
62- List <Map <String , String >> phasesConfig = getPhasesConfig (hook );
63- if (phasesConfig .isEmpty ()) {
64- return app .getName ();
65- }
66-
67- String currentPhase = buildCurrentPhaseString (context , hook );
68- String targetApp = phasesConfig .stream ()
69- .filter (config -> currentPhase .equals (config .get (PHASE_KEY )))
70- .map (config -> config .get (TARGET_APP_KEY ))
71- .findFirst ()
72- .orElse (null );
73-
74- if (targetApp == null ) {
75- return app .getName ();
76- }
77-
78- return resolveAppNameForTarget (context , app , targetApp );
79- }
80-
81- @ SuppressWarnings ("unchecked" )
82- private List <Map <String , String >> getPhasesConfig (Hook hook ) {
83- Object phasesConfigValue = hook .getParameters ()
84- .get (SupportedParameters .PHASES_CONFIG );
85- if (phasesConfigValue instanceof List ) {
86- return (List <Map <String , String >>) phasesConfigValue ;
87- }
88- return List .of ();
89- }
90-
91- private String buildCurrentPhaseString (ProcessContext context , Hook hook ) {
92- String hookExecutionPhase = context .getVariable (Variables .HOOK_EXECUTION_PHASE );
93- return hook .getPhases ()
94- .stream ()
95- .filter (p -> p .equals (hookExecutionPhase ))
96- .findFirst ()
97- .orElseGet (() -> hook .getPhases ()
98- .stream ()
99- .filter (p -> p .contains (APPLICATION_PHASE_SUBSTRING ))
100- .findFirst ()
101- .orElse ("" ));
102- }
103-
104- private String resolveAppNameForTarget (ProcessContext context , CloudApplicationExtended app , String targetApp ) {
105- ApplicationColor idleColor = context .getVariable (Variables .IDLE_MTA_COLOR );
106- ApplicationColor liveColor = context .getVariable (Variables .LIVE_MTA_COLOR );
107-
108- if (idleColor == null || liveColor == null ) {
109- return app .getName ();
110- }
111-
112- if (HookPhaseProcessType .HookProcessPhase .IDLE .getType ()
113- .equals (targetApp )) {
114- return swapColorSuffix (app .getName (), liveColor , idleColor );
115- }
116- if (HookPhaseProcessType .HookProcessPhase .LIVE .getType ()
117- .equals (targetApp )) {
118- return swapColorSuffix (app .getName (), idleColor , liveColor );
119- }
120- return app .getName ();
121- }
122-
123- private String swapColorSuffix (String appName , ApplicationColor fromColor , ApplicationColor toColor ) {
124- String fromSuffix = fromColor .asSuffix ();
125- String toSuffix = toColor .asSuffix ();
126- if (appName .endsWith (fromSuffix )) {
127- return appName .substring (0 , appName .length () - fromSuffix .length ()) + toSuffix ;
128- }
129- if (!appName .endsWith (toSuffix )) {
130- return appName + toSuffix ;
131- }
132- return appName ;
133- }
134-
13545 @ Override
13646 protected String getStepErrorMessage (ProcessContext context ) {
13747 CloudApplicationExtended app = context .getVariable (Variables .APP_TO_PROCESS );
0 commit comments