Skip to content

Commit

Permalink
Merge pull request #18 from apigee/feat/kvm-compare
Browse files Browse the repository at this point in the history
Added support for comparing state with target
  • Loading branch information
anaik91 authored Feb 4, 2025
2 parents bb63a2b + c9508bd commit e1a6d75
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 29 deletions.
50 changes: 39 additions & 11 deletions core_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def export_artifacts(cfg, resources_list):
return export_data


def validate_artifacts(cfg, resources_list, export_data): # noqa pylint: disable=R0914
def validate_artifacts(cfg, resources_list, export_data): # noqa pylint: disable=R0914,R0912,R0915
"""Validates exported artifacts against the target environment.
Validates the exported Apigee artifacts against the constraints of
Expand Down Expand Up @@ -240,7 +240,10 @@ def validate_artifacts(cfg, resources_list, export_data): # noqa pylint: disabl
'oauth',
True
)
target_resources = ['targetservers', 'flowhooks', 'resourcefiles', 'apis', 'sharedflows'] # noqa pylint: disable=C0301
target_resources = ['targetservers', 'flowhooks', 'resourcefiles',
'apis', 'sharedflows', 'org_keyvaluemaps',
'keyvaluemaps', 'apps', 'apiproducts',
'developers']
target_resource_list = []
if 'all' in resources_list:
target_resource_list = target_resources
Expand All @@ -255,17 +258,42 @@ def validate_artifacts(cfg, resources_list, export_data): # noqa pylint: disabl
target_servers = export_data['envConfig'][env]['targetServers']
resourcefiles = export_data['envConfig'][env]['resourcefiles']
flowhooks = export_data['envConfig'][env]['flowhooks']
report[env + SEPERATOR +
'targetServers'] = apigee_validator.validate_env_targetservers(env, target_servers) # noqa pylint: disable=C0301
report[env + SEPERATOR +
keyvaluemaps = export_data['envConfig'][env]['kvms']
if 'all' in resources_list or 'keyvaluemaps' in resources_list:
report[env + SEPERATOR +
'targetServers'] = apigee_validator.validate_env_targetservers(env, target_servers) # noqa pylint: disable=C0301
if 'all' in resources_list or 'resourcefiles' in resources_list:
report[env + SEPERATOR +
'resourcefiles'] = apigee_validator.validate_env_resourcefiles(env, resourcefiles) # noqa pylint: disable=C0301
report[env + SEPERATOR +
if 'all' in resources_list or 'flowhooks' in resources_list:
report[env + SEPERATOR +
'flowhooks'] = apigee_validator.validate_env_flowhooks(env, flowhooks) # noqa

validation = apigee_validator.validate_proxy_bundles(export_dir)
# Todo # pylint: disable=W0511
# validate proxy unifier output bundles
report.update(validation)
if 'all' in resources_list or 'keyvaluemaps' in resources_list:
report[env + SEPERATOR +
'keyvaluemaps'] = apigee_validator.validate_kvms(env, keyvaluemaps) # noqa

if 'all' in resources_list or 'org_keyvaluemaps' in resources_list:
org_keyvaluemaps = export_data['orgConfig']['kvms']
report['org_keyvaluemaps'] = apigee_validator.validate_kvms(None, org_keyvaluemaps) # noqa
if 'all' in resources_list or 'developers' in resources_list:
developers = export_data['orgConfig']['developers']
report['developers'] = apigee_validator.validate_org_resource('developers', developers) # noqa pylint: disable=C0301
if 'all' in resources_list or 'apiproducts' in resources_list:
api_products = export_data['orgConfig']['apiProducts']
report['apiProducts'] = apigee_validator.validate_org_resource('apiProducts', api_products) # noqa pylint: disable=C0301
if 'all' in resources_list or 'apps' in resources_list:
apps = export_data['orgConfig']['apps']
report['apps'] = apigee_validator.validate_org_resource('apps', apps) # noqa pylint: disable=C0301
if 'all' in resources_list or 'apis' in resources_list:
apis_validation = apigee_validator.validate_proxy_bundles(export_dir, 'apis') # noqa pylint: disable=C0301
# Todo # pylint: disable=W0511
# validate proxy unifier output bundles
report.update(apis_validation)
if 'all' in resources_list or 'sharedflows' in resources_list:
sf_validation = apigee_validator.validate_proxy_bundles(export_dir, 'sharedflows') # noqa pylint: disable=C0301
# Todo # pylint: disable=W0511
# validate proxy unifier output bundles
report.update(sf_validation)
return report


Expand Down
2 changes: 1 addition & 1 deletion nextgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def _apigee_object_util(self, org_object, each_org_object_data, expand=False):
if isinstance(each_org_object_data, list):
objects.extend(each_org_object_data)
if isinstance(each_org_object_data, dict):
org_objects_list = each_org_object_data.get(expand_key)
org_objects_list = each_org_object_data.get(expand_key, [])
for each_object in org_objects_list:
if expand:
objects.append(each_object)
Expand Down
6 changes: 6 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def parse_config(config_file):
"""
config = configparser.ConfigParser()
config.read(config_file)
if len(config.sections()) == 0:
logger.error(
'Unable to read input.properties file.') # noqa pylint: disable=C0301
logger.error(
'Check if input.properties file exists OR if you permissions to view it') # noqa pylint: disable=C0301
sys.exit(1)
return config


