|
33 | 33 | import org.apache.cloudstack.api.response.UserVmResponse; |
34 | 34 | import org.apache.cloudstack.api.response.VolumeResponse; |
35 | 35 | import org.apache.cloudstack.context.CallContext; |
| 36 | +import org.apache.commons.collections.CollectionUtils; |
| 37 | +import org.apache.commons.collections.MapUtils; |
| 38 | +import org.apache.commons.lang3.BooleanUtils; |
36 | 39 |
|
37 | 40 | import com.cloud.exception.ConcurrentOperationException; |
38 | 41 | import com.cloud.exception.InsufficientCapacityException; |
|
41 | 44 | import com.cloud.exception.ResourceUnavailableException; |
42 | 45 | import com.cloud.uservm.UserVm; |
43 | 46 | import com.cloud.utils.exception.CloudRuntimeException; |
| 47 | +import com.cloud.utils.net.Dhcp; |
| 48 | +import com.cloud.utils.net.NetUtils; |
| 49 | +import com.cloud.utils.StringUtils; |
44 | 50 | import com.cloud.vm.VirtualMachine; |
45 | 51 |
|
46 | 52 | @APICommand(name = "deployVirtualMachine", description = "Creates and automatically starts an Instance based on a service offering, disk offering, and Template.", responseObject = UserVmResponse.class, responseView = ResponseView.Restricted, entityType = {VirtualMachine.class}, |
@@ -69,6 +75,138 @@ public class DeployVMCmd extends BaseDeployVMCmd { |
69 | 75 | /////////////////// Accessors /////////////////////// |
70 | 76 | ///////////////////////////////////////////////////// |
71 | 77 |
|
| 78 | + public String getAccountName() { |
| 79 | + if (accountName == null) { |
| 80 | + return CallContext.current().getCallingAccount().getAccountName(); |
| 81 | + } |
| 82 | + return accountName; |
| 83 | + } |
| 84 | + |
| 85 | + public Long getDiskOfferingId() { |
| 86 | + return diskOfferingId; |
| 87 | + } |
| 88 | + |
| 89 | + public String getDeploymentPlanner() { |
| 90 | + return deploymentPlanner; |
| 91 | + } |
| 92 | + |
| 93 | + public String getDisplayName() { |
| 94 | + if (StringUtils.isEmpty(displayName)) { |
| 95 | + displayName = name; |
| 96 | + } |
| 97 | + return displayName; |
| 98 | + } |
| 99 | + |
| 100 | + public Long getDomainId() { |
| 101 | + if (domainId == null) { |
| 102 | + return CallContext.current().getCallingAccount().getDomainId(); |
| 103 | + } |
| 104 | + return domainId; |
| 105 | + } |
| 106 | + |
| 107 | + public ApiConstants.BootType getBootType() { |
| 108 | + if (StringUtils.isNotBlank(bootType)) { |
| 109 | + try { |
| 110 | + String type = bootType.trim().toUpperCase(); |
| 111 | + return ApiConstants.BootType.valueOf(type); |
| 112 | + } catch (IllegalArgumentException e) { |
| 113 | + String errMesg = "Invalid bootType " + bootType + "Specified for Instance " + getName() |
| 114 | + + " Valid values are: " + Arrays.toString(ApiConstants.BootType.values()); |
| 115 | + logger.warn(errMesg); |
| 116 | + throw new InvalidParameterValueException(errMesg); |
| 117 | + } |
| 118 | + } |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + public ApiConstants.BootMode getBootMode() { |
| 123 | + if (StringUtils.isNotBlank(bootMode)) { |
| 124 | + try { |
| 125 | + String mode = bootMode.trim().toUpperCase(); |
| 126 | + return ApiConstants.BootMode.valueOf(mode); |
| 127 | + } catch (IllegalArgumentException e) { |
| 128 | + String msg = String.format("Invalid %s: %s specified for Instance: %s. Valid values are: %s", |
| 129 | + ApiConstants.BOOT_MODE, bootMode, getName(), Arrays.toString(ApiConstants.BootMode.values())); |
| 130 | + logger.error(msg); |
| 131 | + throw new InvalidParameterValueException(msg); |
| 132 | + } |
| 133 | + } |
| 134 | + if (ApiConstants.BootType.UEFI.equals(getBootType())) { |
| 135 | + String msg = String.format("%s must be specified for the Instance with boot type: %s. Valid values are: %s", |
| 136 | + ApiConstants.BOOT_MODE, getBootType(), Arrays.toString(ApiConstants.BootMode.values())); |
| 137 | + logger.error(msg); |
| 138 | + throw new InvalidParameterValueException(msg); |
| 139 | + } |
| 140 | + return null; |
| 141 | + } |
| 142 | + |
| 143 | + public Map<String, String> getVmProperties() { |
| 144 | + Map<String, String> map = new HashMap<>(); |
| 145 | + if (MapUtils.isNotEmpty(vAppProperties)) { |
| 146 | + Collection parameterCollection = vAppProperties.values(); |
| 147 | + Iterator iterator = parameterCollection.iterator(); |
| 148 | + while (iterator.hasNext()) { |
| 149 | + HashMap<String, String> entry = (HashMap<String, String>)iterator.next(); |
| 150 | + map.put(entry.get("key"), entry.get("value")); |
| 151 | + } |
| 152 | + } |
| 153 | + return map; |
| 154 | + } |
| 155 | + |
| 156 | + public Map<Integer, Long> getVmNetworkMap() { |
| 157 | + Map<Integer, Long> map = new HashMap<>(); |
| 158 | + if (MapUtils.isNotEmpty(vAppNetworks)) { |
| 159 | + Collection parameterCollection = vAppNetworks.values(); |
| 160 | + Iterator iterator = parameterCollection.iterator(); |
| 161 | + while (iterator.hasNext()) { |
| 162 | + HashMap<String, String> entry = (HashMap<String, String>) iterator.next(); |
| 163 | + Integer nic; |
| 164 | + try { |
| 165 | + nic = Integer.valueOf(entry.get(VmDetailConstants.NIC)); |
| 166 | + } catch (NumberFormatException nfe) { |
| 167 | + nic = null; |
| 168 | + } |
| 169 | + String networkUuid = entry.get(VmDetailConstants.NETWORK); |
| 170 | + if (logger.isTraceEnabled()) { |
| 171 | + logger.trace(String.format("nic, '%s', goes on net, '%s'", nic, networkUuid)); |
| 172 | + } |
| 173 | + if (nic == null || StringUtils.isEmpty(networkUuid) || _entityMgr.findByUuid(Network.class, networkUuid) == null) { |
| 174 | + throw new InvalidParameterValueException(String.format("Network ID: %s for NIC ID: %s is invalid", networkUuid, nic)); |
| 175 | + } |
| 176 | + map.put(nic, _entityMgr.findByUuid(Network.class, networkUuid).getId()); |
| 177 | + } |
| 178 | + } |
| 179 | + return map; |
| 180 | + } |
| 181 | + |
| 182 | + public String getGroup() { |
| 183 | + return group; |
| 184 | + } |
| 185 | + |
| 186 | + public HypervisorType getHypervisor() { |
| 187 | + return HypervisorType.getType(hypervisor); |
| 188 | + } |
| 189 | + |
| 190 | + public Boolean isDisplayVm() { |
| 191 | + return displayVm; |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public boolean isDisplay() { |
| 196 | + if(displayVm == null) |
| 197 | + return true; |
| 198 | + else |
| 199 | + return displayVm; |
| 200 | + } |
| 201 | + |
| 202 | + public List<String> getSecurityGroupNameList() { |
| 203 | + return securityGroupNameList; |
| 204 | + } |
| 205 | + |
| 206 | + public List<Long> getSecurityGroupIdList() { |
| 207 | + return securityGroupIdList; |
| 208 | + } |
| 209 | + |
72 | 210 | public Long getServiceOfferingId() { |
73 | 211 | return serviceOfferingId; |
74 | 212 | } |
|
0 commit comments