commit ba410ee5861e537d0e4ab7e9acf1536d143dc101 from: mischa date: Fri Jul 08 21:57:34 2022 UTC adding cxp-wip.py, work in progress to create CXPs commit - e8b8de7ca0bc8bcc141ac043054779f78a369a8d commit + ba410ee5861e537d0e4ab7e9acf1536d143dc101 blob - /dev/null blob + 6dcb6a85ed3912ce3e0e9e56235308ffd6631f35 (mode 755) --- /dev/null +++ cxp-wip.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python3 +# +# Copyright 2022, Mischa Peters , Alkira. +# cxp-wip.py +# +# 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. +# +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.DEBUG) +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) + match int(str(response.status_code)[0]): + case 4: + logging.error(response.content) + response.raise_for_status() + except Exception as e: + logging.error(f'Error: {str(e)}') + sys.exit(1) + return response + +def alkira_put(session, uri, body): + url = f'{ALKIRA_BASE_URI}{uri}' + try: + response = session.put(url, data=json.dumps(body), headers=headers) + match int(str(response.status_code)[0]): + case 4: + logging.error(response.content) + 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 +logging.info('=== Authenticating') +s = alkira_login() +logging.debug(s) + +# Get TenantID +logging.info('=== Fetching Tenant Info') +r = alkira_get(s, '/tenantnetworks') +data = r.json() +tenantNetworkId = data[0]['id'] +tenantName = data[0]['name'] +logging.info(f'Tenant Name: {tenantName}') +logging.info(f'Tenant ID: {tenantNetworkId}') + +body = {"name": tenantName, "cxps": {"name": "US-EAST2"}} +r = alkira_post(s, f'/tenantnetworks/{tenantNetworkId}', body) +print(r.status_code) +print(r.content) blob - 35caf4492b3a503e6206fe3b1de114db81de891d blob + 6b17b02bfa2b4a1c15fb5eb6f277b517ff2d6c79 --- users-wip.py +++ users-wip.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # # Copyright 2022, Mischa Peters , Alkira. -# wip-users.py +# users-wip.py # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above