Expand Down
86 changes: 69 additions & 17 deletions validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,61 @@ def __init__(self, project_id, token, env_type, target_export_data):
self.xorhybrid = ApigeeNewGen(project_id, token, env_type)
self.target_export_data = target_export_data

def validate_org_resource(self, resource_type, resources):
"""Validates environment keyvaluemaps.
Args:
env (str): Environment name.
keyvaluemaps (dict): A dictionary of target
server configurations.
Returns:
list: A list of validated keyvaluemaps
objects with importability status and
reasons.
"""
validation_resources = []
target_resources = self.target_export_data.get('orgConfig', {}).get(resource_type, {}).keys() # noqa pylint: disable=C0301
for each_obj, obj in resources.items():
if resource_type == 'developers':
obj['name'] = each_obj
obj['importable'], obj['reason'] = True, []
if each_obj in target_resources:
obj['imported'] = True
else:
obj['imported'] = False
validation_resources.append(obj)
return validation_resources

def validate_kvms(self, env, keyvaluemaps):
"""Validates environment keyvaluemaps.
Args:
env (str): Environment name.
keyvaluemaps (dict): A dictionary of target
server configurations.
Returns:
list: A list of validated keyvaluemaps
objects with importability status and
reasons.
"""
validation_kvms = []
if env is not None:
kvms = self.target_export_data.get('envConfig', {}).get(env, {}).get('kvms', {}).keys() # noqa pylint: disable=C0301
else:
kvms = self.target_export_data.get('orgConfig', {}).get('kvms', {}).keys() # noqa pylint: disable=C0301
for each_kvm, obj in keyvaluemaps.items():
if 'name' not in obj:
obj['name'] = each_kvm
obj['importable'], obj['reason'] = True, []
if each_kvm in kvms:
obj['imported'] = True
else:
obj['imported'] = False
validation_kvms.append(obj)
return validation_kvms

def validate_env_targetservers(self, env, target_servers):
"""Validates environment target servers.
Expand All @@ -66,10 +121,10 @@ def validate_env_targetservers(self, env, target_servers):
reasons.
"""
validation_targetservers = []
ts = self.target_export_data.get('envConfig', {}).get(env, {}).get('targetServers', {}).keys() # noqa pylint: disable=C0301
for _, target_server_data in target_servers.items():
obj = copy.copy(target_server_data)
obj['importable'], obj['reason'] = self.validate_env_targetserver_resource(target_server_data) # noqa pylint: disable=C0301
ts = self.target_export_data.get('envConfig', {}).get(env, {}).get('targetServers', {}).keys() # noqa pylint: disable=C0301
if target_server_data['name'] in ts:
obj['imported'] = True
else:
Expand Down Expand Up @@ -114,10 +169,10 @@ def validate_env_resourcefiles(self, env, resourcefiles):
reasons.
"""
validation_rfiles = []
rf = self.target_export_data.get('envConfig', {}).get(env, {}).get('resourcefiles', {}).keys() # noqa pylint: disable=C0301
for resourcefile in resourcefiles.keys():
obj = copy.copy(resourcefiles[resourcefile])
obj['importable'], obj['reason'] = self.validate_env_resourcefile_resource(resourcefiles[resourcefile]) # noqa pylint: disable=C0301
rf = self.target_export_data.get('envConfig', {}).get(env, {}).get('resourcefiles', {}).keys() # noqa pylint: disable=C0301
if resourcefile in rf:
obj['imported'] = True
else:
Expand Down Expand Up @@ -147,7 +202,7 @@ def validate_env_resourcefile_resource(self, metadata):
return True, []
return False, errors

def validate_proxy_bundles(self, export_dir):
def validate_proxy_bundles(self, export_dir, api_type):
"""Validates proxy bundles.
Args:
Expand All @@ -158,20 +213,17 @@ def validate_proxy_bundles(self, export_dir):
dict: Validation results for APIs and
sharedflows.
"""
apis = self.target_export_data.get('orgConfig', {}).get('apis', {}).keys() # noqa pylint: disable=C0301
sharedflows = self.target_export_data.get('orgConfig', {}).get('sharedflows', {}).keys() # noqa pylint: disable=C0301
apis_sf_list = {'apis': apis, 'sharedflows': sharedflows}
validation = {'apis': [], 'sharedflows': []}
for each_api_type in ['apis', 'sharedflows']:
bundle_dir = f"{export_dir}/{each_api_type}"
for proxy_bundle in list_dir(bundle_dir):
each_validation = self.validate_proxy(bundle_dir, each_api_type, proxy_bundle) # noqa pylint: disable=C0301
api_name = proxy_bundle.split(".zip")[0]
if api_name in apis_sf_list[each_api_type]:
each_validation['imported'] = True
else:
each_validation['imported'] = False
validation[each_api_type].append(each_validation)
objects = self.target_export_data.get('orgConfig', {}).get(api_type, {}).keys() # noqa pylint: disable=C0301
validation = {api_type: []}
bundle_dir = f"{export_dir}/{api_type}"
for proxy_bundle in list_dir(bundle_dir):
each_validation = self.validate_proxy(bundle_dir, api_type, proxy_bundle) # noqa pylint: disable=C0301
api_name = proxy_bundle.split(".zip")[0]
if api_name in objects:
each_validation['imported'] = True
else:
each_validation['imported'] = False
validation[api_type].append(each_validation)
return validation

@retry()
Expand Down

0 comments on commit e1a6d75

Please sign in to comment.