Skip to content
This repository has been archived by the owner on Feb 4, 2019. It is now read-only.

Add an example to create/delete/list Google Cloud Storage buckets. #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions google/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,10 @@
<artifactId>google-compute-engine</artifactId>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.labs</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.8.0</version>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use jclouds.version here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have not (intentionally) released google-cloud-storage yet. Please see jclouds/jclouds-labs-google#48 where this provider is under active development. We should close this pull request until we are satisfied with the existing APIs and release the code in either 2.0.0 or some future 1.8.x release.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I got it. Thanks for all the comments - I applied them and will keep them until google-cloud-storage is released.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I got it. Thanks for all the comments - I applied them and will keep them until google-cloud-storage is released.

Thanks for the updates, @najtmar!

</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.google.cloudstorage;

/**
* Constants used by the Google Cloud Storage Examples.
*/
public interface Constants {
String PROVIDER = System.getProperty("provider.cs", "google-cloud-storage");
String ZONE = System.getProperty("zone", "europe-west1-a");

String NAME = "jclouds-example";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this (the use of constants?) a pattern we're using elsewhere too..?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* 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.google.cloudstorage;

import com.google.common.io.Closeables;
import com.google.common.io.Files;

import org.jclouds.ContextBuilder;
import org.jclouds.googlecloudstorage.GoogleCloudStorageApi;
import org.jclouds.googlecloudstorage.domain.Bucket;
import org.jclouds.googlecloudstorage.domain.BucketTemplate;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

import static org.jclouds.examples.google.cloudstorage.Constants.PROVIDER;

/**
* This example manages buckets of Google Cloud Storage.
*/
public class ManageBuckets implements Closeable {
private final GoogleCloudStorageApi cloudStorageApi;

/**
* To get a service account and its private key see [TODO: write some
* documentation on the website and put a link to it]
*
* The first argument (args[0]) is your service account email address
* (https://developers.google.com/console/help/new/#serviceaccounts).
* The second argument (args[1]) is a path to your service account private key PEM file without a password. It is
* used for server-to-server interactions (https://developers.google.com/console/help/new/#serviceaccounts).
* The key is not transmitted anywhere.
* The third argument (args[2]) is the command you want to perform: "create", "delete", or "list".
* The fourth argument (args[3]) is the project name
* (see https://developers.google.com/storage/docs/signup#activate).
* This argument is skipped for command "delete".
* The fifth argument (args[4]) is the name of the bucket that you want to create or delete.
* This argument is skipped for command list.
* NOTE: Bucket names must be unique across the entire Google Cloud Storage namespace
* (https://developers.google.com/storage/docs/bucketnaming#requirements).
*
* Examples:
*
* java org.jclouds.examples.google.cloudstorage.ManageBuckets \
* [email protected] \
* /home/planetnik/Work/Cloud/OSS/certificate/gcp-oss.pem \
* create \
* myprojectname \
* planetnikbucketname
*
* java org.jclouds.examples.google.cloudstorage.ManageBuckets \
* [email protected] \
* /home/planetnik/Work/Cloud/OSS/certificate/gcp-oss.pem \
* delete \
* planetnikbucketname
*
* java org.jclouds.examples.google.cloudstorage.ManageBuckets \
* [email protected] \
* /home/planetnik/Work/Cloud/OSS/certificate/gcp-oss.pem \
* list \
* myprojectname
*/
public static void main(final String[] args) {
String serviceAccountEmailAddress = args[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about combining the arg parsing with the comment selection? Something like:

// parse args required for all commands and command
ManageBuckets manageBuckets = ...
if (command.equals(...)) {
  // parse additional args for this command
  manageBuckets...
} else if (command.equals(...)) {
  // parse options for this command
  ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, thanks!

String serviceAccountKey = null;
try {
serviceAccountKey = Files.toString(new File(args[1]), Charset.defaultCharset());
} catch (IOException e) {
System.err.println("Cannot open service account private key PEM file: " + args[1] + "\n" + e.getMessage());
System.exit(1);
}
ManageBuckets manageBuckets = new ManageBuckets(serviceAccountEmailAddress, serviceAccountKey);

String command = args[2];
String projectName = null;
String bucketName = null;
try {
if (command.equals("create")) {
projectName = args[3];
bucketName = args[4];
manageBuckets.createBucket(projectName, bucketName);
} else if (command.equals("delete")) {
bucketName = args[3];
if (args.length >= 5) {
System.err.println("Warning: Command 'delete' require only one additional parameter (bucketName).");
}
manageBuckets.deleteBucket(bucketName);
} else if (command.equals("list")) {
projectName = args[3];
if (args.length >= 5) {
System.err.println("Warning: Command 'list' require only one additional parameters (projectName).");
}
manageBuckets.listBuckets(projectName);
} else {
System.err.println("Unknown command: " + command);
System.exit(1);
}
} catch (RuntimeException e) {
System.err.format("Command %s failed: %s%n", command, e.getMessage());
System.exit(1);
}

try {
manageBuckets.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}

public ManageBuckets(final String serviceAccountEmailAddress, final String serviceAccountKey) {
cloudStorageApi = ContextBuilder.newBuilder(PROVIDER)
.credentials(serviceAccountEmailAddress, serviceAccountKey)
.buildApi(GoogleCloudStorageApi.class);
}

/**
* Creates a bucket in a given project.
* @param projectName Name of the project in which the bucket should be created.
* @param bucketName Name of the bucket to create.
*/
public final void createBucket(final String projectName, final String bucketName) {
Bucket bucket = cloudStorageApi.getBucketApi().createBucket(projectName, new BucketTemplate().name(bucketName));
if (bucket != null) {
System.out.print("Bucket " + bucket.getName() + " successfully created in project " + projectName + " .");
} else {
System.err.println("Creating bucket " + bucketName + "failed.");
System.exit(1);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove all the try...catch in the individual methods and have one central try/catch in the main method? See e.g. this example...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}

/**
* Deletes a bucket.
* @param bucketName Name of the bucket to delete.
*/
public final void deleteBucket(final String bucketName) {
cloudStorageApi.getBucketApi().deleteBucket(bucketName);
System.out.print("Bucket " + bucketName + " successfully deleted.");
}

/**
* Lists all buckets in a given project.
* @param projectName Name of the project in which the buckets should be listed.
*/
public final void listBuckets(final String projectName) {
System.out.println("List of buckets for project " + projectName + ":");
for (Bucket bucket : cloudStorageApi.getBucketApi().listBucket(projectName)) {
System.out.println("* " + bucket.getName());
}
}

/**
* Always close your service when you're done with it.
*
* Note that closing quietly like this is not necessary in Java 7.
* You would use try-with-resources in the main method instead.
*/
public final void close() throws IOException {
Closeables.close(cloudStorageApi, true);
}
}