|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.cloudstack.api.command.admin.cluster; |
| 21 | + |
| 22 | +import com.cloud.event.EventTypes; |
| 23 | +import com.cloud.exception.InvalidParameterValueException; |
| 24 | +import com.cloud.host.Host; |
| 25 | +import com.cloud.user.Account; |
| 26 | +import com.cloud.utils.UuidUtils; |
| 27 | +import com.cloud.vm.VirtualMachine; |
| 28 | +import org.apache.cloudstack.api.APICommand; |
| 29 | +import org.apache.cloudstack.api.ApiCommandResourceType; |
| 30 | +import org.apache.cloudstack.api.ApiConstants; |
| 31 | +import org.apache.cloudstack.api.BaseAsyncCmd; |
| 32 | +import org.apache.cloudstack.api.Parameter; |
| 33 | +import org.apache.cloudstack.api.response.ClusterDrsPlanResponse; |
| 34 | +import org.apache.cloudstack.api.response.ClusterResponse; |
| 35 | +import org.apache.cloudstack.cluster.ClusterDrsService; |
| 36 | +import org.apache.commons.collections.MapUtils; |
| 37 | + |
| 38 | +import javax.inject.Inject; |
| 39 | +import java.util.Collection; |
| 40 | +import java.util.HashMap; |
| 41 | +import java.util.Iterator; |
| 42 | +import java.util.Map; |
| 43 | + |
| 44 | +@APICommand(name = "executeClusterDrsPlan", |
| 45 | + description = "Execute DRS for a cluster. If there is another plan in progress for the same cluster, " + |
| 46 | + "this command will fail.", |
| 47 | + responseObject = ClusterDrsPlanResponse.class, since = "4.19.0", requestHasSensitiveInfo = false, |
| 48 | + responseHasSensitiveInfo = false) |
| 49 | +public class ExecuteClusterDrsPlanCmd extends BaseAsyncCmd { |
| 50 | + |
| 51 | + @Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = ClusterResponse.class, required = true, |
| 52 | + description = "ID of cluster") |
| 53 | + private Long id; |
| 54 | + |
| 55 | + @Parameter( |
| 56 | + name = ApiConstants.MIGRATE_TO, |
| 57 | + type = CommandType.MAP, |
| 58 | + description = "Virtual Machine to destination host mapping. This parameter specifies the mapping between " + |
| 59 | + "a vm and a host to migrate that VM. clusterid is required if this parameter is set." + |
| 60 | + "Format of this parameter: migrateto[vm-index].vm=<uuid>&migrateto[vm-index].host=<uuid> " + |
| 61 | + "Where, [vm-index] indicates the index to identify the vm that you want to migrate, " + |
| 62 | + "vm=<uuid> indicates the UUID of the vm that you want to migrate, and " + |
| 63 | + "host=<uuid> indicates the UUID of the host where you want to migrate the vm. " + |
| 64 | + "Example: migrateto[0].vm=<71f43cd6-69b0-4d3b-9fbc-67f50963d60b>" + |
| 65 | + "&migrateto[0].host=<a382f181-3d2b-4413-b92d-b8931befa7e1>" + |
| 66 | + "&migrateto[1].vm=<88de0173-55c0-4c1c-a269-83d0279eeedf>" + |
| 67 | + "&migrateto[1].host=<95d6e97c-6766-4d67-9a30-c449c15011d1>" + |
| 68 | + "&migrateto[2].vm=<1b331390-59f2-4796-9993-bf11c6e76225>" + |
| 69 | + "&migrateto[2].host=<41fdb564-9d3b-447d-88ed-7628f7640cbc>") |
| 70 | + private Map<String, String> migrateVmTo; |
| 71 | + |
| 72 | + @Inject |
| 73 | + private ClusterDrsService clusterDrsService; |
| 74 | + |
| 75 | + public Map<VirtualMachine, Host> getVmToHostMap() { |
| 76 | + Map<VirtualMachine, Host> vmToHostMap = new HashMap<>(); |
| 77 | + if (MapUtils.isNotEmpty(migrateVmTo)) { |
| 78 | + Collection<?> allValues = migrateVmTo.values(); |
| 79 | + Iterator<?> iter = allValues.iterator(); |
| 80 | + while (iter.hasNext()) { |
| 81 | + HashMap<String, String> vmToHost = (HashMap<String, String>) iter.next(); |
| 82 | + |
| 83 | + String vmId = vmToHost.get("vm"); |
| 84 | + String hostId = vmToHost.get("host"); |
| 85 | + |
| 86 | + VirtualMachine vm; |
| 87 | + Host host; |
| 88 | + if (UuidUtils.isUuid(vmId)) { |
| 89 | + vm = _entityMgr.findByUuid(VirtualMachine.class, vmId); |
| 90 | + } else { |
| 91 | + vm = _entityMgr.findById(VirtualMachine.class, Long.parseLong(vmId)); |
| 92 | + } |
| 93 | + |
| 94 | + if (UuidUtils.isUuid(hostId)) { |
| 95 | + host = _entityMgr.findByUuid(Host.class, hostId); |
| 96 | + } else { |
| 97 | + host = _entityMgr.findById(Host.class, Long.parseLong(hostId)); |
| 98 | + } |
| 99 | + |
| 100 | + if (vm == null || host == null) { |
| 101 | + throw new InvalidParameterValueException( |
| 102 | + String.format("Unable to find the vm/host for vmId=%s, destHostId=%s", vmId, hostId)); |
| 103 | + } |
| 104 | + |
| 105 | + vmToHostMap.put(vm, host); |
| 106 | + } |
| 107 | + } |
| 108 | + return vmToHostMap; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void execute() { |
| 113 | + ClusterDrsPlanResponse response = clusterDrsService.executeDrsPlan(this); |
| 114 | + response.setResponseName(getCommandName()); |
| 115 | + this.setResponseObject(response); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public long getEntityOwnerId() { |
| 120 | + return Account.ACCOUNT_ID_SYSTEM; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public Long getApiResourceId() { |
| 125 | + return getId(); |
| 126 | + } |
| 127 | + |
| 128 | + public Long getId() { |
| 129 | + return id; |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public ApiCommandResourceType getApiResourceType() { |
| 134 | + return ApiCommandResourceType.Cluster; |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + @Override |
| 139 | + public String getEventType() { |
| 140 | + return EventTypes.EVENT_CLUSTER_DRS; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public String getEventDescription() { |
| 145 | + return String.format("Executing DRS plan for cluster: %d", getId()); |
| 146 | + } |
| 147 | +} |
0 commit comments