Fix private flavor tnenant issues

Change-Id: I5a89c6e4c497c72ce1856d0dd9aff8799cec07fa
This commit is contained in:
st6218 2019-12-20 13:15:05 -08:00 committed by STEW TY
parent 19b035bfb6
commit 93d6e9d115
3 changed files with 46 additions and 44 deletions
orm
services
flavor_manager/fms_rest/logic
resource_distributor/rds/services
tests/unit/fms

@ -26,25 +26,20 @@ def validate_tenants_regions_list(requested_tenants, requested_regions,
valid_tenants_list, valid_regions_list = [], []
# the first element in the results tuple is the tenant
# prep result_tenant_list from result_rows and remove NoneTypes from list
result_tenant_list = [x[0] for x in results]
result_tenant_list = filter(None, result_tenant_list)
# lastly clean up valid_tenants_list list by removing duplicate items
valid_tenants_list = list(dict.fromkeys(result_tenant_list))
if results:
# the first element in the results tuple is the tenant
# prep result_tenant_list from result_rows and remove NoneTypes from list
result_tenant_list = [x[0] for x in results]
result_tenant_list = filter(None, result_tenant_list)
# lastly clean up valid_tenants_list list by removing duplicate items
valid_tenants_list = list(dict.fromkeys(result_tenant_list))
# second element in the results tuple is the region - compile the region
# data into valid_regions_list and eliminate duplicates from the list
valid_regions_list = [x[1] for x in results]
valid_regions_list = list(dict.fromkeys(valid_regions_list))
# second element in the results tuple is the region - compile the region
# data into valid_regions_list and eliminate duplicates from the list
valid_regions_list = [x[1] for x in results]
valid_regions_list = list(dict.fromkeys(valid_regions_list))
# update the tenants list with validated tenants from query results
# and will be reflected in the create/get flavor as well as
# add/ tenant responses
requested_tenants = valid_tenants_list
requested_regions = valid_regions_list
return requested_tenants, requested_regions
return valid_tenants_list, valid_regions_list
@di.dependsOn('data_manager')
@ -64,18 +59,20 @@ def create_flavor(flavor, flavor_uuid, transaction_id):
for region in flavor.flavor.regions:
flavor_regions.append(region.name)
# validate which tenants from the original tenants list to
# be assigned to the private flavor; if no valid tenants
# found, the valid_tenants_list will return empty list
if flavor.flavor.visibility == 'private':
valid_tenants_list, valid_regions_list = \
validate_tenants_regions_list(flavor.flavor.tenants,
# flavor.flavor.regions,
flavor_regions,
'create', datamanager)
# replace the original tenant list in the private flavor
# with the valid_tenants_list
flavor.flavor.tenants = valid_tenants_list
# Execute the following logic only if at least one region AND one
# tenant are provided in a private flavor:
# Validate which tenants from the original tenants list to
# be assigned to the private flavor; if no valid tenants
# found, the valid_tenants_list will return empty list
if (flavor_regions and flavor.flavor.visibility == 'private' and
flavor.flavor.tenants):
valid_tenants_list, valid_regions_list = \
validate_tenants_regions_list(flavor.flavor.tenants,
flavor_regions,
'create', datamanager)
# replace the original tenant list in the private flavor
# with the valid_tenants_list
flavor.flavor.tenants = valid_tenants_list
sql_flavor = flavor.to_db_model()
@ -376,16 +373,16 @@ def add_tenants(flavor_uuid, tenants, transaction_id):
for x in existing_region_names:
existing_region_list.append(x)
if tenants.tenants:
valid_tenants_list, valid_regions_list = \
validate_tenants_regions_list(tenants.tenants,
existing_region_list,
'create', datamanager)
if tenants.tenants:
valid_tenants_list, valid_regions_list = \
validate_tenants_regions_list(tenants.tenants,
existing_region_list,
'create', datamanager)
# replace tenants.tenants with only the valid tenants
tenants.tenants = valid_tenants_list
# issue error message if tenant list is empty
if not tenants.tenants:
else:
raise ValueError("At least one valid tenant must be provided")
for tenant in tenants.tenants:

@ -129,14 +129,17 @@ def _create_template_data(input_data):
elif input_data.resource_type == "group":
yamldata = yaml_group_builder.yamlbuilder(jsondata, target)
elif input_data.resource_type == "flavor":
# save off the original "input_data.model['tenants']" value
tenants_list = input_data.model['tenants']
if input_data.model['visibility'] == 'private':
ok_tenants_list = []
ok_tenants = {}
# skip tenant validation if tenants list is empty
if input_data.model['tenants']:
if tenants_list:
valid_tenants_list = get_valid_tenants(
input_data.model['tenants'], target['name'])
# input_data.model['tenants'], target['name'])
tenants_list, target['name'])
for tenant in valid_tenants_list:
ok_tenants['tenant_id'] = tenant
@ -145,13 +148,15 @@ def _create_template_data(input_data):
# Note: If ok_tenant_list is empty, just create heat template
# for private flavor with empty tenant list
if not ok_tenants_list:
ok_tenants['tenant_id'] = None
ok_tenants['tenant_id'] = ''
ok_tenants_list.append(ok_tenants.copy())
jsondata['tenants'] = ok_tenants_list
# now issue yamldata for flavor either public or private
yamldata = yaml_flavor_builder.yamlbuilder(jsondata, target)
# restore the original "input_data.model['tenants']" value
input_data.model['tenants'] = tenants_list
elif input_data.resource_type == "image":
yamldata = yaml_image_builder.yamlbuilder(jsondata, target)
targetslist.append({"region_id": target['name'],

@ -543,11 +543,11 @@ class TestFlavorLogic(FunctionalTest):
error = 31
moq = MagicMock()
moq.tenants = [1337]
self.assertRaises(ValueError,
flavor_logic.add_tenants, 'uuid',
moq,
'transaction')
# moq.tenants = [1337]
# self.assertRaises(flavor_logic.ErrorStatus,
# flavor_logic.add_tenants, 'uuid',
# moq,
# 'transaction')
mock_strin.side_effect = exc.FlushError(
'conflicts with persistent instance')