1 2737f413 2022-06-14 mischa #!/usr/bin/env python3
3 87914c17 2022-06-17 mischa # Copyright 2022, Mischa Peters <mischa AT alkira DOT net>, Alkira.
5 87914c17 2022-06-17 mischa # Version 0.1 - 20220617 - initial release
6 8485fb19 2022-06-21 mischa # Version 0.2 - 20220621 - simplified structure, added -i for IDs only, added -u for ugly JSON
8 87914c17 2022-06-17 mischa # Permission to use, copy, modify, and distribute this software for any
9 87914c17 2022-06-17 mischa # purpose with or without fee is hereby granted, provided that the above
10 87914c17 2022-06-17 mischa # copyright notice and this permission notice appear in all copies.
12 87914c17 2022-06-17 mischa # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 87914c17 2022-06-17 mischa # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 87914c17 2022-06-17 mischa # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 87914c17 2022-06-17 mischa # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 87914c17 2022-06-17 mischa # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 87914c17 2022-06-17 mischa # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 87914c17 2022-06-17 mischa # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 2737f413 2022-06-14 mischa import sys
23 2737f413 2022-06-14 mischa import json
24 2737f413 2022-06-14 mischa import time
25 2737f413 2022-06-14 mischa import logging
26 2737f413 2022-06-14 mischa import requests
27 2737f413 2022-06-14 mischa import configparser
28 87914c17 2022-06-17 mischa import argparse
30 87914c17 2022-06-17 mischa # Parse all arguments
31 465a2620 2022-06-17 mischa parser = argparse.ArgumentParser(description="Get JSON config to AlkiraAPI")
32 d0021f95 2022-06-17 mischa parser.add_argument("-t", "--tenant", type=str, default='alkira.cnf', help="location of alikira.cnf (default: alkira.cnf)")
33 8485fb19 2022-06-21 mischa parser.add_argument("-i", "--id", help="", action="store_true")
34 8485fb19 2022-06-21 mischa parser.add_argument("-u", "--ugly", help="make the JSON ugly!", action="store_true")
35 d0021f95 2022-06-17 mischa parser.add_argument("-v", "--verbose", type=int, default=0, help="Verbose level 0 or 1 (default: 0)")
38 87914c17 2022-06-17 mischa args = parser.parse_args()
39 87914c17 2022-06-17 mischa ALKIRA_CONFIG = args.tenant
40 87914c17 2022-06-17 mischa except argparse.ArgumentError as e:
41 87914c17 2022-06-17 mischa print(str(e))
42 87914c17 2022-06-17 mischa sys.exit()
44 87914c17 2022-06-17 mischa loglevel = {
45 87914c17 2022-06-17 mischa 0: logging.INFO,
46 87914c17 2022-06-17 mischa 1: logging.DEBUG
47 87914c17 2022-06-17 mischa }[args.verbose]
48 87914c17 2022-06-17 mischa except KeyError:
49 87914c17 2022-06-17 mischa loglevel = logging.INFO
51 2737f413 2022-06-14 mischa ###############################################
53 d0021f95 2022-06-17 mischa # Set logging.INFO to logging.DEBUG for debug information
54 d0021f95 2022-06-17 mischa logging.basicConfig(level=loglevel)
55 d0021f95 2022-06-17 mischa logging = logging.getLogger('AlkiraAPI')
57 87914c17 2022-06-17 mischa # Tenant config
58 6861e6d7 2022-06-17 mischa if not os.path.isfile(ALKIRA_CONFIG):
59 6861e6d7 2022-06-17 mischa logging.error(f"The config file {ALKIRA_CONFIG} doesn't exist")
60 2737f413 2022-06-14 mischa sys.exit(1)
61 6861e6d7 2022-06-17 mischa alkira = configparser.RawConfigParser()
62 6861e6d7 2022-06-17 mischa alkira.read(ALKIRA_CONFIG)
64 6861e6d7 2022-06-17 mischa ALKIRA_TENANT = alkira.get('alkira', 'ALKIRA_TENANT')
65 6861e6d7 2022-06-17 mischa ALKIRA_USERNAME = alkira.get('alkira', 'ALKIRA_USERNAME')
66 6861e6d7 2022-06-17 mischa ALKIRA_PASSWORD = alkira.get('alkira', 'ALKIRA_PASSWORD')
67 2737f413 2022-06-14 mischa ALKIRA_BASE_URI = f'https://{ALKIRA_TENANT}/api'
69 2737f413 2022-06-14 mischa ###############################################
71 2737f413 2022-06-14 mischa # Set default headers
72 2737f413 2022-06-14 mischa headers = {'Content-Type': "application/json"}
74 2737f413 2022-06-14 mischa # Naming exceptions
75 2737f413 2022-06-14 mischa service_exceptions = {
76 2737f413 2022-06-14 mischa "saas": "internet",
77 2737f413 2022-06-14 mischa "pan": "panfw",
78 2737f413 2022-06-14 mischa "ftntfw": "ftnt-fw-",
79 2737f413 2022-06-14 mischa "chkpfw": "chkp-fw-"
82 2737f413 2022-06-14 mischa def alkira_login():
83 2737f413 2022-06-14 mischa body = {'userName': ALKIRA_USERNAME,
84 2737f413 2022-06-14 mischa 'password': ALKIRA_PASSWORD}
85 2737f413 2022-06-14 mischa session = requests.session()
86 2737f413 2022-06-14 mischa response = alkira_post(session, '/login', body)
87 2737f413 2022-06-14 mischa return session
89 2737f413 2022-06-14 mischa def alkira_post(session, uri, body):
90 2737f413 2022-06-14 mischa url = f'{ALKIRA_BASE_URI}{uri}'
92 2737f413 2022-06-14 mischa response = session.post(url, data=json.dumps(body), headers=headers)
93 2737f413 2022-06-14 mischa response.raise_for_status()
94 2737f413 2022-06-14 mischa except Exception as e:
95 2737f413 2022-06-14 mischa logging.error(f'Error: {str(e)}')
96 2737f413 2022-06-14 mischa sys.exit(1)
97 2737f413 2022-06-14 mischa return response
99 2737f413 2022-06-14 mischa def alkira_get(session, uri):
100 2737f413 2022-06-14 mischa url = f'{ALKIRA_BASE_URI}{uri}'
102 2737f413 2022-06-14 mischa response = session.get(url, headers=headers)
103 2737f413 2022-06-14 mischa response.raise_for_status()
104 2737f413 2022-06-14 mischa except Exception as e:
105 2737f413 2022-06-14 mischa logging.error(f'Error: {str(e)}')
106 2737f413 2022-06-14 mischa sys.exit(1)
107 2737f413 2022-06-14 mischa return response
109 2737f413 2022-06-14 mischa def alkira_delete(session, uri):
110 2737f413 2022-06-14 mischa url = f'{ALKIRA_BASE_URI}{uri}'
112 2737f413 2022-06-14 mischa response = session.delete(url, headers=headers)
113 2737f413 2022-06-14 mischa response.raise_for_status()
114 2737f413 2022-06-14 mischa except Exception as e:
115 2737f413 2022-06-14 mischa logging.error(f'Error: {str(e)}')
116 2737f413 2022-06-14 mischa sys.exit(1)
117 2737f413 2022-06-14 mischa return response
119 2737f413 2022-06-14 mischa # Authenticate
120 60140d14 2022-06-20 mischa logging.info('=== Authenticating')
121 2737f413 2022-06-14 mischa s = alkira_login()
122 2737f413 2022-06-14 mischa logging.debug(s)
124 2737f413 2022-06-14 mischa # Get TenantID
125 60140d14 2022-06-20 mischa logging.info('=== Fetching Tenant Info')
126 2737f413 2022-06-14 mischa r = alkira_get(s, '/tenantnetworks')
127 2737f413 2022-06-14 mischa data = r.json()
128 2737f413 2022-06-14 mischa tenantNetworkId = data[0]['id']
129 2737f413 2022-06-14 mischa tenantName = data[0]['name']
130 2737f413 2022-06-14 mischa logging.info(f'Tenant Name: {tenantName}')
131 2737f413 2022-06-14 mischa logging.info(f'Tenant ID: {tenantNetworkId}')
133 8485fb19 2022-06-21 mischa # Collecting data with base url: /
134 8485fb19 2022-06-21 mischa collect = [
135 8485fb19 2022-06-21 mischa 'tenantenabledfeatures',
136 8485fb19 2022-06-21 mischa 'credentials'
139 8485fb19 2022-06-21 mischa if not args.id:
140 8485fb19 2022-06-21 mischa for item in collect:
141 8485fb19 2022-06-21 mischa logging.info(f'=== Collecting {item}')
142 8485fb19 2022-06-21 mischa r = alkira_get(s, f'/{item}')
143 8485fb19 2022-06-21 mischa data = r.json()
144 8485fb19 2022-06-21 mischa print(f'# {item}')
145 8485fb19 2022-06-21 mischa if args.ugly:
146 8485fb19 2022-06-21 mischa print(json.dumps(data))
148 8485fb19 2022-06-21 mischa print(json.dumps(data, indent=4))
150 8485fb19 2022-06-21 mischa # Collecting data with base url: /tenantnetworks/{tenantNetworkId}
151 8485fb19 2022-06-21 mischa collect = [
152 8485fb19 2022-06-21 mischa 'connectors',
153 8485fb19 2022-06-21 mischa 'services',
154 1f805065 2022-07-06 mischa 'groups',
155 1f805065 2022-07-06 mischa 'segments',
156 4965908c 2022-06-27 mischa 'alkira-remote-access-connectors',
157 8485fb19 2022-06-21 mischa 'global-cidr-lists'
160 8485fb19 2022-06-21 mischa for item in collect:
161 8485fb19 2022-06-21 mischa logging.info(f'=== Collecting {item}')
162 8485fb19 2022-06-21 mischa r = alkira_get(s, f'/tenantnetworks/{tenantNetworkId}/{item}')
163 8485fb19 2022-06-21 mischa data = r.json()
164 8485fb19 2022-06-21 mischa print(f'# {item}')
165 8485fb19 2022-06-21 mischa if args.id:
166 8485fb19 2022-06-21 mischa for i in data:
167 8485fb19 2022-06-21 mischa str = f"{i['id']:10}"
168 8485fb19 2022-06-21 mischa if 'type' in i:
169 8485fb19 2022-06-21 mischa str = str + (f"\t{i['type']:20}")
171 8485fb19 2022-06-21 mischa if 'name' in i:
172 8485fb19 2022-06-21 mischa str = str + (f"\t{i['name']:20}")
173 8485fb19 2022-06-21 mischa print(str)
175 8485fb19 2022-06-21 mischa if args.ugly:
176 8485fb19 2022-06-21 mischa print(json.dumps(data))
178 8485fb19 2022-06-21 mischa print(json.dumps(data, indent=4))