Skip to content
Permalink
Browse files
Expose wait for predicates on relevant API
  • Loading branch information
Boris Trishkin authored and nacx committed Jan 14, 2019
1 parent 748058c commit 12af7c1badf3c54bb51968267367d6ef67560d5f
Showing 5 changed files with 39 additions and 167 deletions.
@@ -25,7 +25,7 @@
<parent>
<groupId>org.apache.jclouds.examples</groupId>
<artifactId>jclouds-examples</artifactId>
<version>2.2.0-SNAPSHOT</version>
<version>2.1.0</version>
</parent>

<artifactId>dimensiondata-cloudcontrol-examples</artifactId>
@@ -19,7 +19,6 @@
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;
@@ -29,8 +28,6 @@
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>
@@ -66,11 +63,6 @@ public static void main(String[] args)
.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();

/*
@@ -81,9 +73,9 @@ public static void main(String[] args)
final String serverName = "jclouds-server";
final String vlanName = "jclouds-example-vlan";

deleteServer(api, injector, serverName);
deleteVlan(api, injector, vlanName, networkDomainId);
deleteNetworkDomain(api, injector, networkDomainId);
deleteServer(api, serverName);
deleteVlan(api, vlanName, networkDomainId);
deleteNetworkDomain(api, networkDomainId);
deleteTagKey(api, "jclouds");
}
}
@@ -115,7 +107,7 @@ private static String getNetworkDomainId(DimensionDataCloudControlApi api, final
return api.getNetworkApi().listNetworkDomainsWithDatacenterIdAndName(ZONE, networkDomainName).concat().toList().get(0).id();
}

private static void deleteVlan(DimensionDataCloudControlApi api, Injector injector, final String vlanName, String networkDomainId)
private static void deleteVlan(DimensionDataCloudControlApi api, final String vlanName, String networkDomainId)
{
/*
* Find the Vlan that was deployed by listing all Vlans for the Network Domain and filtering by name
@@ -141,11 +133,11 @@ public boolean apply(Vlan input)
* 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);
api.getNetworkApi().vlanDeletedPredicate().apply(vlan.id());
}
}

private static void deleteNetworkDomain(DimensionDataCloudControlApi api, Injector injector, String networkDomainId)
private static void deleteNetworkDomain(DimensionDataCloudControlApi api, String networkDomainId)
{
/*
* Network Domain is deleted using the id.
@@ -156,10 +148,10 @@ private static void deleteNetworkDomain(DimensionDataCloudControlApi api, Inject
* 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);
api.getNetworkApi().networkDomainDeletedPredicate().apply(networkDomainId);
}

private static void deleteServer(DimensionDataCloudControlApi api, Injector injector, final String serverName)
private static void deleteServer(DimensionDataCloudControlApi api, 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.
@@ -187,7 +179,7 @@ public boolean apply(Server input)
* 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);
api.getServerApi().serverStoppedPredicate().apply(server.id());
}

/*
@@ -199,7 +191,7 @@ public boolean apply(Server input)
* 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);
api.getServerApi().serverDeletedPredicate().apply(server.id());

}
}
@@ -19,7 +19,6 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.inject.Injector;
import org.jclouds.ContextBuilder;
import org.jclouds.dimensiondata.cloudcontrol.DimensionDataCloudControlApi;
import org.jclouds.dimensiondata.cloudcontrol.domain.Disk;
@@ -33,8 +32,6 @@
import java.util.Collections;
import java.util.List;

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

/**
* This class will attempt to Deploy:
* <ul>
@@ -71,28 +68,22 @@ public static void main(String[] args)
.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();

/*
* Create a tag key. We will use this to tag the assets that we create.
*/
String tagKeyId = api.getTagApi().createTagKey("jclouds", "owner of the asset", true, false);

String networkDomainId = deployNetworkDomain(api, injector, tagKeyId);
String vlanId = deployVlan(api, injector, networkDomainId, tagKeyId);
String networkDomainId = deployNetworkDomain(api, tagKeyId);
String vlanId = deployVlan(api, networkDomainId, tagKeyId);

deployServer(api, injector, networkDomainId, vlanId, tagKeyId);
deployServer(api, networkDomainId, vlanId, tagKeyId);
}

}

