Search in sources :

Example 1 with VmsResource

use of org.ovirt.engine.api.resource.VmsResource in project ovirt-engine by oVirt.

the class V3VmDisksServer method list.

@GET
public V3Disks list() {
    SystemResource systemResource = BackendApiResource.getInstance();
    // In version 4 of the API the collection of disks of a virtual machine has been replaced by the collection of
    // disk attachments, so we need to fetch the disk attachments:
    VmsResource vmsResource = systemResource.getVmsResource();
    VmResource vmResource = vmsResource.getVmResource(vmId);
    DiskAttachmentsResource attachmentsResource = vmResource.getDiskAttachmentsResource();
    DiskAttachments attachments = attachmentsResource.list();
    // For each disk, we need now to fetch it from the top level disks collection, and add the information from
    // the corresponding attachment:
    DisksResource disksResource = systemResource.getDisksResource();
    V3Disks disks = new V3Disks();
    for (DiskAttachment attachment : attachments.getDiskAttachments()) {
        DiskResource diskResource = disksResource.getDiskResource(attachment.getDisk().getId());
        Disk v4Disk = diskResource.get();
        V3Disk v3Disk = adaptOut(v4Disk);
        V3VmHelper.addDiskAttachmentDetails(attachment, v3Disk);
        V3VmHelper.fixDiskLinks(vmId, v3Disk);
        disks.getDisks().add(v3Disk);
    }
    return disks;
}
Also used : VmResource(org.ovirt.engine.api.resource.VmResource) DiskResource(org.ovirt.engine.api.resource.DiskResource) DiskAttachmentsResource(org.ovirt.engine.api.resource.DiskAttachmentsResource) DiskAttachment(org.ovirt.engine.api.model.DiskAttachment) V3Disk(org.ovirt.engine.api.v3.types.V3Disk) VmsResource(org.ovirt.engine.api.resource.VmsResource) V3Disks(org.ovirt.engine.api.v3.types.V3Disks) VmDisksResource(org.ovirt.engine.api.resource.VmDisksResource) DisksResource(org.ovirt.engine.api.resource.DisksResource) SystemResource(org.ovirt.engine.api.resource.SystemResource) DiskAttachments(org.ovirt.engine.api.model.DiskAttachments) Disk(org.ovirt.engine.api.model.Disk) V3Disk(org.ovirt.engine.api.v3.types.V3Disk) GET(javax.ws.rs.GET)

Example 2 with VmsResource

use of org.ovirt.engine.api.resource.VmsResource in project ovirt-engine by oVirt.

the class V3NICHelper method setVnicProfile.

/**
 * Finds the VNIC profile that correspond to the given V3 NIC and assigns it to the given V4 NIC.
 *
 * @param vmId the identifier of the virtual machine that the NIC is going to be added to
 * @param v3Nic the V3 NIC where the information will be extracted from
 * @param v4Nic the V4 NIC that will be populated
 */
