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/01/14 09:09:00 UTC

[jclouds-examples] branch master updated: Expose wait for predicates on relevant API

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 12af7c1  Expose wait for predicates on relevant API
12af7c1 is described below

commit 12af7c1badf3c54bb51968267367d6ef67560d5f
Author: Boris Trishkin <bo...@itaas.dimensiondata.com>
AuthorDate: Thu Jan 10 10:39:29 2019 +0000

    Expose wait for predicates on relevant API
---
 dimensiondata/pom.xml                              |   2 +-
 .../DeleteServerVlanAndNetworkDomain.java          |  28 ++----
 .../DeployNetworkDomainVlanAndServer.java          |  29 ++----
 .../cloudcontrol/NetworkDomainTearDown.java        |  39 ++++----
 .../dimensiondata/cloudcontrol/WaitForUtils.java   | 108 ---------------------
 5 files changed, 39 insertions(+), 167 deletions(-)

diff --git a/dimensiondata/pom.xml b/dimensiondata/pom.xml
index 8dcb1c2..cb7ffc0 100644
--- a/dimensiondata/pom.xml
+++ b/dimensiondata/pom.xml
@@ -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>
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 8260277..6035c54 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
@@ -19,7 +19,6 @@ 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;
@@ -29,8 +28,6 @@ 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>
@@ -66,11 +63,6 @@ public class DeleteServerVlanAndNetworkDomain
                 .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 class DeleteServerVlanAndNetworkDomain
             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 @@ public class DeleteServerVlanAndNetworkDomain
         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 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.
              */
-            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 @@ 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.
          */
-        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 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.
                  */
-                waitForServerStopped(injector, server);
+               api.getServerApi().serverStoppedPredicate().apply(server.id());
             }
 
             /*
@@ -199,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.
              */
-            waitForServerDeleted(injector, server);
+           api.getServerApi().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 f6521e4..9eace32 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
@@ -19,7 +19,6 @@ package org.jclouds.examples.dimensiondata.cloudcontrol;
 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 org.jclouds.rest.ApiContext;
 import java.util.Collections;
 import java.util.List;
 
-import static org.jclouds.examples.dimensiondata.cloudcontrol.WaitForUtils.*;
-
 /**
  * This class will attempt to Deploy:
  * <ul>
@@ -71,11 +68,6 @@ public class DeployNetworkDomainVlanAndServer
                 .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();
 
             /*
@@ -83,16 +75,15 @@ public class DeployNetworkDomainVlanAndServer
              */
             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 @@ 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.
          */
-        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 @@ public class DeployNetworkDomainVlanAndServer
         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 @@ 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.
          */
-        waitForNetworkDomainNormal(injector, networkDomainId);
+       api.getNetworkApi().networkDomainNormalPredicate().apply(networkDomainId);
 
         /*
          * Apply a Tag to the Network Domain. We use AssetType NETWORK_DOMAIN.
@@ -163,8 +155,7 @@ public class DeployNetworkDomainVlanAndServer
         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 @@ 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.
          */
-        waitForVlanNormal(injector, vlanId);
+       api.getNetworkApi().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 2526fb9..112a780 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
@@ -18,17 +18,20 @@ package org.jclouds.examples.dimensiondata.cloudcontrol;
 
 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 class NetworkDomainTearDown
                 .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 class NetworkDomainTearDown
 
             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 class NetworkDomainTearDown
                 }
                 return;
             }
-            deleteVlans(api, injector, networkDomain);
+            deleteVlans(api, networkDomain);
 
-            deleteNetworkDomain(networkDomainId, api, injector);
+            deleteNetworkDomain(networkDomainId, api);
         }
     }
 
@@ -135,14 +132,14 @@ public class NetworkDomainTearDown
         }
     }
 
-    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 @@ public class NetworkDomainTearDown
                 }
                 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 @@ public class NetworkDomainTearDown
         }
     }
 
