Images V2 (Glance)
If you are unfamiliar with an OpenStack image then you can think of it as a “virtual machine template”. Images can also be standard installation media such as ISO images.
Detailed information about how to use the Glance calls can be found in the OpenStack Glance V2 docs.
Creating, Reserving and Uploading Images
When an image (template) is created it is in what is known as a reserved state. A reserved state means we have reserved a placeholder but it contains no image data (uploaded media). The status will be marked as Queued
when in a reserve state.
Create an Image
Image will be in queued state until image data is uploaded.
Image image = os.imagesV2().create(
Builders.imageV2()
.name("Cirros 0.3.0 x64")
.containerFormat(ContainerFormat.BARE)
.visibility(Image.ImageVisibility.PUBLIC)
.diskFormat(DiskFormat.QCOW2)
.minDisk(0)
.minRam(0)
.build()
);
Upload Image Data
Payloads can be created with URLs, files, or input streams.
// Create a Payload - we will use URL in this example
Payload<URL> payload = Payloads.create(new URL("https://some/url/cirros-0.3.0-x86_64-disk.img"));
// Get image object to use, or send null
Image image = os.imagesV2().get("imageId");
ActionResponse upload = os.imagesV2.upload(
image.getId(),
payload,
image);
Download Image data
File file = new File(new URI("file:///some/path/to/image.iso"));
ActionResponse download = os.imagesV2().download(image.getId(), file);
Updating, Deleting and Querying
Updating an Image
Update an image by providing a changed image object, or a json patch object.
Use Original Image
Image image = os.imagesV2().update(
originalImage.toBuilder().
containerFormat(ContainerFormat.BARE)
.name("New Name")
.build()
);
Use Json Patch Operation
Image updatedImage = os.imagesV2().update("imageId",
Builders.imageUpdateV2().ops(
new PatchOperation(
PatchOperation.OperationType.REPLACE,
"/container_format",
ContainerFormat.BARE
)
).build()
);
Delete an Image
Permanently delete image.
os.imagesV2().delete("imageId");
Querying for Images
Pagination for images is done with url parameters as described here.
List all Images
List<? extends Image> images = os.imagesV2().list();
Get Image by Id
Image image = os.imagesV2().get("imageId");
Activating, Deactivating and Tagging
You can’t download a deactivated image. Additionally, only administrative users can view image locations for deactivated images.
Activation and Deactivation
Deactivate Image
os.imagesV2().deactivate("imageId");
Activate Image
os.imagesV2.activate("imageid");
Image Tags
Image tags are strings.
Add Tag
os.imagesV2().updateTag("imageId", "tag");
Remove Tag
os.imagesV2().deleteTag("imageId", "tag");
Image Memberships
Image memberships are a way to share a private image with other tenants. Those tenants can also be assigned rights to re-share the private image. Public images are available but private images require memberships for non-owners to access.
Querying Memberships for an Image
Get All Members
List<? extends Member> members = os.imagesV2().listMembers("imageId");
Get a Single Member
Member member = os.imagesV2().getMember("imageId", "memberId");
Create a Member (authorizing a tenant for a private image)
Member member = os.imagesV2().createMember("imageId", "memberId");
Updating a Member
Member member = os.imagesV2().updateMember("imageId", "memberId", Member.MemberStatus.ACCEPTED));
Removing a Member (revoking a tenant)
os.imagesV2().deleteMember("imageId", "memberId");
Tasks
Glance tasks are intended to offer end users a front end to long running asynchronous operations – the type of operation you kick off and don’t expect to finish until you’ve gone to the coffee shop, had a pleasant chat with your barista, had a coffee, had a pleasant walk home, etc.
Create Task
Here the json object for input is created using a HashMap, but it can be any valid json object.
// Create json object for input
Map<String, Object> input = new HashMap<String, Object>();
// Fill with the input
// Create task
Task task1 = os.imagesV2().tasks().create(
Builders.taskBuilder()
.type("import")
.input(input)
.build()
);
List Tasks
List all Tasks
List<? extends Task> tasks = os.imagesV2().tasks().list();
List all Tasks with Filtering
Map<String, String> params = new HashMap<String, String>();
params.put("id","taskId");
List<? extends Task> list = os.imagesV2().tasks().list(params);
Show Task Details
Task task = os.imagesV2().tasks().get("taskId");