Skip to content
Permalink
Browse files
Changed examples to use the Nova ServerApi. Added new examples CloudF…
…ilesPublish and CloudServersPublish.
  • Loading branch information
Everett Toews committed Sep 27, 2012
1 parent 1ff0904 commit 48cc260231eaaceb2f3fcbfc3f3aa35ae29dd30c
Show file tree
Hide file tree
Showing 10 changed files with 611 additions and 136 deletions.
@@ -23,7 +23,7 @@ $ pwd
$ ls
README.md images/ lein lib/ project.clj src/
```
If you want to run the Logging example (which is strongly encouraged!) then you'll need the Logback JAR files. [Download](http://logback.qos.ch/download.html) the zip or tar.gz file, extract, and drop the JARs into the lib directory.
If you want to run the [Logging example](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/Logging.java) (which is strongly encouraged!) then you'll need the Logback JAR files. [Download](http://logback.qos.ch/download.html) the zip or tar.gz file, extract, and drop the JARs into the lib directory.

If you don't, you'll see this error when you try to run the Logging example but the example will still work.

@@ -78,9 +78,17 @@ Click Run and watch the Console for the output!
Some suggestions.

1. Change the examples to do different things that you want to do
2. After running some examples, compare the output with what you see in the [Cloud Control Panel](https://mycloud.rackspace.com/)
3. Browse the [documentation](http://www.jclouds.org/documentation/)
3. Return to the [Installation Guide](http://www.jclouds.org/documentation/userguide/installation-guide/) and have a look at the different ways to integrate jclouds with your project
4. Join the [jclouds mailing list](https://groups.google.com/forum/?fromgroups#!forum/jclouds) or maybe even the [jclouds developer mailing list](https://groups.google.com/forum/?fromgroups#!forum/jclouds-dev)
1. After running some examples, compare the output with what you see in the [Cloud Control Panel](https://mycloud.rackspace.com/)
1. Browse the [documentation](http://www.jclouds.org/documentation/) and have a look at the [Javadoc](http://www.jclouds.org/documentation/releasenotes/) (choose the Javadoc for the current version)
1. Return to the [Installation Guide](http://www.jclouds.org/documentation/userguide/installation-guide/) and have a look at the different ways to integrate jclouds with your project
1. Join the [jclouds mailing list](https://groups.google.com/forum/?fromgroups#!forum/jclouds) or maybe even the [jclouds developer mailing list](https://groups.google.com/forum/?fromgroups#!forum/jclouds-dev)

Welcome to the jclouds [community](http://www.jclouds.org/documentation/community/)!
Welcome to the jclouds [community](http://www.jclouds.org/documentation/community/)!

## Support and Feedback

Your feedback is appreciated! If you have specific issues with Rackspace support in jclouds, we'd prefer that you file an issue via [Github](https://github.com/jclouds/jclouds/issues).

For general feedback and support requests, send an email to:

[sdk-support@rackspace.com](mailto:sdk-support@rackspace.com)
@@ -18,11 +18,13 @@
*/
package org.jclouds.examples.rackspace;

import org.jclouds.examples.rackspace.cloudfiles.CloudFilesPublish;
import org.jclouds.examples.rackspace.cloudfiles.CreateContainer;
import org.jclouds.examples.rackspace.cloudfiles.CreateObjects;
import org.jclouds.examples.rackspace.cloudfiles.DeleteObjectsAndContainer;
import org.jclouds.examples.rackspace.cloudfiles.ListContainers;
import org.jclouds.examples.rackspace.cloudfiles.ListObjects;
import org.jclouds.examples.rackspace.cloudservers.CloudServersPublish;
import org.jclouds.examples.rackspace.cloudservers.CreateServer;
import org.jclouds.examples.rackspace.cloudservers.DeleteServer;
import org.jclouds.examples.rackspace.cloudservers.ListServersWithFiltering;
@@ -50,11 +52,13 @@ private void smokeTest(String[] args) {
Authentication.main(args);
Logging.main(args);

CloudServersPublish.main(args);
CreateServer.main(args);
ListServersWithFiltering.main(args);
ServerMetadata.main(args);
DeleteServer.main(args);

CloudFilesPublish.main(args);
CreateContainer.main(args);
ListContainers.main(args);
CreateObjects.main(args);
@@ -0,0 +1,136 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.rackspace.cloudfiles;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;

import org.jclouds.ContextBuilder;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.cloudfiles.CloudFilesApiMetadata;
import org.jclouds.cloudfiles.CloudFilesClient;
import org.jclouds.openstack.swift.CommonSwiftAsyncClient;
import org.jclouds.openstack.swift.CommonSwiftClient;
import org.jclouds.openstack.swift.domain.SwiftObject;
import org.jclouds.rest.RestContext;

/**
* This example will create a container, put a file in it, and publish it on the internet!
*/
public class CloudFilesPublish {
private static final String CONTAINER = "jclouds-example-publish";
private static final String FILENAME = "createObjectFromFile";
private static final String SUFFIX = ".html";


private BlobStore storage;
private RestContext<CommonSwiftClient, CommonSwiftAsyncClient> swift;
private CloudFilesClient rackspace;

/**
* 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) {
CloudFilesPublish cloudFilesPublish = new CloudFilesPublish();

try {
cloudFilesPublish.init(args);
cloudFilesPublish.createContainer();
cloudFilesPublish.createObjectFromFile();
cloudFilesPublish.enableCdnContainer();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
cloudFilesPublish.close();
}
}

private void init(String[] args) {
// The provider configures jclouds to use the Rackspace open cloud
String provider = "cloudfiles-us";

String username = args[0];
String apiKey = args[1];

BlobStoreContext context = ContextBuilder.newBuilder(provider)
.credentials(username, apiKey)
.buildView(BlobStoreContext.class);
storage = context.getBlobStore();
swift = context.unwrap();
rackspace = context.unwrap(CloudFilesApiMetadata.CONTEXT_TOKEN).getApi();
}

/**
* This method will create a container in Cloud Files where you can store and
* retrieve any kind of digital asset.
*/
private void createContainer() {
System.out.println("Create Container");
swift.getApi().createContainer(CONTAINER);
System.out.println(" " + CONTAINER);
}

/**
* This method will put a plain text object into the container.
*/
private void createObjectFromFile() throws IOException {
System.out.println("Create Object From File");

File tempFile = File.createTempFile(FILENAME, SUFFIX);
tempFile.deleteOnExit();

BufferedWriter out = new BufferedWriter(new FileWriter(tempFile));
out.write("Hello Cloud Files");
out.close();

SwiftObject object = swift.getApi().newSwiftObject();
object.getInfo().setName(FILENAME + SUFFIX);
object.setPayload(tempFile);

swift.getApi().putObject(CONTAINER, object);

System.out.println(" " + FILENAME + SUFFIX);
}

/**
* This method will put your container on a Content Distribution Network and
* make it 100% publicly accessible over the Internet.
*/
private void enableCdnContainer() {
System.out.println("Enable CDN Container");
URI cdnURI = rackspace.enableCDN(CONTAINER);
System.out.println(" Go to " + cdnURI + "/" + FILENAME + SUFFIX);
}

/**
* Always close your service when you're done with it.
*/
private void close() {
if (storage != null) {
storage.getContext().close();
}
}
}
@@ -59,7 +59,8 @@ public static void main(String[] args) {
createContainer.createObjectFromFile();
createContainer.createObjectFromString();
createContainer.createObjectFromStringWithMetadata();
} catch (IOException e) {
}
catch (IOException e) {
e.printStackTrace();
}
finally {
@@ -25,7 +25,9 @@
import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.openstack.swift.CommonSwiftAsyncClient;
import org.jclouds.openstack.swift.CommonSwiftClient;
import org.jclouds.openstack.swift.domain.ContainerMetadata;
import org.jclouds.openstack.swift.domain.ObjectInfo;
import org.jclouds.openstack.swift.options.ListContainerOptions;
import org.jclouds.rest.RestContext;

/**
@@ -51,8 +53,7 @@ public static void main(String[] args) {

try {
deleteObjectsAndContainer.init(args);
deleteObjectsAndContainer.deleteObjects();
deleteObjectsAndContainer.deleteContainer();
deleteObjectsAndContainer.deleteObjectsAndContainer();
}
finally {
deleteObjectsAndContainer.close();
@@ -73,23 +74,27 @@ private void init(String[] args) {
swift = context.unwrap();
}

private void deleteObjects() {
System.out.println("Delete Objects");

Set<ObjectInfo> objects = swift.getApi().listObjects(CONTAINER);

for (ObjectInfo object: objects) {
System.out.println(" " + object.getName());
swift.getApi().removeObject(CONTAINER, object.getName());
}
}

private void deleteContainer() {
/**
* This will delete all containers that start with {@link CONTAINER} and the objects within those containers.
*/
private void deleteObjectsAndContainer() {
System.out.println("Delete Container");

swift.getApi().deleteContainerIfEmpty(CONTAINER);
Set<ContainerMetadata> containers = swift.getApi().listContainers(ListContainerOptions.Builder.withPrefix(CONTAINER));

for (ContainerMetadata container: containers) {
System.out.println(" " + container.getName());

Set<ObjectInfo> objects = swift.getApi().listObjects(container.getName());

for (ObjectInfo object: objects) {
System.out.println(" " + object.getName());

swift.getApi().removeObject(container.getName(), object.getName());
}

System.out.println(" " + CONTAINER);
swift.getApi().deleteContainerIfEmpty(container.getName());
}
}

/**

0 comments on commit 48cc260

Please sign in to comment.