-    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 @@ 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());
-                    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 @@ public class NetworkDomainTearDown
                 {
                     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)
             {
diff --git a/dimensiondata/src/main/java/org/jclouds/examples/dimensiondata/cloudcontrol/WaitForUtils.java b/dimensiondata/src/main/java/org/jclouds/examples/dimensiondata/cloudcontrol/WaitForUtils.java
deleted file mode 100644
index 53bae18..0000000
--- a/dimensiondata/src/main/java/org/jclouds/examples/dimensiondata/cloudcontrol/WaitForUtils.java
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- * 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.Predicate;
-import com.google.inject.Injector;
-import com.google.inject.Key;
-import com.google.inject.TypeLiteral;
-import com.google.inject.name.Names;
-import org.jclouds.dimensiondata.cloudcontrol.domain.Server;
-import org.jclouds.dimensiondata.cloudcontrol.domain.Vlan;
-
-public class WaitForUtils
-{
-
-    private static final String SERVER_STARTED_PREDICATE = "SERVER_STARTED_PREDICATE";
-    private static final String SERVER_NORMAL_PREDICATE = "SERVER_NORMAL_PREDICATE";
-    private static final String NETWORK_DOMAIN_NORMAL_PREDICATE = "NETWORK_DOMAIN_NORMAL_PREDICATE";
-    private static final String VLAN_NORMAL_PREDICATE = "VLAN_NORMAL_PREDICATE";
-    private static final String SERVER_DELETED_PREDICATE = "SERVER_DELETED_PREDICATE";
-    private static final String NETWORK_DOMAIN_DELETED_PREDICATE = "NETWORK_DOMAIN_DELETED_PREDICATE";
-    private static final String VLAN_DELETED_PREDICATE = "VLAN_DELETED_PREDICATE";
-    private static final String SERVER_STOPPED_PREDICATE = "SERVER_STOPPED_PREDICATE";
-
-    static void waitForServerStopped(Injector injector, Server server)
-    {
-        Predicate<String> serverStoppedPredicate = injector.getInstance(Key.get(new TypeLiteral<Predicate<String>>()
-        {
-        }, Names.named(SERVER_STOPPED_PREDICATE)));
-
-        // Wait for Server to be STOPPED
-        serverStoppedPredicate.apply(server.id());
-    }
-
-    static void waitForDeleteVlan(Injector injector, Vlan vlan)
-    {
-        Predicate<String> vlanDeletedPredicate = injector.getInstance(Key.get(new TypeLiteral<Predicate<String>>()
-        {
-        }, Names.named(VLAN_DELETED_PREDICATE)));
-
-        // Wait for VLAN to be DELETED
-        vlanDeletedPredicate.apply(vlan.id());
-    }
-
-    static void waitForDeleteNetworkDomain(Injector injector, String networkDomainId)
-    {
-        Predicate<String> networkDomainDeletedPredicate = injector.getInstance(Key.get(new TypeLiteral<Predicate<String>>()
-        {
-        }, Names.named(NETWORK_DOMAIN_DELETED_PREDICATE)));
-
-        // Wait for NETWORK DOMAIN to be DELETED
-        networkDomainDeletedPredicate.apply(networkDomainId);
-    }
-
-    static void waitForServerDeleted(Injector injector, Server server)
-    {
-        Predicate<String> serverDeletedPredicate = injector.getInstance(Key.get(new TypeLiteral<Predicate<String>>()
-        {
-        }, Names.named(SERVER_DELETED_PREDICATE)));
-
-        // Wait for Server to be DELETED
-        serverDeletedPredicate.apply(server.id());
-    }
-
-    static void waitForServerStartedAndNormal(Injector injector, String serverId)
-    {
-        Predicate<String> serverStartedPredicate = injector.getInstance(Key.get(new TypeLiteral<Predicate<String>>()
-        {
-        }, Names.named(SERVER_STARTED_PREDICATE)));
-        Predicate<String> serverNormalPredicate = injector.getInstance(Key.get(new TypeLiteral<Predicate<String>>()
-        {
-        }, Names.named(SERVER_NORMAL_PREDICATE)));
-
-        // Wait for Server to be started and NORMAL
-        serverStartedPredicate.apply(serverId);
-        serverNormalPredicate.apply(serverId);
-    }
-
-    static void waitForNetworkDomainNormal(Injector injector, String networkDomainId)
-    {
-        Predicate<String> networkDomainNormalPredicate = injector.getInstance(Key.get(new TypeLiteral<Predicate<String>>()
-        {
-        }, Names.named(NETWORK_DOMAIN_NORMAL_PREDICATE)));
-        networkDomainNormalPredicate.apply(networkDomainId);
-    }
-
-    static void waitForVlanNormal(Injector injector, String vlanId)
-    {
-        Predicate<String> vlanNormalPredicate = injector.getInstance(Key.get(new TypeLiteral<Predicate<String>>()
-        {
-        }, Names.named(VLAN_NORMAL_PREDICATE)));
-        vlanNormalPredicate.apply(vlanId);
-    }
-}