This is a guest post by Ignasi Barrera, a PMC member on the Apache jclouds® project and active contributor to jclouds.
Apache jclouds® is a cloud agnostic library for the JVM that enables developers to access over 30 cloud providers using one API. It provides common interfaces to access different services in the cloud, allowing users can easily write portable code:
Apart from the common interfaces, jclouds also provides provider specific APIs, allowing users to use specific features of each cloud provider and leveraging full control over the cloud resources.
CloudSigma is one of the providers that has been supported in jclouds for a long time. Early releases of jclouds include support for the v1 API, and starting from jclouds 1.7.0, the version 2 of the API is supported too.
CloudSigma is not only supporting jclouds by contributing code and keeping the provider up to date, but is also helping the project by providing testing accounts so live tests can be executed in every release to make sure everything works as expected. This has significantly helped identifying issues during development and improving the quality of the provider.
In this blog post we are going to show an overview of jclouds, and how the CloudSigma v2 API can be used directly to effectively manage the cloud.
Using the CloudSigma API is easy. In the following examples we’re going to see how to use the ClousSigma v2 API with jclouds. First of all, the CloudSigma v2 dependency has to be added to the project:
<dependency> <groupId>org.apache.jclouds.labs</groupId> <artifactId>cloudsigma2-lvs</artifactId> <version>1.7.1</version> </dependency>
We’ll be using Las Vegas region, but jclouds also supports Zürich and Washington D.C for the version 2 of the API. To use them, just change the artifactId to cloudsigma-zrh or cloudsigma-wdc.
The first thing that needs to be done is to create the API using the ContextBuilder:
// Note that you can directly create the API classes instead of building // an abstraction view (such as the Compute one). This approach can be // used when you are just going to use the provider specific APIs CloudSigma2Api api = ContextBuilder.newBuilder("cloudsigma2-lvs") .credentials("identity", "credential") .buildApi(CloudSigma2Api.class);
In this example we’ll show how to use the v2 API to:
// First of all we're going to select the desired drive from the library String driveName = "Ubuntu 13.10 Server"; FluentIterable<LibraryDrive> drives = api.listLibraryDrives().concat(); Optional<LibraryDrive> libraryDrive = drives.firstMatch(and(name(driveName), media(MediaType.DISK))); // Next step is to clone the drive and identify it in our drive list api.cloneLibraryDrive(libraryDrive.get().getUuid(), null); Optional<DriveInfo> cloned = api.listDrivesInfo().concat() .firstMatch(and(name(driveName), status(UNAVAILABLE))); // Once identified, we'll wait until the clone operation has finned and the // drive is available, and then configure how it will be attached to the // server Predicates2.retry(status(UNMOUNTED), 60000).apply(cloned.get()); ServerDrive drive = cloned.get().toServerDrive(1, "0:1", VIRTIO); // We are also going to assign a public IP to our server Optional<IPInfo> ip = api.listIPInfo().concat().firstMatch(available()); NIC nic = ip.get().toNIC(); // Finally, we can create the server with the cloned drive and the selected // public IP ServerInfo serverToCreate = new ServerInfo.Builder() .name("New Server with VLAN") .memory(new BigInteger("5368709120")) .cpu(3000) .vncPassword("new_password") .nics(ImmutableList.of(nic)) .drives(ImmutableList.of(drive)) .build(); ServerInfo createdServer = api.createServer(serverToCreate); api.startServer(createdServer.getUuid());
In this example we’ll show how you can create a private VLAN so we can use private networking in the servers we create:
// Create the VLAN CreateSubscriptionRequest request = new CreateSubscriptionRequest.Builder() .amount("1") .period("1 month") .resource(SubscriptionResource.VLAN) .build(); Subscription subscription = api.createSubscription(request); // Get the created VLAN info and convert it to a NIC so it can be attached // to a server String vlanUuid = subscription.getSubscribedObject(); NIC nic = api.getVLANInfo(vlanUuid).toNIC();
Now the NIC can be attached to a server just as shown in the previous example:
This blog post is just an introduction to Apache jclouds® and the CloudSigma API. There are many features that can be explored, such as the compute extensions, automatic pagination, integration with Chef, etc. I strongly encourage you to try the examples in this post and start playing with jclouds and CloudSigma.
To learn more, you just have to check the links below:
You will also find help in the jclouds mailing lists and the #jclouds IRC channel at Freenode.