mirror of https://github.com/apache/jclouds.git
simplified example as it was too complicated
git-svn-id: http://jclouds.googlecode.com/svn/trunk@1938 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
parent
f316edd42d
commit
c2b62a0a03
|
@ -1,56 +0,0 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
|
|
||||||
*
|
|
||||||
* ====================================================================
|
|
||||||
* 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.aws.s3;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.TimeoutException;
|
|
||||||
|
|
||||||
import org.jclouds.aws.s3.domain.BucketMetadata;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* CreateListOwnedBuckets is a class contaning operations to creates a bucket if it doesn't exist
|
|
||||||
* and lists all buckets owned by the user.
|
|
||||||
*
|
|
||||||
* @author Carlos Fernandes
|
|
||||||
*/
|
|
||||||
public class CreateListOwnedBuckets {
|
|
||||||
|
|
||||||
S3Context s3Context;
|
|
||||||
|
|
||||||
public CreateListOwnedBuckets(S3Context context) {
|
|
||||||
this.s3Context = context;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<BucketMetadata> list() throws InterruptedException, ExecutionException,
|
|
||||||
TimeoutException {
|
|
||||||
return s3Context.getApi().listContainers();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Boolean createBucket(String bucketName) throws InterruptedException, ExecutionException,
|
|
||||||
TimeoutException {
|
|
||||||
return s3Context.getApi().createContainer(bucketName).get(10, TimeUnit.SECONDS);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -23,17 +23,13 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.aws.s3.samples;
|
package org.jclouds.aws.s3.samples;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
|
|
||||||
import org.jclouds.aws.s3.CreateListOwnedBuckets;
|
|
||||||
import org.jclouds.aws.s3.S3Context;
|
import org.jclouds.aws.s3.S3Context;
|
||||||
import org.jclouds.aws.s3.S3ContextFactory;
|
import org.jclouds.aws.s3.S3ContextFactory;
|
||||||
import org.jclouds.aws.s3.domain.BucketMetadata;
|
import org.jclouds.aws.s3.domain.BucketMetadata;
|
||||||
import org.jclouds.logging.Logger;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This the Main class of an Application that demonstrates the use of the CreateListOwnedBuckets
|
* This the Main class of an Application that demonstrates the use of the CreateListOwnedBuckets
|
||||||
|
@ -45,9 +41,6 @@ import org.jclouds.logging.Logger;
|
||||||
*/
|
*/
|
||||||
public class MainApp {
|
public class MainApp {
|
||||||
|
|
||||||
@Resource
|
|
||||||
protected static Logger logger = Logger.NULL;
|
|
||||||
|
|
||||||
public static int PARAMETERS = 3;
|
public static int PARAMETERS = 3;
|
||||||
public static String INVALID_SYNTAX = "Invalid number of parameters. Syntax is: \"accesskeyid\" \"secretekey\" \"bucketName\" ";
|
public static String INVALID_SYNTAX = "Invalid number of parameters. Syntax is: \"accesskeyid\" \"secretekey\" \"bucketName\" ";
|
||||||
|
|
||||||
|
@ -57,29 +50,21 @@ public class MainApp {
|
||||||
if (args.length < PARAMETERS)
|
if (args.length < PARAMETERS)
|
||||||
throw new IllegalArgumentException(INVALID_SYNTAX);
|
throw new IllegalArgumentException(INVALID_SYNTAX);
|
||||||
|
|
||||||
// Variables
|
|
||||||
S3Context context = null;
|
|
||||||
CreateListOwnedBuckets listMyOwnBuckets = null;
|
|
||||||
List<BucketMetadata> myBuckets = null;
|
|
||||||
|
|
||||||
// Args
|
// Args
|
||||||
String accesskeyid = args[0];
|
String accesskeyid = args[0];
|
||||||
String secretkey = args[1];
|
String secretkey = args[1];
|
||||||
String bucketName = args[2];
|
String bucketName = args[2];
|
||||||
|
|
||||||
// Init
|
// Init
|
||||||
context = S3ContextFactory.createS3Context(accesskeyid, secretkey);
|
S3Context context = S3ContextFactory.createContext(accesskeyid, secretkey);
|
||||||
listMyOwnBuckets = new CreateListOwnedBuckets(context);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// Create Bucket
|
// Create Bucket
|
||||||
listMyOwnBuckets.createBucket(bucketName);
|
context.getApi().createContainer(bucketName).get(10, TimeUnit.SECONDS);
|
||||||
|
|
||||||
// List bucket
|
// List bucket
|
||||||
myBuckets = listMyOwnBuckets.list();
|
for (BucketMetadata bucketObj : context.getApi().listContainers()) {
|
||||||
|
|
||||||
for (BucketMetadata bucketObj : myBuckets) {
|
|
||||||
System.out.println(String.format(" %1$s", bucketObj));
|
System.out.println(String.format(" %1$s", bucketObj));
|
||||||
System.out.println(String.format(": %1$s entries%n", context.createInputStreamMap(
|
System.out.println(String.format(": %1$s entries%n", context.createInputStreamMap(
|
||||||
bucketObj.getName()).size()));
|
bucketObj.getName()).size()));
|
||||||
|
@ -88,7 +73,6 @@ public class MainApp {
|
||||||
} finally {
|
} finally {
|
||||||
// Close connecton
|
// Close connecton
|
||||||
context.close();
|
context.close();
|
||||||
context = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,104 +0,0 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
|
|
||||||
*
|
|
||||||
* ====================================================================
|
|
||||||
* 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.aws.s3.samples.test;
|
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import org.jclouds.aws.s3.CreateListOwnedBuckets;
|
|
||||||
import org.jclouds.aws.s3.S3Context;
|
|
||||||
import org.jclouds.aws.s3.S3ContextBuilder;
|
|
||||||
import org.jclouds.aws.s3.S3ContextFactory;
|
|
||||||
import org.jclouds.aws.s3.config.StubS3BlobStoreModule;
|
|
||||||
import org.jclouds.aws.s3.reference.S3Constants;
|
|
||||||
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
|
|
||||||
import org.testng.annotations.AfterClass;
|
|
||||||
import org.testng.annotations.BeforeClass;
|
|
||||||
import org.testng.annotations.Optional;
|
|
||||||
import org.testng.annotations.Parameters;
|
|
||||||
import org.testng.annotations.Test;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Integration Unit test for simple CreateListOwnedBuckets.
|
|
||||||
*
|
|
||||||
* @author Carlos Fernandes
|
|
||||||
*/
|
|
||||||
@Test(testName = "s3.createListOwnedBucketsIntegrationTest")
|
|
||||||
public class CreateListOwnedBucketsIntegrationTest {
|
|
||||||
|
|
||||||
private S3Context context;
|
|
||||||
private final String sysAWSAccessKeyId = System
|
|
||||||
.getProperty(S3Constants.PROPERTY_AWS_ACCESSKEYID);
|
|
||||||
private final String sysAWSSecretAccessKey = System
|
|
||||||
.getProperty(S3Constants.PROPERTY_AWS_SECRETACCESSKEY);
|
|
||||||
private String bucketPrefix = (System.getProperty("user.name") + "." + this.getClass()
|
|
||||||
.getSimpleName()).toLowerCase();
|
|
||||||
|
|
||||||
@BeforeClass(inheritGroups = false, groups = { "integration", "live" })
|
|
||||||
@Parameters( { S3Constants.PROPERTY_AWS_ACCESSKEYID, S3Constants.PROPERTY_AWS_SECRETACCESSKEY })
|
|
||||||
public void setUpTest(@Optional String AWSAccessKeyId, @Optional String AWSSecretAccessKey) {
|
|
||||||
|
|
||||||
AWSAccessKeyId = AWSAccessKeyId != null ? AWSAccessKeyId : sysAWSAccessKeyId;
|
|
||||||
AWSSecretAccessKey = AWSSecretAccessKey != null ? AWSSecretAccessKey : sysAWSSecretAccessKey;
|
|
||||||
|
|
||||||
if ((AWSAccessKeyId != null) && (AWSSecretAccessKey != null))
|
|
||||||
context = S3ContextBuilder.newBuilder(AWSAccessKeyId, AWSSecretAccessKey).withSaxDebug()
|
|
||||||
.relaxSSLHostname().withModules(new Log4JLoggingModule()).buildContext();
|
|
||||||
else
|
|
||||||
context = S3ContextFactory.createS3Context("stub", "stub", new StubS3BlobStoreModule());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(groups = { "integration", "live" })
|
|
||||||
public void s3Test() throws Exception {
|
|
||||||
|
|
||||||
// Init
|
|
||||||
CreateListOwnedBuckets listMyOwnBuckets = new CreateListOwnedBuckets(context);
|
|
||||||
String bucketName = bucketPrefix + "needstoexist";
|
|
||||||
|
|
||||||
// Create Bucket
|
|
||||||
assert listMyOwnBuckets.createBucket(bucketName); // Creates a
|
|
||||||
// random bucket
|
|
||||||
// first to
|
|
||||||
// make sure list() will return
|
|
||||||
// something.
|
|
||||||
|
|
||||||
// List bucket
|
|
||||||
String string = listMyOwnBuckets.list().toString();
|
|
||||||
assert string.length() > 0; // This test will validate if the list() operation will return any
|
|
||||||
// string
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterClass
|
|
||||||
public void tearDownClient() throws Exception {
|
|
||||||
|
|
||||||
// Removes the bucket created for test purposes only
|
|
||||||
assert context.getApi().deleteContainer(bucketPrefix + "needstoexist").get(10,
|
|
||||||
TimeUnit.SECONDS);
|
|
||||||
|
|
||||||
context.close();
|
|
||||||
context = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,84 +0,0 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
|
|
||||||
*
|
|
||||||
* ====================================================================
|
|
||||||
* 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.aws.s3.samples.test;
|
|
||||||
|
|
||||||
import org.jclouds.aws.s3.CreateListOwnedBuckets;
|
|
||||||
import org.jclouds.aws.s3.S3Context;
|
|
||||||
import org.jclouds.aws.s3.S3ContextBuilder;
|
|
||||||
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
|
|
||||||
import org.testng.annotations.AfterClass;
|
|
||||||
import org.testng.annotations.BeforeClass;
|
|
||||||
import org.testng.annotations.Test;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Live Unit test for simple CreateListOwnedBuckets.
|
|
||||||
*
|
|
||||||
* @author Carlos Fernandes
|
|
||||||
*/
|
|
||||||
@Test(testName = "s3.createListOwnedBucketsLiveTest")
|
|
||||||
public class CreateListOwnedBucketsLiveTest {
|
|
||||||
|
|
||||||
private String bucketPrefix = (System.getProperty("user.name") + "." + this.getClass()
|
|
||||||
.getSimpleName()).toLowerCase();
|
|
||||||
private S3Context context;
|
|
||||||
|
|
||||||
@BeforeClass(inheritGroups = false, groups = { "live" })
|
|
||||||
public void setUpTest() {
|
|
||||||
String account = System.getProperty("jclouds.test.user");
|
|
||||||
String key = System.getProperty("jclouds.test.key");
|
|
||||||
|
|
||||||
context = S3ContextBuilder.newBuilder(account, key).withSaxDebug().relaxSSLHostname()
|
|
||||||
.withModules(new Log4JLoggingModule()).buildContext();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(groups = { "live" })
|
|
||||||
public void s3Test() throws Exception {
|
|
||||||
|
|
||||||
// Init
|
|
||||||
CreateListOwnedBuckets listMyOwnBuckets = new CreateListOwnedBuckets(context);
|
|
||||||
String bucketName = bucketPrefix + "needstoexist";
|
|
||||||
|
|
||||||
// Create Bucket
|
|
||||||
assert listMyOwnBuckets.createBucket(bucketName); // Creates a random bucket first to make
|
|
||||||
// sure list() will return something.
|
|
||||||
|
|
||||||
// List bucket
|
|
||||||
String string = listMyOwnBuckets.list().toString();
|
|
||||||
assert string.length() > 0; // This test will validate if the list() operation will return any
|
|
||||||
// string
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterClass
|
|
||||||
public void tearDownClient() throws Exception {
|
|
||||||
|
|
||||||
// Removes the bucket created for test purposes only
|
|
||||||
context.getApi().deleteContainer(bucketPrefix + "needstoexist");
|
|
||||||
|
|
||||||
context.close();
|
|
||||||
context = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue