reworked configfile parsing and setting of JSON

This commit is contained in:
mischa 2022-06-17 14:56:32 +02:00
parent 8dccb11da5
commit af1fc945e2
3 changed files with 32 additions and 27 deletions

View File

@ -1,7 +1,6 @@
{ {
"name": "SanJose", "name": "SanJose",
"cxp": "US-EAST-2", "cxp": "US-EAST-2",
"cxpId": "5ada678d-ce4a-428a-ac8d-0dc636b72b10",
"type": "IP_SEC", "type": "IP_SEC",
"group": "Users", "group": "Users",
"segments": [ "segments": [
@ -13,7 +12,6 @@
"customerGwAsn": "60131" "customerGwAsn": "60131"
} }
}, },
"groupType": "USER",
"sites": [ "sites": [
{ {
"name": "PAN", "name": "PAN",

View File

@ -1,7 +1,6 @@
{ {
"name": "Amsterdam", "name": "Amsterdam",
"cxp": "US-EAST-2", "cxp": "US-EAST-2",
"cxpId": "5ada678d-ce4a-428a-ac8d-0dc636b72b10",
"type": "IP_SEC", "type": "IP_SEC",
"group": "Users", "group": "Users",
"segments": [ "segments": [
@ -13,7 +12,6 @@
"customerGwAsn": "60131" "customerGwAsn": "60131"
} }
}, },
"groupType": "USER",
"sites": [ "sites": [
{ {
"name": "PAN", "name": "PAN",

55
push.py
View File

@ -126,22 +126,6 @@ logging.info('Push Connectors')
for connector in config.sections(): for connector in config.sections():
section = config[connector] section = config[connector]
region = section['cxp']
segments = section['segments']
logging.debug(f'{section} - {region} - {segments}')
if 'region' in config:
region = section['region']
logging.debug(region)
if 'segments' in config:
segments = section['segments']
logging.debug(segments)
if 'group' in config:
group = section['group']
logging.debug(group)
if 'billingtags' in config:
billingtags = section['billingtags']
logging.debug(billingtags)
connector_result = re.match(r'(\w+)(\d+)', connector) connector_result = re.match(r'(\w+)(\d+)', connector)
connector_name = connector_result.group(1) connector_name = connector_result.group(1)
connector_number = connector_result.group(2) connector_number = connector_result.group(2)
@ -149,16 +133,41 @@ for connector in config.sections():
config_path = (f'{connector_folder}/{connector_name}{connector_number}.txt') config_path = (f'{connector_folder}/{connector_name}{connector_number}.txt')
with open (config_path, 'r') as f: with open (config_path, 'r') as f:
body = json.load(f) body = json.load(f)
body['cxp'] = region
body['segments'][0] = segments
if 'group' in body and 'group' in globals():
body['group'] = group
if 'billingTags' in body and 'billingtags' in globals(): if 'cxp' in body:
body['billingTags'][0] = billingtags cxp = body['cxp']
logging.debug(f'JSON cxp: {cxp}')
if 'cxp' in section:
cxp = section['cxp']
logging.debug(f'CONFIG cxp: {cxp}')
body['cxp'] = cxp
if 'segments' in body:
segments = body['segments'][0]
logging.debug(f'JSON segments: {segments}')
if 'segments' in section:
segments = section['segments']
logging.debug(f'CONFIG segments: {segments}')
body['segments'][0] = segments
if 'group' in body:
group = body['group']
logging.debug(f'JSON group: {group}')
if 'group' in section:
group = section['group']
logging.debug(f'CONFIG group: {group}')
body['group'] = group
if 'billingTags' in body:
billingtags = body['billingTags'][0]
logging.debug(f'JSON billingtags: {billingtags}')
if 'billingtags' in section:
billingtags = section['billingtags']
logging.debug(f'CONFIG billingtags: {billingtags}')
body['billingTags'][0] = billingtags
logging.debug(json.dumps(body)) logging.debug(json.dumps(body))
logging.info(f'Pushing {connector_name} to {region} (network segment: {segments})') logging.info(f'Pushing {connector_name} to {cxp} (network segment: {segments}; group: {group})')
r = alkira_post(s, f'/tenantnetworks/{tenantNetworkId}/{connector_name}', body) r = alkira_post(s, f'/tenantnetworks/{tenantNetworkId}/{connector_name}', body)
logging.info(r.status_code) logging.info(r.status_code)
logging.debug(r.content) logging.debug(r.content)