Search in sources :

Example 1 with UnmanagedInstanceResponse

use of org.apache.cloudstack.api.response.UnmanagedInstanceResponse in project cloudstack by apache.

the class UnmanagedVMsManagerImpl method listUnmanagedInstances.

@Override
public ListResponse<UnmanagedInstanceResponse> listUnmanagedInstances(ListUnmanagedInstancesCmd cmd) {
    final Account caller = CallContext.current().getCallingAccount();
    if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
        throw new PermissionDeniedException(String.format("Cannot perform this operation, Calling account is not root admin: %s", caller.getUuid()));
    }
    final Long clusterId = cmd.getClusterId();
    if (clusterId == null) {
        throw new InvalidParameterValueException(String.format("Cluster ID cannot be null"));
    }
    final Cluster cluster = clusterDao.findById(clusterId);
    if (cluster == null) {
        throw new InvalidParameterValueException(String.format("Cluster ID: %d cannot be found", clusterId));
    }
    if (cluster.getHypervisorType() != Hypervisor.HypervisorType.VMware) {
        throw new InvalidParameterValueException(String.format("VM ingestion is currently not supported for hypervisor: %s", cluster.getHypervisorType().toString()));
    }
    String keyword = cmd.getKeyword();
    if (StringUtils.isNotEmpty(keyword)) {
        keyword = keyword.toLowerCase();
    }
    List<HostVO> hosts = resourceManager.listHostsInClusterByStatus(clusterId, Status.Up);
    List<String> additionalNameFilters = getAdditionalNameFilters(cluster);
    List<UnmanagedInstanceResponse> responses = new ArrayList<>();
    for (HostVO host : hosts) {
        if (host.isInMaintenanceStates()) {
            continue;
        }
        List<String> managedVms = new ArrayList<>();
        managedVms.addAll(additionalNameFilters);
        managedVms.addAll(getHostManagedVms(host));
        GetUnmanagedInstancesCommand command = new GetUnmanagedInstancesCommand();
        command.setInstanceName(cmd.getName());
        command.setManagedInstancesNames(managedVms);
        Answer answer = agentManager.easySend(host.getId(), command);
        if (!(answer instanceof GetUnmanagedInstancesAnswer)) {
            continue;
        }
        GetUnmanagedInstancesAnswer unmanagedInstancesAnswer = (GetUnmanagedInstancesAnswer) answer;
        HashMap<String, UnmanagedInstanceTO> unmanagedInstances = new HashMap<>(unmanagedInstancesAnswer.getUnmanagedInstances());
        Set<String> keys = unmanagedInstances.keySet();
        for (String key : keys) {
            UnmanagedInstanceTO instance = unmanagedInstances.get(key);
            if (StringUtils.isNotEmpty(keyword) && !instance.getName().toLowerCase().contains(keyword)) {
                continue;
            }
            responses.add(createUnmanagedInstanceResponse(instance, cluster, host));
        }
    }
    ListResponse<UnmanagedInstanceResponse> listResponses = new ListResponse<>();
    listResponses.setResponses(responses, responses.size());
    return listResponses;
}
Also used : Account(com.cloud.user.Account) ListResponse(org.apache.cloudstack.api.response.ListResponse) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Cluster(com.cloud.org.Cluster) GetUnmanagedInstancesCommand(com.cloud.agent.api.GetUnmanagedInstancesCommand) HostVO(com.cloud.host.HostVO) UnmanagedInstanceResponse(org.apache.cloudstack.api.response.UnmanagedInstanceResponse) Answer(com.cloud.agent.api.Answer) GetUnmanagedInstancesAnswer(com.cloud.agent.api.GetUnmanagedInstancesAnswer) PrepareUnmanageVMInstanceAnswer(com.cloud.agent.api.PrepareUnmanageVMInstanceAnswer) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) GetUnmanagedInstancesAnswer(com.cloud.agent.api.GetUnmanagedInstancesAnswer) PermissionDeniedException(com.cloud.exception.PermissionDeniedException)

Example 2 with UnmanagedInstanceResponse

use of org.apache.cloudstack.api.response.UnmanagedInstanceResponse in project cloudstack by apache.