public static void setVnicProfile(String vmId, V3NIC v3Nic, Nic v4Nic) {
    // Do nothing if the profile is already set:
    if (v4Nic.isSetVnicProfile()) {
        return;
    }
    // In version 4 of the API the "network" and "port_mirroring" properties of the NIC have been completely
    // removed, and instead of using them it is required to specify a VNIC profile. This means that if the user
    // isn't explicitly indicating the VNIC profile we need to find one that is compatible with the given network
    // and port mirroring configuration. The only VNIC profiles to consider are the ones corresponding to the
    // networks of the cluster where the VM resides, so we need to retrieve the VM, then the cluster, and then the
    // identifiers of the networks:
    SystemResource systemService = BackendApiResource.getInstance();
    VmsResource vmsService = systemService.getVmsResource();
    VmResource vmService = vmsService.getVmResource(vmId);
    Vm vm = vmService.get();
    ClustersResource clustersService = systemService.getClustersResource();
    ClusterResource clusterService = clustersService.getClusterResource(vm.getCluster().getId());
    ClusterNetworksResource networksService = clusterService.getNetworksResource();
    Set<String> validNetworkIds = networksService.list().getNetworks().stream().map(Network::getId).collect(toSet());
    // Find a VNIC profile that is in the set of valid networks and that is compatible with the NIC:
    VnicProfilesResource profilesService = systemService.getVnicProfilesResource();
    profilesService.list().getVnicProfiles().stream().filter(profile -> validNetworkIds.contains(profile.getNetwork().getId())).filter(profile -> isProfileCompatible(profile, v3Nic)).sorted(comparing(VnicProfile::getName)).map(VnicProfile::getId).findFirst().ifPresent(id -> {
        VnicProfile v4Profile = new VnicProfile();
        v4Profile.setId(id);
        v4Nic.setVnicProfile(v4Profile);
    });
}
Also used : VmResource(org.ovirt.engine.api.resource.VmResource) VnicProfilesResource(org.ovirt.engine.api.resource.VnicProfilesResource) VnicProfileResource(org.ovirt.engine.api.resource.VnicProfileResource) BackendApiResource(org.ovirt.engine.api.restapi.resource.BackendApiResource) SystemResource(org.ovirt.engine.api.resource.SystemResource) V3NIC(org.ovirt.engine.api.v3.types.V3NIC) V3PortMirroring(org.ovirt.engine.api.v3.types.V3PortMirroring) Vm(org.ovirt.engine.api.model.Vm) Set(java.util.Set) ClusterResource(org.ovirt.engine.api.resource.ClusterResource) NetworkResource(org.ovirt.engine.api.resource.NetworkResource) V3Network(org.ovirt.engine.api.v3.types.V3Network) VnicProfile(org.ovirt.engine.api.model.VnicProfile) Objects(java.util.Objects) V3Networks(org.ovirt.engine.api.v3.types.V3Networks) Nic(org.ovirt.engine.api.model.Nic) ClusterNetworksResource(org.ovirt.engine.api.resource.ClusterNetworksResource) Network(org.ovirt.engine.api.model.Network) VnicProfilesResource(org.ovirt.engine.api.resource.VnicProfilesResource) Comparator.comparing(java.util.Comparator.comparing) NetworksResource(org.ovirt.engine.api.resource.NetworksResource) VmResource(org.ovirt.engine.api.resource.VmResource) Collectors.toSet(java.util.stream.Collectors.toSet) VmsResource(org.ovirt.engine.api.resource.VmsResource) ClustersResource(org.ovirt.engine.api.resource.ClustersResource) VmsResource(org.ovirt.engine.api.resource.VmsResource) Vm(org.ovirt.engine.api.model.Vm) VnicProfile(org.ovirt.engine.api.model.VnicProfile) SystemResource(org.ovirt.engine.api.resource.SystemResource) ClusterNetworksResource(org.ovirt.engine.api.resource.ClusterNetworksResource) ClustersResource(org.ovirt.engine.api.resource.ClustersResource) ClusterResource(org.ovirt.engine.api.resource.ClusterResource)

Example 3 with VmsResource

use of org.ovirt.engine.api.resource.VmsResource in project ovirt-engine by oVirt.

the class V3VmHelper method addDiskAttachmentDetails.

/**
 * In version 4 of the API the interface, some attributes have been moved from the disk to the disk attachment, as
 * they are specific of the relationship between a particular VM and the disk. But in version 3 of the API we need
 * to continue supporting them. To do so we need to find the disk attachment and copy these attributes to the disk.
 */
public static void addDiskAttachmentDetails(String vmId, V3Disk disk) {
    if (vmId != null) {
        SystemResource systemResource = BackendApiResource.getInstance();
        VmsResource vmsResource = systemResource.getVmsResource();
        VmResource vmResource = vmsResource.getVmResource(vmId);
        DiskAttachmentsResource attachmentsResource = vmResource.getDiskAttachmentsResource();
        String diskId = disk.getId();
        if (diskId != null) {
            DiskAttachmentResource attachmentResource = attachmentsResource.getAttachmentResource(diskId);
            try {
                DiskAttachment attachment = attachmentResource.get();
                addDiskAttachmentDetails(attachment, disk);
            } catch (WebApplicationException exception) {
            // If an application exception is generated while retrieving the details of the disk attachment
            // it is safe to ignore it, as it may be that the user just doesn't have permission to see
            // attachment, but she may still have permissions to see the other details of the disk.
            }
        }
    }
}
Also used : VmResource(org.ovirt.engine.api.resource.VmResource) DiskAttachmentsResource(org.ovirt.engine.api.resource.DiskAttachmentsResource) DiskAttachmentResource(org.ovirt.engine.api.resource.DiskAttachmentResource) DiskAttachment(org.ovirt.engine.api.model.DiskAttachment) WebApplicationException(javax.ws.rs.WebApplicationException) VmsResource(org.ovirt.engine.api.resource.VmsResource) SystemResource(org.ovirt.engine.api.resource.SystemResource)

Example 4 with VmsResource

use of org.ovirt.engine.api.resource.VmsResource in project ovirt-engine by oVirt.

the class V3DiskSnapshotOutAdapter method adapt.

