Tanmoy
Tanmoy

Reputation: 1399

Java API for downloading Openstack images

I want to take snapshot of a running server (Openstack env) in .vmdk format and download the .vmdk file in my local. Is there any java API for the same?

I want Java API similar to openstack CLI "nova image-create" and "glance image-download".

Upvotes: 0

Views: 479

Answers (3)

user7610
user7610

Reputation: 28841

OpenStack provides REST API.

There are no project-endorsed API clients for Java, which means you have a choice of three from https://wiki.openstack.org/wiki/SDKs#Java.

Alternatively you can treat OpenStack like any other REST service and code directly against the API documented at https://developer.openstack.org/api-ref/image/ using any HTTP client library for Java which you prefer, say https://github.com/google/google-http-java-client.

I've been solving a similar problem and I decided to use the Apache jclouds library from the list above.

Downloading image data is only available in the beta openstack-glance package which I included in my build file (Gradle Script Kotlin) using

compile(group="org.apache.jclouds", name="jclouds-all", version="2.1.0")
compile(group="org.apache.jclouds.labs", name="openstack-glance", version = "2.1.0")

and I had to override gson which is a workaround for some bad interaction with Sprint Boot which I also use in my project (I did not investigate this deeply)

// resolve com.google.inject.internal.util.$ComputationException: com.google.inject.internal.util.$ComputationException: com.google.inject.internal.util.$ComputationException: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
// https://stackoverflow.com/questions/43574426/how-to-resolve-java-lang-noclassdeffounderror-javax-xml-bind-jaxbexception-in-j
runtime(group="javax.xml.bind", name="jaxb-api", version="2.3.0")

Now, I can list all images on OpenStack by doing

val tenantName = "xxx"
val userName = "yyy"
val password = "zzz"
val endpoint = "https://aaa.com:13000/v2.0"
val region = "regionOne"

val identity = "$tenantName:$userName"
//    val modules = setOf(SLF4JLoggingModule())  // not actually necessary

val novaApi = ContextBuilder.newBuilder("openstack-nova")
        .endpoint(endpoint)
        .credentials(identity, password)
//            .modules(modules)
        .buildApi(NovaApi::class.java)
val imageApi = novaApi.getImageApi(region)

    for (images in imageApi.listInDetail()) {
        for (image in images) {
            print(image.id)
        }
    }
}

novaApi.close()

You cannot use the NovaApi to actually download the data, for that, you have to do

val glanceApi = ContextBuilder.newBuilder("openstack-glance")
        .endpoint(endpoint)
        .credentials(identity, password)
        .buildApi(org.jclouds.openstack.glance.v1_0.GlanceApi::class.java)
val imageApi = glanceApi.getImageApi(region)

    val length = imageApi.get(id).size.or(-1)
    val stream = imageApi.getAsStream(id)

glanceApi.close()

The reason for using both the NovaApi and GlanceApi is that when I was listing images, I could not get the pagination with GlanceApi to work. It worked ok with NovaApi.

At this point, the stream is just an InputStream instance which you can write into a file. There is a helpful method in Java 7 to do this.

Files.copy(InputStream in, Path target)

Upvotes: 0

Al Kari
Al Kari

Reputation: 73

Not sure I understand the relationship between a .vmdk snapshot and a java api for OpenStack. If you are looking for an alternative to using OpenStack native api, check out the java based https://github.com/paypal/aurora

Upvotes: 0

Jeremy Daggett
Jeremy Daggett

Reputation: 91

The jclouds openstack-nova API supports creating an image from a server.

Also, depending on the version of Glance you are using, jclouds has v1 support for downloading an image.

Upvotes: 1

Related Questions