You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by za...@apache.org on 2015/05/07 19:05:35 UTC
jclouds-examples git commit: Adds neutron examples and some cleanup
Repository: jclouds-examples
Updated Branches:
refs/heads/master 58761e38e -> d57451d9b
Adds neutron examples and some cleanup
Project: http://git-wip-us.apache.org/repos/asf/jclouds-examples/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds-examples/commit/d57451d9
Tree: http://git-wip-us.apache.org/repos/asf/jclouds-examples/tree/d57451d9
Diff: http://git-wip-us.apache.org/repos/asf/jclouds-examples/diff/d57451d9
Branch: refs/heads/master
Commit: d57451d9b24d05d7095c0057bd3afd544fe60fc0
Parents: 58761e3
Author: Zack Shoylev <za...@rackspace.com>
Authored: Tue May 5 15:21:53 2015 -0500
Committer: Zack Shoylev <za...@rackspace.com>
Committed: Thu May 7 12:05:19 2015 -0500
----------------------------------------------------------------------
rackspace/pom.xml | 10 ++
.../jclouds/examples/rackspace/SmokeTest.java | 7 +-
.../examples/rackspace/cdn/CreateService.java | 6 +-
.../rackspace/cdn/PurgeServiceAssets.java | 6 +-
.../examples/rackspace/cdn/UpdateService.java | 6 +-
.../rackspace/cloudnetworks/Constants.java | 29 +++++
.../rackspace/cloudnetworks/CreateNetwork.java | 87 ++++++++++++++
.../rackspace/cloudnetworks/CreatePort.java | 118 +++++++++++++++++++
.../cloudnetworks/CreateSecurityGroup.java | 109 +++++++++++++++++
.../rackspace/cloudnetworks/CreateSubnet.java | 113 ++++++++++++++++++
10 files changed, 481 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/d57451d9/rackspace/pom.xml
----------------------------------------------------------------------
diff --git a/rackspace/pom.xml b/rackspace/pom.xml
index 995ffd6..4c37f9c 100644
--- a/rackspace/pom.xml
+++ b/rackspace/pom.xml
@@ -82,6 +82,11 @@
<artifactId>rackspace-cdn-us</artifactId>
<version>${jclouds.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.jclouds.labs</groupId>
+ <artifactId>rackspace-cloudnetworks-us</artifactId>
+ <version>${jclouds.version}</version>
+ </dependency>
<!-- Rackspace UK dependencies -->
<dependency>
<groupId>org.apache.jclouds.provider</groupId>
@@ -128,6 +133,11 @@
<artifactId>rackspace-cdn-uk</artifactId>
<version>${jclouds.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.jclouds.labs</groupId>
+ <artifactId>rackspace-cloudnetworks-uk</artifactId>
+ <version>${jclouds.version}</version>
+ </dependency>
<!-- 3rd party dependencies -->
<dependency>
<groupId>ch.qos.logback</groupId>
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/d57451d9/rackspace/src/main/java/org/jclouds/examples/rackspace/SmokeTest.java
----------------------------------------------------------------------
diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/SmokeTest.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/SmokeTest.java
index 94662ca..936aac3 100644
--- a/rackspace/src/main/java/org/jclouds/examples/rackspace/SmokeTest.java
+++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/SmokeTest.java
@@ -31,7 +31,7 @@ import org.jclouds.examples.rackspace.cloudservers.*;
import org.jclouds.examples.rackspace.clouddatabases.*;
import org.jclouds.examples.rackspace.autoscale.*;
import org.jclouds.examples.rackspace.cdn.*;
-import org.jclouds.openstack.poppy.v1.domain.*;
+import org.jclouds.examples.rackspace.cloudnetworks.*;
/**
* This example smoke tests all of the other examples in these packages.
@@ -126,5 +126,10 @@ public class SmokeTest {
UpdateService.main(args);
PurgeServiceAssets.main(args);
DeleteService.main(args);
+
+ CreateNetwork.main(args);
+ CreateSubnet.main(args);
+ CreatePort.main(args);
+ CreateSecurityGroup.main(args);
}
}
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/d57451d9/rackspace/src/main/java/org/jclouds/examples/rackspace/cdn/CreateService.java
----------------------------------------------------------------------
diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cdn/CreateService.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cdn/CreateService.java
index f8d4229..7da8a90 100644
--- a/rackspace/src/main/java/org/jclouds/examples/rackspace/cdn/CreateService.java
+++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cdn/CreateService.java
@@ -57,16 +57,16 @@ public class CreateService implements Closeable {
* The second argument (args[1]) must be your API key
*/
public static void main(String[] args) throws IOException {
- CreateService createPolicy = new CreateService(args[0], args[1]);
+ CreateService createService = new CreateService(args[0], args[1]);
try {
- createPolicy.createService();
+ createService.createService();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
- createPolicy.close();
+ createService.close();
}
}
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/d57451d9/rackspace/src/main/java/org/jclouds/examples/rackspace/cdn/PurgeServiceAssets.java
----------------------------------------------------------------------
diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cdn/PurgeServiceAssets.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cdn/PurgeServiceAssets.java
index c772116..c6e6c4d 100644
--- a/rackspace/src/main/java/org/jclouds/examples/rackspace/cdn/PurgeServiceAssets.java
+++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cdn/PurgeServiceAssets.java
@@ -46,16 +46,16 @@ public class PurgeServiceAssets implements Closeable {
* The second argument (args[1]) must be your API key
*/
public static void main(String[] args) throws IOException {
- PurgeServiceAssets createPolicy = new PurgeServiceAssets(args[0], args[1]);
+ PurgeServiceAssets purgeServiceAssets = new PurgeServiceAssets(args[0], args[1]);
try {
- createPolicy.updateService();
+ purgeServiceAssets.updateService();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
- createPolicy.close();
+ purgeServiceAssets.close();
}
}
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/d57451d9/rackspace/src/main/java/org/jclouds/examples/rackspace/cdn/UpdateService.java
----------------------------------------------------------------------
diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cdn/UpdateService.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cdn/UpdateService.java
index 7b0141d..7bd066d 100644
--- a/rackspace/src/main/java/org/jclouds/examples/rackspace/cdn/UpdateService.java
+++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cdn/UpdateService.java
@@ -49,16 +49,16 @@ public class UpdateService implements Closeable {
* The second argument (args[1]) must be your API key
*/
public static void main(String[] args) throws IOException {
- UpdateService createPolicy = new UpdateService(args[0], args[1]);
+ UpdateService updateService = new UpdateService(args[0], args[1]);
try {
- createPolicy.updateService();
+ updateService.updateService();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
- createPolicy.close();
+ updateService.close();
}
}
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/d57451d9/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/Constants.java
----------------------------------------------------------------------
diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/Constants.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/Constants.java
new file mode 100644
index 0000000..961f9c8
--- /dev/null
+++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/Constants.java
@@ -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.rackspace.cloudnetworks;
+
+/**
+ * Constants used by the Rackspace Examples.
+ */
+public class Constants {
+ // The provider configures jclouds to use the Rackspace Cloud (US).
+ // To use the Rackspace Cloud (UK) set the system property or default value to "rackspace-cloudnetworks-uk".
+ public static final String PROVIDER = System.getProperty("provider.cloudnetworks", "rackspace-cloudnetworks-us");
+ public static final String REGION = System.getProperty("region", "DFW");
+ public static final String NAME = "jclouds-example";
+}
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/d57451d9/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/CreateNetwork.java
----------------------------------------------------------------------
diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/CreateNetwork.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/CreateNetwork.java
new file mode 100644
index 0000000..d3d556d
--- /dev/null
+++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/CreateNetwork.java
@@ -0,0 +1,87 @@
+/*
+ * 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.rackspace.cloudnetworks;
+
+import static org.jclouds.examples.rackspace.cloudnetworks.Constants.REGION;
+
+import java.io.Closeable;
+import java.io.IOException;
+
+import org.jclouds.ContextBuilder;
+import org.jclouds.openstack.neutron.v2.NeutronApi;
+import org.jclouds.openstack.neutron.v2.domain.Network;
+import org.jclouds.openstack.neutron.v2.features.NetworkApi;
+
+import com.google.common.io.Closeables;
+
+/**
+ * Demonstrates how to create a Poppy service on Rackspace (Rackspace CDN).
+ * Cleans up the service on fail.
+ */
+public class CreateNetwork implements Closeable {
+ private final NeutronApi neutronApi;
+
+ /**
+ * To get a username and API key see
+ * http://jclouds.apache.org/guides/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) throws IOException {
+ CreateNetwork createNetwork = new CreateNetwork(args[0], args[1]);
+
+ try {
+ createNetwork.createNetwork();
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+ finally {
+ createNetwork.close();
+ }
+ }
+
+ public CreateNetwork(String username, String apiKey) {
+ neutronApi = ContextBuilder.newBuilder("rackspace-cloudnetworks-us")
+ .credentials(username, apiKey)
+ .buildApi(NeutronApi.class);
+ }
+
+ private void createNetwork() {
+ NetworkApi networkApi = neutronApi.getNetworkApi(REGION);
+ Network net = null;
+ try {
+ net = networkApi.create(Network.createBuilder("jclouds-test").build());
+ } finally {
+ // Cleanup
+ if (net != null) {
+ networkApi.delete(net.getId());
+ }
+ }
+ }
+
+ /**
+ * 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 void close() throws IOException {
+ Closeables.close(neutronApi, true);
+ }
+}
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/d57451d9/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/CreatePort.java
----------------------------------------------------------------------
diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/CreatePort.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/CreatePort.java
new file mode 100644
index 0000000..1762be3
--- /dev/null
+++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/CreatePort.java
@@ -0,0 +1,118 @@
+/*
+ * 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.rackspace.cloudnetworks;
+
+import static org.jclouds.examples.rackspace.cloudnetworks.Constants.PROVIDER;
+import static org.jclouds.examples.rackspace.cloudnetworks.Constants.REGION;
+
+import java.io.Closeable;
+import java.io.IOException;
+
+import org.jclouds.ContextBuilder;
+import org.jclouds.openstack.neutron.v2.NeutronApi;
+import org.jclouds.openstack.neutron.v2.domain.IP;
+import org.jclouds.openstack.neutron.v2.domain.Network;
+import org.jclouds.openstack.neutron.v2.domain.Port;
+import org.jclouds.openstack.neutron.v2.domain.Subnet;
+import org.jclouds.openstack.neutron.v2.features.NetworkApi;
+import org.jclouds.openstack.neutron.v2.features.PortApi;
+import org.jclouds.openstack.neutron.v2.features.SubnetApi;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.io.Closeables;
+
+/**
+ * Demonstrates how to create a Poppy service on Rackspace (Rackspace CDN).
+ * Cleans up the service on fail.
+ */
+public class CreatePort implements Closeable {
+ private final NeutronApi neutronApi;
+
+ /**
+ * To get a username and API key see
+ * http://jclouds.apache.org/guides/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) throws IOException {
+ CreatePort createPort = new CreatePort(args[0], args[1]);
+
+ try {
+ createPort.createPort();
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+ finally {
+ createPort.close();
+ }
+ }
+
+ public CreatePort(String username, String apiKey) {
+ neutronApi = ContextBuilder.newBuilder(PROVIDER)
+ .credentials(username, apiKey)
+ .buildApi(NeutronApi.class);
+ }
+
+ private void createPort() {
+ NetworkApi networkApi = neutronApi.getNetworkApi(REGION);
+ SubnetApi subnetApi = neutronApi.getSubnetApi(REGION);
+ PortApi portApi = neutronApi.getPortApi(REGION);
+
+ Network net = null;
+ Subnet subnet = null;
+ Port port = null;
+
+ try {
+ // Create a network first. The subnet will be created on the network.
+ net = networkApi.create(Network.createBuilder("jclouds-test").build());
+
+ subnet = subnetApi.create(
+ Subnet.createBuilder(net.getId(), "192.168.0.0/30").ipVersion(4)
+ .name("JClouds-Live-IPv4-Subnet").build()
+ );
+
+ port = portApi.create(
+ Port.createBuilder(net.getId()).name("JClouds-Live-IPv4-Port")
+ .fixedIps( ImmutableSet.of(IP.builder().subnetId(subnet.getId()).build() )).build()
+ );
+
+ } finally {
+ // Cleanup
+ if (port != null) {
+ portApi.delete(port.getId());
+ }
+ if (subnet != null) {
+ subnetApi.delete(subnet.getId());
+ }
+ if (net != null) {
+ networkApi.delete(net.getId());
+ }
+ }
+ }
+
+ /**
+ * 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 void close() throws IOException {
+ Closeables.close(neutronApi, true);
+ }
+}
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/d57451d9/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/CreateSecurityGroup.java
----------------------------------------------------------------------
diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/CreateSecurityGroup.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/CreateSecurityGroup.java
new file mode 100644
index 0000000..d3ba679
--- /dev/null
+++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/CreateSecurityGroup.java
@@ -0,0 +1,109 @@
+/*
+ * 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.rackspace.cloudnetworks;
+
+import static org.jclouds.examples.rackspace.cloudnetworks.Constants.PROVIDER;
+import static org.jclouds.examples.rackspace.cloudnetworks.Constants.REGION;
+
+import java.io.Closeable;
+import java.io.IOException;
+
+import org.jclouds.ContextBuilder;
+import org.jclouds.openstack.neutron.v2.NeutronApi;
+import org.jclouds.openstack.neutron.v2.domain.Rule;
+import org.jclouds.openstack.neutron.v2.domain.RuleDirection;
+import org.jclouds.openstack.neutron.v2.domain.RuleEthertype;
+import org.jclouds.openstack.neutron.v2.domain.RuleProtocol;
+import org.jclouds.openstack.neutron.v2.domain.SecurityGroup;
+
+import org.jclouds.openstack.neutron.v2.extensions.SecurityGroupApi;
+import com.google.common.io.Closeables;
+
+/**
+ * Demonstrates how to create a Poppy service on Rackspace (Rackspace CDN).
+ * Cleans up the service on fail.
+ */
+public class CreateSecurityGroup implements Closeable {
+ private final NeutronApi neutronApi;
+
+ /**
+ * To get a username and API key see
+ * http://jclouds.apache.org/guides/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) throws IOException {
+ CreateSecurityGroup createSecurityGroup = new CreateSecurityGroup(args[0], args[1]);
+
+ try {
+ createSecurityGroup.createSecurityGroup();
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+ finally {
+ createSecurityGroup.close();
+ }
+ }
+
+ public CreateSecurityGroup(String username, String apiKey) {
+ neutronApi = ContextBuilder.newBuilder(PROVIDER)
+ .credentials(username, apiKey)
+ .buildApi(NeutronApi.class);
+ }
+
+ private void createSecurityGroup() {
+ SecurityGroupApi sgApi = neutronApi.getSecurityGroupApi(REGION).get();
+ Rule rule = null;
+ SecurityGroup securityGroup = null;
+
+ try {
+ sgApi = neutronApi.getSecurityGroupApi(REGION).get();
+
+ securityGroup = sgApi.create(
+ SecurityGroup.createBuilder().name("jclouds-test").description("jclouds test security group")
+ .build());
+
+ rule = sgApi.create(
+ Rule.createBuilder(RuleDirection.INGRESS, securityGroup.getId())
+ .ethertype(RuleEthertype.IPV6)
+ .portRangeMax(90)
+ .portRangeMin(80)
+ .protocol(RuleProtocol.TCP)
+ .build());
+ } finally {
+ // Cleanup
+ if (rule != null) {
+ sgApi.deleteRule(rule.getId());
+ }
+ if (securityGroup != null) {
+ sgApi.deleteSecurityGroup(securityGroup.getId());
+ }
+ }
+ }
+
+ /**
+ * 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 void close() throws IOException {
+ Closeables.close(neutronApi, true);
+ }
+}
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/d57451d9/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/CreateSubnet.java
----------------------------------------------------------------------
diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/CreateSubnet.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/CreateSubnet.java
new file mode 100644
index 0000000..41779dd
--- /dev/null
+++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudnetworks/CreateSubnet.java
@@ -0,0 +1,113 @@
+/*
+ * 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.rackspace.cloudnetworks;
+
+import static org.jclouds.examples.rackspace.cloudnetworks.Constants.PROVIDER;
+import static org.jclouds.examples.rackspace.cloudnetworks.Constants.REGION;
+
+import java.io.Closeable;
+import java.io.IOException;
+
+import org.jclouds.ContextBuilder;
+import org.jclouds.openstack.neutron.v2.NeutronApi;
+import org.jclouds.openstack.neutron.v2.domain.AllocationPool;
+import org.jclouds.openstack.neutron.v2.domain.HostRoute;
+import org.jclouds.openstack.neutron.v2.domain.Network;
+import org.jclouds.openstack.neutron.v2.domain.Subnet;
+import org.jclouds.openstack.neutron.v2.features.NetworkApi;
+import org.jclouds.openstack.neutron.v2.features.SubnetApi;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.io.Closeables;
+
+/**
+ * Demonstrates how to create a Poppy service on Rackspace (Rackspace CDN).
+ * Cleans up the service on fail.
+ */
+public class CreateSubnet implements Closeable {
+ private final NeutronApi neutronApi;
+
+ /**
+ * To get a username and API key see
+ * http://jclouds.apache.org/guides/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) throws IOException {
+ CreateSubnet createSubnet = new CreateSubnet(args[0], args[1]);
+
+ try {
+ createSubnet.createSubnet();
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+ finally {
+ createSubnet.close();
+ }
+ }
+
+ public CreateSubnet(String username, String apiKey) {
+ neutronApi = ContextBuilder.newBuilder(PROVIDER)
+ .credentials(username, apiKey)
+ .buildApi(NeutronApi.class);
+ }
+
+ private void createSubnet() {
+ NetworkApi networkApi = neutronApi.getNetworkApi(REGION);
+ SubnetApi subnetApi = neutronApi.getSubnetApi(REGION);
+ Network net = null;
+ Subnet subnet = null;
+ try {
+ // Create a network first. The subnet will be created on the network.
+ net = networkApi.create(Network.createBuilder("jclouds-test").build());
+
+ // Assigns an allocation pool to the subnet
+ ImmutableSet<AllocationPool> allocationPools = ImmutableSet.of(
+ AllocationPool.builder().start("a3:bc00::10").end("a3:bc00::20").build(),
+ AllocationPool.builder().start("a3:bc00::50").end("a3:bc00::90").build()
+ );
+
+ // Takes care of routing
+ ImmutableSet<HostRoute> hostRoutes = ImmutableSet.of(
+ HostRoute.builder().destinationCidr("a3:bc00::/48").nextHop("a3:bc00::0004").build()
+ );
+
+ subnet = subnetApi.create(Subnet.createBuilder(net.getId(), "a3:bc00::/48").ipVersion(6).allocationPools(allocationPools).hostRoutes(hostRoutes).build());
+
+ } finally {
+ // Cleanup
+ if (subnet != null) {
+ subnetApi.delete(subnet.getId());
+ }
+ if (net != null) {
+ networkApi.delete(net.getId());
+ }
+ }
+ }
+
+ /**
+ * 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 void close() throws IOException {
+ Closeables.close(neutronApi, true);
+ }
+}