You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by na...@apache.org on 2019/03/31 17:58:39 UTC

[jclouds-examples] branch master updated: JCLOUDS-1492 Dimension Data Feature API Predicates are not usable (#2)

This is an automated email from the ASF dual-hosted git repository.

nacx pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jclouds-examples.git


The following commit(s) were added to refs/heads/master by this push:
     new f6ee52d  JCLOUDS-1492 Dimension Data Feature API Predicates are not usable (#2)
f6ee52d is described below

commit f6ee52da4ad69bc783214ceeb5e289dc7371eba1
Author: Trevor Flanagan <tr...@users.noreply.github.com>
AuthorDate: Sun Mar 31 18:58:32 2019 +0100

    JCLOUDS-1492 Dimension Data Feature API Predicates are not usable (#2)
    
    * JCLOUDS-1492 Dimension Data Feature API Predicates are not usable
    
    * Fixing issue where during the clear down all servers in a Datacenter get deleted instead of just the ones in the Network Domain.
---
 .../DeleteServerVlanAndNetworkDomain.java          |  8 +-
 .../DeployNetworkDomainVlanAndServer.java          |  8 +-
 .../cloudcontrol/NetworkDomainTearDown.java        | 85 ++++++++++++++--------
 3 files changed, 64 insertions(+), 37 deletions(-)

diff --git a/dimensiondata/src/main/java/org/jclouds/examples/dimensiondata/cloudcontrol/DeleteServerVlanAndNetworkDomain.java b/dimensiondata/src/main/java/org/jclouds/examples/dimensiondata/cloudcontrol/DeleteServerVlanAndNetworkDomain.java
index 6035c54..18c4efd 100644
--- a/dimensiondata/src/main/java/org/jclouds/examples/dimensiondata/cloudcontrol/DeleteServerVlanAndNetworkDomain.java
+++ b/dimensiondata/src/main/java/org/jclouds/examples/dimensiondata/cloudcontrol/DeleteServerVlanAndNetworkDomain.java
@@ -133,7 +133,7 @@ public class DeleteServerVlanAndNetworkDomain
              * 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.
              */
-           api.getNetworkApi().vlanDeletedPredicate().apply(vlan.id());
+           api.vlanDeletedPredicate().apply(vlan.id());
         }
     }
 
@@ -148,7 +148,7 @@ public class DeleteServerVlanAndNetworkDomain
          * 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.
          */
-       api.getNetworkApi().networkDomainDeletedPredicate().apply(networkDomainId);
+       api.networkDomainDeletedPredicate().apply(networkDomainId);
     }
 
     private static void deleteServer(DimensionDataCloudControlApi api, final String serverName)
@@ -179,7 +179,7 @@ public class DeleteServerVlanAndNetworkDomain
                  * 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.
                  */
-               api.getServerApi().serverStoppedPredicate().apply(server.id());
+               api.serverStoppedPredicate().apply(server.id());
             }
 
             /*
@@ -191,7 +191,7 @@ public class DeleteServerVlanAndNetworkDomain
              * 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.
              */
-           api.getServerApi().serverDeletedPredicate().apply(server.id());
+           api.serverDeletedPredicate().apply(server.id());
 
         }
     }
diff --git a/dimensiondata/src/main/java/org/jclouds/examples/dimensiondata/cloudcontrol/DeployNetworkDomainVlanAndServer.java b/dimensiondata/src/main/java/org/jclouds/examples/dimensiondata/cloudcontrol/DeployNetworkDomainVlanAndServer.java
index 9eace32..5b2a5bc 100644
--- a/dimensiondata/src/main/java/org/jclouds/examples/dimensiondata/cloudcontrol/DeployNetworkDomainVlanAndServer.java
+++ b/dimensiondata/src/main/java/org/jclouds/examples/dimensiondata/cloudcontrol/DeployNetworkDomainVlanAndServer.java
@@ -114,8 +114,8 @@ public class DeployNetworkDomainVlanAndServer
          * 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.
          */