private static void deployServer(DimensionDataCloudControlApi api, Injector injector, String
networkDomainId, String vlanId, String tagKeyId)
private static void deployServer(DimensionDataCloudControlApi api, String networkDomainId, String vlanId, String tagKeyId)
{
/*
* The server we deploy will use a pre-configured image.
@@ -123,7 +114,8 @@ private static void deployServer(DimensionDataCloudControlApi api, Injector inje
* A Server deployment 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's State has moved from PENDING_ADD to NORMAL.
*/
waitForServerStartedAndNormal(injector, serverId);
api.getServerApi().serverStartedPredicate().apply(serverId);
api.getServerApi().serverNormalPredicate().apply(serverId);

/*
* Apply a Tag to the Server. We use AssetType SERVER.
@@ -140,7 +132,7 @@ private static String getOsImage(DimensionDataCloudControlApi api)
return api.getServerImageApi().listOsImages(DatacenterIdListFilters.Builder.datacenterId(ZONE)).iterator().next().id();
}

private static String deployNetworkDomain(DimensionDataCloudControlApi api, Injector injector, String tagKeyId)
private static String deployNetworkDomain(DimensionDataCloudControlApi api, String tagKeyId)
{

/*
@@ -153,7 +145,7 @@ private static String deployNetworkDomain(DimensionDataCloudControlApi api, Inje
* has built in predicates that will block execution and check that the Network Domain's State has moved from PENDING_ADD to NORMAL.
* We pass the Network Domain Identifier we wish to check the state of.
*/
waitForNetworkDomainNormal(injector, networkDomainId);
api.getNetworkApi().networkDomainNormalPredicate().apply(networkDomainId);

/*
* Apply a Tag to the Network Domain. We use AssetType NETWORK_DOMAIN.
@@ -163,8 +155,7 @@ private static String deployNetworkDomain(DimensionDataCloudControlApi api, Inje
return networkDomainId;
}

private static String deployVlan(DimensionDataCloudControlApi api, Injector injector, String
networkDomainId, String tagKeyId)
private static String deployVlan(DimensionDataCloudControlApi api, String networkDomainId, String tagKeyId)
{

/*
@@ -177,7 +168,7 @@ private static String deployVlan(DimensionDataCloudControlApi api, Injector inje
* A Vlan deployment 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's State has moved from PENDING_ADD to NORMAL.
*/
waitForVlanNormal(injector, vlanId);
api.getNetworkApi().vlanNormalPredicate().apply(vlanId);

/*
* Apply a Tag to the Vlan. We use AssetType VLAN.
@@ -18,17 +18,20 @@

import com.google.common.collect.ImmutableList;
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.*;
import org.jclouds.dimensiondata.cloudcontrol.domain.FirewallRule;
import org.jclouds.dimensiondata.cloudcontrol.domain.NatRule;
import org.jclouds.dimensiondata.cloudcontrol.domain.NetworkDomain;
import org.jclouds.dimensiondata.cloudcontrol.domain.PublicIpBlock;
import org.jclouds.dimensiondata.cloudcontrol.domain.Server;
import org.jclouds.dimensiondata.cloudcontrol.domain.State;
import org.jclouds.dimensiondata.cloudcontrol.domain.Vlan;
import org.jclouds.dimensiondata.cloudcontrol.options.DatacenterIdListFilters;
import org.jclouds.logging.Logger;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.rest.ApiContext;

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

/**
* This example shows how a Network Domain and all of it's associated assets are removed.
* Takes 4 Program Arguments:
@@ -58,14 +61,8 @@ public static void main(String[] args)
.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();


logger.info("Deleting resources for network domain %s", networkDomainId);
NetworkDomain networkDomain = api.getNetworkApi().getNetworkDomain(networkDomainId);
if (networkDomain == null)
@@ -87,7 +84,7 @@ public static void main(String[] args)

deleteFirewallRules(networkDomainId, api);

deleteServers(api, injector, datacenterId);
deleteServers(api, datacenterId);

ImmutableList<Server> servers = api.getServerApi().listServers().concat().toList();
if (!servers.isEmpty())
@@ -99,9 +96,9 @@ public static void main(String[] args)
}
return;
}
deleteVlans(api, injector, networkDomain);
deleteVlans(api, networkDomain);

deleteNetworkDomain(networkDomainId, api, injector);
deleteNetworkDomain(networkDomainId, api);
}
}

@@ -135,14 +132,14 @@ private static void deleteNatRules(String networkDomainId, DimensionDataCloudCon
}
}

private static void deleteNetworkDomain(String networkDomainId, DimensionDataCloudControlApi api, Injector injector)
private static void deleteNetworkDomain(String networkDomainId, DimensionDataCloudControlApi api)
{
logger.info("Deleting Network Domain with Id %s", networkDomainId);
api.getNetworkApi().deleteNetworkDomain(networkDomainId);
waitForDeleteNetworkDomain(injector, networkDomainId);
api.getNetworkApi().networkDomainDeletedPredicate().apply(networkDomainId);
}

private static void deleteVlans(DimensionDataCloudControlApi api, Injector injector, NetworkDomain networkDomain)
private static void deleteVlans(DimensionDataCloudControlApi api, NetworkDomain networkDomain)
{
for (Vlan vlan : api.getNetworkApi().listVlans(networkDomain.id()).concat().toList())
{
@@ -155,7 +152,7 @@ private static void deleteVlans(DimensionDataCloudControlApi api, Injector injec
}
logger.info("Deleting Vlan with Id %s", vlan.id());
api.getNetworkApi().deleteVlan(vlan.id());
waitForDeleteVlan(injector, vlan);
api.getNetworkApi().vlanDeletedPredicate().apply(vlan.id());
}
catch (Exception e)
{
@@ -164,7 +161,7 @@ private static void deleteVlans(DimensionDataCloudControlApi api, Injector injec
}
}

private static void deleteServers(DimensionDataCloudControlApi api, Injector injector, String datacenterId)
private static void deleteServers(DimensionDataCloudControlApi api, String datacenterId)
{
for (Server server : api.getServerApi().listServers(DatacenterIdListFilters.Builder.datacenterId(datacenterId)))
{
@@ -174,7 +171,7 @@ private static void deleteServers(DimensionDataCloudControlApi api, Injector inj
{
logger.info("Server with Id %s is in a FAILED_ADD state, running the clean server operation.", server.id());
api.getServerApi().cleanServer(server.id());
waitForServerDeleted(injector, server);
api.getServerApi().serverDeletedPredicate().apply(server.id());
if (api.getServerApi().getServer(server.id()) != null)
{
logger.info("Failed to clean Server with Id %s", server.id());
@@ -190,11 +187,11 @@ private static void deleteServers(DimensionDataCloudControlApi api, Injector inj
{
logger.info("Shutting down Server with Id %s", server.id());
api.getServerApi().shutdownServer(server.id());
waitForServerStopped(injector, server);
api.getServerApi().serverStoppedPredicate().apply(server.id());
}
logger.info("Deleting Server with Id %s", server.id());
api.getServerApi().deleteServer(server.id());
waitForServerDeleted(injector, server);
api.getServerApi().serverDeletedPredicate().apply(server.id());
}
catch (Exception e)
{

0 comments on commit 12af7c1

Please sign in to comment.