Skip to content

Instantly share code, notes, and snippets.

@etoews
Created April 22, 2016 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etoews/bca2295e610955bbadab44a4c98bb4d2 to your computer and use it in GitHub Desktop.
Save etoews/bca2295e610955bbadab44a4c98bb4d2 to your computer and use it in GitHub Desktop.
Authenticating the jclouds Java SDK with OpenStack or Rackspace and listing servers in all regions

List Servers with OpenStack or Rackspace

Make a directory and copy all of the files below into it then run the following commands.

mvn dependency:copy-dependencies "-DoutputDirectory=./lib"

javac -classpath ".:lib/*" ListServersRackspace.java
java -classpath ".:lib/*" ListServersRackspace $RS_USERNAME $RS_API_KEY

javac -classpath ".:lib/*" ListServersOpenStack.java
java -classpath ".:lib/*" ListServersOpenStack $OS_AUTH_URL $OS_PROJECT_NAME $OS_USERNAME $OS_PASSWORD

An example OS_AUTH_URL looks like http://xxx.xxx.xxx.xxx:5000/v2.0/

import com.google.common.collect.ImmutableSet;
import com.google.common.io.Closeables;
import com.google.inject.Module;
import org.jclouds.ContextBuilder;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.openstack.nova.v2_0.domain.Server;
import org.jclouds.openstack.nova.v2_0.features.ServerApi;
import java.io.Closeable;
import java.io.IOException;
import java.util.Set;
public class ListServersOpenStack implements Closeable {
private final NovaApi novaApi;
private final Set<String> regions;
/**
* The first argument (args[0]) must be your OpenStack endpoint
* The second argument (args[1]) must be your project
* The third argument (args[2]) must be your username
* The fourth argument (args[3]) must be your password
*/
public static void main(String[] args) throws IOException {
String endpoint = args[0];
String project = args[1];
String username = args[2];
String password = args[3];
ListServersOpenStack listServersOpenStack = new ListServersOpenStack(endpoint, project, username, password);
try {
listServersOpenStack.listServers();
listServersOpenStack.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
listServersOpenStack.close();
}
}
public ListServersOpenStack(String endpoint, String project, String username, String password) {
Iterable<Module> modules = ImmutableSet.<Module>of(new SLF4JLoggingModule());
novaApi = ContextBuilder.newBuilder("openstack-nova")
.endpoint(endpoint)
.credentials(project + ":" + username, password)
.modules(modules)
.buildApi(NovaApi.class);
regions = novaApi.getConfiguredRegions();
}
private void listServers() {
for (String region : regions) {
ServerApi serverApi = novaApi.getServerApi(region);
System.out.println("Servers in " + region);
for (Server server : serverApi.listInDetail().concat()) {
System.out.println(" " + server);
}
}
}
public void close() throws IOException {
Closeables.close(novaApi, true);
}
}
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Closeables;
import com.google.inject.Module;
import org.jclouds.ContextBuilder;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.openstack.nova.v2_0.domain.Server;
import org.jclouds.openstack.nova.v2_0.features.ServerApi;
import java.io.Closeable;
import java.io.IOException;
import java.util.Set;
public class ListServersRackspace implements Closeable {
private final NovaApi novaApi;
private final Set<String> regions;
/**
* The first argument (args[0]) must be your username
* The second argument (args[1]) must be your API key
*/
public static void main(String[] args) throws IOException {
String username = args[0];
String apiKey = args[1];
ListServersRackspace listServersRackspace = new ListServersRackspace(username, apiKey);
try {
listServersRackspace.listServers();
listServersRackspace.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
listServersRackspace.close();
}
}
public ListServersRackspace(String username, String apiKey) {
Iterable<Module> modules = ImmutableSet.<Module>of(new SLF4JLoggingModule());
novaApi = ContextBuilder.newBuilder("rackspace-cloudservers-us")
.credentials(username, apiKey)
.modules(modules)
.buildApi(NovaApi.class);
regions = novaApi.getConfiguredRegions();
}
private void listServers() {
for (String region : regions) {
ServerApi serverApi = novaApi.getServerApi(region);
System.out.println("Servers in " + region);
for (Server server : serverApi.listInDetail().concat()) {
System.out.println(" " + server);
}
}
}
public void close() throws IOException {
Closeables.close(novaApi, true);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.jclouds.examples</groupId>
<artifactId>rackspace-examples</artifactId>
<version>1.9.1</version>
<name>rackspace-examples</name>
<properties>
<jclouds.version>1.9.1</jclouds.version>
</properties>
<dependencies>
<!-- jclouds dependencies -->
<dependency>
<groupId>org.apache.jclouds.driver</groupId>
<artifactId>jclouds-slf4j</artifactId>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.driver</groupId>
<artifactId>jclouds-sshj</artifactId>
<version>${jclouds.version}</version>
</dependency>
<!-- Rackspace US dependencies -->
<dependency>
<groupId>org.apache.jclouds.provider</groupId>
<artifactId>rackspace-cloudservers-us</artifactId>
<version>${jclouds.version}</version>
</dependency>
<!-- 3rd party dependencies -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>1.0-rc2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment