use of com.woorea.openstack.glance.model.v2.Images in project ovirt-engine by oVirt.
the class OpenStackImageProviderProxy method createImageFromDiskImage.
public String createImageFromDiskImage(DiskImage diskImage) {
Image glanceImage = new Image();
glanceImage.setName(diskImage.getDiskAlias());
if (diskImage.getVolumeFormat() == VolumeFormat.RAW) {
glanceImage.setDiskFormat(GlanceImageFormat.RAW.getValue());
} else if (diskImage.getVolumeFormat() == VolumeFormat.COW) {
glanceImage.setDiskFormat(GlanceImageFormat.COW.getValue());
} else {
throw new OpenStackImageException(OpenStackImageException.ErrorType.UNSUPPORTED_DISK_FORMAT, "Unknown disk format: " + diskImage.getVolumeFormat());
}
glanceImage.setContainerFormat(GlanceImageContainer.BARE.getValue());
Image retGlanceImage = getClient().images().create(glanceImage).execute();
return retGlanceImage.getId();
}
use of com.woorea.openstack.glance.model.v2.Images in project ovirt-engine by oVirt.
the class OpenStackImageProviderProxy method setCowVirtualSizeAndQcowCompat.
private void setCowVirtualSizeAndQcowCompat(DiskImage diskImage, Image glanceImage) {
// For the qcow2 format we need to download the image header and read the virtual size from there
byte[] imgContent = new byte[72];
ImageDownload downloadImage = getClient().images().download(glanceImage.getId()).execute();
try (InputStream inputStream = downloadImage.getInputStream()) {
int bytesRead = inputStream.read(imgContent, 0, imgContent.length);
if (bytesRead != imgContent.length) {
throw new OpenStackImageException(OpenStackImageException.ErrorType.UNABLE_TO_DOWNLOAD_IMAGE, "Unable to read image header: " + bytesRead);
}
} catch (IOException e) {
throw new OpenStackImageException(OpenStackImageException.ErrorType.UNABLE_TO_DOWNLOAD_IMAGE, "Unable to download image");
}
ByteBuffer b = ByteBuffer.wrap(imgContent);
int qcow2Signature = b.getInt();
int qcow2Version = b.getInt();
QcowCompat qcowCompat = QcowCompat.forQcowHeaderVersion(qcow2Version);
if (qcow2Signature == QCOW2_SIGNATURE && qcowCompat != null && qcowCompat != QcowCompat.Undefined) {
b.position(QCOW2_SIZE_OFFSET);
diskImage.setSize(b.getLong());
diskImage.setQcowCompat(qcowCompat);
} else {
throw new OpenStackImageException(OpenStackImageException.ErrorType.UNRECOGNIZED_IMAGE_FORMAT, "Unable to recognize QCOW2 format");
}
}
use of com.woorea.openstack.glance.model.v2.Images in project ovirt-engine by oVirt.
the class OpenStackImageProviderProxy method getAllImagesAsRepoImages.
public List<RepoImage> getAllImagesAsRepoImages(Integer listSize, Integer totalListSize) {
ArrayList<RepoImage> repoImages = new ArrayList<>();
long currentTime = System.currentTimeMillis();
Images images = null;
do {
OpenStackRequest<Images> listRequest = getClient().images().list(true).queryParam("limit", listSize).queryParam("sort_key", "name").queryParam("sort_dir", "asc");
if (images != null) {
listRequest.queryParam("marker", images.getList().get(images.getList().size() - 1).getId());
}
images = listRequest.execute();
for (Image glanceImage : images) {
RepoImage repoImage = imageToRepoImage(glanceImage);
repoImage.setLastRefreshed(currentTime);
repoImages.add(repoImage);
}
} while (images.getList().size() >= listSize && totalListSize != null && repoImages.size() < totalListSize);
return repoImages;
}
use of com.woorea.openstack.glance.model.v2.Images in project ovirt-engine by oVirt.
the class OpenStackImageProviderProxy method getImageAsDiskImage.
public DiskImage getImageAsDiskImage(String id) {
DiskImage diskImage = new DiskImage();
Image glanceImage;
try {
glanceImage = getClient().images().show(id).execute();
} catch (OpenStackResponseException e) {
log.debug("Exception:", e);
throw new OpenStackImageException(OpenStackImageException.ErrorType.IMAGE_NOT_FOUND, "Cannot find the specified image.");
} catch (RuntimeException rte) {
log.error("Exception:", rte);
throw new RuntimeException("Failed to import from the repository.");
}
validateContainerFormat(glanceImage);
String shortHash = glanceImage.getId().substring(0, 7);
if (glanceImage.getName() != null) {
diskImage.setDiskDescription(glanceImage.getName() + " (" + shortHash + ")");
} else {
diskImage.setDiskDescription("Glance disk: " + shortHash);
}
setDiskAttributes(diskImage, glanceImage);
if (glanceImage.getDiskFormat().equals(GlanceImageFormat.RAW.getValue())) {
diskImage.setVolumeFormat(VolumeFormat.RAW);
} else if (glanceImage.getDiskFormat().equals(GlanceImageFormat.COW.getValue())) {
diskImage.setVolumeFormat(VolumeFormat.COW);
} else {
throw new OpenStackImageException(OpenStackImageException.ErrorType.UNSUPPORTED_DISK_FORMAT, "Unknown disk format: " + glanceImage.getDiskFormat());
}
return diskImage;
}
Aggregations