An extenstion to retrieve network data from an openstack cloud

If the required data is not available, a new resource is created.

Change-Id: I1307234c54f735a79687469daee78b4baa44ce91
This commit is contained in:
Rahul Nair 2016-09-12 15:48:51 -05:00
parent baa8ac2ef7
commit 183667e511
4 changed files with 209 additions and 0 deletions

View File

@ -6,3 +6,4 @@ requests>=2.10.0 # Apache-2.0
oslo.config>=3.14.0 # Apache-2.0
oslo.utils>=3.16.0 # Apache-2.0
python-glanceclient>=2.3.0,!=2.4.0 # Apache-2.0
python-neutronclient>=5.1.0 # Apache-2.0

View File

View File

@ -0,0 +1,171 @@
# Copyright 2016 Intel
# 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.
from keystoneauth1 import identity
from keystoneauth1 import session
from neutronclient.v2_0 import client
from oslo_config import cfg
from syntribos.utils.memoize import memoize
CONF = cfg.CONF
def create_connection(auth_url=None,
project_name=None,
project_domain_name="default",
user_domain_name="default",
project_domain_id="default",
user_domain_id="default",
username=None,
password=None):
"""Method creates a neutron client and returns it."""
if auth_url.endswith("/v3/"):
auth_url = auth_url[:-1]
elif auth_url.endswith("/v3"):
pass
else:
auth_url = "{}/v3".format(auth_url)
auth = identity.Password(auth_url=auth_url,
project_name=project_name,
project_domain_name=project_domain_name,
user_domain_name=user_domain_name,
project_domain_id=project_domain_id,
user_domain_id=user_domain_id,
username=username,
password=password)
return client.Client(session=session.Session(auth=auth))
neutron_client = create_connection(
auth_url=CONF.user.endpoint,
project_name=CONF.user.project_name,
project_domain_id=CONF.user.domain_id,
user_domain_id=CONF.user.domain_id,
project_domain_name=CONF.user.domain_id,
user_domain_name=CONF.user.domain_id,
username=CONF.user.username,
password=CONF.user.password)
def create_network(conn):
data = {"name": "sample_network",
"admin_state_up": True}
return conn.create_network({"network": data})
def list_network_ids(conn):
return [network["id"] for network in conn.list_networks()["networks"]]
def create_subnet(conn, network_id):
data = {"name": "sample_subnet",
"network_id": network_id,
"ip_version": 4,
"cidr": "11.0.3.0/24"}
return conn.create_subnet({"subnet": data})
def list_subnet_ids(conn):
subnet_ids = [subnet["id"] for subnet in conn.list_subnets()["subnets"]]
return subnet_ids
def create_port(conn, network_id):
data = {"network_id": network_id,
"name": "sample_port",
"admin_state_up": True}
return conn.create_port({"port": data})
def list_port_ids(conn):
port_ids = [port["id"] for port in conn.list_ports()["ports"]]
return port_ids
def create_security_group(conn):
data = {"name": "new_servers",
"description": "security group for servers"}
return conn.create_security_group({"security_group": data})
def list_security_group_ids(conn):
sec_gp_ids = [sg["id"] for sg in conn.list_security_groups(
)["security_groups"]]
return sec_gp_ids
def create_router(conn, network_id, subnet_id):
# The network_id should be of an external network
data = {
"name": "router1",
"external_gateway_info": {
"network_id": network_id,
"enable_snat": True,
"external_fixed_ips": [
{
"ip_address": "172.24.4.6",
"subnet_id": subnet_id
}
]
},
"admin_state_up": True
}
return conn.create_router({"router": data})
def list_router_ids(conn):
router_ids = [router["id"] for router in conn.list_routers()["routers"]]
return router_ids
@memoize
def get_port_id():
port_ids = list_port_ids(neutron_client)
if not port_ids:
network_id = get_network_id()
port_ids.append(create_port(neutron_client, network_id)["id"])
return port_ids[-1]
@memoize
def get_network_id():
network_ids = list_network_ids(neutron_client)
if len(network_ids) < 3:
network_ids.append(create_network(neutron_client)["id"])
return network_ids[-1]
@memoize
def get_subnet_id():
subnet_ids = list_subnet_ids(neutron_client)
if not subnet_ids:
network_id = get_network_id()
subnet_ids.append(create_subnet(neutron_client, network_id)["id"])
return subnet_ids[-1]
@memoize
def get_sg_group_id():
sg_ids = list_security_group_ids(neutron_client)
if not sg_ids:
sg_ids.append(create_security_group(neutron_client)["id"])
return sg_ids[-1]
@memoize
def get_router_id():
router_ids = list_router_ids(neutron_client)
if not router_ids:
router_ids.append(create_router(neutron_client)["id"])
return router_ids[-1]

View File

@ -11,6 +11,7 @@
# 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.
import random
import uuid
@ -22,3 +23,39 @@ def get_uuid():
while True:
random_data = str(uuid.uuid4())
yield random_data
def fake_port():
return random.int(0, 65535)
def fake_ip():
return "{}:{}:{}:{}".format(random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255))
def fake_mac():
return "{:x}:{:x}:{:x}:{:x}:{:x}:{:x}".format(random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255))
def random_port():
while True:
yield fake_port()
def random_ip():
while True:
yield fake_ip()
def random_mac():
while True:
yield fake_mac()