Fix service creation under transaction

Currently policy errors out if service entry is not present directly
under service, enev if the entry is specified as Child in same
transational API. This patch works around the problem.

Change-Id: I6c80c9ea6d188f4d282036c5a0a00a09969f7244
This commit is contained in:
Anna Khmelnitsky 2019-02-21 11:33:23 -08:00
parent b89a0ca744
commit a0da933427
3 changed files with 21 additions and 1 deletions

View File

@ -61,6 +61,15 @@ class ResourceDef(object):
self.body = {}
# As of now, for some defs (ex: services) child entry is required,
# meaning parent creation will fail without the child.
# Unfortunately in transactional API policy still fails us, even if
# child is specified as ChildEntry in same transaction.
# To provide a workaround, we need keep reference to the child and
# populate child entry inside parent clause in transactional API.
# TODO(annak): remove this if/when policy solves this
self.mandatory_child_def = None
def get_obj_dict(self):
body = self.body if self.body else {}
if self.resource_type():

View File

@ -263,7 +263,7 @@ class NsxPolicyResourceBase(object):
if transaction:
# Store this def for batch apply for this transaction
transaction.store_def(policy_def, self.policy_api.client)
if child_def:
if child_def and not policy_def.mandatory_child_def:
transaction.store_def(child_def, self.policy_api.client)
else:
# No transaction - apply now
@ -550,6 +550,7 @@ class NsxPolicyL4ServiceApi(NsxPolicyServiceBase):
dest_ports=dest_ports,
tenant=tenant)
service_def.mandatory_child_def = entry_def
self._create_or_store(service_def, entry_def)
return service_id
@ -605,6 +606,7 @@ class NsxPolicyIcmpServiceApi(NsxPolicyServiceBase):
icmp_code=icmp_code,
tenant=tenant)
service_def.mandatory_child_def = entry_def
self._create_or_store(service_def, entry_def)
return service_id
@ -659,6 +661,7 @@ class NsxPolicyIPProtocolServiceApi(NsxPolicyServiceBase):
protocol_number=protocol_number,
tenant=tenant)
service_def.mandatory_child_def = entry_def
self._create_or_store(service_def, entry_def)
return service_id

View File

@ -169,6 +169,14 @@ class NsxPolicyTransaction(object):
parent_dict['children'] = []
resource_class = resource_def.resource_class()
node = resource_def.get_obj_dict()
if resource_def.mandatory_child_def:
# This is a workaround for policy issue that involves required
# children (see comment on definition of mandatory_child_def)
# TODO(annak): remove when policy solves the issue
child_def = resource_def.mandatory_child_def
child_dict_key = child_def.get_last_section_dict_key
node[child_dict_key] = [child_def.get_obj_dict()]
parent_dict['children'].append(
self._build_wrapper_dict(resource_class,
resource_def.get_obj_dict()))