vishesh92 commented on code in PR #7397:
URL: https://github.com/apache/cloudstack/pull/7397#discussion_r1176380401


##########
server/src/main/java/org/apache/cloudstack/vm/schedule/VMScheduleManagerImpl.java:
##########
@@ -0,0 +1,322 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cloudstack.vm.schedule;
+
+import com.cloud.api.query.MutualExclusiveIdsManagerBase;
+import com.cloud.event.ActionEvent;
+import com.cloud.event.EventTypes;
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.user.AccountManager;
+import com.cloud.utils.DateUtil;
+import com.cloud.utils.Pair;
+import com.cloud.utils.component.PluggableService;
+import com.cloud.utils.db.Filter;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.Transaction;
+import com.cloud.utils.db.TransactionCallback;
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.cloud.vm.VirtualMachine;
+import com.cloud.vm.VirtualMachineManager;
+import org.apache.cloudstack.api.command.user.vm.CreateVMScheduleCmd;
+import org.apache.cloudstack.api.command.user.vm.DeleteVMScheduleCmd;
+import org.apache.cloudstack.api.command.user.vm.ListVMScheduleCmd;
+import org.apache.cloudstack.api.command.user.vm.UpdateVMScheduleCmd;
+import org.apache.cloudstack.api.response.ListResponse;
+import org.apache.cloudstack.api.response.VMScheduleResponse;
+import org.apache.cloudstack.context.CallContext;
+import org.apache.cloudstack.framework.messagebus.MessageBus;
+import org.apache.cloudstack.vm.schedule.dao.VMScheduleDao;
+import org.apache.log4j.Logger;
+import org.springframework.scheduling.support.CronExpression;
+
+import javax.inject.Inject;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Objects;
+import java.util.TimeZone;
+
+public class VMScheduleManagerImpl extends MutualExclusiveIdsManagerBase 
implements VMScheduleManager, PluggableService {
+
+    private static Logger LOGGER = 
Logger.getLogger(VMScheduleManagerImpl.class);
+
+    @Inject
+    private VMScheduleDao vmScheduleDao;
+    @Inject
+    private VirtualMachineManager virtualMachineManager;
+    @Inject
+    private VMScheduler vmScheduler;
+    @Inject
+    private MessageBus messageBus;
+    @Inject
+    private AccountManager accountManager;
+
+    @Override
+    public List<Class<?>> getCommands() {
+        final List<Class<?>> cmdList = new ArrayList<>();
+        cmdList.add(CreateVMScheduleCmd.class);
+        cmdList.add(ListVMScheduleCmd.class);
+        cmdList.add(UpdateVMScheduleCmd.class);
+        cmdList.add(DeleteVMScheduleCmd.class);
+        return cmdList;
+    }
+
+    @Override
+    @ActionEvent(eventType = EventTypes.EVENT_VM_SCHEDULE_CREATE, 
eventDescription = "Creating VM Schedule")
+    public VMScheduleResponse createSchedule(CreateVMScheduleCmd cmd) {
+        VirtualMachine vm = virtualMachineManager.findById(cmd.getVmId());
+        accountManager.checkAccess(CallContext.current().getCallingAccount(), 
null, false, vm);
+        if (vm == null) {
+            throw new InvalidParameterValueException(String.format("Invalid 
value for vmId: %s", cmd.getVmId()));
+        }
+
+        VMSchedule.Action action = null;
+        if (cmd.getAction() != null) {
+            try {
+                action = 
VMSchedule.Action.valueOf(cmd.getAction().toUpperCase());
+            } catch (IllegalArgumentException exception) {
+                throw new 
InvalidParameterValueException(String.format("Invalid value for action: %s", 
cmd.getAction()));
+            }
+        }
+
+        Date startDate = cmd.getStartDate();
+        Date endDate = cmd.getEndDate();
+
+        validateStartDateEndDate(startDate, endDate);
+
+        CronExpression cronExpression = 
DateUtil.parseSchedule(cmd.getSchedule());
+
+        String cmdTimeZone = cmd.getTimeZone();
+        TimeZone timeZone = TimeZone.getTimeZone(cmdTimeZone);
+        String timeZoneId = timeZone.getID();
+
+        String description = null;
+        if (cmd.getDescription() == null || cmd.getDescription().isBlank()) {
+            description = String.format("%s - %s", action, 
DateUtil.getHumanReadableSchedule(cronExpression));
+        } else description = cmd.getDescription();
+
+        LOGGER.warn(String.format("Using timezone [%s] for running the 
schedule [%s] for VM [%s], as an equivalent of [%s].", cmd.getVmId(), 
cmd.getName(), cmd.getVmId(),
+                cmdTimeZone));
+
+        String finalDescription = description;
+        VMSchedule.Action finalAction = action;
+        return Transaction.execute((TransactionCallback<VMScheduleResponse>) 
status -> {
+            VMScheduleVO vmSchedule = vmScheduleDao.persist(new 
VMScheduleVO(cmd.getVmId(), cmd.getName(), finalDescription, 
cronExpression.toString(), timeZoneId, finalAction, startDate, endDate, 
cmd.getEnabled()));
+            vmScheduler.scheduleNextJob(vmSchedule);
+
+            return createResponse(vmSchedule);
+        });
+    }
+
+    @Override
+    public VMScheduleResponse createResponse(VMSchedule vmSchedule) {
+        VirtualMachine vm = 
virtualMachineManager.findById(vmSchedule.getVmId());
+        VMScheduleResponse response = new VMScheduleResponse();
+
+        response.setObjectName(VMSchedule.class.getSimpleName().toLowerCase());
+        response.setId(vmSchedule.getUuid());
+        response.setVmId(vm.getUuid());
+        response.setName(vmSchedule.getName());
+        response.setDescription(vmSchedule.getDescription());
+        response.setSchedule(vmSchedule.getSchedule());
+        response.setTimeZone(vmSchedule.getTimeZone());
+        response.setAction(vmSchedule.getAction());
+        response.setEnabled(vmSchedule.getEnabled());
+        response.setStartDate(vmSchedule.getStartDate());
+        response.setEndDate(vmSchedule.getEndDate());
+        return response;
+    }
+
+    @Override
+    public ListResponse<VMScheduleResponse> listSchedule(ListVMScheduleCmd 
cmd) {
+        Long id = cmd.getId();
+        Boolean enabled = cmd.getEnabled();
+        Long vmId = cmd.getVmId();
+
+        VirtualMachine vm = virtualMachineManager.findById(vmId);
+        accountManager.checkAccess(CallContext.current().getCallingAccount(), 
null, false, vm);
+
+        VMSchedule.Action action = null;
+        if (cmd.getAction() != null) {
+            try {
+                action = VMSchedule.Action.valueOf(cmd.getAction());
+            } catch (IllegalArgumentException exception) {
+                throw new InvalidParameterValueException("Invalid value for 
action: " + cmd.getAction());
+            }
+        }
+
+        Filter searchFilter = new Filter(VMScheduleVO.class, "id", true, 
cmd.getStartIndex(), cmd.getPageSizeVal());
+        SearchBuilder<VMScheduleVO> sb = vmScheduleDao.createSearchBuilder();
+        sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
+        sb.and("vm_id", sb.entity().getVmId(), SearchCriteria.Op.EQ);
+        sb.and("action", sb.entity().getAction(), SearchCriteria.Op.EQ);
+        sb.and("enabled", sb.entity().getEnabled(), SearchCriteria.Op.EQ);
+
+        SearchCriteria<VMScheduleVO> sc = sb.create();
+
+        if (id != null) {
+            sc.setParameters("id", id);
+        }
+        if (enabled != null) {
+            sc.setParameters("enabled", enabled);
+        }
+        if (action != null) {
+            sc.setParameters("action", action);
+        }
+        sc.setParameters("vm_id", vmId);
+
+        Pair<List<VMScheduleVO>, Integer> result = 
vmScheduleDao.searchAndCount(sc, searchFilter);
+        ListResponse<VMScheduleResponse> response = new ListResponse<>();
+        List<VMScheduleResponse> responsesList = new ArrayList<>();
+        for (VMSchedule vmSchedule : result.first()) {
+            responsesList.add(createResponse(vmSchedule));
+        }
+        response.setResponses(responsesList, result.second());
+        return response;
+    }
+
+
+    @Override
+    @ActionEvent(eventType = EventTypes.EVENT_VM_SCHEDULE_UPDATE, 
eventDescription = "Updating VM Schedule")
+    public VMScheduleResponse updateSchedule(UpdateVMScheduleCmd cmd) {
+        Long id = cmd.getId();
+        VMScheduleVO vmSchedule = vmScheduleDao.findById(id);
+
+        if (vmSchedule == null) {
+            throw new CloudRuntimeException("VM schedule doesn't exist");
+        }
+
+        VirtualMachine vm = 
virtualMachineManager.findById(vmSchedule.getVmId());
+        accountManager.checkAccess(CallContext.current().getCallingAccount(), 
null, false, vm);
+
+        String name = cmd.getName();
+        CronExpression cronExpression = Objects.requireNonNullElse(
+                DateUtil.parseSchedule(cmd.getSchedule()),
+                DateUtil.parseSchedule(vmSchedule.getSchedule())
+        );
+
+        VMSchedule.Action action = null;
+        if (cmd.getAction() != null) {
+            try {
+                action = 
VMSchedule.Action.valueOf(cmd.getAction().toUpperCase());
+            } catch (IllegalArgumentException exception) {
+                throw new InvalidParameterValueException("Invalid value for 
action: " + cmd.getAction());
+            }
+        }
+
+        String description = cmd.getDescription();
+
+        String cmdTimeZone = cmd.getTimeZone();
+
+        Date startDate = Objects.requireNonNullElse(cmd.getStartDate(), 
vmSchedule.getStartDate());
+
+        Date endDate = cmd.getEndDate();
+        if (endDate == null && vmSchedule.getEndDate() != null) {
+            endDate = vmSchedule.getEndDate();
+        }
+
+        validateStartDateEndDate(startDate, endDate);
+
+        Boolean enabled = cmd.getEnabled();
+
+        if (name != null) {
+            vmSchedule.setName(name);
+        }
+        if (enabled != null) {
+            vmSchedule.setEnabled(enabled);
+        }
+        if (description != null) {
+            vmSchedule.setDescription(description);
+        }
+        if (endDate != null) {
+            vmSchedule.setEndDate(endDate);
+        }
+        if (action != null) {
+            vmSchedule.setAction(action);
+        }
+
+        vmSchedule.setStartDate(startDate);
+        if (cmdTimeZone != null) {
+            TimeZone timeZone = TimeZone.getTimeZone(cmdTimeZone);
+            String timeZoneId = timeZone.getID();
+            if (!timeZoneId.equals(cmdTimeZone)) {
+                LOGGER.warn(String.format("Using timezone [%s] for running the 
schedule [%s] for VM %s, as an equivalent of [%s].",
+                        timeZoneId, vmSchedule.getSchedule(), 
vmSchedule.getVmId(), cmdTimeZone));
+            }
+            vmSchedule.setTimeZone(timeZoneId);
+        }
+        vmSchedule.setSchedule(cronExpression.toString());
+
+        return Transaction.execute((TransactionCallback<VMScheduleResponse>) 
status -> {
+            vmScheduleDao.update(cmd.getId(), vmSchedule);
+            vmScheduler.updateScheduledJob(vmSchedule);
+            return createResponse(vmSchedule);
+        });
+    }
+
+    private void validateStartDateEndDate(Date startDate, Date endDate) {
+        Date now = new Date();
+        if (startDate.before(now)) {
+            throw new InvalidParameterValueException(String.format("Invalid 
value for start date. Start date [%s] can't be less than current time [%s].", 
startDate, now));
+        }
+
+        if (endDate != null) {
+            if (endDate.before(now)) {
+                throw new 
InvalidParameterValueException(String.format("Invalid value for end date. End 
date [%s] can't be less than current time [%s].", endDate, now));
+            }
+            if (endDate.before(startDate)) {
+                throw new 
InvalidParameterValueException(String.format("Invalid value for end date. End 
date [%s] can't be less than start date [%s].", endDate, startDate));
+            }
+        }
+    }
+
+    @Override
+    public long removeScheduleByVmId(long vmId, boolean expunge) {

Review Comment:
   In what all cases do we publish this event?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to