diff --git a/.gitignore b/.gitignore index ded7036..cb86fa8 100644 --- a/.gitignore +++ b/.gitignore @@ -11,8 +11,8 @@ atlassian-ide-plugin.xml *.ipr *.iws *.class -rackspace/lib/ -rackspace/bin/ +lib/ +bin/ lein project.clj .lein-deps-sum diff --git a/openstack/README.md b/openstack/README.md new file mode 100644 index 0000000..09af37e --- /dev/null +++ b/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. diff --git a/openstack/pom.xml b/openstack/pom.xml new file mode 100644 index 0000000..306c2c2 --- /dev/null +++ b/openstack/pom.xml @@ -0,0 +1,97 @@ + + + + 4.0.0 + org.apache.jclouds.examples + openstack-examples + 1.8.0 + openstack-examples + + + 1.8.0 + + + + + + org.apache.jclouds.driver + jclouds-slf4j + ${jclouds.version} + + + org.apache.jclouds.driver + jclouds-sshj + ${jclouds.version} + + + + org.apache.jclouds.api + openstack-keystone + ${jclouds.version} + + + org.apache.jclouds.api + openstack-nova + ${jclouds.version} + + + org.apache.jclouds.labs + openstack-swift + ${jclouds.version} + + + org.apache.jclouds.api + openstack-cinder + ${jclouds.version} + + + org.apache.jclouds.api + openstack-trove + ${jclouds.version} + + + org.apache.jclouds.labs + openstack-glance + ${jclouds.version} + + + org.apache.jclouds.labs + openstack-marconi + ${jclouds.version} + + + org.apache.jclouds.labs + openstack-neutron + ${jclouds.version} + + + + ch.qos.logback + logback-classic + 1.0.13 + + + mysql + mysql-connector-java + 5.1.25 + + + diff --git a/openstack/src/main/java/org/jclouds/examples/openstack/identity/CreateTenantAndUser.java b/openstack/src/main/java/org/jclouds/examples/openstack/identity/CreateTenantAndUser.java new file mode 100644 index 0000000..607fc1f --- /dev/null +++ b/openstack/src/main/java/org/jclouds/examples/openstack/identity/CreateTenantAndUser.java @@ -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 modules = ImmutableSet.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 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 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); + } +} \ No newline at end of file diff --git a/openstack/src/main/resources/logback.xml b/openstack/src/main/resources/logback.xml new file mode 100644 index 0000000..4c8f8be --- /dev/null +++ b/openstack/src/main/resources/logback.xml @@ -0,0 +1,89 @@ + + + + + + + - %m%n + + + + + target/test-data/jclouds.log + + + %d %-5p [%c] [%thread] %m%n + + + + + target/test-data/jclouds-wire.log + + + %d %-5p [%c] [%thread] %m%n + + + + + target/test-data/jclouds-compute.log + + + %d %-5p [%c] [%thread] %m%n + + + + + target/test-data/jclouds-ssh.log + + + %d %-5p [%c] [%thread] %m%n + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +