VM ingestion (#3606)

The VM ingestion feature allows CloudStack to discover, on-board, import existing VMs in an infra. The feature currently works only for VMware, with a hypervisor agnostic framework which may be extended for KVM and XenServer in future.
This commit is contained in:
Abhishek Kumar 2020-02-03 20:13:52 +05:30 committed by GitHub
parent 6d014fa219
commit 0f5b0e67f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
41 changed files with 4166 additions and 299 deletions

View File

@ -97,6 +97,7 @@ public class EventTypes {
public static final String EVENT_VM_MOVE = "VM.MOVE";
public static final String EVENT_VM_RESTORE = "VM.RESTORE";
public static final String EVENT_VM_EXPUNGE = "VM.EXPUNGE";
public static final String EVENT_VM_IMPORT = "VM.IMPORT";
// Domain Router
public static final String EVENT_ROUTER_CREATE = "ROUTER.CREATE";
@ -596,6 +597,7 @@ public class EventTypes {
entityEventDetails.put(EVENT_VM_MOVE, VirtualMachine.class);
entityEventDetails.put(EVENT_VM_RESTORE, VirtualMachine.class);
entityEventDetails.put(EVENT_VM_EXPUNGE, VirtualMachine.class);
entityEventDetails.put(EVENT_VM_IMPORT, VirtualMachine.class);
entityEventDetails.put(EVENT_ROUTER_CREATE, VirtualRouter.class);
entityEventDetails.put(EVENT_ROUTER_DESTROY, VirtualRouter.class);

View File

@ -513,4 +513,8 @@ public interface UserVmService {
void collectVmNetworkStatistics (UserVm userVm);
UserVm importVM(final DataCenter zone, final Host host, final VirtualMachineTemplate template, final String instanceName, final String displayName, final Account owner, final String userData, final Account caller, final Boolean isDisplayVm, final String keyboard,
final long accountId, final long userId, final ServiceOffering serviceOffering, final String sshPublicKey,
final String hostName, final HypervisorType hypervisorType, final Map<String, String> customParameters, final VirtualMachine.PowerState powerState) throws InsufficientCapacityException;
}

View File

@ -54,4 +54,12 @@ public interface VmDetailConstants {
String SSH_PUBLIC_KEY = "SSH.PublicKey";
String PASSWORD = "password";
String ENCRYPTED_PASSWORD = "Encrypted.Password";
// VM import with nic, disk and custom params for custom compute offering
String NIC = "nic";
String NETWORK = "network";
String IP4_ADDRESS = "ip4Address";
String IP6_ADDRESS = "ip6Address";
String DISK = "disk";
String DISK_OFFERING = "diskOffering";
}

View File

@ -22,6 +22,7 @@ public class ApiConstants {
public static final String ACCOUNT_TYPE = "accounttype";
public static final String ACCOUNT_ID = "accountid";
public static final String ACTIVITY = "activity";
public static final String ADAPTER_TYPE = "adaptertype";
public static final String ADDRESS = "address";
public static final String ALGORITHM = "algorithm";
public static final String ALLOCATED_ONLY = "allocatedonly";
@ -43,6 +44,7 @@ public class ApiConstants {
public static final String BYTES_WRITE_RATE_MAX = "byteswriteratemax";
public static final String BYTES_WRITE_RATE_MAX_LENGTH = "byteswriteratemaxlength";
public static final String BYPASS_VLAN_OVERLAP_CHECK = "bypassvlanoverlapcheck";
public static final String CAPACITY = "capacity";
public static final String CATEGORY = "category";
public static final String CAN_REVERT = "canrevert";
public static final String CA_CERTIFICATES = "cacertificates";
@ -50,9 +52,15 @@ public class ApiConstants {
public static final String CERTIFICATE_CHAIN = "certchain";
public static final String CERTIFICATE_FINGERPRINT = "fingerprint";
public static final String CERTIFICATE_ID = "certid";
public static final String CONTROLLER = "controller";
public static final String CONTROLLER_UNIT = "controllerunit";
public static final String COPY_IMAGE_TAGS = "copyimagetags";
public static final String CSR = "csr";
public static final String PRIVATE_KEY = "privatekey";
public static final String DATASTORE_HOST = "datastorehost";
public static final String DATASTORE_NAME = "datastorename";
public static final String DATASTORE_PATH = "datastorepath";
public static final String DATASTORE_TYPE = "datastoretype";
public static final String DOMAIN_SUFFIX = "domainsuffix";
public static final String DNS_SEARCH_ORDER = "dnssearchorder";
public static final String CHAIN_INFO = "chaininfo";
@ -69,6 +77,7 @@ public class ApiConstants {
public static final String COMMAND = "command";
public static final String CMD_EVENT_TYPE = "cmdeventtype";
public static final String COMPONENT = "component";
public static final String CPU_CORE_PER_SOCKET = "cpucorepersocket";
public static final String CPU_NUMBER = "cpunumber";
public static final String CPU_SPEED = "cpuspeed";
public static final String CPU_LOAD_AVERAGE = "cpuloadaverage";
@ -91,6 +100,7 @@ public class ApiConstants {
public static final String DETAILS = "details";
public static final String DEVICE_ID = "deviceid";
public static final String DIRECT_DOWNLOAD = "directdownload";
public static final String DISK = "disk";
public static final String DISK_OFFERING_ID = "diskofferingid";
public static final String NEW_DISK_OFFERING_ID = "newdiskofferingid";
public static final String DISK_KBS_READ = "diskkbsread";
@ -170,6 +180,7 @@ public class ApiConstants {
public static final String PREVIOUS_ACL_RULE_ID = "previousaclruleid";
public static final String NEXT_ACL_RULE_ID = "nextaclruleid";
public static final String MOVE_ACL_CONSISTENCY_HASH = "aclconsistencyhash";
public static final String IMAGE_PATH = "imagepath";
public static final String INTERNAL_DNS1 = "internaldns1";
public static final String INTERNAL_DNS2 = "internaldns2";
public static final String INTERVAL_TYPE = "intervaltype";
@ -181,6 +192,7 @@ public class ApiConstants {
public static final String IOPS_WRITE_RATE_MAX = "iopswriteratemax";
public static final String IOPS_WRITE_RATE_MAX_LENGTH = "iopswriteratemaxlength";
public static final String IP_ADDRESS = "ipaddress";
public static final String IP_ADDRESSES = "ipaddresses";
public static final String IP6_ADDRESS = "ip6address";
public static final String IP_ADDRESS_ID = "ipaddressid";
public static final String IS_ASYNC = "isasync";
@ -224,6 +236,9 @@ public class ApiConstants {
public static final String NETWORK_DOMAIN = "networkdomain";
public static final String NETMASK = "netmask";
public static final String NEW_NAME = "newname";
public static final String NIC = "nic";
public static final String NIC_NETWORK_LIST = "nicnetworklist";
public static final String NIC_IP_ADDRESS_LIST = "nicipaddresslist";
public static final String NUM_RETRIES = "numretries";
public static final String OFFER_HA = "offerha";
public static final String IS_SYSTEM_OFFERING = "issystem";
@ -231,6 +246,7 @@ public class ApiConstants {
public static final String OLD_FORMAT = "oldformat";
public static final String OP = "op";
public static final String OS_CATEGORY_ID = "oscategoryid";
public static final String OS_ID = "osid";
public static final String OS_TYPE_ID = "ostypeid";
public static final String OS_DISPLAY_NAME = "osdisplayname";
public static final String OS_NAME_FOR_HYPERVISOR = "osnameforhypervisor";
@ -255,6 +271,7 @@ public class ApiConstants {
public static final String PORTAL = "portal";
public static final String PORTABLE_IP_ADDRESS = "portableipaddress";
public static final String PORT_FORWARDING_SERVICE_ID = "portforwardingserviceid";
public static final String POSITION = "position";
public static final String POST_URL = "postURL";
public static final String POWER_STATE = "powerstate";
public static final String PRIVATE_INTERFACE = "privateinterface";
@ -360,6 +377,7 @@ public class ApiConstants {
public static final String REMOVE_VLAN = "removevlan";
public static final String VLAN_ID = "vlanid";
public static final String ISOLATED_PVLAN = "isolatedpvlan";
public static final String ISOLATED_PVLAN_TYPE = "isolatedpvlantype";
public static final String ISOLATION_URI = "isolationuri";
public static final String VM_AVAILABLE = "vmavailable";
public static final String VM_LIMIT = "vmlimit";
@ -531,6 +549,7 @@ public class ApiConstants {
public static final String CUSTOM_DISK_OFF_MAX_SIZE = "customdiskofferingmaxsize";
public static final String DEFAULT_ZONE_ID = "defaultzoneid";
public static final String LIVE_MIGRATE = "livemigrate";
public static final String MIGRATE_ALLOWED = "migrateallowed";
public static final String MIGRATE_TO = "migrateto";
public static final String GUID = "guid";
public static final String VSWITCH_TYPE_GUEST_TRAFFIC = "guestvswitchtype";

View File

@ -0,0 +1,300 @@
// 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.api.command.admin.vm;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseAsyncCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ResponseObject;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.ClusterResponse;
import org.apache.cloudstack.api.response.DomainResponse;
import org.apache.cloudstack.api.response.ProjectResponse;
import org.apache.cloudstack.api.response.ServiceOfferingResponse;
import org.apache.cloudstack.api.response.TemplateResponse;
import org.apache.cloudstack.api.response.UserVmResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.vm.VmImportService;
import org.apache.commons.collections.MapUtils;
import org.apache.log4j.Logger;
import com.cloud.event.EventTypes;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientCapacityException;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.NetworkRuleConflictException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.network.Network;
import com.cloud.offering.DiskOffering;
import com.cloud.user.Account;
import com.cloud.utils.net.NetUtils;
import com.cloud.vm.VmDetailConstants;
import com.google.common.base.Strings;
@APICommand(name = ImportUnmanagedInstanceCmd.API_NAME,
description = "Import unmanaged virtual machine from a given cluster.",
responseObject = UserVmResponse.class,
responseView = ResponseObject.ResponseView.Full,
requestHasSensitiveInfo = false,
responseHasSensitiveInfo = true,
authorized = {RoleType.Admin},
since = "4.14.0")
public class ImportUnmanagedInstanceCmd extends BaseAsyncCmd {
public static final Logger LOGGER = Logger.getLogger(ImportUnmanagedInstanceCmd.class);
public static final String API_NAME = "importUnmanagedInstance";
@Inject
public VmImportService vmImportService;
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////
@Parameter(name = ApiConstants.CLUSTER_ID,
type = CommandType.UUID,
entityType = ClusterResponse.class,
required = true,
description = "the cluster ID")
private Long clusterId;
@Parameter(name = ApiConstants.NAME,
type = CommandType.STRING,
required = true,
description = "the hypervisor name of the instance")
private String name;
@Parameter(name = ApiConstants.DISPLAY_NAME,
type = CommandType.STRING,
description = "the display name of the instance")
private String displayName;
@Parameter(name = ApiConstants.HOST_NAME,
type = CommandType.STRING,
description = "the host name of the instance")
private String hostName;
@Parameter(name = ApiConstants.ACCOUNT,
type = CommandType.STRING,
description = "an optional account for the virtual machine. Must be used with domainId.")
private String accountName;
@Parameter(name = ApiConstants.DOMAIN_ID,
type = CommandType.UUID,
entityType = DomainResponse.class,
description = "import instance to the domain specified")
private Long domainId;
@Parameter(name = ApiConstants.PROJECT_ID,
type = CommandType.UUID,
entityType = ProjectResponse.class,
description = "import instance for the project")
private Long projectId;
@Parameter(name = ApiConstants.TEMPLATE_ID,
type = CommandType.UUID,
entityType = TemplateResponse.class,
description = "the ID of the template for the virtual machine")
private Long templateId;
@Parameter(name = ApiConstants.SERVICE_OFFERING_ID,
type = CommandType.UUID,
entityType = ServiceOfferingResponse.class,
required = true,
description = "the ID of the service offering for the virtual machine")
private Long serviceOfferingId;
@Parameter(name = ApiConstants.NIC_NETWORK_LIST,
type = CommandType.MAP,
description = "VM nic to network id mapping using keys nic and network")
private Map nicNetworkList;
@Parameter(name = ApiConstants.NIC_IP_ADDRESS_LIST,
type = CommandType.MAP,
description = "VM nic to ip address mapping using keys nic, ip4Address")
private Map nicIpAddressList;
@Parameter(name = ApiConstants.DATADISK_OFFERING_LIST,
type = CommandType.MAP,
description = "datadisk template to disk-offering mapping using keys disk and diskOffering")
private Map dataDiskToDiskOfferingList;
@Parameter(name = ApiConstants.DETAILS,
type = CommandType.MAP,
description = "used to specify the custom parameters.")
private Map<String, String> details;
@Parameter(name = ApiConstants.MIGRATE_ALLOWED,
type = CommandType.BOOLEAN,
description = "vm and its volumes are allowed to migrate to different host/pool when offerings passed are incompatible with current host/pool")
private Boolean migrateAllowed;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
public Long getClusterId() {
return clusterId;
}
public String getName() {
return name;
}
public String getDisplayName() {
return displayName;
}
public String getHostName() {
return hostName;
}
public String getAccountName() {
return accountName;
}
public Long getDomainId() {
return domainId;
}
public Long getTemplateId() {
return templateId;
}
public Long getProjectId() {
return projectId;
}
public Long getServiceOfferingId() {
return serviceOfferingId;
}
public Map<String, Long> getNicNetworkList() {
Map<String, Long> nicNetworkMap = new HashMap<>();
if (MapUtils.isNotEmpty(nicNetworkList)) {
for (Map<String, String> entry : (Collection<Map<String, String>>)nicNetworkList.values()) {
String nic = entry.get(VmDetailConstants.NIC);
String networkUuid = entry.get(VmDetailConstants.NETWORK);
if (Strings.isNullOrEmpty(nic) || Strings.isNullOrEmpty(networkUuid) || _entityMgr.findByUuid(Network.class, networkUuid) == null) {
throw new InvalidParameterValueException(String.format("Network ID: %s for NIC ID: %s is invalid", networkUuid, nic));
}
nicNetworkMap.put(nic, _entityMgr.findByUuid(Network.class, networkUuid).getId());
}
}
return nicNetworkMap;
}
public Map<String, Network.IpAddresses> getNicIpAddressList() {
Map<String, Network.IpAddresses> nicIpAddressMap = new HashMap<>();
if (MapUtils.isNotEmpty(nicIpAddressList)) {
for (Map<String, String> entry : (Collection<Map<String, String>>)nicIpAddressList.values()) {
String nic = entry.get(VmDetailConstants.NIC);
String ipAddress = Strings.emptyToNull(entry.get(VmDetailConstants.IP4_ADDRESS));
if (Strings.isNullOrEmpty(nic)) {
throw new InvalidParameterValueException(String.format("NIC ID: '%s' is invalid for IP address mapping", nic));
}
if (Strings.isNullOrEmpty(ipAddress)) {
throw new InvalidParameterValueException(String.format("IP address '%s' for NIC ID: %s is invalid", ipAddress, nic));
}
if (!Strings.isNullOrEmpty(ipAddress) && !ipAddress.equals("auto") && !NetUtils.isValidIp4(ipAddress)) {
throw new InvalidParameterValueException(String.format("IP address '%s' for NIC ID: %s is invalid", ipAddress, nic));
}
Network.IpAddresses ipAddresses = new Network.IpAddresses(ipAddress, null);
nicIpAddressMap.put(nic, ipAddresses);
}
}
return nicIpAddressMap;
}
public Map<String, Long> getDataDiskToDiskOfferingList() {
Map<String, Long> dataDiskToDiskOfferingMap = new HashMap<>();
if (MapUtils.isNotEmpty(dataDiskToDiskOfferingList)) {
for (Map<String, String> entry : (Collection<Map<String, String>>)dataDiskToDiskOfferingList.values()) {
String nic = entry.get(VmDetailConstants.DISK);
String offeringUuid = entry.get(VmDetailConstants.DISK_OFFERING);
if (Strings.isNullOrEmpty(nic) || Strings.isNullOrEmpty(offeringUuid) || _entityMgr.findByUuid(DiskOffering.class, offeringUuid) == null) {
throw new InvalidParameterValueException(String.format("Disk offering ID: %s for disk ID: %s is invalid", offeringUuid, nic));
}
dataDiskToDiskOfferingMap.put(nic, _entityMgr.findByUuid(DiskOffering.class, offeringUuid).getId());
}
}
return dataDiskToDiskOfferingMap;
}
public Map<String, String> getDetails() {
if (MapUtils.isEmpty(details)) {
return new HashMap<String, String>();
}
Collection<String> paramsCollection = details.values();
Map<String, String> params = (Map<String, String>) (paramsCollection.toArray())[0];
return params;
}
public Boolean getMigrateAllowed() {
return migrateAllowed == Boolean.TRUE;
}
@Override
public String getEventType() {
return EventTypes.EVENT_VM_IMPORT;
}
@Override
public String getEventDescription() {
return "Importing unmanaged VM";
}
/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
UserVmResponse response = vmImportService.importUnmanagedInstance(this);
response.setResponseName(getCommandName());
setResponseObject(response);
}
@Override
public String getCommandName() {
return API_NAME.toLowerCase() + BaseAsyncCmd.RESPONSE_SUFFIX;
}
@Override
public long getEntityOwnerId() {
Long accountId = _accountService.finalyzeAccountId(accountName, domainId, projectId, true);
if (accountId == null) {
Account account = CallContext.current().getCallingAccount();
if (account != null) {
accountId = account.getId();
} else {
accountId = Account.ACCOUNT_ID_SYSTEM;
}
}
return accountId;
}
}

View File

@ -0,0 +1,113 @@
// 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.api.command.admin.vm;
import javax.inject.Inject;
import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseAsyncCmd;
import org.apache.cloudstack.api.BaseListCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ResponseObject;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.ClusterResponse;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.UnmanagedInstanceResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.vm.UnmanagedInstanceTO;
import org.apache.cloudstack.vm.VmImportService;
import org.apache.log4j.Logger;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientCapacityException;
import com.cloud.exception.NetworkRuleConflictException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.user.Account;
@APICommand(name = ListUnmanagedInstancesCmd.API_NAME,
description = "Lists unmanaged virtual machines for a given cluster.",
responseObject = UnmanagedInstanceResponse.class,
responseView = ResponseObject.ResponseView.Full,
entityType = {UnmanagedInstanceTO.class},
requestHasSensitiveInfo = false,
responseHasSensitiveInfo = true,
authorized = {RoleType.Admin},
since = "4.14.0")
public class ListUnmanagedInstancesCmd extends BaseListCmd {
public static final Logger LOGGER = Logger.getLogger(ListUnmanagedInstancesCmd.class.getName());
public static final String API_NAME = "listUnmanagedInstances";
@Inject
public VmImportService vmImportService;
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////
@Parameter(name = ApiConstants.CLUSTER_ID,
type = CommandType.UUID,
entityType = ClusterResponse.class,
required = true,
description = "the cluster ID")
private Long clusterId;
@Parameter(name = ApiConstants.NAME,
type = CommandType.STRING,
description = "the hypervisor name of the instance")
private String name;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
public Long getClusterId() {
return clusterId;
}
public String getName() {
return name;
}
/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
ListResponse<UnmanagedInstanceResponse> response = vmImportService.listUnmanagedInstances(this);
response.setResponseName(getCommandName());
setResponseObject(response);
}
@Override
public String getCommandName() {
return API_NAME.toLowerCase() + BaseAsyncCmd.RESPONSE_SUFFIX;
}
@Override
public long getEntityOwnerId() {
Account account = CallContext.current().getCallingAccount();
if (account != null) {
return account.getId();
}
return Account.ACCOUNT_ID_SYSTEM;
}
}

View File

@ -266,6 +266,10 @@ public class DomainRouterResponse extends BaseResponse implements ControlledView
this.gateway = gateway;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

View File

@ -16,14 +16,15 @@
// under the License.
package org.apache.cloudstack.api.response;
import com.cloud.serializer.Param;
import com.cloud.vm.Nic;
import com.google.gson.annotations.SerializedName;
import java.util.List;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseResponse;
import org.apache.cloudstack.api.EntityReference;
import java.util.List;
import com.cloud.serializer.Param;
import com.cloud.vm.Nic;
import com.google.gson.annotations.SerializedName;
@SuppressWarnings("unused")
@EntityReference(value = Nic.class)
@ -113,6 +114,26 @@ public class NicResponse extends BaseResponse {
@Param(description = "Id of the NSX Logical Switch Port (if NSX based), null otherwise", since="4.6.0")
private String nsxLogicalSwitchPort;
@SerializedName(ApiConstants.VLAN_ID)
@Param(description = "ID of the VLAN/VNI if available", since="4.14.0")
private Integer vlanId;
@SerializedName(ApiConstants.ISOLATED_PVLAN)
@Param(description = "the isolated private VLAN if available", since="4.14.0")
private Integer isolatedPvlanId;
@SerializedName(ApiConstants.ISOLATED_PVLAN_TYPE)
@Param(description = "the isolated private VLAN type if available", since="4.14.0")
private String isolatedPvlanType;
@SerializedName(ApiConstants.ADAPTER_TYPE)
@Param(description = "Type of adapter if available", since="4.14.0")
private String adapterType;
@SerializedName(ApiConstants.IP_ADDRESSES)
@Param(description = "IP addresses associated with NIC found for unmanaged VM", since="4.14.0")
private List<String> ipAddresses;
public void setVmId(String vmId) {
this.vmId = vmId;
}
@ -303,4 +324,44 @@ public class NicResponse extends BaseResponse {
public String getNsxLogicalSwitchPort() {
return nsxLogicalSwitchPort;
}
public Integer getVlanId() {
return vlanId;
}
public void setVlanId(Integer vlanId) {
this.vlanId = vlanId;
}
public Integer getIsolatedPvlanId() {
return isolatedPvlanId;
}
public void setIsolatedPvlanId(Integer isolatedPvlanId) {
this.isolatedPvlanId = isolatedPvlanId;
}
public String getIsolatedPvlanType() {
return isolatedPvlanType;
}
public void setIsolatedPvlanType(String isolatedPvlanType) {
this.isolatedPvlanType = isolatedPvlanType;
}
public String getAdapterType() {
return adapterType;
}
public void setAdapterType(String adapterType) {
this.adapterType = adapterType;
}
public List<String> getIpAddresses() {
return ipAddresses;
}
public void setIpAddresses(List<String> ipAddresses) {
this.ipAddresses = ipAddresses;
}
}

View File

@ -0,0 +1,159 @@
// 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.api.response;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseResponse;
import com.cloud.serializer.Param;
import com.google.gson.annotations.SerializedName;
public class UnmanagedInstanceDiskResponse extends BaseResponse {
@SerializedName(ApiConstants.ID)
@Param(description = "the ID of the disk")
private String diskId;
@SerializedName(ApiConstants.LABEL)
@Param(description = "the label of the disk")
private String label;
@SerializedName(ApiConstants.CAPACITY)
@Param(description = "the capacity of the disk in bytes")
private Long capacity;
@SerializedName(ApiConstants.IMAGE_PATH)
@Param(description = "the file path of the disk image")
private String imagePath;
@SerializedName(ApiConstants.CONTROLLER)
@Param(description = "the controller of the disk")
private String controller;
@SerializedName(ApiConstants.CONTROLLER_UNIT)
@Param(description = "the controller unit of the disk")
private Integer controllerUnit;
@SerializedName(ApiConstants.POSITION)
@Param(description = "the position of the disk")
private Integer position;
@SerializedName(ApiConstants.DATASTORE_NAME)
@Param(description = "the controller of the disk")
private String datastoreName;
@SerializedName(ApiConstants.DATASTORE_HOST)
@Param(description = "the controller of the disk")
private String datastoreHost;
@SerializedName(ApiConstants.DATASTORE_PATH)
@Param(description = "the controller of the disk")
private String datastorePath;
@SerializedName(ApiConstants.DATASTORE_TYPE)
@Param(description = "the controller of the disk")
private String datastoreType;
public String getDiskId() {
return diskId;
}
public void setDiskId(String diskId) {
this.diskId = diskId;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public Long getCapacity() {
return capacity;
}
public void setCapacity(Long capacity) {
this.capacity = capacity;
}
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
public String getController() {
return controller;
}
public void setController(String controller) {
this.controller = controller;
}
public Integer getControllerUnit() {
return controllerUnit;
}
public void setControllerUnit(Integer controllerUnit) {
this.controllerUnit = controllerUnit;
}
public Integer getPosition() {
return position;
}
public void setPosition(Integer position) {
this.position = position;
}
public String getDatastoreName() {
return datastoreName;
}
public void setDatastoreName(String datastoreName) {
this.datastoreName = datastoreName;
}
public String getDatastoreHost() {
return datastoreHost;
}
public void setDatastoreHost(String datastoreHost) {
this.datastoreHost = datastoreHost;
}
public String getDatastorePath() {
return datastorePath;
}
public void setDatastorePath(String datastorePath) {
this.datastorePath = datastorePath;
}
public String getDatastoreType() {
return datastoreType;
}
public void setDatastoreType(String datastoreType) {
this.datastoreType = datastoreType;
}
}

View File

@ -0,0 +1,190 @@
// 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.api.response;
import java.util.LinkedHashSet;
import java.util.Set;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseResponse;
import org.apache.cloudstack.api.EntityReference;
import org.apache.cloudstack.vm.UnmanagedInstanceTO;
import com.cloud.serializer.Param;
import com.google.gson.annotations.SerializedName;
@EntityReference(value = UnmanagedInstanceTO.class)
public class UnmanagedInstanceResponse extends BaseResponse {
@SerializedName(ApiConstants.NAME)
@Param(description = "the name of the virtual machine")
private String name;
@SerializedName(ApiConstants.CLUSTER_ID)
@Param(description = "the ID of the cluster to which virtual machine belongs")
private String clusterId;
@SerializedName(ApiConstants.HOST_ID)
@Param(description = "the ID of the host to which virtual machine belongs")
private String hostId;
@SerializedName(ApiConstants.POWER_STATE)
@Param(description = "the power state of the virtual machine")
private String powerState;
@SerializedName(ApiConstants.CPU_NUMBER)
@Param(description = "the CPU cores of the virtual machine")
private Integer cpuCores;
@SerializedName(ApiConstants.CPU_CORE_PER_SOCKET)
@Param(description = "the CPU cores per socket for the virtual machine. VMware specific")
private Integer cpuCoresPerSocket;
@SerializedName(ApiConstants.CPU_SPEED)
@Param(description = "the CPU speed of the virtual machine")
private Integer cpuSpeed;
@SerializedName(ApiConstants.MEMORY)
@Param(description = "the memory of the virtual machine in MB")
private Integer memory;
@SerializedName(ApiConstants.OS_ID)
@Param(description = "the operating system ID of the virtual machine")
private String operatingSystemId;
@SerializedName(ApiConstants.OS_DISPLAY_NAME)
@Param(description = "the operating system of the virtual machine")
private String operatingSystem;
@SerializedName(ApiConstants.DISK)
@Param(description = "the list of disks associated with the virtual machine", responseObject = UnmanagedInstanceDiskResponse.class)
private Set<UnmanagedInstanceDiskResponse> disks;
@SerializedName(ApiConstants.NIC)
@Param(description = "the list of nics associated with the virtual machine", responseObject = NicResponse.class)
private Set<NicResponse> nics;
public UnmanagedInstanceResponse() {
disks = new LinkedHashSet<UnmanagedInstanceDiskResponse>();
nics = new LinkedHashSet<NicResponse>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClusterId() {
return clusterId;
}
public void setClusterId(String clusterId) {
this.clusterId = clusterId;
}
public String getHostId() {
return hostId;
}
public void setHostId(String hostId) {
this.hostId = hostId;
}
public String getPowerState() {
return powerState;
}
public void setPowerState(String powerState) {
this.powerState = powerState;
}
public Integer getCpuCores() {
return cpuCores;
}
public void setCpuCores(Integer cpuCores) {
this.cpuCores = cpuCores;
}
public Integer getCpuCoresPerSocket() {
return cpuCoresPerSocket;
}
public void setCpuCoresPerSocket(Integer cpuCoresPerSocket) {
this.cpuCoresPerSocket = cpuCoresPerSocket;
}
public Integer getCpuSpeed() {
return cpuSpeed;
}
public void setCpuSpeed(Integer cpuSpeed) {
this.cpuSpeed = cpuSpeed;
}
public Integer getMemory() {
return memory;
}
public void setMemory(Integer memory) {
this.memory = memory;
}
public String getOperatingSystemId() {
return operatingSystemId;
}
public void setOperatingSystemId(String operatingSystemId) {
this.operatingSystemId = operatingSystemId;
}
public String getOperatingSystem() {
return operatingSystem;
}
public void setOperatingSystem(String operatingSystem) {
this.operatingSystem = operatingSystem;
}
public Set<UnmanagedInstanceDiskResponse> getDisks() {
return disks;
}
public void setDisks(Set<UnmanagedInstanceDiskResponse> disks) {
this.disks = disks;
}
public void addDisk(UnmanagedInstanceDiskResponse disk) {
this.disks.add(disk);
}
public Set<NicResponse> getNics() {
return nics;
}
public void setNics(Set<NicResponse> nics) {
this.nics = nics;
}
public void addNic(NicResponse nic) {
this.nics.add(nic);
}
}

View File

@ -0,0 +1,353 @@
// 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;
import java.util.List;
public class UnmanagedInstanceTO {
public enum PowerState {
PowerUnknown,
PowerOn,
PowerOff
}
private String name;
private PowerState powerState;
private Integer cpuCores;
private Integer cpuCoresPerSocket;
private Integer memory;
private Integer cpuSpeed;
private String operatingSystemId;
private String operatingSystem;
private List<Disk> disks;
private List<Nic> nics;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public PowerState getPowerState() {
return powerState;
}
public void setPowerState(PowerState powerState) {
this.powerState = powerState;
}
public Integer getCpuCores() {
return cpuCores;
}
public void setCpuCores(Integer cpuCores) {
this.cpuCores = cpuCores;
}
public Integer getCpuCoresPerSocket() {
return cpuCoresPerSocket;
}
public void setCpuCoresPerSocket(Integer cpuCoresPerSocket) {
this.cpuCoresPerSocket = cpuCoresPerSocket;
}
public Integer getMemory() {
return memory;
}
public void setMemory(Integer memory) {
this.memory = memory;
}
public Integer getCpuSpeed() {
return cpuSpeed;
}
public void setCpuSpeed(Integer cpuSpeed) {
this.cpuSpeed = cpuSpeed;
}
public String getOperatingSystemId() {
return operatingSystemId;
}
public void setOperatingSystemId(String operatingSystemId) {
this.operatingSystemId = operatingSystemId;
}
public String getOperatingSystem() {
return operatingSystem;
}
public void setOperatingSystem(String operatingSystem) {
this.operatingSystem = operatingSystem;
}
public List<Disk> getDisks() {
return disks;
}
public void setDisks(List<Disk> disks) {
this.disks = disks;
}
public List<Nic> getNics() {
return nics;
}
public void setNics(List<Nic> nics) {
this.nics = nics;
}
public static class Disk {
private String diskId;
private String label;
private Long capacity;
private String fileBaseName;
private String imagePath;
private String controller;
private Integer controllerUnit;
private Integer position;
private String chainInfo;
private String datastoreName;
private String datastoreHost;
private String datastorePath;
private String datastoreType;
public String getDiskId() {
return diskId;
}
public void setDiskId(String diskId) {
this.diskId = diskId;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public Long getCapacity() {
return capacity;
}
public void setCapacity(Long capacity) {
this.capacity = capacity;
}
public String getFileBaseName() {
return fileBaseName;
}
public void setFileBaseName(String fileBaseName) {
this.fileBaseName = fileBaseName;
}
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
public String getController() {
return controller;
}
public void setController(String controller) {
this.controller = controller;
}
public Integer getControllerUnit() {
return controllerUnit;
}
public void setControllerUnit(Integer controllerUnit) {
this.controllerUnit = controllerUnit;
}
public Integer getPosition() {
return position;
}
public void setPosition(Integer position) {
this.position = position;
}
public String getChainInfo() {
return chainInfo;
}
public void setChainInfo(String chainInfo) {
this.chainInfo = chainInfo;
}
public String getDatastoreName() {
return datastoreName;
}
public void setDatastoreName(String datastoreName) {
this.datastoreName = datastoreName;
}
public String getDatastoreHost() {
return datastoreHost;
}
public void setDatastoreHost(String datastoreHost) {
this.datastoreHost = datastoreHost;
}
public String getDatastorePath() {
return datastorePath;
}
public void setDatastorePath(String datastorePath) {
this.datastorePath = datastorePath;
}
public String getDatastoreType() {
return datastoreType;
}
public void setDatastoreType(String datastoreType) {
this.datastoreType = datastoreType;
}
}
public static class Nic {
private String nicId;
private String adapterType;
private String macAddress;
private String network;
private Integer vlan;
private Integer pvlan;
private String pvlanType;
private List<String> ipAddress;
private String pciSlot;
public String getNicId() {
return nicId;
}
public void setNicId(String nicId) {
this.nicId = nicId;
}
public String getAdapterType() {
return adapterType;
}
public void setAdapterType(String adapterType) {
this.adapterType = adapterType;
}
public String getMacAddress() {
return macAddress;
}
public void setMacAddress(String macAddress) {
this.macAddress = macAddress;
}
public String getNetwork() {
return network;
}
public void setNetwork(String network) {
this.network = network;
}
public Integer getVlan() {
return vlan;
}
public void setVlan(Integer vlan) {
this.vlan = vlan;
}
public Integer getPvlan() {
return pvlan;
}
public void setPvlan(Integer pvlan) {
this.pvlan = pvlan;
}
public String getPvlanType() {
return pvlanType;
}
public void setPvlanType(String pvlanType) {
this.pvlanType = pvlanType;
}
public List<String> getIpAddress() {
return ipAddress;
}
public void setIpAddress(List<String> ipAddress) {
this.ipAddress = ipAddress;
}
public String getPciSlot() {
return pciSlot;
}
public void setPciSlot(String pciSlot) {
this.pciSlot = pciSlot;
}
}
}

View File

@ -0,0 +1,31 @@
// 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;
import org.apache.cloudstack.api.command.admin.vm.ImportUnmanagedInstanceCmd;
import org.apache.cloudstack.api.command.admin.vm.ListUnmanagedInstancesCmd;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.UnmanagedInstanceResponse;
import org.apache.cloudstack.api.response.UserVmResponse;
import com.cloud.utils.component.PluggableService;
public interface VmImportService extends PluggableService {
ListResponse<UnmanagedInstanceResponse> listUnmanagedInstances(ListUnmanagedInstancesCmd cmd);
UserVmResponse importUnmanagedInstance(ImportUnmanagedInstanceCmd cmd);
}

View File

@ -0,0 +1,58 @@
// 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 com.cloud.agent.api;
import java.util.HashMap;
import org.apache.cloudstack.vm.UnmanagedInstanceTO;
@LogLevel(LogLevel.Log4jLevel.Trace)
public class GetUnmanagedInstancesAnswer extends Answer {
private String instanceName;
private HashMap<String, UnmanagedInstanceTO> unmanagedInstances;
GetUnmanagedInstancesAnswer() {
}
public GetUnmanagedInstancesAnswer(GetUnmanagedInstancesCommand cmd, String details, HashMap<String, UnmanagedInstanceTO> unmanagedInstances) {
super(cmd, true, details);
this.instanceName = cmd.getInstanceName();
this.unmanagedInstances = unmanagedInstances;
}
public String getInstanceName() {
return instanceName;
}
public void setInstanceName(String instanceName) {
this.instanceName = instanceName;
}
public HashMap<String, UnmanagedInstanceTO> getUnmanagedInstances() {
return unmanagedInstances;
}
public void setUnmanagedInstances(HashMap<String, UnmanagedInstanceTO> unmanagedInstances) {
this.unmanagedInstances = unmanagedInstances;
}
public String getString() {
return "GetUnmanagedInstancesAnswer [instanceName=" + instanceName + "]";
}
}

View File

@ -0,0 +1,73 @@
// 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 com.cloud.agent.api;
import java.util.List;
/**
* Unmanaged instances are those virtual machines which are present at hypervisor end but not available in CloudStack.
* Such virtual machines might have been created out of band, directly from hypervisor.
* Managed instances will be those virtual machines that are created from CloudStack or CloudStack have database record for them.
* All managed instances will be filtered while trying to find unmanaged instances.
*/
@LogLevel(LogLevel.Log4jLevel.Trace)
public class GetUnmanagedInstancesCommand extends Command {
String instanceName;
List<String> managedInstancesNames;
public GetUnmanagedInstancesCommand() {
}
public GetUnmanagedInstancesCommand(String instanceName) {
this.instanceName = instanceName;
}
public String getInstanceName() {
return instanceName;
}
public void setInstanceName(String instanceName) {
this.instanceName = instanceName;
}
public List<String> getManagedInstancesNames() {
return managedInstancesNames;
}
public void setManagedInstancesNames(List<String> managedInstancesNames) {
this.managedInstancesNames = managedInstancesNames;
}
public boolean hasManagedInstance(String name) {
if (managedInstancesNames!=null && !managedInstancesNames.isEmpty()) {
return managedInstancesNames.contains(name);
}
return false;
}
@Override
public boolean executeInSequence() {
return false;
}
public String getString() {
return "GetUnmanagedInstancesCommand [instanceName=" + instanceName + "]";
}
}

View File

@ -26,6 +26,7 @@
/usr/share/cloudstack-common/scripts/vm/hypervisor/kvm/*
/usr/share/cloudstack-common/scripts/vm/hypervisor/update_host_passwd.sh
/usr/share/cloudstack-common/scripts/vm/hypervisor/versions.sh
/usr/share/cloudstack-common/scripts/vm/hypervisor/vmware/*
/usr/share/cloudstack-common/scripts/vm/hypervisor/xenserver/*
/usr/share/cloudstack-common/lib/*
/usr/bin/cloudstack-set-guest-password

View File

@ -23,6 +23,7 @@ import java.util.Map;
import org.apache.cloudstack.acl.ControlledEntity.ACLType;
import org.apache.cloudstack.framework.config.ConfigKey;
import org.apache.cloudstack.framework.config.ConfigKey.Scope;
import com.cloud.deploy.DataCenterDeployment;
import com.cloud.deploy.DeployDestination;
import com.cloud.deploy.DeploymentPlan;
@ -313,4 +314,6 @@ public interface NetworkOrchestrationService {
* Remove entry from /etc/dhcphosts and /etc/hosts on virtual routers
*/
void cleanupNicDhcpDnsEntry(Network network, VirtualMachineProfile vmProfile, NicProfile nicProfile);
Pair<NicProfile, Integer> importNic(final String macAddress, int deviceId, final Network network, final Boolean isDefaultNic, final VirtualMachine vm, final Network.IpAddresses ipAddresses) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException;
}

View File

@ -88,6 +88,8 @@ public interface VolumeOrchestrationService {
Volume migrateVolume(Volume volume, StoragePool destPool) throws StorageUnavailableException;
Volume liveMigrateVolume(Volume volume, StoragePool destPool);
void cleanupStorageJobs();
void destroyVolume(Volume volume);
@ -127,4 +129,24 @@ public interface VolumeOrchestrationService {
StoragePool findStoragePool(DiskProfile dskCh, DataCenter dc, Pod pod, Long clusterId, Long hostId, VirtualMachine vm, Set<StoragePool> avoid);
void updateVolumeDiskChain(long volumeId, String path, String chainInfo);
/**
* Imports an existing volume for a VM into database. Useful while ingesting an unmanaged VM.
* @param type Type of the volume - ROOT, DATADISK, etc
* @param name Name of the volume
* @param offering DiskOffering for the volume
* @param size DiskOffering for the volume
* @param minIops minimum IOPS for the disk, if not passed DiskOffering value will be used
* @param maxIops maximum IOPS for the disk, if not passed DiskOffering value will be used
* @param vm VirtualMachine this volume is attached to
* @param template Template of the VM of the volume
* @param owner owner Account for the volume
* @param deviceId device ID of the virtual disk
* @param poolId ID of pool in which volume is stored
* @param path image path of the volume
* @param chainInfo chain info for the volume. Hypervisor specific.
* @return DiskProfile of imported volume
*/
DiskProfile importVolume(Type type, String name, DiskOffering offering, Long size, Long minIops, Long maxIops, VirtualMachine vm, VirtualMachineTemplate template,
Account owner, Long deviceId, Long poolId, String path, String chainInfo);
}

View File

@ -3952,6 +3952,71 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra
return _nicDao.persist(nic);
}
@DB
@Override
public Pair<NicProfile, Integer> importNic(final String macAddress, int deviceId, final Network network, final Boolean isDefaultNic, final VirtualMachine vm, final Network.IpAddresses ipAddresses)
throws ConcurrentOperationException, InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException {
s_logger.debug("Allocating nic for vm " + vm.getUuid() + " in network " + network + " during import");
String guestIp = null;
if (ipAddresses != null && !Strings.isNullOrEmpty(ipAddresses.getIp4Address())) {
if (ipAddresses.getIp4Address().equals("auto")) {
ipAddresses.setIp4Address(null);
}
if (network.getGuestType() != GuestType.L2) {
guestIp = _ipAddrMgr.acquireGuestIpAddress(network, ipAddresses.getIp4Address());
} else {
guestIp = null;
}
if (guestIp == null && network.getGuestType() != GuestType.L2 && !_networkModel.listNetworkOfferingServices(network.getNetworkOfferingId()).isEmpty()) {
throw new InsufficientVirtualNetworkCapacityException("Unable to acquire Guest IP address for network " + network, DataCenter.class,
network.getDataCenterId());
}
}
final String finalGuestIp = guestIp;
final NicVO vo = Transaction.execute(new TransactionCallback<NicVO>() {
@Override
public NicVO doInTransaction(TransactionStatus status) {
NicVO vo = new NicVO(network.getGuruName(), vm.getId(), network.getId(), vm.getType());
vo.setMacAddress(macAddress);
vo.setAddressFormat(Networks.AddressFormat.Ip4);
if (NetUtils.isValidIp4(finalGuestIp) && !Strings.isNullOrEmpty(network.getGateway())) {
vo.setIPv4Address(finalGuestIp);
vo.setIPv4Gateway(network.getGateway());
if (!Strings.isNullOrEmpty(network.getCidr())) {
vo.setIPv4Netmask(NetUtils.cidr2Netmask(network.getCidr()));
}
}
vo.setBroadcastUri(network.getBroadcastUri());
vo.setMode(network.getMode());
vo.setState(Nic.State.Reserved);
vo.setReservationStrategy(ReservationStrategy.Start);
vo.setReservationId(UUID.randomUUID().toString());
vo.setIsolationUri(network.getBroadcastUri());
vo.setDeviceId(deviceId);
vo.setDefaultNic(isDefaultNic);
vo = _nicDao.persist(vo);
int count = 1;
if (vo.getVmType() == VirtualMachine.Type.User) {
s_logger.debug("Changing active number of nics for network id=" + network.getUuid() + " on " + count);
_networksDao.changeActiveNicsBy(network.getId(), count);
}
if (vo.getVmType() == VirtualMachine.Type.User
|| vo.getVmType() == VirtualMachine.Type.DomainRouter && _networksDao.findById(network.getId()).getTrafficType() == TrafficType.Guest) {
_networksDao.setCheckForGc(network.getId());
}
return vo;
}
});
final Integer networkRate = _networkModel.getNetworkRate(network.getId(), vm.getId());
final NicProfile vmNic = new NicProfile(vo, network, vo.getBroadcastUri(), vo.getIsolationUri(), networkRate, _networkModel.isSecurityGroupSupportedInNetwork(network),
_networkModel.getNetworkTag(vm.getHypervisorType(), network));
return new Pair<NicProfile, Integer>(vmNic, Integer.valueOf(deviceId));
}
@Override
public String getConfigComponentName() {
return NetworkOrchestrationService.class.getSimpleName();

View File

@ -1005,10 +1005,12 @@ public class VolumeOrchestrator extends ManagerBase implements VolumeOrchestrati
}
}
@Override
@DB
protected Volume liveMigrateVolume(Volume volume, StoragePool destPool) {
public Volume liveMigrateVolume(Volume volume, StoragePool destPool) {
VolumeInfo vol = volFactory.getVolume(volume.getId());
AsyncCallFuture<VolumeApiResult> future = volService.migrateVolume(vol, (DataStore)destPool);
DataStore dataStoreTarget = dataStoreMgr.getDataStore(destPool.getId(), DataStoreRole.Primary);
AsyncCallFuture<VolumeApiResult> future = volService.migrateVolume(vol, dataStoreTarget);
try {
VolumeApiResult result = future.get();
if (result.isFailed()) {
@ -1626,4 +1628,56 @@ public class VolumeOrchestrator extends ManagerBase implements VolumeOrchestrati
_volsDao.update(volumeId, vol);
}
}
@Override
public DiskProfile importVolume(Type type, String name, DiskOffering offering, Long size, Long minIops, Long maxIops,
VirtualMachine vm, VirtualMachineTemplate template, Account owner,
Long deviceId, Long poolId, String path, String chainInfo) {
if (size == null) {
size = offering.getDiskSize();
} else {
size = (size * 1024 * 1024 * 1024);
}
minIops = minIops != null ? minIops : offering.getMinIops();
maxIops = maxIops != null ? maxIops : offering.getMaxIops();
VolumeVO vol = new VolumeVO(type, name, vm.getDataCenterId(), owner.getDomainId(), owner.getId(), offering.getId(), offering.getProvisioningType(), size, minIops, maxIops, null);
if (vm != null) {
vol.setInstanceId(vm.getId());
}
if (deviceId != null) {
vol.setDeviceId(deviceId);
} else if (type.equals(Type.ROOT)) {
vol.setDeviceId(0l);
} else {
vol.setDeviceId(1l);
}
if (template != null) {
if (ImageFormat.ISO.equals(template.getFormat())) {
vol.setIsoId(template.getId());
} else if (Storage.TemplateType.DATADISK.equals(template.getTemplateType())) {
vol.setTemplateId(template.getId());
}
if (type == Type.ROOT) {
vol.setTemplateId(template.getId());
}
}
// display flag matters only for the User vms
if (VirtualMachine.Type.User.equals(vm.getType())) {
UserVmVO userVm = _userVmDao.findById(vm.getId());
vol.setDisplayVolume(userVm.isDisplayVm());
}
vol.setFormat(getSupportedImageFormatForCluster(vm.getHypervisorType()));
vol.setPoolId(poolId);
vol.setPath(path);
vol.setChainInfo(chainInfo);
vol.setState(Volume.State.Ready);
vol = _volsDao.persist(vol);
return toDiskProfile(vol, offering);
}
}

View File

@ -48,7 +48,7 @@ public interface ServiceOfferingDao extends GenericDao<ServiceOfferingVO, Long>
boolean isDynamic(long serviceOfferingId);
ServiceOfferingVO getcomputeOffering(ServiceOfferingVO serviceOffering, Map<String, String> customParameters);
ServiceOfferingVO getComputeOffering(ServiceOfferingVO serviceOffering, Map<String, String> customParameters);
ServiceOfferingVO findDefaultSystemOffering(String offeringName, Boolean useLocalStorage);
}

View File

@ -161,7 +161,7 @@ public class ServiceOfferingDaoImpl extends GenericDaoBase<ServiceOfferingVO, Lo
throw new CloudRuntimeException("missing argument vmId");
}
Map<String, String> dynamicOffering = userVmDetailsDao.listDetailsKeyPairs(vmId);
return getcomputeOffering(offering, dynamicOffering);
return getComputeOffering(offering, dynamicOffering);
}
return offering;
}
@ -175,7 +175,7 @@ public class ServiceOfferingDaoImpl extends GenericDaoBase<ServiceOfferingVO, Lo
throw new CloudRuntimeException("missing argument vmId");
}
Map<String, String> dynamicOffering = userVmDetailsDao.listDetailsKeyPairs(vmId);
return getcomputeOffering(offering, dynamicOffering);
return getComputeOffering(offering, dynamicOffering);
}
return offering;
}
@ -187,7 +187,7 @@ public class ServiceOfferingDaoImpl extends GenericDaoBase<ServiceOfferingVO, Lo
}
@Override
public ServiceOfferingVO getcomputeOffering(ServiceOfferingVO serviceOffering, Map<String, String> customParameters) {
public ServiceOfferingVO getComputeOffering(ServiceOfferingVO serviceOffering, Map<String, String> customParameters) {
ServiceOfferingVO dummyoffering = new ServiceOfferingVO(serviceOffering);
dummyoffering.setDynamicFlag(true);
if (customParameters.containsKey(UsageEventVO.DynamicParameters.cpuNumber.name())) {

View File

@ -230,6 +230,13 @@ public class VMTemplateVO implements VirtualMachineTemplate {
return template;
}
public static VMTemplateVO createSystemIso(Long id, String uniqueName, String name, boolean isPublic,
String url, boolean requiresHvm, int bits, long accountId, String cksum,
String displayText, boolean enablePassword, long guestOSId) {
return new VMTemplateVO(id, uniqueName, name, ImageFormat.ISO, isPublic, false, TemplateType.SYSTEM, url, null, requiresHvm, bits, accountId, cksum, displayText, enablePassword,
guestOSId, false, null);
}
public VMTemplateVO(Long id, String uniqueName, String name, ImageFormat format, boolean isPublic, boolean featured, TemplateType type, String url, Date created,
boolean requiresHvm, int bits, long accountId, String cksum, String displayText, boolean enablePassword, long guestOSId, boolean bootable,
HypervisorType hyperType) {

View File

@ -29,4 +29,6 @@ public interface GuestOSHypervisorDao extends GenericDao<GuestOSHypervisorVO, Lo
boolean removeGuestOsMapping(Long id);
GuestOSHypervisorVO findByOsIdAndHypervisorAndUserDefined(long guestOsId, String hypervisorType, String hypervisorVersion, boolean isUserDefined);
GuestOSHypervisorVO findByOsNameAndHypervisor(String guestOsName, String hypervisorType, String hypervisorVersion);
}

View File

@ -17,12 +17,14 @@
package com.cloud.storage.dao;
import java.util.Date;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.storage.GuestOSHypervisorVO;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
@ -33,6 +35,7 @@ public class GuestOSHypervisorDaoImpl extends GenericDaoBase<GuestOSHypervisorVO
protected final SearchBuilder<GuestOSHypervisorVO> guestOsSearch;
protected final SearchBuilder<GuestOSHypervisorVO> mappingSearch;
protected final SearchBuilder<GuestOSHypervisorVO> userDefinedMappingSearch;
protected final SearchBuilder<GuestOSHypervisorVO> guestOsNameSearch;
protected GuestOSHypervisorDaoImpl() {
guestOsSearch = createSearchBuilder();
@ -51,6 +54,12 @@ public class GuestOSHypervisorDaoImpl extends GenericDaoBase<GuestOSHypervisorVO
userDefinedMappingSearch.and("hypervisor_version", userDefinedMappingSearch.entity().getHypervisorVersion(), SearchCriteria.Op.EQ);
userDefinedMappingSearch.and("is_user_defined", userDefinedMappingSearch.entity().getIsUserDefined(), SearchCriteria.Op.EQ);
userDefinedMappingSearch.done();
guestOsNameSearch = createSearchBuilder();
guestOsNameSearch.and("guest_os_name", guestOsNameSearch.entity().getGuestOsName(), SearchCriteria.Op.EQ);
guestOsNameSearch.and("hypervisor_type", guestOsNameSearch.entity().getHypervisorType(), SearchCriteria.Op.EQ);
guestOsNameSearch.and("hypervisor_version", guestOsNameSearch.entity().getHypervisorVersion(), SearchCriteria.Op.EQ);
guestOsNameSearch.done();
}
@Override
@ -97,4 +106,19 @@ public class GuestOSHypervisorDaoImpl extends GenericDaoBase<GuestOSHypervisorVO
return super.remove(id);
}
@Override
public GuestOSHypervisorVO findByOsNameAndHypervisor(String guestOsName, String hypervisorType, String hypervisorVersion) {
SearchCriteria<GuestOSHypervisorVO> sc = guestOsNameSearch.create();
String version = "default";
if (!(hypervisorVersion == null || hypervisorVersion.isEmpty())) {
version = hypervisorVersion;
}
sc.setParameters("guest_os_name", guestOsName);
sc.setParameters("hypervisor_type", hypervisorType);
sc.setParameters("hypervisor_version", version);
final Filter filter = new Filter(GuestOSHypervisorVO.class, "guestOsId", true, null, null);
List<GuestOSHypervisorVO> results = listIncludingRemovedBy(sc, filter);
return CollectionUtils.isNotEmpty(results) ? results.get(0) : null;
}
}

View File

@ -96,6 +96,8 @@ public interface VolumeDao extends GenericDao<VolumeVO, Long>, StateDao<Volume.S
List<Long> listZoneWidePoolIdsByVolumeCount(long dcId, long accountId);
List<VolumeVO> findIncludingRemovedByZone(long zoneId);
/**
* Gets the Total Primary Storage space allocated for an account
*

View File

@ -598,6 +598,13 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> implements Vol
}
}
@Override
public List<VolumeVO> findIncludingRemovedByZone(long zoneId) {
SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
sc.setParameters("dcId", zoneId);
return searchIncludingRemoved(sc, null, null, false);
}
@Override
@DB()
public Pair<Long, Long> getNonDestroyedCountAndTotalByPool(long poolId) {

View File

@ -100,6 +100,8 @@ public interface VMInstanceDao extends GenericDao<VMInstanceVO, Long>, StateDao<
List<VMInstanceVO> listByLastHostId(Long hostId);
List<VMInstanceVO> listByLastHostIdAndStates(Long hostId, State... states);
List<VMInstanceVO> listByTypeAndState(VirtualMachine.Type type, State state);
List<VMInstanceVO> listByAccountId(long accountId);

View File

@ -94,6 +94,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem
protected SearchBuilder<VMInstanceVO> HostAndStateSearch;
protected SearchBuilder<VMInstanceVO> StartingWithNoHostSearch;
protected SearchBuilder<VMInstanceVO> NotMigratingSearch;
protected SearchBuilder<VMInstanceVO> LastHostAndStatesSearch;
@Inject
ResourceTagDao _tagsDao;
@ -286,6 +287,11 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem
NotMigratingSearch.and("lastHost", NotMigratingSearch.entity().getLastHostId(), Op.EQ);
NotMigratingSearch.and("state", NotMigratingSearch.entity().getState(), Op.NEQ);
NotMigratingSearch.done();
LastHostAndStatesSearch = createSearchBuilder();
LastHostAndStatesSearch.and("lastHost", LastHostAndStatesSearch.entity().getLastHostId(), Op.EQ);
LastHostAndStatesSearch.and("states", LastHostAndStatesSearch.entity().getState(), Op.IN);
LastHostAndStatesSearch.done();
}
@Override
@ -560,6 +566,14 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem
return listBy(sc);
}
@Override
public List<VMInstanceVO> listByLastHostIdAndStates(Long hostId, State... states) {
SearchCriteria<VMInstanceVO> sc = LastHostAndStatesSearch.create();
sc.setParameters("lastHost", hostId);
sc.setParameters("states", (Object[])states);
return listBy(sc);
}
@Override
public List<Long> findIdsOfAllocatedVirtualRoutersForAccount(long accountId) {
SearchCriteria<Long> sc = FindIdsOfVirtualRoutersByAccount.create();

View File

@ -0,0 +1,288 @@
#!/usr/bin/env python3
# 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.
from __future__ import print_function
from pyVim.connect import SmartConnect, SmartConnectNoSSL, Disconnect
from pyVmomi import vim
import atexit
import sys
import argparse
import json
isDebugLogs = False
hostClusterNameDict = {}
pgHostNameDict = {}
networksDict = {}
def log_message(msg):
if isDebugLogs == True:
print(msg)
def get_clusters(content, cluster=None):
if cluster is not None:
log_message("Getting clusters (name=" + cluster + ") ...")
else:
log_message("Getting clusters ...")
cluster_view = content.viewManager.CreateContainerView(content.rootFolder,
[vim.ClusterComputeResource],
True)
clusters = []
if cluster is not None:
for c in cluster_view.view:
if c.name == cluster:
clusters.append(c)
hosts = c.host
for host in hosts:
hostClusterNameDict[host.name] = c.name
break
else:
for c in cluster_view.view:
clusters.append(c)
hosts = c.host
for host in hosts:
hostClusterNameDict[host.name] = c.name
cluster_view.Destroy()
log_message('\t{} cluster(s) found'.format(len(clusters)))
for c in clusters:
log_message('\t' + c.name)
return clusters
def get_vm_hosts(clusters):
log_message("Getting ESX hosts ...")
hosts = []
for cluster in clusters:
hosts.extend(cluster.host)
log_message('\t{} host(s) found'.format(len(hosts)))
for host in hosts:
log_message('\t' + host.name)
return hosts
def get_vms(content):
log_message("Getting VMs ...")
vm_view = content.viewManager.CreateContainerView(content.rootFolder,
[vim.VirtualMachine],
True)
obj = [vm for vm in vm_view.view]
vm_view.Destroy()
return obj
def get_hosts_port_groups(hosts):
log_message("Collecting portGroups on hosts. This may take a while ...")
hostPgDict = {}
for host in hosts:
pgs = host.config.network.portgroup
hostPgDict[host] = pgs
for pg in pgs:
pgHostNameDict[pg.spec.name] = host.name
log_message("\tHost {} done.".format(host.name))
log_message("\tPortgroup collection complete.")
return hostPgDict
def get_vm_info(vm, hostPgDict):
vmPowerState = vm.runtime.powerState
log_message('\tVM: ' + vm.name + '(' + vmPowerState + ')')
get_vm_nics(vm, hostPgDict)
def get_vm_nics(vm, hostPgDict):
try:
for dev in vm.config.hardware.device:
if isinstance(dev, vim.vm.device.VirtualEthernetCard):
dev_backing = dev.backing
portGroup = None
vlanId = None
isolatedPvlan = None
isolatedPvlanType = None
vSwitch = None
if hasattr(dev_backing, 'port'):
portGroupKey = dev.backing.port.portgroupKey
dvsUuid = dev.backing.port.switchUuid
try:
dvs = content.dvSwitchManager.QueryDvsByUuid(dvsUuid)
except:
log_message('\tError: Unable retrieve details for distributed vSwitch ' + dvsUuid)
portGroup = ''
vlanId = ''
vSwitch = ''
else:
pgObj = dvs.LookupDvPortGroup(portGroupKey)
portGroup = pgObj.config.name
try:
if isinstance(pgObj.config.defaultPortConfig.vlan, vim.dvs.VmwareDistributedVirtualSwitch.PvlanSpec):
for pvlanConfig in dvs.config.pvlanConfig:
if pvlanConfig.secondaryVlanId == pgObj.config.defaultPortConfig.vlan.pvlanId:
vlanId = str(pvlanConfig.primaryVlanId)
isolatedPvlanType = pvlanConfig.pvlanType
isolatedPvlan = str(pgObj.config.defaultPortConfig.vlan.pvlanId)
break
else:
vlanId = str(pgObj.config.defaultPortConfig.vlan.vlanId)
except AttributeError:
log_message('\tError: Unable retrieve details for portgroup ' + portGroup)
vlanId = ''
vSwitch = str(dvs.name)
else:
portGroup = dev.backing.network.name
vmHost = vm.runtime.host
# global variable hostPgDict stores portGroups per host
pgs = hostPgDict[vmHost]
for p in pgs:
if portGroup in p.key:
vlanId = str(p.spec.vlanId)
vSwitch = str(p.spec.vswitchName)
if portGroup is None:
portGroup = ''
if vlanId is None:
vlanId = ''
vmHostName = None
vmClusterName = None
try:
vmHostName = vm.runtime.host.name
except AttributeError:
vmHostName = ''
try:
vmClusterName = vm.runtime.host.parent.name
except AttributeError:
vmClusterName = ''
add_network(portGroup, vlanId, isolatedPvlanType, isolatedPvlan, vSwitch, vm.name, dev.deviceInfo.label, dev.macAddress, vmClusterName, vmHostName)
log_message('\t\t' + dev.deviceInfo.label + '->' + dev.macAddress +
' @ ' + vSwitch + '->' + portGroup +
' (VLAN ' + vlanId + ')')
except AttributeError:
log_message('\tError: Unable retrieve details for ' + vm.name)
def add_network(portGroup, vlanId, isolatedPvlanType, isolatedPvlan, vSwitch, vmName, vmDeviceLabel, vmMacAddress, vmClusterName, vmHostName):
key = vSwitch + '->' + portGroup + ' (VLAN ' + vlanId + ')'
device = {"label": vmDeviceLabel, "macaddress": vmMacAddress}
vm = {"name":vmName, "device": device}
if key in networksDict:
network = networksDict[key]
network["virtualmachines"].append(vm)
networksDict[key] = network
else:
vms = [vm]
try:
host = pgHostNameDict[portGroup]
except KeyError:
host = vmHostName
try:
cluster = hostClusterNameDict[host]
except KeyError:
cluster = vmClusterName
network = {"portgroup": portGroup, "cluster": cluster, "host": host, "switch": vSwitch, "virtualmachines": vms}
if vlanId != '':
network["vlanid"] = vlanId
if isolatedPvlan is not None:
network["isolatedpvlan"] = isolatedPvlan
if isolatedPvlanType is not None:
network["isolatedpvlantype"] = isolatedPvlanType
networksDict[key] = network
def get_args():
parser = argparse.ArgumentParser(
description='Arguments for talking to vCenter')
parser.add_argument('-s', '--host',
required=True,
action='store',
help='vSpehre service to connect to')
parser.add_argument('-o', '--port',
type=int,
default=443,
action='store',
help='Port to connect on')
parser.add_argument('-u', '--user',
required=True,
action='store',
help='User name to use')
parser.add_argument('-p', '--password',
required=False,
action='store',
help='Password to use')
parser.add_argument('-c', '--cluster',
required=False,
action='store',
help='Cluster for which discover networks')
parser.add_argument('-S', '--disable_ssl_verification',
required=False,
action='store_true',
help='Disable ssl host certificate verification')
parser.add_argument('-d', '--debug',
required=False,
action='store_true',
help='Debug log messages')
args = parser.parse_args()
return args
def main():
global content, isDebugLogs, hostClusterNameDict, pgHostNameDict, networksDict
args = get_args()
if args.password:
password = args.password
else:
password = getpass.getpass(prompt='Enter password for host %s and '
'user %s: ' % (args.host, args.user))
if args.debug:
isDebugLogs = True
if args.disable_ssl_verification:
serviceInstance = SmartConnectNoSSL(host=args.host,
user=args.user,
pwd=password,
port=int(args.port))
else:
serviceInstance = SmartConnect(host=args.host,
user=args.user,
pwd=password,
port=int(args.port))
atexit.register(Disconnect, serviceInstance)
content = serviceInstance.RetrieveContent()
if args.cluster:
clusters = get_clusters(content, args.cluster)
else:
clusters = get_clusters(content)
hosts = []
if len(clusters) > 0:
hosts = get_vm_hosts(clusters)
if len(hosts) > 0:
hostPgDict = get_hosts_port_groups(hosts)
vms = get_vms(content)
log_message('\t{} VM(s) found'.format(len(vms)))
for vm in vms:
get_vm_info(vm, hostPgDict)
networks = list(networksDict.values())
response = {"count": len(networks), "networks": networks}
print(json.dumps(response, indent=2, sort_keys=True))
# Main section
if __name__ == "__main__":
sys.exit(main())

View File

@ -4082,7 +4082,7 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe
if (newServiceOffering.isDynamic()) {
newServiceOffering.setDynamicFlag(true);
_userVmMgr.validateCustomParameters(newServiceOffering, customparameters);
newServiceOffering = _offeringDao.getcomputeOffering(newServiceOffering, customparameters);
newServiceOffering = _offeringDao.getComputeOffering(newServiceOffering, customparameters);
}
_itMgr.checkIfCanUpgrade(systemVm, newServiceOffering);

View File

@ -1010,7 +1010,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
if (newServiceOffering.isDynamic()) {
newServiceOffering.setDynamicFlag(true);
validateCustomParameters(newServiceOffering, cmd.getDetails());
newServiceOffering = _offeringDao.getcomputeOffering(newServiceOffering, customParameters);
newServiceOffering = _offeringDao.getComputeOffering(newServiceOffering, customParameters);
}
ServiceOfferingVO currentServiceOffering = _offeringDao.findByIdIncludingRemoved(vmInstance.getId(), vmInstance.getServiceOfferingId());
@ -1121,7 +1121,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
if (newServiceOffering.isDynamic()) {
newServiceOffering.setDynamicFlag(true);
validateCustomParameters(newServiceOffering, customParameters);
newServiceOffering = _offeringDao.getcomputeOffering(newServiceOffering, customParameters);
newServiceOffering = _offeringDao.getComputeOffering(newServiceOffering, customParameters);
}
ServiceOfferingVO currentServiceOffering = _offeringDao.findByIdIncludingRemoved(vmInstance.getId(), vmInstance.getServiceOfferingId());
@ -1771,7 +1771,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
if (newServiceOffering.isDynamic()) {
newServiceOffering.setDynamicFlag(true);
validateCustomParameters(newServiceOffering, customParameters);
newServiceOffering = _offeringDao.getcomputeOffering(newServiceOffering, customParameters);
newServiceOffering = _offeringDao.getComputeOffering(newServiceOffering, customParameters);
}
// Check that the specified service offering ID is valid
@ -3498,7 +3498,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
if (offering.isDynamic()) {
offering.setDynamicFlag(true);
validateCustomParameters(offering, customParameters);
offering = _offeringDao.getcomputeOffering(offering, customParameters);
offering = _offeringDao.getComputeOffering(offering, customParameters);
}
// check if account/domain is with in resource limits to create a new vm
boolean isIso = Storage.ImageFormat.ISO == template.getFormat();
@ -3869,12 +3869,12 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
return _instance + "-" + uuidName;
}
private UserVmVO commitUserVm(final DataCenter zone, final VirtualMachineTemplate template, final String hostName, final String displayName, final Account owner,
final Long diskOfferingId, final Long diskSize, final String userData, final Account caller, final Boolean isDisplayVm, final String keyboard,
final long accountId, final long userId, final ServiceOfferingVO offering, final boolean isIso, final String sshPublicKey, final LinkedHashMap<String, NicProfile> networkNicMap,
final long id, final String instanceName, final String uuidName, final HypervisorType hypervisorType, final Map<String, String> customParameters, final Map<String,
Map<Integer, String>> extraDhcpOptionMap, final Map<Long, DiskOffering> dataDiskTemplateToDiskOfferingMap,
Map<String, String> userVmOVFPropertiesMap) throws InsufficientCapacityException {
private UserVmVO commitUserVm(final boolean isImport, final DataCenter zone, final Host host, final Host lastHost, final VirtualMachineTemplate template, final String hostName, final String displayName, final Account owner,
final Long diskOfferingId, final Long diskSize, final String userData, final Account caller, final Boolean isDisplayVm, final String keyboard,
final long accountId, final long userId, final ServiceOffering offering, final boolean isIso, final String sshPublicKey, final LinkedHashMap<String, NicProfile> networkNicMap,
final long id, final String instanceName, final String uuidName, final HypervisorType hypervisorType, final Map<String, String> customParameters,
final Map<String, Map<Integer, String>> extraDhcpOptionMap, final Map<Long, DiskOffering> dataDiskTemplateToDiskOfferingMap,
final Map<String, String> userVmOVFPropertiesMap, final VirtualMachine.PowerState powerState) throws InsufficientCapacityException {
return Transaction.execute(new TransactionCallbackWithException<UserVmVO, InsufficientCapacityException>() {
@Override
public UserVmVO doInTransaction(TransactionStatus status) throws InsufficientCapacityException {
@ -3896,7 +3896,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
vm.setDetail(VmDetailConstants.KEYBOARD, keyboard);
}
if (isIso) {
if (!isImport && isIso) {
vm.setIsoId(template.getId());
}
Long rootDiskSize = null;
@ -3948,9 +3948,21 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
}
}
if (isImport) {
vm.setDataCenterId(zone.getId());
vm.setHostId(host.getId());
if (lastHost != null) {
vm.setLastHostId(lastHost.getId());
}
vm.setPowerState(powerState);
if (powerState == VirtualMachine.PowerState.PowerOn) {
vm.setState(State.Running);
}
}
_vmDao.persist(vm);
for (String key : customParameters.keySet()) {
if( key.equalsIgnoreCase(VmDetailConstants.CPU_NUMBER) ||
if (key.equalsIgnoreCase(VmDetailConstants.CPU_NUMBER) ||
key.equalsIgnoreCase(VmDetailConstants.CPU_SPEED) ||
key.equalsIgnoreCase(VmDetailConstants.MEMORY)) {
// handle double byte strings.
@ -3984,45 +3996,62 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
}
_vmDao.saveDetails(vm);
if (!isImport) {
s_logger.debug("Allocating in the DB for vm");
DataCenterDeployment plan = new DataCenterDeployment(zone.getId());
s_logger.debug("Allocating in the DB for vm");
DataCenterDeployment plan = new DataCenterDeployment(zone.getId());
List<String> computeTags = new ArrayList<String>();
computeTags.add(offering.getHostTag());
List<String> computeTags = new ArrayList<String>();
computeTags.add(offering.getHostTag());
List<String> rootDiskTags = new ArrayList<String>();
rootDiskTags.add(offering.getTags());
List<String> rootDiskTags = new ArrayList<String>();
rootDiskTags.add(offering.getTags());
if (isIso) {
_orchSrvc.createVirtualMachineFromScratch(vm.getUuid(), Long.toString(owner.getAccountId()), vm.getIsoId().toString(), hostName, displayName,
hypervisorType.name(), guestOSCategory.getName(), offering.getCpu(), offering.getSpeed(), offering.getRamSize(), diskSize, computeTags, rootDiskTags,
networkNicMap, plan, extraDhcpOptionMap);
} else {
_orchSrvc.createVirtualMachine(vm.getUuid(), Long.toString(owner.getAccountId()), Long.toString(template.getId()), hostName, displayName, hypervisorType.name(),
offering.getCpu(), offering.getSpeed(), offering.getRamSize(), diskSize, computeTags, rootDiskTags, networkNicMap, plan, rootDiskSize, extraDhcpOptionMap, dataDiskTemplateToDiskOfferingMap);
}
if (isIso) {
_orchSrvc.createVirtualMachineFromScratch(vm.getUuid(), Long.toString(owner.getAccountId()), vm.getIsoId().toString(), hostName, displayName,
hypervisorType.name(), guestOSCategory.getName(), offering.getCpu(), offering.getSpeed(), offering.getRamSize(), diskSize, computeTags, rootDiskTags,
networkNicMap, plan, extraDhcpOptionMap);
} else {
_orchSrvc.createVirtualMachine(vm.getUuid(), Long.toString(owner.getAccountId()), Long.toString(template.getId()), hostName, displayName, hypervisorType.name(),
offering.getCpu(), offering.getSpeed(), offering.getRamSize(), diskSize, computeTags, rootDiskTags, networkNicMap, plan, rootDiskSize, extraDhcpOptionMap, dataDiskTemplateToDiskOfferingMap);
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Successfully allocated DB entry for " + vm);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Successfully allocated DB entry for " + vm);
}
}
CallContext.current().setEventDetails("Vm Id: " + vm.getUuid());
if (!offering.isDynamic()) {
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_CREATE, accountId, zone.getId(), vm.getId(), vm.getHostName(), offering.getId(), template.getId(),
hypervisorType.toString(), VirtualMachine.class.getName(), vm.getUuid(), vm.isDisplayVm());
} else {
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_CREATE, accountId, zone.getId(), vm.getId(), vm.getHostName(), offering.getId(), template.getId(),
hypervisorType.toString(), VirtualMachine.class.getName(), vm.getUuid(), customParameters, vm.isDisplayVm());
}
if (!isImport) {
if (!offering.isDynamic()) {
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_CREATE, accountId, zone.getId(), vm.getId(), vm.getHostName(), offering.getId(), template.getId(),
hypervisorType.toString(), VirtualMachine.class.getName(), vm.getUuid(), vm.isDisplayVm());
} else {
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_CREATE, accountId, zone.getId(), vm.getId(), vm.getHostName(), offering.getId(), template.getId(),
hypervisorType.toString(), VirtualMachine.class.getName(), vm.getUuid(), customParameters, vm.isDisplayVm());
}
//Update Resource Count for the given account
resourceCountIncrement(accountId, isDisplayVm, new Long(offering.getCpu()), new Long(offering.getRamSize()));
//Update Resource Count for the given account
resourceCountIncrement(accountId, isDisplayVm, new Long(offering.getCpu()), new Long(offering.getRamSize()));
}
return vm;
}
});
}
private UserVmVO commitUserVm(final DataCenter zone, final VirtualMachineTemplate template, final String hostName, final String displayName, final Account owner,
final Long diskOfferingId, final Long diskSize, final String userData, final Account caller, final Boolean isDisplayVm, final String keyboard,
final long accountId, final long userId, final ServiceOfferingVO offering, final boolean isIso, final String sshPublicKey, final LinkedHashMap<String, NicProfile> networkNicMap,
final long id, final String instanceName, final String uuidName, final HypervisorType hypervisorType, final Map<String, String> customParameters, final Map<String,
Map<Integer, String>> extraDhcpOptionMap, final Map<Long, DiskOffering> dataDiskTemplateToDiskOfferingMap,
Map<String, String> userVmOVFPropertiesMap) throws InsufficientCapacityException {
return commitUserVm(false, zone, null, null, template, hostName, displayName, owner,
diskOfferingId, diskSize, userData, caller, isDisplayVm, keyboard,
accountId, userId, offering, isIso, sshPublicKey, networkNicMap,
id, instanceName, uuidName, hypervisorType, customParameters,
extraDhcpOptionMap, dataDiskTemplateToDiskOfferingMap,
userVmOVFPropertiesMap, null);
}
public void validateRootDiskResize(final HypervisorType hypervisorType, Long rootDiskSize, VMTemplateVO templateVO, UserVmVO vm, final Map<String, String> customParameters) throws InvalidParameterValueException
{
// rootdisksize must be larger than template.
@ -6159,7 +6188,10 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
_resourceLimitMgr.checkResourceLimit(newAccount, ResourceType.primary_storage, totalVolumesSize);
// VV 4: Check if new owner can use the vm template
VirtualMachineTemplate template = _templateDao.findById(vm.getTemplateId());
VirtualMachineTemplate template = _templateDao.findByIdIncludingRemoved(vm.getTemplateId());
if (template == null) {
throw new InvalidParameterValueException(String.format("Template for VM: %s cannot be found", vm.getUuid()));
}
if (!template.isPublicTemplate()) {
Account templateOwner = _accountMgr.getAccount(template.getAccountId());
_accountMgr.checkAccess(newAccount, null, true, templateOwner);
@ -7045,4 +7077,32 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
}
}
}
}
@Override
public UserVm importVM(final DataCenter zone, final Host host, final VirtualMachineTemplate template, final String instanceName, final String displayName,
final Account owner, final String userData, final Account caller, final Boolean isDisplayVm, final String keyboard,
final long accountId, final long userId, final ServiceOffering serviceOffering, final String sshPublicKey,
final String hostName, final HypervisorType hypervisorType, final Map<String, String> customParameters, final VirtualMachine.PowerState powerState) throws InsufficientCapacityException {
if (zone == null) {
throw new InvalidParameterValueException("Unable to import virtual machine with invalid zone");
}
if (host == null) {
throw new InvalidParameterValueException("Unable to import virtual machine with invalid host");
}
final long id = _vmDao.getNextInSequence(Long.class, "id");
if (hostName != null) {
// Check is hostName is RFC compliant
checkNameForRFCCompliance(hostName);
}
final String uuidName = _uuidMgr.generateUuid(UserVm.class, null);
final Host lastHost = powerState != VirtualMachine.PowerState.PowerOn ? host : null;
return commitUserVm(true, zone, host, lastHost, template, hostName, displayName, owner,
null, null, userData, caller, isDisplayVm, keyboard,
accountId, userId, serviceOffering, template.getFormat().equals(ImageFormat.ISO), sshPublicKey, null,
id, instanceName, uuidName, hypervisorType, customParameters,
null, null, null, powerState);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -35,4 +35,6 @@
<property name="name" value="LXCGuru" />
</bean>
<bean id="vmImportService" class="org.apache.cloudstack.vm.VmImportManagerImpl" />
</beans>

View File

@ -978,4 +978,9 @@ public class MockNetworkManagerImpl extends ManagerBase implements NetworkOrches
public AcquirePodIpCmdResponse allocatePodIp(Account account, String zoneId, String podId) throws ResourceAllocationException, ConcurrentOperationException {
return null;
}
@Override
public Pair<NicProfile, Integer> importNic(String macAddress, int deviceId, Network network, Boolean isDefaultNic, VirtualMachine vm, IpAddresses ipAddresses) {
return null;
}
}

View File

@ -0,0 +1,351 @@
// 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;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import org.apache.cloudstack.api.ResponseGenerator;
import org.apache.cloudstack.api.ResponseObject;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.command.admin.vm.ImportUnmanagedInstanceCmd;
import org.apache.cloudstack.api.command.admin.vm.ListUnmanagedInstancesCmd;
import org.apache.cloudstack.api.response.UserVmResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
import org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.cloud.agent.AgentManager;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.GetUnmanagedInstancesAnswer;
import com.cloud.agent.api.GetUnmanagedInstancesCommand;
import com.cloud.configuration.Resource;
import com.cloud.dc.ClusterVO;
import com.cloud.dc.DataCenter;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.dao.ClusterDao;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.event.UsageEventUtils;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.PermissionDeniedException;
import com.cloud.host.Host;
import com.cloud.host.HostVO;
import com.cloud.host.Status;
import com.cloud.hypervisor.Hypervisor;
import com.cloud.network.Network;
import com.cloud.network.NetworkModel;
import com.cloud.network.dao.NetworkDao;
import com.cloud.network.dao.NetworkVO;
import com.cloud.offering.DiskOffering;
import com.cloud.offering.ServiceOffering;
import com.cloud.resource.ResourceManager;
import com.cloud.service.ServiceOfferingVO;
import com.cloud.service.dao.ServiceOfferingDao;
import com.cloud.storage.DiskOfferingVO;
import com.cloud.storage.StoragePool;
import com.cloud.storage.VMTemplateStoragePoolVO;
import com.cloud.storage.VMTemplateVO;
import com.cloud.storage.Volume;
import com.cloud.storage.VolumeApiService;
import com.cloud.storage.VolumeVO;
import com.cloud.storage.dao.DiskOfferingDao;
import com.cloud.storage.dao.VMTemplateDao;
import com.cloud.storage.dao.VMTemplatePoolDao;
import com.cloud.storage.dao.VolumeDao;
import com.cloud.template.VirtualMachineTemplate;
import com.cloud.user.Account;
import com.cloud.user.AccountService;
import com.cloud.user.AccountVO;
import com.cloud.user.ResourceLimitService;
import com.cloud.user.User;
import com.cloud.user.UserVO;
import com.cloud.user.dao.UserDao;
import com.cloud.uservm.UserVm;
import com.cloud.utils.Pair;
import com.cloud.vm.DiskProfile;
import com.cloud.vm.NicProfile;
import com.cloud.vm.UserVmManager;
import com.cloud.vm.UserVmVO;
import com.cloud.vm.VMInstanceVO;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.dao.VMInstanceDao;
@RunWith(PowerMockRunner.class)
@PrepareForTest(UsageEventUtils.class)
public class VmImportManagerImplTest {
@InjectMocks
private VmImportService vmIngestionService = new VmImportManagerImpl();
@Mock
private UserVmManager userVmManager;
@Mock
private ClusterDao clusterDao;
@Mock
private ResourceManager resourceManager;
@Mock
private VMTemplatePoolDao templatePoolDao;
@Mock
private AgentManager agentManager;
@Mock
private AccountService accountService;
@Mock
private UserDao userDao;
@Mock
private DataCenterDao dataCenterDao;
@Mock
private VMTemplateDao templateDao;
@Mock
private VMInstanceDao vmDao;
@Mock
private ServiceOfferingDao serviceOfferingDao;
@Mock
private DiskOfferingDao diskOfferingDao;
@Mock
private NetworkDao networkDao;
@Mock
private NetworkOrchestrationService networkOrchestrationService;
@Mock
private VolumeOrchestrationService volumeManager;
@Mock
public ResponseGenerator responseGenerator;
@Mock
private VolumeDao volumeDao;
@Mock
private ResourceLimitService resourceLimitService;
@Mock
private PrimaryDataStoreDao primaryDataStoreDao;
@Mock
private VolumeApiService volumeApiService;
@Mock
private NetworkModel networkModel;
@Mock
private ConfigurationDao configurationDao;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
AccountVO account = new AccountVO("admin", 1L, "", Account.ACCOUNT_TYPE_ADMIN, "uuid");
UserVO user = new UserVO(1, "adminuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
CallContext.register(user, account);
UnmanagedInstanceTO instance = new UnmanagedInstanceTO();
instance.setName("TestInstance");
instance.setCpuCores(2);
instance.setCpuCoresPerSocket(1);
instance.setCpuSpeed(1000);
instance.setMemory(1024);
instance.setOperatingSystem("CentOS 7");
List<UnmanagedInstanceTO.Disk> instanceDisks = new ArrayList<>();
UnmanagedInstanceTO.Disk instanceDisk = new UnmanagedInstanceTO.Disk();
instanceDisk.setDiskId("1000-1");
instanceDisk.setLabel("DiskLabel");
instanceDisk.setController("scsi");
instanceDisk.setImagePath("[b6ccf44a1fa13e29b3667b4954fa10ee] TestInstance/ROOT-1.vmdk");
instanceDisk.setCapacity(5242880L);
instanceDisk.setDatastoreName("Test");
instanceDisk.setDatastoreHost("Test");
instanceDisk.setDatastorePath("Test");
instanceDisk.setDatastoreType("NFS");
instanceDisks.add(instanceDisk);
instance.setDisks(instanceDisks);
List<UnmanagedInstanceTO.Nic> instanceNics = new ArrayList<>();
UnmanagedInstanceTO.Nic instanceNic = new UnmanagedInstanceTO.Nic();
instanceNic.setNicId("NIC 1");
instanceNic.setAdapterType("VirtualE1000E");
instanceNic.setMacAddress("02:00:2e:0f:00:02");
instanceNic.setVlan(1024);
instanceNics.add(instanceNic);
instance.setNics(instanceNics);
instance.setPowerState(UnmanagedInstanceTO.PowerState.PowerOn);
ClusterVO clusterVO = new ClusterVO(1L, 1L, "Cluster");
clusterVO.setHypervisorType(Hypervisor.HypervisorType.VMware.toString());
when(clusterDao.findById(Mockito.anyLong())).thenReturn(clusterVO);
when(configurationDao.getValue(Mockito.anyString())).thenReturn(null);
doNothing().when(resourceLimitService).checkResourceLimit(any(Account.class), any(Resource.ResourceType.class), anyLong());
List<HostVO> hosts = new ArrayList<>();
HostVO hostVO = Mockito.mock(HostVO.class);
when(hostVO.isInMaintenanceStates()).thenReturn(false);
hosts.add(hostVO);
when(resourceManager.listHostsInClusterByStatus(Mockito.anyLong(), Mockito.any(Status.class))).thenReturn(hosts);
List<VMTemplateStoragePoolVO> templates = new ArrayList<>();
when(templatePoolDao.listAll()).thenReturn(templates);
List<VolumeVO> volumes = new ArrayList<>();
when(volumeDao.findIncludingRemovedByZone(Mockito.anyLong())).thenReturn(volumes);
List<VMInstanceVO> vms = new ArrayList<>();
when(vmDao.listByHostId(Mockito.anyLong())).thenReturn(vms);
when(vmDao.listByLastHostIdAndStates(Mockito.anyLong())).thenReturn(vms);
GetUnmanagedInstancesCommand cmd = Mockito.mock(GetUnmanagedInstancesCommand.class);
HashMap<String, UnmanagedInstanceTO> map = new HashMap<>();
map.put(instance.getName(), instance);
Answer answer = new GetUnmanagedInstancesAnswer(cmd, "", map);
when(agentManager.easySend(Mockito.anyLong(), Mockito.any(GetUnmanagedInstancesCommand.class))).thenReturn(answer);
DataCenterVO zone = Mockito.mock(DataCenterVO.class);
when(zone.getId()).thenReturn(1L);
when(dataCenterDao.findById(Mockito.anyLong())).thenReturn(zone);
when(accountService.getActiveAccountById(Mockito.anyLong())).thenReturn(Mockito.mock(Account.class));
List<UserVO> users = new ArrayList<>();
users.add(Mockito.mock(UserVO.class));
when(userDao.listByAccount(Mockito.anyLong())).thenReturn(users);
VMTemplateVO template = Mockito.mock(VMTemplateVO.class);
when(template.getId()).thenReturn(1L);
when(template.getName()).thenReturn("Template");
when(templateDao.findById(Mockito.anyLong())).thenReturn(template);
when(templateDao.findByName(Mockito.anyString())).thenReturn(template);
ServiceOfferingVO serviceOffering = Mockito.mock(ServiceOfferingVO.class);
when(serviceOffering.getId()).thenReturn(1L);
when(serviceOffering.getTags()).thenReturn("");
when(serviceOffering.isDynamic()).thenReturn(false);
when(serviceOffering.getCpu()).thenReturn(instance.getCpuCores());
when(serviceOffering.getRamSize()).thenReturn(instance.getMemory());
when(serviceOffering.getSpeed()).thenReturn(instance.getCpuSpeed());
when(serviceOfferingDao.findById(Mockito.anyLong())).thenReturn(serviceOffering);
DiskOfferingVO diskOfferingVO = Mockito.mock(DiskOfferingVO.class);
when(diskOfferingVO.isCustomized()).thenReturn(false);
when(diskOfferingVO.getDiskSize()).thenReturn(Long.MAX_VALUE);
when(diskOfferingDao.findById(Mockito.anyLong())).thenReturn(diskOfferingVO);
UserVmVO userVm = Mockito.mock(UserVmVO.class);
when(userVm.getAccountId()).thenReturn(1L);
when(userVm.getDataCenterId()).thenReturn(1L);
when(userVm.getHostName()).thenReturn(instance.getName());
when(userVm.getTemplateId()).thenReturn(1L);
when(userVm.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.VMware);
when(userVm.getUuid()).thenReturn("abcd");
when(userVm.isDisplayVm()).thenReturn(true);
// Skip usage publishing and resource increment for test
when(userVm.getType()).thenReturn(VirtualMachine.Type.Instance);
userVm.setInstanceName(instance.getName());
userVm.setHostName(instance.getName());
StoragePoolVO poolVO = Mockito.mock(StoragePoolVO.class);
when(poolVO.getDataCenterId()).thenReturn(1L);
when(poolVO.getClusterId()).thenReturn(clusterVO.getId());
List<StoragePoolVO> pools = new ArrayList<>();
pools.add(poolVO);
when(primaryDataStoreDao.listPoolByHostPath(Mockito.anyString(), Mockito.anyString())).thenReturn(pools);
when(userVmManager.importVM(Mockito.any(DataCenter.class), Mockito.any(Host.class), Mockito.any(VirtualMachineTemplate.class), Mockito.anyString(), Mockito.anyString(),
Mockito.any(Account.class), Mockito.anyString(), Mockito.any(Account.class), Mockito.anyBoolean(), Mockito.anyString(),
Mockito.anyLong(), Mockito.anyLong(), Mockito.any(ServiceOffering.class), Mockito.anyString(),
Mockito.anyString(), Mockito.any(Hypervisor.HypervisorType.class), Mockito.anyMap(), Mockito.any(VirtualMachine.PowerState.class))).thenReturn(userVm);
when(volumeApiService.doesTargetStorageSupportDiskOffering(Mockito.any(StoragePool.class), Mockito.anyString())).thenReturn(true);
NetworkVO networkVO = Mockito.mock(NetworkVO.class);
when(networkVO.getGuestType()).thenReturn(Network.GuestType.L2);
when(networkVO.getBroadcastUri()).thenReturn(URI.create(String.format("vlan://%d", instanceNic.getVlan())));
when(networkVO.getDataCenterId()).thenReturn(1L);
when(networkDao.findById(Mockito.anyLong())).thenReturn(networkVO);
List<NetworkVO> networks = new ArrayList<>();
networks.add(networkVO);
when(networkDao.listByZone(Mockito.anyLong())).thenReturn(networks);
doNothing().when(networkModel).checkNetworkPermissions(Mockito.any(Account.class), Mockito.any(Network.class));
doNothing().when(networkModel).checkRequestedIpAddresses(Mockito.anyLong(), Mockito.any(Network.IpAddresses.class));
NicProfile profile = Mockito.mock(NicProfile.class);
Integer deviceId = 100;
Pair<NicProfile, Integer> pair = new Pair<NicProfile, Integer>(profile, deviceId);
when(networkOrchestrationService.importNic(Mockito.anyString(), Mockito.anyInt(), Mockito.any(Network.class), Mockito.anyBoolean(), Mockito.any(VirtualMachine.class), Mockito.any(Network.IpAddresses.class))).thenReturn(pair);
when(volumeManager.importVolume(Mockito.any(Volume.Type.class), Mockito.anyString(), Mockito.any(DiskOffering.class), Mockito.anyLong(),
Mockito.anyLong(), Mockito.anyLong(), Mockito.any(VirtualMachine.class), Mockito.any(VirtualMachineTemplate.class),
Mockito.any(Account.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString(), Mockito.anyString())).thenReturn(Mockito.mock(DiskProfile.class));
when(volumeDao.findByInstance(Mockito.anyLong())).thenReturn(volumes);
List<UserVmResponse> userVmResponses = new ArrayList<>();
UserVmResponse userVmResponse = new UserVmResponse();
userVmResponse.setInstanceName(instance.getName());
userVmResponses.add(userVmResponse);
when(responseGenerator.createUserVmResponse(Mockito.any(ResponseObject.ResponseView.class), Mockito.anyString(), Mockito.any(UserVm.class))).thenReturn(userVmResponses);
}
@After
public void tearDown() {
CallContext.unregister();
}
@Test
public void listUnmanagedInstancesTest() {
ListUnmanagedInstancesCmd cmd = Mockito.mock(ListUnmanagedInstancesCmd.class);
vmIngestionService.listUnmanagedInstances(cmd);
}
@Test(expected = InvalidParameterValueException.class)
public void listUnmanagedInstancesInvalidHypervisorTest() {
ListUnmanagedInstancesCmd cmd = Mockito.mock(ListUnmanagedInstancesCmd.class);
ClusterVO cluster = new ClusterVO(1, 1, "Cluster");
cluster.setHypervisorType(Hypervisor.HypervisorType.KVM.toString());
when(clusterDao.findById(Mockito.anyLong())).thenReturn(cluster);
vmIngestionService.listUnmanagedInstances(cmd);
}
@Test(expected = PermissionDeniedException.class)
public void listUnmanagedInstancesInvalidCallerTest() {
CallContext.unregister();
AccountVO account = new AccountVO("user", 1L, "", Account.ACCOUNT_TYPE_NORMAL, "uuid");
UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
CallContext.register(user, account);
ListUnmanagedInstancesCmd cmd = Mockito.mock(ListUnmanagedInstancesCmd.class);
vmIngestionService.listUnmanagedInstances(cmd);
}
@Test
public void importUnmanagedInstanceTest() {
ImportUnmanagedInstanceCmd importUnmanageInstanceCmd = Mockito.mock(ImportUnmanagedInstanceCmd.class);
when(importUnmanageInstanceCmd.getName()).thenReturn("TestInstance");
when(importUnmanageInstanceCmd.getAccountName()).thenReturn(null);
when(importUnmanageInstanceCmd.getDomainId()).thenReturn(null);
PowerMockito.mockStatic(UsageEventUtils.class);
vmIngestionService.importUnmanagedInstance(importUnmanageInstanceCmd);
}
@Test(expected = InvalidParameterValueException.class)
public void importUnmanagedInstanceInvalidHostnameTest() {
ImportUnmanagedInstanceCmd importUnmanageInstanceCmd = Mockito.mock(ImportUnmanagedInstanceCmd.class);
when(importUnmanageInstanceCmd.getName()).thenReturn("TestInstance");
when(importUnmanageInstanceCmd.getName()).thenReturn("some name");
when(importUnmanageInstanceCmd.getMigrateAllowed()).thenReturn(false);
vmIngestionService.importUnmanagedInstance(importUnmanageInstanceCmd);
}
@Test(expected = ServerApiException.class)
public void importUnmanagedInstanceMissingInstanceTest() {
ImportUnmanagedInstanceCmd importUnmanageInstanceCmd = Mockito.mock(ImportUnmanagedInstanceCmd.class);
when(importUnmanageInstanceCmd.getName()).thenReturn("SomeInstance");
when(importUnmanageInstanceCmd.getAccountName()).thenReturn(null);
when(importUnmanageInstanceCmd.getDomainId()).thenReturn(null);
vmIngestionService.importUnmanagedInstance(importUnmanageInstanceCmd);
}
}

View File

@ -189,6 +189,7 @@ known_categories = {
'Sioc' : 'Sioc',
'Diagnostics': 'Diagnostics',
'Management': 'Management',
'UnmanagedInstance': 'Virtual Machine'
}

View File

@ -216,6 +216,19 @@ public class ClusterMO extends BaseMO implements VmwareHypervisorHost {
return _mor;
}
@Override
public synchronized List<VirtualMachineMO> listVmsOnHyperHost(String vmName) throws Exception {
List<VirtualMachineMO> vms = new ArrayList<>();
List<ManagedObjectReference> hosts = _context.getVimClient().getDynamicProperty(_mor, "host");
if (hosts != null && hosts.size() > 0) {
for (ManagedObjectReference morHost : hosts) {
HostMO hostMo = new HostMO(_context, morHost);
vms.addAll(hostMo.listVmsOnHyperHost(vmName));
}
}
return vms;
}
@Override
public VirtualMachineMO findVmOnHyperHost(String name) throws Exception {

View File

@ -24,6 +24,8 @@ import java.util.concurrent.ConcurrentHashMap;
import org.apache.log4j.Logger;
import com.cloud.hypervisor.vmware.util.VmwareContext;
import com.cloud.utils.Pair;
import com.vmware.vim25.DVPortgroupConfigSpec;
import com.vmware.vim25.DVSConfigInfo;
import com.vmware.vim25.ManagedObjectReference;
@ -32,8 +34,6 @@ import com.vmware.vim25.VMwareDVSConfigInfo;
import com.vmware.vim25.VMwareDVSConfigSpec;
import com.vmware.vim25.VMwareDVSPvlanMapEntry;
import com.cloud.hypervisor.vmware.util.VmwareContext;
public class DistributedVirtualSwitchMO extends BaseMO {
@SuppressWarnings("unused")
private static final Logger s_logger = Logger.getLogger(DistributedVirtualSwitchMO.class);
@ -169,4 +169,28 @@ public class DistributedVirtualSwitchMO extends BaseMO {
return result;
}
public Pair<Integer, HypervisorHostHelper.PvlanType> retrieveVlanFromPvlan(int pvlanid, ManagedObjectReference dvSwitchMor) throws Exception {
assert (dvSwitchMor != null);
Pair<Integer, HypervisorHostHelper.PvlanType> result = null;
VMwareDVSConfigInfo configinfo = (VMwareDVSConfigInfo)_context.getVimClient().getDynamicProperty(dvSwitchMor, "config");
List<VMwareDVSPvlanMapEntry> pvlanConfig = null;
pvlanConfig = configinfo.getPvlanConfig();
if (null == pvlanConfig || 0 == pvlanConfig.size()) {
return result;
}
// Iterate through the pvlanMapList and check if the specified pvlan id exist. If it does, set the fields in result accordingly.
for (VMwareDVSPvlanMapEntry mapEntry : pvlanConfig) {
int entryVlanid = mapEntry.getPrimaryVlanId();
int entryPvlanid = mapEntry.getSecondaryVlanId();
if (pvlanid == entryPvlanid) {
result = new Pair<>(entryVlanid, HypervisorHostHelper.PvlanType.valueOf(mapEntry.getPvlanType()));
break;
}
}
return result;
}
}

View File

@ -496,6 +496,18 @@ public class HostMO extends BaseMO implements VmwareHypervisorHost {
return (String)_context.getVimClient().getDynamicProperty(_mor, "name");
}
@Override
public synchronized List<VirtualMachineMO> listVmsOnHyperHost(String vmName) throws Exception {
List<VirtualMachineMO> vms = new ArrayList<>();
if (vmName != null && !vmName.isEmpty()) {
vms.add(findVmOnHyperHost(vmName));
} else {
loadVmCache();
vms.addAll(_vmCache.values());
}
return vms;
}
@Override
public synchronized VirtualMachineMO findVmOnHyperHost(String vmName) throws Exception {
if (s_logger.isDebugEnabled())

View File

@ -16,6 +16,8 @@
// under the License.
package com.cloud.hypervisor.vmware.mo;
import java.util.List;
import com.vmware.vim25.ClusterDasConfigInfo;
import com.vmware.vim25.ComputeResourceSummary;
import com.vmware.vim25.ManagedObjectReference;
@ -51,6 +53,8 @@ public interface VmwareHypervisorHost {
String getHyperHostDefaultGateway() throws Exception;
List<VirtualMachineMO> listVmsOnHyperHost(String name) throws Exception;
VirtualMachineMO findVmOnHyperHost(String name) throws Exception;
VirtualMachineMO findVmOnPeerHyperHost(String name) throws Exception;