@Override
public V3DiskSnapshot adapt(DiskSnapshot from) {
    V3DiskSnapshot to = new V3DiskSnapshot();
    if (from.isSetLinks()) {
        to.getLinks().addAll(adaptOut(from.getLinks()));
    }
    if (from.isSetActions()) {
        to.setActions(adaptOut(from.getActions()));
    }
    if (from.isSetActualSize()) {
        to.setActualSize(from.getActualSize());
    }
    if (from.isSetAlias()) {
        to.setAlias(from.getAlias());
    }
    if (from.isSetComment()) {
        to.setComment(from.getComment());
    }
    if (from.isSetDescription()) {
        to.setDescription(from.getDescription());
    }
    if (from.isSetDisk()) {
        to.setDisk(adaptOut(from.getDisk()));
    }
    if (from.isSetDiskProfile()) {
        to.setDiskProfile(adaptOut(from.getDiskProfile()));
    }
    if (from.isSetFormat()) {
        to.setFormat(from.getFormat().value());
    }
    if (from.isSetId()) {
        to.setId(from.getId());
    }
    if (from.isSetHref()) {
        to.setHref(from.getHref());
    }
    if (from.isSetImageId()) {
        to.setImageId(from.getImageId());
    }
    if (from.isSetInstanceType()) {
        to.setInstanceType(adaptOut(from.getInstanceType()));
    }
    if (from.isSetLogicalName()) {
        to.setLogicalName(from.getLogicalName());
    }
    if (from.isSetLunStorage()) {
        to.setLunStorage(adaptOut(from.getLunStorage()));
    }
    if (from.isSetName()) {
        to.setName(from.getName());
    }
    if (from.isSetOpenstackVolumeType()) {
        to.setOpenstackVolumeType(adaptOut(from.getOpenstackVolumeType()));
    }
    if (from.isSetPropagateErrors()) {
        to.setPropagateErrors(from.isPropagateErrors());
    }
    if (from.isSetProvisionedSize()) {
        to.setProvisionedSize(from.getProvisionedSize());
    }
    if (from.isSetQuota()) {
        to.setQuota(adaptOut(from.getQuota()));
    }
    if (from.isSetReadOnly()) {
        to.setReadOnly(from.isReadOnly());
    }
    if (from.isSetSgio()) {
        to.setSgio(from.getSgio().value());
    }
    if (from.isSetShareable()) {
        to.setShareable(from.isShareable());
    }
    if (from.isSetSnapshot()) {
        to.setSnapshot(adaptOut(from.getSnapshot()));
    }
    if (from.isSetSparse()) {
        to.setSparse(from.isSparse());
    }
    if (from.isSetStatistics()) {
        to.setStatistics(new V3Statistics());
        to.getStatistics().getStatistics().addAll(adaptOut(from.getStatistics().getStatistics()));
    }
    if (from.isSetStatus()) {
        V3Status status = new V3Status();
        status.setState(from.getStatus().value());
        to.setStatus(status);
    }
    if (from.isSetStorageDomain()) {
        to.setStorageDomain(adaptOut(from.getStorageDomain()));
    }
    if (from.isSetStorageDomains()) {
        to.setStorageDomains(new V3StorageDomains());
        to.getStorageDomains().getStorageDomains().addAll(adaptOut(from.getStorageDomains().getStorageDomains()));
    }
    if (from.isSetStorageType()) {
        to.setStorageType(from.getStorageType().value());
    }
    if (from.isSetTemplate()) {
        to.setTemplate(adaptOut(from.getTemplate()));
    }
    if (from.isSetUsesScsiReservation()) {
        to.setUsesScsiReservation(from.isUsesScsiReservation());
    }
    if (from.isSetVm()) {
        to.setVm(adaptOut(from.getVm()));
    }
    if (from.isSetVms()) {
        to.setVms(new V3VMs());
        to.getVms().getVMs().addAll(adaptOut(from.getVms().getVms()));
    }
    if (from.isSetWipeAfterDelete()) {
        to.setWipeAfterDelete(from.isWipeAfterDelete());
    }
    // continue supporting it. To do so we need to find the disk attachment and copy this attribute to the disk.
    if (to.isSetId() && to.isSetVm() && to.getVm().isSetId()) {
        String diskId = to.getId();
        String vmId = to.getVm().getId();
        VmsResource vmsResource = BackendApiResource.getInstance().getVmsResource();
        VmResource vmResource = vmsResource.getVmResource(vmId);
        DiskAttachmentsResource attachmentsResource = vmResource.getDiskAttachmentsResource();
        DiskAttachmentResource attachmentResource = attachmentsResource.getAttachmentResource(diskId);
        DiskAttachment attachment = attachmentResource.get();
        if (attachment.isSetActive()) {
            to.setActive(attachment.isActive());
        }
    }
    return to;
}
Also used : VmResource(org.ovirt.engine.api.resource.VmResource) DiskAttachmentsResource(org.ovirt.engine.api.resource.DiskAttachmentsResource) DiskAttachmentResource(org.ovirt.engine.api.resource.DiskAttachmentResource) DiskAttachment(org.ovirt.engine.api.model.DiskAttachment) VmsResource(org.ovirt.engine.api.resource.VmsResource) V3DiskSnapshot(org.ovirt.engine.api.v3.types.V3DiskSnapshot) V3Status(org.ovirt.engine.api.v3.types.V3Status) V3Statistics(org.ovirt.engine.api.v3.types.V3Statistics) V3VMs(org.ovirt.engine.api.v3.types.V3VMs) V3StorageDomains(org.ovirt.engine.api.v3.types.V3StorageDomains)

