Move network example into directory

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

Change-Id: I2f5b49ccf3fdea1ef1ef534b68dde9ae0e59bd2b
This commit is contained in:
TerryHowe 2015-04-29 09:37:51 -06:00
parent 43ee0a6343
commit 2ef6d4dda1
5 changed files with 70 additions and 37 deletions

View File

@ -25,7 +25,7 @@ import sys
from examples import common from examples import common
from examples import connection from examples import connection
from examples.keypair import create as keypair from examples.keypair import create as keypair
from examples import network from examples.network import create as network
def create_jenkins(conn, name, opts): def create_jenkins(conn, name, opts):

View File

@ -24,7 +24,7 @@ import sys
from examples import common from examples import common
from examples import connection from examples import connection
from examples.keypair import delete as keypair from examples.keypair import delete as keypair
from examples import network from examples.network import delete as network
def delete_jenkins(conn, name, opts): def delete_jenkins(conn, name, opts):

View File

View File

@ -13,10 +13,10 @@
""" """
Network examples 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: To run:
python examples/network.py python examples/network/create.py
""" """
import sys import sys
@ -68,42 +68,9 @@ def create(conn, name, opts, ports_to_open=[80, 22]):
return network 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): def run_network(opts):
argument = opts.argument
name = opts.data.pop('name', 'netty') name = opts.data.pop('name', 'netty')
conn = connection.make_connection(opts) conn = connection.make_connection(opts)
if argument == "delete":
return(delete(conn, name))
return(create(conn, name, opts)) return(create(conn, name, opts))

View File

@ -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))