|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package org.apache.cloudstack.schedule.autoscale; |
| 18 | + |
| 19 | +import com.cloud.event.ActionEventUtils; |
| 20 | +import com.cloud.exception.InvalidParameterValueException; |
| 21 | +import com.cloud.network.as.AutoScaleManager; |
| 22 | +import com.cloud.network.as.AutoScaleVmGroup; |
| 23 | +import com.cloud.network.as.AutoScaleVmGroupVO; |
| 24 | +import com.cloud.network.as.dao.AutoScaleVmGroupDao; |
| 25 | +import com.cloud.user.User; |
| 26 | +import org.apache.cloudstack.api.ApiCommandResourceType; |
| 27 | +import org.apache.cloudstack.api.command.user.autoscale.UpdateAutoScaleVmGroupCmd; |
| 28 | +import org.apache.cloudstack.schedule.BaseScheduleWorker; |
| 29 | +import org.apache.cloudstack.schedule.ResourceSchedule; |
| 30 | +import org.apache.cloudstack.schedule.ResourceScheduledJobVO; |
| 31 | +import org.apache.cloudstack.schedule.dao.ResourceScheduleDetailsDao; |
| 32 | +import org.apache.commons.collections4.MapUtils; |
| 33 | +import org.apache.commons.lang3.EnumUtils; |
| 34 | +import org.apache.commons.lang3.StringUtils; |
| 35 | + |
| 36 | +import javax.inject.Inject; |
| 37 | +import java.util.Arrays; |
| 38 | +import java.util.HashMap; |
| 39 | +import java.util.Map; |
| 40 | + |
| 41 | +import static org.apache.cloudstack.api.ApiConstants.MAX_MEMBERS; |
| 42 | +import static org.apache.cloudstack.api.ApiConstants.MIN_MEMBERS; |
| 43 | + |
| 44 | +public class AutoScaleScheduleWorker extends BaseScheduleWorker { |
| 45 | + |
| 46 | + @Inject |
| 47 | + private AutoScaleManager autoScaleManager; |
| 48 | + |
| 49 | + @Inject |
| 50 | + private AutoScaleVmGroupDao autoScaleVmGroupDao; |
| 51 | + |
| 52 | + @Inject |
| 53 | + private ResourceScheduleDetailsDao resourceScheduleDetailsDao; |
| 54 | + |
| 55 | + @Override |
| 56 | + public ApiCommandResourceType getApiResourceType() { |
| 57 | + return ApiCommandResourceType.AutoScaleVmGroup; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean isResourceValid(long resourceId) { |
| 62 | + AutoScaleVmGroupVO group = autoScaleVmGroupDao.findById(resourceId); |
| 63 | + return group != null && !AutoScaleVmGroup.State.REVOKE.equals(group.getState()); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public long getEntityOwnerId(long resourceId) { |
| 68 | + AutoScaleVmGroupVO group = autoScaleVmGroupDao.findById(resourceId); |
| 69 | + return group != null ? group.getAccountId() : User.UID_SYSTEM; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public AutoScaleScheduleAction parseAction(String actionName) { |
| 74 | + AutoScaleScheduleAction action = EnumUtils.getEnumIgnoreCase(AutoScaleScheduleAction.class, actionName); |
| 75 | + if (action == null) { |
| 76 | + throw new InvalidParameterValueException(String.format( |
| 77 | + "Invalid action for AutoScaleVmGroup schedule: %s. Supported actions: %s", |
| 78 | + actionName, Arrays.toString(AutoScaleScheduleAction.values()))); |
| 79 | + } |
| 80 | + return action; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void validateDetails(ResourceSchedule.Action action, Map<String, String> details) { |
| 85 | + if (!(action instanceof AutoScaleScheduleAction)) { |
| 86 | + throw new InvalidParameterValueException("Invalid action type for AutoScaleVmGroup schedule"); |
| 87 | + } |
| 88 | + if (MapUtils.isEmpty(details)) { |
| 89 | + throw new InvalidParameterValueException("Details are required for AutoScaleVmGroup schedule"); |
| 90 | + } |
| 91 | + if (!details.keySet().stream().allMatch(key -> MIN_MEMBERS.equalsIgnoreCase(key) || MAX_MEMBERS.equalsIgnoreCase(key))) { |
| 92 | + throw new InvalidParameterValueException("Only minmembers and maxmembers are supported for AutoScaleVmGroup schedule details"); |
| 93 | + } |
| 94 | + |
| 95 | + String minMembersRaw = details.get(MIN_MEMBERS); |
| 96 | + String maxMembersRaw = details.get(MAX_MEMBERS); |
| 97 | + if (StringUtils.isBlank(minMembersRaw) || StringUtils.isBlank(maxMembersRaw)) { |
| 98 | + throw new InvalidParameterValueException("Both minmembers and maxmembers are required for AutoScaleVmGroup schedule"); |
| 99 | + } |
| 100 | + |
| 101 | + int minMembers; |
| 102 | + int maxMembers; |
| 103 | + try { |
| 104 | + minMembers = Integer.parseInt(minMembersRaw); |
| 105 | + maxMembers = Integer.parseInt(maxMembersRaw); |
| 106 | + } catch (NumberFormatException e) { |
| 107 | + throw new InvalidParameterValueException("minmembers and maxmembers must be valid integers"); |
| 108 | + } |
| 109 | + |
| 110 | + autoScaleManager.validateMinMaxMembers(minMembers, maxMembers); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + protected Long processJob(ResourceScheduledJobVO job) { |
| 115 | + AutoScaleVmGroupVO group = autoScaleVmGroupDao.findById(job.getResourceId()); |
| 116 | + if (group == null || AutoScaleVmGroup.State.REVOKE.equals(group.getState())) { |
| 117 | + logger.warn("AutoScaleVmGroup id={} not found/invalid; skipping scheduled job {}", job.getResourceId(), job); |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + AutoScaleScheduleAction action = parseAction(job.getActionName()); |
| 122 | + Map<String, String> details = resourceScheduleDetailsDao.listDetailsKeyPairs(job.getScheduleId(), true); |
| 123 | + validateDetails(action, details); |
| 124 | + |
| 125 | + long eventId = ActionEventUtils.onCompletedActionEvent( |
| 126 | + User.UID_SYSTEM, group.getAccountId(), null, |
| 127 | + action.getEventType(), true, |
| 128 | + String.format("Executing action (%s) for AutoScaleVmGroup: %s", action, group.getUuid()), |
| 129 | + group.getId(), ApiCommandResourceType.AutoScaleVmGroup.toString(), 0); |
| 130 | + |
| 131 | + Map<String, String> params = new HashMap<>(); |
| 132 | + params.put(MIN_MEMBERS, details.get(MIN_MEMBERS)); |
| 133 | + params.put(MAX_MEMBERS, details.get(MAX_MEMBERS)); |
| 134 | + return submitAsyncJob(UpdateAutoScaleVmGroupCmd.class, group.getAccountId(), group.getId(), eventId, params); |
| 135 | + } |
| 136 | +} |
0 commit comments