Skip to content
Permalink
Browse files
Example of Dimension Data CloudControl provider
  • Loading branch information
trevorflanagan authored and nacx committed Oct 11, 2018
1 parent 934cdb0 commit a145a8fd2eaffc20610d5b24396307e7d22156cf
Showing 10 changed files with 957 additions and 1 deletion.
@@ -34,4 +34,3 @@
<jclouds.version>2.1.0</jclouds.version>
</properties>
</project>

@@ -41,6 +41,11 @@
<artifactId>jclouds-allcompute</artifactId>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.labs</groupId>
<artifactId>dimensiondata-cloudcontrol</artifactId>
<version>2.2.0-SNAPSHOT</version>
</dependency>
<!-- note that if you want a smaller distribution
remove the above dependency and place something
like below -->
@@ -0,0 +1,57 @@
# Dimension Data Examples
Example code that uses jclouds to perform common tasks on an Dimension Data CloudControl. 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 Dimension Data CloudControl - See the [Getting Started guide](http://jclouds.apache.org/guides/dimensiondata/).
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/dimensiondata/
```

To package the examples jar file and dependencies run:

```
mvn package
```

## Examples

To run individual examples from the command line use these commands:

Every example class has a main method that takes the following arguments in the listed order:

1. API Endpoint
1. Username
1. Password

If there are other arguments required they will follow. The command line format looks like this:
```
java -cp target\dimensiondata-cloudcontrol-examples-<VERSION>-jar-with-dependencies.jar <MAIN_CLASS> apiEndpoint username password <PARAMETERS>
```

Try out an example.

```
java -cp target\dimensiondata-cloudcontrol-examples-<VERSION>-jar-with-dependencies.jar org.jclouds.examples.dimensiondata.cloudcontrol.DeployNetworkDomainVlanAndServer apiEndpoint username password
```

Watch the terminal for output!

## Support and Feedback

Your feedback is appreciated! If you have specific issues with Dimension Data CloudControl 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.
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.jclouds.examples</groupId>
<artifactId>jclouds-examples</artifactId>
<version>2.2.0-SNAPSHOT</version>
</parent>

<artifactId>dimensiondata-cloudcontrol-examples</artifactId>
<name>dimensiondata-cloudcontrol-examples</name>
<version>2.2.0-SNAPSHOT</version>

<properties>
<jclouds.version>2.2.0-SNAPSHOT</jclouds.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.jclouds.labs</groupId>
<artifactId>dimensiondata-cloudcontrol</artifactId>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds</groupId>
<artifactId>jclouds-compute</artifactId>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.driver</groupId>
<artifactId>jclouds-slf4j</artifactId>
<version>${jclouds.version}</version>
</dependency>
<!-- 3rd party dependencies -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.driver</groupId>
<artifactId>jclouds-slf4j</artifactId>
<version>${jclouds.version}</version>
<scope>compile</scope>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,207 @@
/*
* 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.dimensiondata.cloudcontrol;

import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Injector;
import org.jclouds.ContextBuilder;
import org.jclouds.dimensiondata.cloudcontrol.DimensionDataCloudControlApi;
import org.jclouds.dimensiondata.cloudcontrol.domain.Server;
import org.jclouds.dimensiondata.cloudcontrol.domain.TagKey;
import org.jclouds.dimensiondata.cloudcontrol.domain.Vlan;
import org.jclouds.dimensiondata.cloudcontrol.options.DatacenterIdListFilters;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.rest.ApiContext;

import static org.jclouds.examples.dimensiondata.cloudcontrol.WaitForUtils.*;

/**
* This class will attempt to delete the assets created in org.jclouds.examples.dimensiondata.cloudcontrol.DeployNetworkDomainVlanAndServer:
* <ul>
* <li>Server</li>
* <li>Vlan</li>
* <li>Network Domain</li>
* <li>Tag Key</li>
* </ul>
*/
public class DeleteServerVlanAndNetworkDomain
{
private static final String ZONE = System.getProperty("jclouds.zone", "AU9");
private static final String DIMENSIONDATA_CLOUDCONTROL_PROVIDER = "dimensiondata-cloudcontrol";

public static void main(String[] args)
{
/*
* Build an instance of the Dimension DataCloud Control Provider using the endpoint provided.
* Typically the endpoint will be of the form https://api-GEO.dimensiondata.com/caas
* We also need to provide authenticate details, a username and password.
*
* Internally the Dimension Data CloudControl Provider will use the org.jclouds.dimensiondata.cloudcontrol.features.AccountApi
* to lookup the organization identifier so that it is used as part of the requests.
*
*/
String endpoint = args[0];
String username = args[1];
String password = args[2];

try (ApiContext<DimensionDataCloudControlApi> ctx = ContextBuilder.newBuilder(DIMENSIONDATA_CLOUDCONTROL_PROVIDER)
.endpoint(endpoint)
.credentials(username, password)
.modules(ImmutableSet.of(new SLF4JLoggingModule()))
.build())
{
/*
* Retrieve the Guice injector from the context.
* We will use this for retrieving the some Predicates that are used by the following operations.
*/
Injector injector = ctx.utils().injector();
DimensionDataCloudControlApi api = ctx.getApi();

/*
* Referencing the asset created in org.jclouds.examples.dimensiondata.cloudcontrol.DeployNetworkDomainVlanAndServer
*/
final String networkDomainName = "jclouds-example";
String networkDomainId = getNetworkDomainId(api, networkDomainName);
final String serverName = "jclouds-server";
final String vlanName = "jclouds-example-vlan";

deleteServer(api, injector, serverName);
deleteVlan(api, injector, vlanName, networkDomainId);
deleteNetworkDomain(api, injector, networkDomainId);
deleteTagKey(api, "jclouds");
}
}

private static void deleteTagKey(DimensionDataCloudControlApi api, final String tagkeyName)
{
/*
* Find the Tag Key and Delete using the id.
*/
Optional<TagKey> tagKeyOptional = api.getTagApi().listTagKeys().concat().firstMatch(new Predicate<TagKey>()
{
@Override
public boolean apply(TagKey input)
{
return input.name().equals(tagkeyName);
}
});
if (tagKeyOptional.isPresent())
{
api.getTagApi().deleteTagKey(tagKeyOptional.get().id());
}
}

private static String getNetworkDomainId(DimensionDataCloudControlApi api, final String networkDomainName)
{
/*
* Find the Network Domain that was deployed by doing a filtered lookup using the datacenter and the network domain name.
*/
return api.getNetworkApi().listNetworkDomainsWithDatacenterIdAndName(ZONE, networkDomainName).concat().toList().get(0).id();
}

private static void deleteVlan(DimensionDataCloudControlApi api, Injector injector, final String vlanName, String networkDomainId)
{
/*
* Find the Vlan that was deployed by listing all Vlans for the Network Domain and filtering by name
*/
Optional<Vlan> vlanOptional = api.getNetworkApi().listVlans(networkDomainId).concat().firstMatch(new Predicate<Vlan>()
{
@Override
public boolean apply(Vlan input)
{
return input.name().equals(vlanName);
}
});
if (vlanOptional.isPresent())
{
Vlan vlan = vlanOptional.get();

/*
* Delete the Vlan using the id.
*/
api.getNetworkApi().deleteVlan(vlan.id());

/*
* A Vlan delete is an asynchronous process. We need to wait for it to complete. The Dimension Data provider
* has built in predicates that will block execution and check that the Vlan is not found.
*/
waitForDeleteVlan(injector, vlan);
}
}

private static void deleteNetworkDomain(DimensionDataCloudControlApi api, Injector injector, String networkDomainId)
{
/*
* Network Domain is deleted using the id.
*/
api.getNetworkApi().deleteNetworkDomain(networkDomainId);

/*
* A Network Domain delete is an asynchronous process. We need to wait for it to complete. The Dimension Data provider
* has built in predicates that will block execution and check that the Network Domain is not found.
*/
waitForDeleteNetworkDomain(injector, networkDomainId);
}

private static void deleteServer(DimensionDataCloudControlApi api, Injector injector, final String serverName)
{
/*
* We list all servers known to this organisation for the datacenter we are operating on. We filter the one that matches the server name we used to create it.
*/
Optional<Server> serverOptional = api.getServerApi().listServers(DatacenterIdListFilters.Builder.datacenterId(ZONE)).firstMatch(new Predicate<Server>()
{
@Override
public boolean apply(Server input)
{
return input.name().equals(serverName);
}
});

if (serverOptional.isPresent())
{
Server server = serverOptional.get();
if (server.started())
{
/*
* A Server must not be started in order to delete it. We call the shutdown server operation.
*/
api.getServerApi().shutdownServer(server.id());

/*
* A Shutdown Server operation is an asynchronous process. We need to wait for it to complete. The Dimension Data provider
* has built in predicates that will block execution and check that the Server is shutdown.
*/
waitForServerStopped(injector, server);
}

/*
* Server is deleted using the id.
*/
api.getServerApi().deleteServer(server.id());

/*
* A Server delete is an asynchronous process. We need to wait for it to complete. The Dimension Data provider
* has built in predicates that will block execution and check that the Server is not found.
*/
waitForServerDeleted(injector, server);

}
}

}

0 comments on commit a145a8f

Please sign in to comment.