Skip to content

Commit

Permalink
The start of examples for OpenStack
Browse files Browse the repository at this point in the history
  • Loading branch information
etoews committed Sep 4, 2014
1 parent 22c5bdf commit 46f452d
Show file tree
Hide file tree
Showing 5 changed files with 377 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Expand Up @@ -11,8 +11,8 @@ atlassian-ide-plugin.xml
*.ipr
*.iws
*.class
rackspace/lib/
rackspace/bin/
lib/
bin/
lein
project.clj
.lein-deps-sum
Expand Down
64 changes: 64 additions & 0 deletions openstack/README.md
@@ -0,0 +1,64 @@
# OpenStack Examples
Example code that uses jclouds to perform common tasks on an OpenStack Cloud. The class names are self explanatory and the code is well commented for you to follow along.

- [Requirements](#requirements)
- [Environment](#environment)
- [The Examples](#examples)
- [Support and Feedback](#support-and-feedback)

## Requirements

1. Username and password for an OpenStack Cloud - See the [Getting Started guide](http://jclouds.apache.org/guides/openstack/).
1. Java Development Kit (JDK) version 6 or later - [Download](http://www.oracle.com/technetwork/java/javase/downloads/index.html).
1. Apache Maven - [Maven in 5 Minutes](http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html).
1. Git - [Download](http://git-scm.com/downloads).

## Environment
To setup an environment to compile and run the examples use these commands:

```
git clone https://github.com/jclouds/jclouds-examples.git
cd jclouds-examples/openstack/
```

To download all dependencies, run:

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

If you also want to download the source jars, run:

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

## Examples

To run individual examples from the command line use these commands:

Note: If you're on Windows, the only change you need to make is to use a ';' instead of a ':' in the paths.

```
javac -classpath "lib/*:src/main/java/:src/main/resources/" src/main/java/org/jclouds/examples/openstack/identity/*.java
```

Every example class has a main method that takes the following arguments in the listed order

1. Identity (Keystone) endpoint (e.g. an IP address or URL)
1. Tenant name
1. User name
1. Password

Try out an example.

```
java -classpath "lib/*:src/main/java/:src/main/resources/" org.jclouds.examples.openstack.identity.CreateTenantAndUser identityEndpoint myTenantname myUsername myPassword
```
Watch the terminal for output!

## Support and Feedback

Your feedback is appreciated! If you have specific issues with OpenStack support in jclouds, we'd prefer that you file an issue via [JIRA](https://issues.apache.org/jira/browse/JCLOUDS).

If you have questions or need help, please join our [community](http://jclouds.apache.org/community/) and subscribe to the jclouds user mailing list.
97 changes: 97 additions & 0 deletions openstack/pom.xml
@@ -0,0 +1,97 @@
<?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>openstack-examples</artifactId>
<version>1.8.0</version>
<name>openstack-examples</name>

<properties>
<jclouds.version>1.8.0</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>
<!-- jclouds OpenStack dependencies -->
<dependency>
<groupId>org.apache.jclouds.api</groupId>
<artifactId>openstack-keystone</artifactId>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.api</groupId>
<artifactId>openstack-nova</artifactId>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.labs</groupId>
<artifactId>openstack-swift</artifactId>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.api</groupId>
<artifactId>openstack-cinder</artifactId>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.api</groupId>
<artifactId>openstack-trove</artifactId>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.labs</groupId>
<artifactId>openstack-glance</artifactId>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.labs</groupId>
<artifactId>openstack-marconi</artifactId>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.labs</groupId>
<artifactId>openstack-neutron</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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,125 @@
/*
* 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.
*/
package org.jclouds.examples.openstack.identity;

import com.google.common.base.Optional;
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.keystone.v2_0.KeystoneApi;
import org.jclouds.openstack.keystone.v2_0.domain.Tenant;
import org.jclouds.openstack.keystone.v2_0.domain.User;
import org.jclouds.openstack.keystone.v2_0.extensions.TenantAdminApi;
import org.jclouds.openstack.keystone.v2_0.extensions.UserAdminApi;
import org.jclouds.openstack.keystone.v2_0.options.CreateTenantOptions;
import org.jclouds.openstack.keystone.v2_0.options.CreateUserOptions;

import java.io.Closeable;
import java.io.IOException;

public class CreateTenantAndUser implements Closeable {
private final KeystoneApi keystoneApi;

/**
* The first argument (args[0]) must be your Identity (Keystone) endpoint (e.g. an IP address or URL)
* The second argument (args[1]) must be your tenant name
* The third argument (args[2]) must be your user name
* The fourth argument (args[3]) must be your password
*
* For this example your endpoint must be the *admin endpoint* of your Identity service
* (e.g. "http://111.222.333.444:35357/v2.0/")
*/
public static void main(String[] args) throws IOException {
CreateTenantAndUser createTenantAndUser = new CreateTenantAndUser(args[0], args[1], args[2], args[3]);

try {
Tenant tenant = createTenantAndUser.createTenant();
createTenantAndUser.createUser(tenant);
createTenantAndUser.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
createTenantAndUser.close();
}
}

public CreateTenantAndUser(String endpoint, String tenantName, String userName, String password) {
System.out.format("%s%n", this.getClass().getName());

Iterable<Module> modules = ImmutableSet.<Module>of(new SLF4JLoggingModule());

String provider = "openstack-keystone";
String identity = tenantName + ":" + userName;

keystoneApi = ContextBuilder.newBuilder(provider)
.endpoint(endpoint)
.credentials(identity, password)
.modules(modules)
.buildApi(KeystoneApi.class);
}

private Tenant createTenant() {
System.out.format(" Create Tenant%n");

Optional<? extends TenantAdminApi> tenantAdminApiExtension = keystoneApi.getTenantAdminApi();

if (tenantAdminApiExtension.isPresent()) {
System.out.format(" TenantAdminApi is present%n");

TenantAdminApi tenantAdminApi = tenantAdminApiExtension.get();
CreateTenantOptions tenantOptions = CreateTenantOptions.Builder
.description("My New Tenant");
Tenant tenant = tenantAdminApi.create("newTenant", tenantOptions);

System.out.format(" %s%n", tenant);

return tenant;
} else {
System.out.format(" TenantAdminApi is *not* present%n");
System.exit(1);

return null;
}
}

private void createUser(Tenant tenant) {
System.out.format(" Create User%n");

Optional<? extends UserAdminApi> userAdminApiExtension = keystoneApi.getUserAdminApi();

if (userAdminApiExtension.isPresent()) {
System.out.format(" UserAdminApi is present%n");

UserAdminApi userAdminApi = userAdminApiExtension.get();
CreateUserOptions userOptions = CreateUserOptions.Builder
.tenant(tenant.getId())
.email("new.email@example.com");
User user = userAdminApi.create("newUser", "newPassword", userOptions);

System.out.format(" %s%n", user);
} else {
System.out.format(" UserAdminApi is *not* present%n");
System.exit(1);
}
}

public void close() throws IOException {
Closeables.close(keystoneApi, true);
}
}
89 changes: 89 additions & 0 deletions openstack/src/main/resources/logback.xml
@@ -0,0 +1,89 @@
<?xml version="1.0"?>
<!--
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.
-->

<configuration scan="false">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>- %m%n</pattern>
</encoder>
</appender>

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>target/test-data/jclouds.log</file>

<encoder>
<Pattern>%d %-5p [%c] [%thread] %m%n</Pattern>
</encoder>
</appender>

<appender name="WIREFILE" class="ch.qos.logback.core.FileAppender">
<file>target/test-data/jclouds-wire.log</file>

<encoder>
<Pattern>%d %-5p [%c] [%thread] %m%n</Pattern>
</encoder>
</appender>

<appender name="COMPUTEFILE" class="ch.qos.logback.core.FileAppender">
<file>target/test-data/jclouds-compute.log</file>

<encoder>
<Pattern>%d %-5p [%c] [%thread] %m%n</Pattern>
</encoder>
</appender>

<appender name="SSHFILE" class="ch.qos.logback.core.FileAppender">
<file>target/test-data/jclouds-ssh.log</file>

<encoder>
<Pattern>%d %-5p [%c] [%thread] %m%n</Pattern>
</encoder>
</appender>

<root>
<level value="warn" />
</root>

<logger name="org.jclouds">
<level value="DEBUG" />
<appender-ref ref="FILE" />
</logger>

<logger name="jclouds.wire">
<level value="DEBUG" />
<appender-ref ref="STDOUT" />
</logger>

<logger name="jclouds.headers">
<level value="DEBUG" />
<appender-ref ref="STDOUT" />
</logger>

<logger name="jclouds.compute">
<level value="DEBUG" />
<appender-ref ref="COMPUTEFILE" />
</logger>

<logger name="jclouds.ssh">
<level value="DEBUG" />
<appender-ref ref="SSHFILE" />
</logger>

</configuration>

0 comments on commit 46f452d

Please sign in to comment.