-       api.getServerApi().serverStartedPredicate().apply(serverId);
-       api.getServerApi().serverNormalPredicate().apply(serverId);
+       api.serverStartedPredicate().apply(serverId);
+       api.serverNormalPredicate().apply(serverId);
 
         /*
          * Apply a Tag to the Server. We use AssetType SERVER.
@@ -145,7 +145,7 @@ public class DeployNetworkDomainVlanAndServer
          * 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.
          */
-       api.getNetworkApi().networkDomainNormalPredicate().apply(networkDomainId);
+       api.networkDomainNormalPredicate().apply(networkDomainId);
 
         /*
          * Apply a Tag to the Network Domain. We use AssetType NETWORK_DOMAIN.
@@ -168,7 +168,7 @@ public class DeployNetworkDomainVlanAndServer
          * 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.
          */
-       api.getNetworkApi().vlanNormalPredicate().apply(vlanId);
+       api.vlanNormalPredicate().apply(vlanId);
 
         /*
          * Apply a Tag to the Vlan. We use AssetType VLAN.
diff --git a/dimensiondata/src/main/java/org/jclouds/examples/dimensiondata/cloudcontrol/NetworkDomainTearDown.java b/dimensiondata/src/main/java/org/jclouds/examples/dimensiondata/cloudcontrol/NetworkDomainTearDown.java
index 1938dc5..a5eadbe 100644
--- a/dimensiondata/src/main/java/org/jclouds/examples/dimensiondata/cloudcontrol/NetworkDomainTearDown.java
+++ b/dimensiondata/src/main/java/org/jclouds/examples/dimensiondata/cloudcontrol/NetworkDomainTearDown.java
@@ -16,6 +16,8 @@
  */
 package org.jclouds.examples.dimensiondata.cloudcontrol;
 
+import com.google.common.base.Predicate;
+import com.google.common.collect.FluentIterable;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import org.jclouds.ContextBuilder;
@@ -52,8 +54,9 @@ public class NetworkDomainTearDown
         String endpoint = args[0];
         String username = args[1];
         String password = args[2];
-        String networkDomainId = args[3];
+        String networkDomainIdArg = args[3];
 
