2022-06-15 10:43:29 +02:00
|
|
|
#!/usr/bin/env python3
|
2022-06-17 15:45:20 +02:00
|
|
|
#
|
|
|
|
# Copyright 2022, Mischa Peters <mischa AT alkira DOT net>, Alkira.
|
|
|
|
# push-debug.py
|
|
|
|
# Version 0.1 - 20220617 - initial release
|
|
|
|
#
|
|
|
|
# Permission to use, copy, modify, and distribute this software for any
|
|
|
|
# purpose with or without fee is hereby granted, provided that the above
|
|
|
|
# copyright notice and this permission notice appear in all copies.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
#
|
2022-06-15 10:43:29 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
import json
|
|
|
|
import time
|
|
|
|
import logging
|
|
|
|
import requests
|
|
|
|
import configparser
|
2022-06-17 15:36:07 +02:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
# Parse all arguments
|
2022-06-17 16:16:41 +02:00
|
|
|
parser = argparse.ArgumentParser(description="Push JSON config to AlkiraAPI (debug)")
|
2022-06-17 16:01:23 +02:00
|
|
|
parser.add_argument("-t", "--tenant", type=str, default='alkira.cnf', help="location of alikira.cnf (default: alkira.cnfi)")
|
2022-06-17 15:36:07 +02:00
|
|
|
parser.add_argument("-f", "--file", type=str, help="location of the JSON connector file")
|
|
|
|
parser.add_argument("-p", "--pretty", help="make the JSON pretty!", action="store_true")
|
2022-06-17 16:01:23 +02:00
|
|
|
parser.add_argument("-v", "--verbose", type=int, default=0, help="Verbose level 0 or 1 (default: 0)")
|
|
|
|
|
2022-06-17 15:36:07 +02:00
|
|
|
if len(sys.argv)==1:
|
|
|
|
parser.print_help(sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
try:
|
|
|
|
args = parser.parse_args()
|
|
|
|
ALKIRA_CONFIG = args.tenant
|
|
|
|
json_file = args.file
|
|
|
|
|
|
|
|
except argparse.ArgumentError as e:
|
|
|
|
print(str(e))
|
|
|
|
sys.exit()
|
2022-06-15 10:43:29 +02:00
|
|
|
|
2022-06-17 16:01:23 +02:00
|
|
|
try:
|
|
|
|
loglevel = {
|
|
|
|
0: logging.INFO,
|
|
|
|
1: logging.DEBUG
|
|
|
|
}[args.verbose]
|
|
|
|
except KeyError:
|
|
|
|
loglevel = logging.INFO
|
|
|
|
|
2022-06-15 10:43:29 +02:00
|
|
|
###############################################
|
|
|
|
|
2022-06-17 16:01:23 +02:00
|
|
|
# Set loglevel (logging.INFO, logging.DEBUG)
|
|
|
|
logging.basicConfig(level=loglevel)
|
|
|
|
logging = logging.getLogger('AlkiraAPI')
|
|
|
|
|
2022-06-17 15:36:07 +02:00
|
|
|
# Tenant config
|
2022-06-17 12:45:57 +02:00
|
|
|
if not os.path.isfile(ALKIRA_CONFIG):
|
|
|
|
logging.error(f"The config file {ALKIRA_CONFIG} doesn't exist")
|
2022-06-15 10:43:29 +02:00
|
|
|
sys.exit(1)
|
2022-06-17 12:45:57 +02:00
|
|
|
alkira = configparser.RawConfigParser()
|
2022-06-17 14:07:23 +02:00
|
|
|
alkira.read(ALKIRA_CONFIG)
|
2022-06-15 10:43:29 +02:00
|
|
|
|
2022-06-17 12:45:57 +02:00
|
|
|
ALKIRA_TENANT = alkira.get('alkira', 'ALKIRA_TENANT')
|
|
|
|
ALKIRA_USERNAME = alkira.get('alkira', 'ALKIRA_USERNAME')
|
|
|
|
ALKIRA_PASSWORD = alkira.get('alkira', 'ALKIRA_PASSWORD')
|
2022-06-15 10:43:29 +02:00
|
|
|
ALKIRA_BASE_URI = f'https://{ALKIRA_TENANT}/api'
|
|
|
|
|
|
|
|
###############################################
|
|
|
|
|
|
|
|
# Set default headers
|
|
|
|
headers = {'Content-Type': "application/json"}
|
|
|
|
|
2022-06-17 18:18:03 +02:00
|
|
|
# Naming exceptions
|
|
|
|
service_exceptions = {
|
|
|
|
"saas": "internet",
|
|
|
|
"pan": "panfw",
|
|
|
|
"ftntfw": "ftnt-fw-",
|
|
|
|
"chkpfw": "chkp-fw-",
|
|
|
|
"ocivcnconnectors": "oci-vcn-connectors"
|
|
|
|
}
|
|
|
|
|
2022-06-15 10:43:29 +02:00
|
|
|
# Authenticate
|
|
|
|
body = {'userName': ALKIRA_USERNAME,
|
|
|
|
'password': ALKIRA_PASSWORD}
|
|
|
|
url = f'{ALKIRA_BASE_URI}/login'
|
|
|
|
session = requests.session()
|
|
|
|
response = session.post(url, data=json.dumps(body), headers=headers)
|
|
|
|
|
|
|
|
# Get TenantID
|
|
|
|
url = f'{ALKIRA_BASE_URI}/tenantnetworks'
|
|
|
|
response = session.get(url, headers=headers)
|
|
|
|
data = response.json()
|
|
|
|
tenantNetworkId = data[0]['id']
|
|
|
|
tenantName = data[0]['name']
|
|
|
|
logging.info(f'Tenant Name: {tenantName}')
|
|
|
|
logging.info(f'Tenant ID: {tenantNetworkId}')
|
|
|
|
|
2022-06-17 15:36:07 +02:00
|
|
|
print(json_file)
|
|
|
|
|
2022-06-15 10:43:29 +02:00
|
|
|
# Do Things
|
2022-06-17 18:18:03 +02:00
|
|
|
connector_result = re.match(r'(\w+)(\d+)', json_file)
|
2022-06-17 15:36:07 +02:00
|
|
|
connector_name = connector_result.group(1)
|
|
|
|
connector_number = connector_result.group(2)
|
2022-06-17 18:22:29 +02:00
|
|
|
logging.debug(f'Connector Name: {connector_name} - Number: {connector_number}')
|
2022-06-17 15:36:07 +02:00
|
|
|
with open (json_file, 'r') as f:
|
2022-06-15 10:43:29 +02:00
|
|
|
body = json.load(f)
|
2022-06-17 15:36:07 +02:00
|
|
|
if args.pretty:
|
|
|
|
print(json.dumps(body, indent=4))
|
|
|
|
else:
|
|
|
|
print(json.dumps(body))
|
|
|
|
|
2022-06-17 18:18:03 +02:00
|
|
|
if connector_name in service_exceptions.keys():
|
|
|
|
connector_name = service_exceptions[connector_name]
|
2022-06-17 15:36:07 +02:00
|
|
|
url = f'{ALKIRA_BASE_URI}/tenantnetworks/{tenantNetworkId}/{connector_name}'
|
2022-06-15 10:43:29 +02:00
|
|
|
response = session.post(url, data=json.dumps(body), headers=headers)
|
|
|
|
print(response.status_code)
|
|
|
|
print(response.content)
|