Example 5 with VmsResource

use of org.ovirt.engine.api.resource.VmsResource in project ovirt-engine by oVirt.

the class V3VmHelper method addGuestIp.

/**
 * If the V4 virtual machine has IP addresses reported, then add them to the V3 {@code guest_info} element.
 */
public static void addGuestIp(V3VM vm) {
    String vmId = vm.getId();
    if (vmId != null) {
        SystemResource systemResource = BackendApiResource.getInstance();
        VmsResource vmsResource = systemResource.getVmsResource();
        VmResource vmResource = vmsResource.getVmResource(vmId);
        VmNicsResource nicsResource = vmResource.getNicsResource();
        try {
            Nics fromNics = nicsResource.list();
            List<Ip> fromIps = new ArrayList<>();
            for (Nic fromNic : fromNics.getNics()) {
                ReportedDevices fromDevices = fromNic.getReportedDevices();
                if (fromDevices != null) {
                    for (ReportedDevice fromDevice : fromDevices.getReportedDevices()) {
                        Ips deviceIps = fromDevice.getIps();
                        if (deviceIps != null) {
                            fromIps.addAll(deviceIps.getIps());
                        }
                    }
                }
            }
            if (!fromIps.isEmpty()) {
                V3GuestInfo guestInfo = vm.getGuestInfo();
                if (guestInfo == null) {
                    guestInfo = new V3GuestInfo();
                    vm.setGuestInfo(guestInfo);
                }
                V3IPs ips = guestInfo.getIps();
                if (ips == null) {
                    ips = new V3IPs();
                    guestInfo.setIps(ips);
                }
                ips.getIPs().addAll(adaptOut(fromIps));
            }
        } catch (WebApplicationException exception) {
        // If an application exception is generated while retrieving the details of the NICs is safe to ignore
        // it, as it may be that the user just doesn't have permission to see the NICs, but she may still have
        // permissions to see the other details of the virtual machine.
        }
    }
}
Also used : ReportedDevice(org.ovirt.engine.api.model.ReportedDevice) WebApplicationException(javax.ws.rs.WebApplicationException) Ip(org.ovirt.engine.api.model.Ip) ArrayList(java.util.ArrayList) Nic(org.ovirt.engine.api.model.Nic) ReportedDevices(org.ovirt.engine.api.model.ReportedDevices) Ips(org.ovirt.engine.api.model.Ips) Nics(org.ovirt.engine.api.model.Nics) VmResource(org.ovirt.engine.api.resource.VmResource) VmNicsResource(org.ovirt.engine.api.resource.VmNicsResource) VmsResource(org.ovirt.engine.api.resource.VmsResource) SystemResource(org.ovirt.engine.api.resource.SystemResource) V3GuestInfo(org.ovirt.engine.api.v3.types.V3GuestInfo) V3IPs(org.ovirt.engine.api.v3.types.V3IPs)

Aggregations

VmResource (org.ovirt.engine.api.resource.VmResource)5 VmsResource (org.ovirt.engine.api.resource.VmsResource)5 SystemResource (org.ovirt.engine.api.resource.SystemResource)4 DiskAttachment (org.ovirt.engine.api.model.DiskAttachment)3 DiskAttachmentsResource (org.ovirt.engine.api.resource.DiskAttachmentsResource)3 WebApplicationException (javax.ws.rs.WebApplicationException)2 Nic (org.ovirt.engine.api.model.Nic)2 DiskAttachmentResource (org.ovirt.engine.api.resource.DiskAttachmentResource)2 ArrayList (java.util.ArrayList)1 Comparator.comparing (java.util.Comparator.comparing)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 GET (javax.ws.rs.GET)1 Disk (org.ovirt.engine.api.model.Disk)1 DiskAttachments (org.ovirt.engine.api.model.DiskAttachments)1 Ip (org.ovirt.engine.api.model.Ip)1 Ips (org.ovirt.engine.api.model.Ips)1 Network (org.ovirt.engine.api.model.Network)1 Nics (org.ovirt.engine.api.model.Nics)1