Added a way to query cidr for existing subnets

Also fixed a select-from-source function to properly handle empty path

Change-Id: I87c694cfa7725e32956062b63529c3bda5804ba7
This commit is contained in:
ativelkov 2013-12-06 15:38:38 +04:00
parent f3e779ea33
commit 4c7ff16951
3 changed files with 51 additions and 12 deletions

View File

@ -31,6 +31,7 @@ class NeutronExecutor(CommandBase):
self.address = muranoconductor.config.CONF.env_ip_template
self.cidr_waiting_per_router = {}
self.cidr_waiting_per_network = {}
self.router_requests = []
self.network_requests = []
self.tenant_id = tenant_id
@ -57,7 +58,8 @@ class NeutronExecutor(CommandBase):
insecure=neutron_settings.insecure)
self.command_map = {
"get_subnet": self._schedule_get_subnet,
"get_new_subnet": self._schedule_get_new_subnet,
"get_existing_subnet": self._schedule_get_existing_subnet,
"get_router": self._schedule_get_router,
"get_network": self._schedule_get_network
}
@ -68,15 +70,17 @@ class NeutronExecutor(CommandBase):
def has_pending_commands(self):
return len(self.cidr_waiting_per_router) + len(
self.router_requests) + len(self.network_requests) > 0
self.cidr_waiting_per_network) + len(self.router_requests) + len(
self.network_requests) > 0
def execute_pending(self):
r1 = self._execute_pending_cidr_requests()
r1 = self._execute_pending_new_cidr_requests()
r2 = self._execute_pending_net_requests()
r3 = self._execute_pending_router_requests()
return r1 or r2 or r3
r4 = self._execute_pending_existing_cidr_requests()
return r1 or r2 or r3 or r4
def _execute_pending_cidr_requests(self):
def _execute_pending_new_cidr_requests(self):
if not len(self.cidr_waiting_per_router):
return False
for router, callbacks in self.cidr_waiting_per_router.items():
@ -86,11 +90,21 @@ class NeutronExecutor(CommandBase):
self.cidr_waiting_per_router = {}
return True
def _execute_pending_existing_cidr_requests(self):
if not len(self.cidr_waiting_per_network):
return False
for network, callbacks in self.cidr_waiting_per_network.items():
result = self._get_existing_subnet(network)
for callback in callbacks:
callback(result)
self.cidr_waiting_per_network = {}
return True
def _execute_pending_router_requests(self):
if not len(self.router_requests):
return False
routers = self.neutron.list_routers(tenant_id=self.tenant_id).\
routers = self.neutron.list_routers(tenant_id=self.tenant_id). \
get("routers")
if not len(routers):
routerId = None
@ -153,6 +167,13 @@ class NeutronExecutor(CommandBase):
taken_cidrs.append(res)
return results
def _get_existing_subnet(self, network_id):
subnets = self.neutron.list_subnets(network_id=network_id)['subnets']
if not subnets:
return None
else:
return subnets[0]['cidr']
def _get_taken_cidrs_by_router(self, routerId):
ports = self.neutron.list_ports(device_id=routerId)["ports"]
subnet_ids = []
@ -182,7 +203,7 @@ class NeutronExecutor(CommandBase):
return str(subnet)
return None
def _schedule_get_subnet(self, callback, **kwargs):
def _schedule_get_new_subnet(self, callback, **kwargs):
routerId = kwargs.get("routerId")
if not routerId:
routerId = "*"
@ -191,6 +212,14 @@ class NeutronExecutor(CommandBase):
else:
self.cidr_waiting_per_router[routerId] = [callback]
def _schedule_get_existing_subnet(self, callback, **kwargs):
existing_network = kwargs.get("existingNetwork")
if existing_network in self.cidr_waiting_per_network:
self.cidr_waiting_per_network[existing_network].append(callback)
else:
self.cidr_waiting_per_network[existing_network] = [callback]
def _schedule_get_router(self, callback, **kwargs):
self.router_requests.append(callback)

View File

@ -20,7 +20,8 @@ from openstack.common import log as logging
log = logging.getLogger(__name__)
def get_available_subnet(engine, context, body, routerId=None, result=None):
def get_subnet(engine, context, body, routerId=None, existingNetwork=None,
result=None):
command_dispatcher = context['/commandDispatcher']
def callback(result_value):
@ -31,9 +32,15 @@ def get_available_subnet(engine, context, body, routerId=None, result=None):
if success_handler is not None:
engine.evaluate_content(success_handler, context)
if existingNetwork:
command = "get_existing_subnet"
else:
command = "get_new_subnet"
command_dispatcher.execute(
name="net",
command="get_subnet",
command=command,
existingNetwork=existingNetwork,
routerId=routerId,
callback=callback)
@ -77,7 +84,7 @@ def get_network_topology(engine, context, body, result=None):
xml_code_engine.XmlCodeEngine.register_function(
get_available_subnet, "get-cidr")
get_subnet, "get-cidr")
xml_code_engine.XmlCodeEngine.register_function(
get_default_router, "get-default-router-id")

View File

@ -127,8 +127,11 @@ class Workflow(object):
elif path.startswith('#'):
result = context[path[1:]]
elif source is not None:
result = Workflow._get_path(
context[source], path.split('.'))
if path == '':
p = []
else:
p = path.split('.')
result = Workflow._get_path(context[source], p)
else:
result = Workflow._get_path(
context['/dataSource'],