From a7583fef3c30614ed2dddff027b3fe317d13eca4 Mon Sep 17 00:00:00 2001 From: mischa Date: Tue, 14 Jun 2022 21:17:47 +0200 Subject: [PATCH] added Global CIDR and dict for exceptions --- api-03.py | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ clean.py | 40 +++++++++++++----- 2 files changed, 147 insertions(+), 11 deletions(-) create mode 100755 api-03.py diff --git a/api-03.py b/api-03.py new file mode 100755 index 0000000..e3c0627 --- /dev/null +++ b/api-03.py @@ -0,0 +1,118 @@ +#!/usr/bin/env python3 + +import os +import sys +import re +import json +import time +import logging +import requests +import configparser + +############################################### + +CONFIG_FILE = "/Users/mischa/Alkira/xDev/alkira.cnf" +if not os.path.isfile(CONFIG_FILE): + logging.error(f"The config file {CONFIG_FILE} doesn't exist") + sys.exit(1) +config = configparser.RawConfigParser() +config.read(CONFIG_FILE) + +ALKIRA_TENANT = config.get('alkira', 'ALKIRA_TENANT') +ALKIRA_USERNAME = config.get('alkira', 'ALKIRA_USERNAME') +ALKIRA_PASSWORD = config.get('alkira', 'ALKIRA_PASSWORD') +ALKIRA_BASE_URI = f'https://{ALKIRA_TENANT}/api' + +############################################### + +# Set default headers +headers = {'Content-Type': "application/json"} + +# Set logging.INFO to logging.DEBUG for debug information +logging.basicConfig(level=logging.INFO) +logging = logging.getLogger('AlkiraAPI') + +def alkira_login(): + body = {'userName': ALKIRA_USERNAME, + 'password': ALKIRA_PASSWORD} + session = requests.session() + response = alkira_post(session, '/login', body) + return session + +def alkira_post(session, uri, body): + url = f'{ALKIRA_BASE_URI}{uri}' + try: + response = session.post(url, data=json.dumps(body), headers=headers) + response.raise_for_status() + except Exception as e: + logging.error(f'Error: {str(e)}') + sys.exit(1) + return response + +def alkira_get(session, uri): + url = f'{ALKIRA_BASE_URI}{uri}' + try: + response = session.get(url, headers=headers) + response.raise_for_status() + except Exception as e: + logging.error(f'Error: {str(e)}') + sys.exit(1) + return response + +def alkira_delete(session, uri): + url = f'{ALKIRA_BASE_URI}{uri}' + try: + response = session.delete(url, headers=headers) + response.raise_for_status() + except Exception as e: + logging.error(f'Error: {str(e)}') + sys.exit(1) + return response + +# Authenticate +s = alkira_login() +logging.debug(s) + +# Get TenantID +r = alkira_get(s, '/tenantnetworks') +data = r.json() +print('Tenant Name:', data[0]['name']) +print('Tenant ID:', data[0]['id']) +tenantNetworkId = data[0]['id'] + +r = alkira_get(s, f'/tenantnetworks/{tenantNetworkId}/connectors') +data = r.json() +#print(json.dumps(data, indent=4)) +for item in data: + #print("ID:", item.get('id')) + #print("Name:", item.get('name')) + #print("Type:", item.get('type')) + name = item.get('name') + connectorId = item.get('id') + type = item.get('type').lower().replace('_', '') + if (type == 'saas'): + type = 'internet' + #print(f'/tenantnetworks/{tenantNetworkId}/{type}connectors/{connectorId}') + logging.info(f'Removing {name}') + r = alkira_delete(s, f'/tenantnetworks/{tenantNetworkId}/{type}connectors/{connectorId}') + logging.info(r.status_code) + +r = alkira_get(s, f'/tenantnetworks/{tenantNetworkId}/services') +data = r.json() +#print(json.dumps(data, indent=4)) +for item in data: + #print("ID:", item.get('id')) + #print("Name:", item.get('name')) + ##print("Type:", item.get('type')) + name = item.get('name') + serviceId = item.get('id') + type = item.get('type').lower() + if (type == 'pan'): + type = 'panfw' + if (type == 'ftntfw'): + type = 'ftnt-fw-' + #print(f'/tenantnetworks/{tenantNetworkId}/{type}services/{serviceId}') + logging.info(f'Removing {name}') + r = alkira_delete(s, f'/tenantnetworks/{tenantNetworkId}/{type}services/{serviceId}') + logging.info(r.status_code) + diff --git a/clean.py b/clean.py index e3c0627..c7c589c 100755 --- a/clean.py +++ b/clean.py @@ -73,6 +73,13 @@ def alkira_delete(session, uri): s = alkira_login() logging.debug(s) +ducks = { + "saas": "internet", + "pan": "panfw", + "ftntfw": "ftnt-fw-", + "chkpfw": "chkp-fw-" + } + # Get TenantID r = alkira_get(s, '/tenantnetworks') data = r.json() @@ -82,7 +89,7 @@ tenantNetworkId = data[0]['id'] r = alkira_get(s, f'/tenantnetworks/{tenantNetworkId}/connectors') data = r.json() -#print(json.dumps(data, indent=4)) +logging.debug(json.dumps(data, indent=4)) for item in data: #print("ID:", item.get('id')) #print("Name:", item.get('name')) @@ -90,29 +97,40 @@ for item in data: name = item.get('name') connectorId = item.get('id') type = item.get('type').lower().replace('_', '') - if (type == 'saas'): - type = 'internet' - #print(f'/tenantnetworks/{tenantNetworkId}/{type}connectors/{connectorId}') + if type in ducks.keys(): + type = ducks[type] + logging.debug(f'/tenantnetworks/{tenantNetworkId}/{type}connectors/{connectorId}') logging.info(f'Removing {name}') r = alkira_delete(s, f'/tenantnetworks/{tenantNetworkId}/{type}connectors/{connectorId}') logging.info(r.status_code) r = alkira_get(s, f'/tenantnetworks/{tenantNetworkId}/services') data = r.json() -#print(json.dumps(data, indent=4)) +logging.debug(json.dumps(data, indent=4)) for item in data: #print("ID:", item.get('id')) #print("Name:", item.get('name')) - ##print("Type:", item.get('type')) + #print("Type:", item.get('type')) name = item.get('name') serviceId = item.get('id') type = item.get('type').lower() - if (type == 'pan'): - type = 'panfw' - if (type == 'ftntfw'): - type = 'ftnt-fw-' - #print(f'/tenantnetworks/{tenantNetworkId}/{type}services/{serviceId}') + if type in ducks.keys(): + type = ducks[type] + logging.debug(f'/tenantnetworks/{tenantNetworkId}/{type}services/{serviceId}') logging.info(f'Removing {name}') r = alkira_delete(s, f'/tenantnetworks/{tenantNetworkId}/{type}services/{serviceId}') logging.info(r.status_code) +r = alkira_get(s, f'/tenantnetworks/{tenantNetworkId}/global-cidr-lists') +data = r.json() +logging.debug(json.dumps(data, indent=4)) +for item in data: + #print("ID:", item.get('id')) + #print("Name:", item.get('name')) + name = item.get('name') + GlobalCidrListId = item.get('id') + logging.debug(f'/tenantnetworks/{tenantNetworkId}/global-cidr-lists/{GlobalCidrListId}') + logging.info(f'Removing {name}') + r = alkira_delete(s, f'/tenantnetworks/{tenantNetworkId}/global-cidr-lists/{GlobalCidrListId}') + logging.info(r.status_code) +