From 2ef6d4dda10248d677047b16f093efcc27fb9a31 Mon Sep 17 00:00:00 2001
From: TerryHowe <terrylhowe@gmail.com>
Date: Wed, 29 Apr 2015 09:37:51 -0600
Subject: [PATCH] Move network example into directory

Make the examples a bit easier to use when one file means one
example.

Change-Id: I2f5b49ccf3fdea1ef1ef534b68dde9ae0e59bd2b
---
 examples/jenkins/create.py                 |  2 +-
 examples/jenkins/delete.py                 |  2 +-
 examples/network/__init__.py               |  0
 examples/{network.py => network/create.py} | 37 +-----------
 examples/network/delete.py                 | 66 ++++++++++++++++++++++
 5 files changed, 70 insertions(+), 37 deletions(-)
 create mode 100644 examples/network/__init__.py
 rename examples/{network.py => network/create.py} (70%)
 create mode 100644 examples/network/delete.py

diff --git a/examples/jenkins/create.py b/examples/jenkins/create.py
index e9193fada..d1e9f2335 100644
--- a/examples/jenkins/create.py
+++ b/examples/jenkins/create.py
@@ -25,7 +25,7 @@ import sys
 from examples import common
 from examples import connection
 from examples.keypair import create as keypair
-from examples import network
+from examples.network import create as network
 
 
 def create_jenkins(conn, name, opts):
diff --git a/examples/jenkins/delete.py b/examples/jenkins/delete.py
index 177c6ef7a..2b7efc59d 100644
--- a/examples/jenkins/delete.py
+++ b/examples/jenkins/delete.py
@@ -24,7 +24,7 @@ import sys
 from examples import common
 from examples import connection
 from examples.keypair import delete as keypair
-from examples import network
+from examples.network import delete as network
 
 
 def delete_jenkins(conn, name, opts):
diff --git a/examples/network/__init__.py b/examples/network/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/examples/network.py b/examples/network/create.py
similarity index 70%
rename from examples/network.py
rename to examples/network/create.py
index 631ebb677..e948ccc8a 100644
--- a/examples/network.py
+++ b/examples/network/create.py
@@ -13,10 +13,10 @@
 """
 Network examples
 
-Create and destroy all the pieces parts to have a working network.
+Create all the pieces parts to have a working network.
 
 To run:
-    python examples/network.py
+    python examples/network/create.py
 """
 
 import sys
@@ -68,42 +68,9 @@ def create(conn, name, opts, ports_to_open=[80, 22]):
     return network
 
 
-def delete(conn, name):
-
-    router = conn.network.find_router(name)
-    if router is not None:
-        print(str(router))
-
-    subnet = conn.network.find_subnet(name)
-    if subnet is not None:
-        print(str(subnet))
-        if router:
-            try:
-                conn.network.router_remove_interface(router, subnet.id)
-            except Exception:
-                pass
-        for port in conn.network.get_subnet_ports(subnet.id):
-            print(str(port))
-            conn.delete(port)
-
-    if router is not None:
-        conn.delete(router)
-
-    if subnet:
-        conn.delete(subnet)
-
-    network = conn.network.find_network(name)
-    if network is not None:
-        print(str(network))
-        conn.delete(network)
-
-
 def run_network(opts):
-    argument = opts.argument
     name = opts.data.pop('name', 'netty')
     conn = connection.make_connection(opts)
-    if argument == "delete":
-        return(delete(conn, name))
     return(create(conn, name, opts))
 
 
diff --git a/examples/network/delete.py b/examples/network/delete.py
new file mode 100644
index 000000000..50f359e74
--- /dev/null
+++ b/examples/network/delete.py
@@ -0,0 +1,66 @@
+# Licensed 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.
+
+"""
+Network examples
+
+Destroy all the pieces parts of a working network.
+
+To run:
+    python examples/network/delete.py
+"""
+
+import sys
+
+from examples import common
+from examples import connection
+
+
+def delete(conn, name):
+
+    router = conn.network.find_router(name)
+    if router is not None:
+        print(str(router))
+
+    subnet = conn.network.find_subnet(name)
+    if subnet is not None:
+        print(str(subnet))
+        if router:
+            try:
+                conn.network.router_remove_interface(router, subnet.id)
+            except Exception:
+                pass
+        for port in conn.network.get_subnet_ports(subnet.id):
+            print(str(port))
+            conn.delete(port)
+
+    if router is not None:
+        conn.delete(router)
+
+    if subnet:
+        conn.delete(subnet)
+
+    network = conn.network.find_network(name)
+    if network is not None:
+        print(str(network))
+        conn.delete(network)
+
+
+def run_network(opts):
+    name = opts.data.pop('name', 'netty')
+    conn = connection.make_connection(opts)
+    return(delete(conn, name))
+
+
+if __name__ == "__main__":
+    opts = common.setup()
+    sys.exit(common.main(opts, run_network))