Blame


1 ba410ee5 2022-07-08 mischa #!/usr/bin/env python3
2 ba410ee5 2022-07-08 mischa #
3 ba410ee5 2022-07-08 mischa # Copyright 2022, Mischa Peters <mischa AT alkira DOT net>, Alkira.
4 ba410ee5 2022-07-08 mischa # cxp-wip.py
5 ba410ee5 2022-07-08 mischa #
6 ba410ee5 2022-07-08 mischa # Permission to use, copy, modify, and distribute this software for any
7 ba410ee5 2022-07-08 mischa # purpose with or without fee is hereby granted, provided that the above
8 ba410ee5 2022-07-08 mischa # copyright notice and this permission notice appear in all copies.
9 ba410ee5 2022-07-08 mischa #
10 ba410ee5 2022-07-08 mischa # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 ba410ee5 2022-07-08 mischa # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 ba410ee5 2022-07-08 mischa # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ba410ee5 2022-07-08 mischa # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 ba410ee5 2022-07-08 mischa # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ba410ee5 2022-07-08 mischa # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 ba410ee5 2022-07-08 mischa # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 ba410ee5 2022-07-08 mischa #
18 ba410ee5 2022-07-08 mischa import os
19 ba410ee5 2022-07-08 mischa import sys
20 ba410ee5 2022-07-08 mischa import re
21 ba410ee5 2022-07-08 mischa import json
22 ba410ee5 2022-07-08 mischa import time
23 ba410ee5 2022-07-08 mischa import logging
24 ba410ee5 2022-07-08 mischa import requests
25 ba410ee5 2022-07-08 mischa import configparser
26 ba410ee5 2022-07-08 mischa
27 ba410ee5 2022-07-08 mischa ###############################################
28 ba410ee5 2022-07-08 mischa
29 ba410ee5 2022-07-08 mischa CONFIG_FILE = "/Users/mischa/Alkira/xDev/alkira.cnf"
30 ba410ee5 2022-07-08 mischa if not os.path.isfile(CONFIG_FILE):
31 ba410ee5 2022-07-08 mischa logging.error(f"The config file {CONFIG_FILE} doesn't exist")
32 ba410ee5 2022-07-08 mischa sys.exit(1)
33 ba410ee5 2022-07-08 mischa config = configparser.RawConfigParser()
34 ba410ee5 2022-07-08 mischa config.read(CONFIG_FILE)
35 ba410ee5 2022-07-08 mischa
36 ba410ee5 2022-07-08 mischa ALKIRA_TENANT = config.get('alkira', 'ALKIRA_TENANT')
37 ba410ee5 2022-07-08 mischa ALKIRA_USERNAME = config.get('alkira', 'ALKIRA_USERNAME')
38 ba410ee5 2022-07-08 mischa ALKIRA_PASSWORD = config.get('alkira', 'ALKIRA_PASSWORD')
39 ba410ee5 2022-07-08 mischa ALKIRA_BASE_URI = f'https://{ALKIRA_TENANT}/api'
40 ba410ee5 2022-07-08 mischa
41 ba410ee5 2022-07-08 mischa ###############################################
42 ba410ee5 2022-07-08 mischa
43 ba410ee5 2022-07-08 mischa # Set default headers
44 ba410ee5 2022-07-08 mischa headers = {'Content-Type': "application/json"}
45 ba410ee5 2022-07-08 mischa
46 ba410ee5 2022-07-08 mischa # Set logging.INFO to logging.DEBUG for debug information
47 ba410ee5 2022-07-08 mischa logging.basicConfig(level=logging.DEBUG)
48 ba410ee5 2022-07-08 mischa logging = logging.getLogger('AlkiraAPI')
49 ba410ee5 2022-07-08 mischa
50 ba410ee5 2022-07-08 mischa def alkira_login():
51 ba410ee5 2022-07-08 mischa body = {'userName': ALKIRA_USERNAME,
52 ba410ee5 2022-07-08 mischa 'password': ALKIRA_PASSWORD}
53 ba410ee5 2022-07-08 mischa session = requests.session()
54 ba410ee5 2022-07-08 mischa response = alkira_post(session, '/login', body)
55 ba410ee5 2022-07-08 mischa return session
56 ba410ee5 2022-07-08 mischa
57 ba410ee5 2022-07-08 mischa def alkira_post(session, uri, body):
58 ba410ee5 2022-07-08 mischa url = f'{ALKIRA_BASE_URI}{uri}'
59 ba410ee5 2022-07-08 mischa try:
60 ba410ee5 2022-07-08 mischa response = session.post(url, data=json.dumps(body), headers=headers)
61 ba410ee5 2022-07-08 mischa match int(str(response.status_code)[0]):
62 ba410ee5 2022-07-08 mischa case 4:
63 ba410ee5 2022-07-08 mischa logging.error(response.content)
64 ba410ee5 2022-07-08 mischa response.raise_for_status()
65 ba410ee5 2022-07-08 mischa except Exception as e:
66 ba410ee5 2022-07-08 mischa logging.error(f'Error: {str(e)}')
67 ba410ee5 2022-07-08 mischa sys.exit(1)
68 ba410ee5 2022-07-08 mischa return response
69 ba410ee5 2022-07-08 mischa
70 ba410ee5 2022-07-08 mischa def alkira_put(session, uri, body):
71 ba410ee5 2022-07-08 mischa url = f'{ALKIRA_BASE_URI}{uri}'
72 ba410ee5 2022-07-08 mischa try:
73 ba410ee5 2022-07-08 mischa response = session.put(url, data=json.dumps(body), headers=headers)
74 ba410ee5 2022-07-08 mischa match int(str(response.status_code)[0]):
75 ba410ee5 2022-07-08 mischa case 4:
76 ba410ee5 2022-07-08 mischa logging.error(response.content)
77 ba410ee5 2022-07-08 mischa response.raise_for_status()
78 ba410ee5 2022-07-08 mischa except Exception as e:
79 ba410ee5 2022-07-08 mischa logging.error(f'Error: {str(e)}')
80 ba410ee5 2022-07-08 mischa sys.exit(1)
81 ba410ee5 2022-07-08 mischa return response
82 ba410ee5 2022-07-08 mischa
83 ba410ee5 2022-07-08 mischa def alkira_get(session, uri):
84 ba410ee5 2022-07-08 mischa url = f'{ALKIRA_BASE_URI}{uri}'
85 ba410ee5 2022-07-08 mischa try:
86 ba410ee5 2022-07-08 mischa response = session.get(url, headers=headers)
87 ba410ee5 2022-07-08 mischa response.raise_for_status()
88 ba410ee5 2022-07-08 mischa except Exception as e:
89 ba410ee5 2022-07-08 mischa logging.error(f'Error: {str(e)}')
90 ba410ee5 2022-07-08 mischa sys.exit(1)
91 ba410ee5 2022-07-08 mischa return response
92 ba410ee5 2022-07-08 mischa
93 ba410ee5 2022-07-08 mischa def alkira_delete(session, uri):
94 ba410ee5 2022-07-08 mischa url = f'{ALKIRA_BASE_URI}{uri}'
95 ba410ee5 2022-07-08 mischa try:
96 ba410ee5 2022-07-08 mischa response = session.delete(url, headers=headers)
97 ba410ee5 2022-07-08 mischa response.raise_for_status()
98 ba410ee5 2022-07-08 mischa except Exception as e:
99 ba410ee5 2022-07-08 mischa logging.error(f'Error: {str(e)}')
100 ba410ee5 2022-07-08 mischa sys.exit(1)
101 ba410ee5 2022-07-08 mischa return response
102 ba410ee5 2022-07-08 mischa
103 ba410ee5 2022-07-08 mischa # Authenticate
104 ba410ee5 2022-07-08 mischa logging.info('=== Authenticating')
105 ba410ee5 2022-07-08 mischa s = alkira_login()
106 ba410ee5 2022-07-08 mischa logging.debug(s)
107 ba410ee5 2022-07-08 mischa
108 ba410ee5 2022-07-08 mischa # Get TenantID
109 ba410ee5 2022-07-08 mischa logging.info('=== Fetching Tenant Info')
110 ba410ee5 2022-07-08 mischa r = alkira_get(s, '/tenantnetworks')
111 ba410ee5 2022-07-08 mischa data = r.json()
112 ba410ee5 2022-07-08 mischa tenantNetworkId = data[0]['id']
113 ba410ee5 2022-07-08 mischa tenantName = data[0]['name']
114 ba410ee5 2022-07-08 mischa logging.info(f'Tenant Name: {tenantName}')
115 ba410ee5 2022-07-08 mischa logging.info(f'Tenant ID: {tenantNetworkId}')
116 ba410ee5 2022-07-08 mischa
117 ba410ee5 2022-07-08 mischa body = {"name": tenantName, "cxps": {"name": "US-EAST2"}}
118 ba410ee5 2022-07-08 mischa r = alkira_post(s, f'/tenantnetworks/{tenantNetworkId}', body)
119 ba410ee5 2022-07-08 mischa print(r.status_code)
120 ba410ee5 2022-07-08 mischa print(r.content)