+        boolean deleteAllNetworkDomains = networkDomainIdArg.equalsIgnoreCase("ALL");
         try (ApiContext<DimensionDataCloudControlApi> ctx = ContextBuilder.newBuilder(provider)
                 .endpoint(endpoint)
                 .credentials(username, password)
@@ -63,43 +66,67 @@ public class NetworkDomainTearDown
 
             DimensionDataCloudControlApi api = ctx.getApi();
 
-            logger.info("Deleting resources for network domain %s", networkDomainId);
-            NetworkDomain networkDomain = api.getNetworkApi().getNetworkDomain(networkDomainId);
-            if (networkDomain == null)
-            {
-                logger.info("Network Domain with Id %s is not found", networkDomainId);
-                return;
+            if(deleteAllNetworkDomains){
+                  ImmutableList<NetworkDomain> networkDomains = api.getNetworkApi().listNetworkDomains().concat().toList();
+
+                  for (NetworkDomain nd : networkDomains)
+                  {
+                      deleteAssetsAndNetworkDomain(api, nd);
+                  }
             }
-            if (networkDomain.state() != State.NORMAL)
-            {
-                logger.info("Network Domain with Id %s is not in a NORMAL state, cannot delete", networkDomain.id());
-                return;
+            else {
+                  NetworkDomain networkDomain = api.getNetworkApi().getNetworkDomain(networkDomainIdArg);
+                  if (networkDomain == null)
+                  {
+                      logger.info("Network Domain with Id %s is not found", networkDomainIdArg);
+                  }
+                  else {
+                      deleteAssetsAndNetworkDomain(api, networkDomain);
+                  }
             }
+        }
 
-            String datacenterId = networkDomain.datacenterId();
+    }
+
+    private static void deleteAssetsAndNetworkDomain(DimensionDataCloudControlApi api, NetworkDomain networkDomain) {
+        final String networkDomainId = networkDomain.id();
+        logger.info("Deleting resources for network domain %s", networkDomainId);
+
+        if (networkDomain.state() != State.NORMAL)
+        {
+            logger.info("Network Domain with Id %s is not in a NORMAL state, cannot delete", networkDomain.id());
+            return;
+        }
 
-            removePublicIpBlocks(networkDomainId, api);
+        String datacenterId = networkDomain.datacenterId();
 
-            deleteNatRules(networkDomainId, api);
+        removePublicIpBlocks(networkDomainId, api);
 
-            deleteFirewallRules(networkDomainId, api);
+        deleteNatRules(networkDomainId, api);
 
-            deleteServers(api, datacenterId, networkDomainId);
+        deleteFirewallRules(networkDomainId, api);
 
-            ImmutableList<Server> servers = api.getServerApi().listServers().concat().toList();
-            if (!servers.isEmpty())
+        deleteServers(api, datacenterId, networkDomainId);
+
+        FluentIterable<Server> serversForNetworkDomain = api.getServerApi().listServers().concat().filter(new Predicate<Server>()
+        {
+            @Override
+            public boolean apply(Server input)
             {
-                logger.info("Could not delete all Servers. Servers not deleted:");
-                for (Server server : servers)
-                {
-                    logger.info("Id %s, Name %s, State, %s", server.id(), server.name(), server.state());
-                }
-                return;
+                return input.networkInfo().networkDomainId().equals(networkDomainId);
             }
+        });
+
+        if (serversForNetworkDomain.isEmpty())
+        {
             deleteVlans(api, networkDomain);
 
             deleteNetworkDomain(networkDomainId, api);
         }
+        else
+        {
+            logger.info("Servers existing - %s - for Network Domain, cannot delete", serversForNetworkDomain.toList());
+        }
     }
 
     private static void removePublicIpBlocks(String networkDomainId, DimensionDataCloudControlApi api)
@@ -136,7 +163,7 @@ public class NetworkDomainTearDown
     {
         logger.info("Deleting Network Domain with Id %s", networkDomainId);
         api.getNetworkApi().deleteNetworkDomain(networkDomainId);
-        api.getNetworkApi().networkDomainDeletedPredicate().apply(networkDomainId);
+        api.networkDomainDeletedPredicate().apply(networkDomainId);
     }
 
     private static void deleteVlans(DimensionDataCloudControlApi api, NetworkDomain networkDomain)
@@ -152,7 +179,7 @@ public class NetworkDomainTearDown
                 }
                 logger.info("Deleting Vlan with Id %s", vlan.id());
                 api.getNetworkApi().deleteVlan(vlan.id());
-                api.getNetworkApi().vlanDeletedPredicate().apply(vlan.id());
+                api.vlanDeletedPredicate().apply(vlan.id());
             }
             catch (Exception e)
             {
@@ -173,7 +200,7 @@ public class NetworkDomainTearDown
                     {
                         logger.info("Server with Id %s is in a FAILED_ADD state, running the clean server operation.", server.id());
                         api.getServerApi().cleanServer(server.id());
-                        api.getServerApi().serverDeletedPredicate().apply(server.id());
+                        api.serverDeletedPredicate().apply(server.id());
                         if (api.getServerApi().getServer(server.id()) != null)
                         {
                             logger.info("Failed to clean Server with Id %s", server.id());
@@ -189,11 +216,11 @@ public class NetworkDomainTearDown
                     {
                         logger.info("Shutting down Server with Id %s", server.id());
                         api.getServerApi().shutdownServer(server.id());
-                        api.getServerApi().serverStoppedPredicate().apply(server.id());
+                        api.serverStoppedPredicate().apply(server.id());
                     }
                     logger.info("Deleting Server with Id %s", server.id());
                     api.getServerApi().deleteServer(server.id());
-                    api.getServerApi().serverDeletedPredicate().apply(server.id());
+                    api.serverDeletedPredicate().apply(server.id());
                 }
                 catch (Exception e)
                 {