4646import org .apache .cloudstack .framework .config .Configurable ;
4747import org .apache .cloudstack .schedule .dao .ResourceScheduleDao ;
4848import org .apache .cloudstack .schedule .dao .ResourceScheduleDetailsDao ;
49+ import org .apache .commons .collections4 .MapUtils ;
4950import org .apache .commons .lang .time .DateUtils ;
5051import org .apache .commons .lang3 .ObjectUtils ;
5152import org .apache .commons .lang3 .StringUtils ;
@@ -193,10 +194,11 @@ public ResourceScheduleResponse createSchedule(ApiCommandResourceType resourceTy
193194 validateStartDateEndDate (startDate , endDate , timeZone );
194195
195196 if (StringUtils .isBlank (description )) {
196- description = String . format ( "%s - %s" , parsedAction . name (), DateUtil . getHumanReadableSchedule ( cronExpression ) );
197+ description = worker . getDescription ( parsedAction , cronExpression , details );
197198 }
198199
199- logger .warn ("Using timezone [{}] for running the schedule for resource [{}], as an equivalent of [{}]." , timeZoneId , resourceUuid , timeZoneStr );
200+ logger .warn ("Using timezone [{}] for running the schedule for resource [{}], as an equivalent of [{}]." ,
201+ timeZoneId , resourceUuid , timeZoneStr );
200202
201203 String finalDescription = description ;
202204 String finalAction = parsedAction .name ();
@@ -209,7 +211,7 @@ public ResourceScheduleResponse createSchedule(ApiCommandResourceType resourceTy
209211 finalDescription , cronExpression .toString (), timeZoneId ,
210212 finalAction , finalStartDate , finalEndDate , enabled ));
211213
212- if (details != null && ! details . isEmpty ( )) {
214+ if (MapUtils . isNotEmpty ( details )) {
213215 List <ResourceScheduleDetailVO > detailVOs = new ArrayList <>();
214216 for (Map .Entry <String , String > entry : details .entrySet ()) {
215217 detailVOs .add (new ResourceScheduleDetailVO (scheduleVO .getId (), entry .getKey (), entry .getValue (), true ));
@@ -221,6 +223,7 @@ public ResourceScheduleResponse createSchedule(ApiCommandResourceType resourceTy
221223
222224 CallContext .current ().setEventResourceId (internalResourceId );
223225 CallContext .current ().setEventResourceType (worker .getApiResourceType ());
226+ CallContext .current ().setEventDetails (String .format ("Created resource schedule %s" , scheduleVO ));
224227 return createResponse (scheduleVO , details );
225228 });
226229 }
@@ -298,8 +301,9 @@ public ResourceScheduleResponse updateSchedule(Long id, String description, Stri
298301 long ownerId = worker .getEntityOwnerId (scheduleVO .getResourceId ());
299302 accountManager .checkAccess (CallContext .current ().getCallingAccount (), null , false , accountManager .getAccount (ownerId ));
300303
301- if (details != null && !details .isEmpty ()) {
302- worker .validateDetails (worker .parseAction (scheduleVO .getActionName ()), details );
304+ ResourceSchedule .Action parsedAction = worker .parseAction (scheduleVO .getActionName ());
305+ if (MapUtils .isNotEmpty (details )) {
306+ worker .validateDetails (parsedAction , details );
303307 }
304308
305309 CronExpression cronExpression = Objects .requireNonNullElse (
@@ -308,7 +312,10 @@ public ResourceScheduleResponse updateSchedule(Long id, String description, Stri
308312 );
309313
310314 if (description == null && scheduleVO .getDescription () == null ) {
311- description = String .format ("%s - %s" , scheduleVO .getActionName (), DateUtil .getHumanReadableSchedule (cronExpression ));
315+ Map <String , String > effectiveDetails = MapUtils .isNotEmpty (details )
316+ ? details
317+ : resourceScheduleDetailsDao .listDetailsKeyPairs (id , true );
318+ description = worker .getDescription (parsedAction , cronExpression , effectiveDetails );
312319 }
313320
314321 final String originalTimeZone = scheduleVO .getTimeZone ();
@@ -365,23 +372,21 @@ public ResourceScheduleResponse updateSchedule(Long id, String description, Stri
365372 return Transaction .execute ((TransactionCallback <ResourceScheduleResponse >) status -> {
366373 resourceScheduleDao .update (id , scheduleVO );
367374
368- if (details != null ) {
369- if (details .isEmpty ()) {
370- resourceScheduleDetailsDao .removeDetails (id );
371- } else {
372- List <ResourceScheduleDetailVO > detailVOs = new ArrayList <>();
373- for (Map .Entry <String , String > entry : details .entrySet ()) {
374- detailVOs .add (new ResourceScheduleDetailVO (id , entry .getKey (), entry .getValue (), true ));
375- }
376- resourceScheduleDetailsDao .saveDetails (detailVOs );
375+ if (MapUtils .isNotEmpty (details )) {
376+ List <ResourceScheduleDetailVO > detailVOs = new ArrayList <>();
377+ for (Map .Entry <String , String > entry : details .entrySet ()) {
378+ detailVOs .add (new ResourceScheduleDetailVO (id , entry .getKey (), entry .getValue (), true ));
377379 }
380+ resourceScheduleDetailsDao .saveDetails (detailVOs );
378381 }
379382
380383 worker .updateScheduledJob (scheduleVO );
381384
382385 CallContext .current ().setEventResourceId (scheduleVO .getResourceId ());
383386 CallContext .current ().setEventResourceType (worker .getApiResourceType ());
384387
388+ CallContext .current ().setEventDetails (String .format ("Updated resource schedule %s" , scheduleVO ));
389+
385390 // Re-load details if they weren't fully replaced
386391 Map <String , String > currentDetails = resourceScheduleDetailsDao .listDetailsKeyPairs (id , true );
387392 return createResponse (scheduleVO , currentDetails );
0 commit comments