the class UnmanagedVMsManagerImpl method createUnmanagedInstanceResponse.

private UnmanagedInstanceResponse createUnmanagedInstanceResponse(UnmanagedInstanceTO instance, Cluster cluster, Host host) {
    UnmanagedInstanceResponse response = new UnmanagedInstanceResponse();
    response.setName(instance.getName());
    if (cluster != null) {
        response.setClusterId(cluster.getUuid());
    }
    if (host != null) {
        response.setHostId(host.getUuid());
        response.setHostName(host.getName());
    }
    response.setPowerState(instance.getPowerState().toString());
    response.setCpuCores(instance.getCpuCores());
    response.setCpuSpeed(instance.getCpuSpeed());
    response.setCpuCoresPerSocket(instance.getCpuCoresPerSocket());
    response.setMemory(instance.getMemory());
    response.setOperatingSystemId(instance.getOperatingSystemId());
    response.setOperatingSystem(instance.getOperatingSystem());
    response.setObjectName("unmanagedinstance");
    if (instance.getDisks() != null) {
        for (UnmanagedInstanceTO.Disk disk : instance.getDisks()) {
            UnmanagedInstanceDiskResponse diskResponse = new UnmanagedInstanceDiskResponse();
            diskResponse.setDiskId(disk.getDiskId());
            if (StringUtils.isNotEmpty(disk.getLabel())) {
                diskResponse.setLabel(disk.getLabel());
            }
            diskResponse.setCapacity(disk.getCapacity());
            diskResponse.setController(disk.getController());
            diskResponse.setControllerUnit(disk.getControllerUnit());
            diskResponse.setPosition(disk.getPosition());
            diskResponse.setImagePath(disk.getImagePath());
            diskResponse.setDatastoreName(disk.getDatastoreName());
            diskResponse.setDatastoreHost(disk.getDatastoreHost());
            diskResponse.setDatastorePath(disk.getDatastorePath());
            diskResponse.setDatastoreType(disk.getDatastoreType());
            response.addDisk(diskResponse);
        }
    }
    if (instance.getNics() != null) {
        for (UnmanagedInstanceTO.Nic nic : instance.getNics()) {
            NicResponse nicResponse = new NicResponse();
            nicResponse.setId(nic.getNicId());
            nicResponse.setNetworkName(nic.getNetwork());
            nicResponse.setMacAddress(nic.getMacAddress());
            if (StringUtils.isNotEmpty(nic.getAdapterType())) {
                nicResponse.setAdapterType(nic.getAdapterType());
            }
            if (!CollectionUtils.isEmpty(nic.getIpAddress())) {
                nicResponse.setIpAddresses(nic.getIpAddress());
            }
            nicResponse.setVlanId(nic.getVlan());
            nicResponse.setIsolatedPvlanId(nic.getPvlan());
            nicResponse.setIsolatedPvlanType(nic.getPvlanType());
            response.addNic(nicResponse);
        }
    }
    return response;
}
Also used : UnmanagedInstanceResponse(org.apache.cloudstack.api.response.UnmanagedInstanceResponse) UnmanagedInstanceDiskResponse(org.apache.cloudstack.api.response.UnmanagedInstanceDiskResponse) NicResponse(org.apache.cloudstack.api.response.NicResponse)

Aggregations

UnmanagedInstanceResponse (org.apache.cloudstack.api.response.UnmanagedInstanceResponse)2 Answer (com.cloud.agent.api.Answer)1 GetUnmanagedInstancesAnswer (com.cloud.agent.api.GetUnmanagedInstancesAnswer)1 GetUnmanagedInstancesCommand (com.cloud.agent.api.GetUnmanagedInstancesCommand)1 PrepareUnmanageVMInstanceAnswer (com.cloud.agent.api.PrepareUnmanageVMInstanceAnswer)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 HostVO (com.cloud.host.HostVO)1 Cluster (com.cloud.org.Cluster)1 Account (com.cloud.user.Account)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ListResponse (org.apache.cloudstack.api.response.ListResponse)1 NicResponse (org.apache.cloudstack.api.response.NicResponse)1 UnmanagedInstanceDiskResponse (org.apache.cloudstack.api.response.UnmanagedInstanceDiskResponse)1