cisco_fmc_api_via_excel_operations
/
put_func.py
409 строк · 20.4 Кб
1#!/usr/bin/python3
2import cfg
3import json
4import requests
5from datetime import datetime
6import logging
7from get_func import get_domain_uuid, get_domain_name_by_uuid, api_call_counter, check_token_status
8
9
10def post_network_objects(domain_data, domains_uuid):
11fmc_ip = cfg.fmc_ip
12headers_json = cfg.headers_json
13
14logging.info(f'\n {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Starting to add network objects in domain {get_domain_name_by_uuid(domains_uuid)}\n')
15try:
16check_token_status()
17api_call_counter()
18resp = requests.post(
19f"https://{fmc_ip}/api/fmc_config/v1/domain/{domains_uuid}/object/networks?bulk=true",
20headers=headers_json, data=json.dumps(domain_data), verify=False)
21# time.sleep(0.7)
22# logging.info(f'request_body:\n{resp.request.body}')
23# logging.info(f'Headers: {str(resp.headers)}\n')
24# logging.info(f'Text: {str(resp.text)}\n')
25logging.info(
26f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Network objects add operation in domain {get_domain_name_by_uuid(domains_uuid)} status Code: {str(resp.status_code)}\n')
27if not str(resp.status_code).startswith('2'):
28errors_filename = 'outputs/errors.txt'
29with open(errors_filename, "a") as f:
30f.write(f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Status code: {resp.status_code}\n Request.body: {resp.request.body}\n Request text: {resp.text}\n')
31except requests.exceptions.HTTPError as errh:
32logging.info(f'{errh}')
33raise SystemExit(errh)
34except requests.exceptions.RequestException as err:
35logging.info(f'{err}')
36raise SystemExit(err)
37logging.info(f'\n{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Finished adding network objects in domain {get_domain_name_by_uuid(domains_uuid)}\n')
38
39
40def post_range_objects(domain_data, domains_uuid):
41fmc_ip = cfg.fmc_ip
42headers_json = cfg.headers_json
43
44logging.info(f'\n {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Starting to add range objects in domain {get_domain_name_by_uuid(domains_uuid)}\n')
45try:
46check_token_status()
47api_call_counter()
48resp = requests.post(
49f"https://{fmc_ip}/api/fmc_config/v1/domain/{domains_uuid}/object/ranges?bulk=true",
50headers=headers_json, data=json.dumps(domain_data), verify=False)
51# time.sleep(0.7)
52# logging.info(f'request_body:\n{resp.request.body}')
53# logging.info(f'Headers: {str(resp.headers)}\n')
54# logging.info(f'Text: {str(resp.text)}\n')
55logging.info(
56f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} range objects add operation in domain {get_domain_name_by_uuid(domains_uuid)} status Code: {str(resp.status_code)}\n')
57if not str(resp.status_code).startswith('2'):
58errors_filename = 'outputs/errors.txt'
59with open(errors_filename, "a") as f:
60f.write(f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Status code: {resp.status_code}\n Request.body: {resp.request.body}\n Request text: {resp.text}\n')
61except requests.exceptions.HTTPError as errh:
62logging.info(f'{errh}')
63raise SystemExit(errh)
64except requests.exceptions.RequestException as err:
65logging.info(f'{err}')
66raise SystemExit(err)
67logging.info(f'\n{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Finished adding range objects in domain {get_domain_name_by_uuid(domains_uuid)}\n')
68
69
70def post_host_objects(domain_data, domains_uuid):
71fmc_ip = cfg.fmc_ip
72headers_json = cfg.headers_json
73
74logging.info(f'\n {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Starting to add host objects in domain {get_domain_name_by_uuid(domains_uuid)}\n')
75try:
76check_token_status()
77api_call_counter()
78resp = requests.post(
79f"https://{fmc_ip}/api/fmc_config/v1/domain/{domains_uuid}/object/hosts?bulk=true",
80headers=headers_json, data=json.dumps(domain_data), verify=False)
81# time.sleep(0.7)
82# logging.info(f'request_body:\n{resp.request.body}')
83# logging.info(f'Headers: {str(resp.headers)}\n')
84# logging.info(f'Text: {str(resp.text)}\n')
85logging.info(f' {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Host objects add operation in domain {get_domain_name_by_uuid(domains_uuid)} status Code: {str(resp.status_code)}\n')
86if not str(resp.status_code).startswith('2'):
87errors_filename = 'outputs/errors.txt'
88with open(errors_filename, "a") as f:
89f.write(
90f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Status code: {resp.status_code}\n Request.body: {resp.request.body}\n Request text: {resp.text}\n')
91except requests.exceptions.HTTPError as errh:
92logging.info(f'{errh}')
93raise SystemExit(errh)
94except requests.exceptions.RequestException as err:
95logging.info(f'{err}')
96raise SystemExit(err)
97logging.info(f'\n{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Finished adding host objests in domain {get_domain_name_by_uuid(domains_uuid)}\n')
98
99
100def post_url_objects(domain_data, domains_uuid):
101fmc_ip = cfg.fmc_ip
102headers_json = cfg.headers_json
103
104logging.info(f'\n {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Starting to add url objects in domain {get_domain_name_by_uuid(domains_uuid)}\n')
105try:
106check_token_status()
107api_call_counter()
108resp = requests.post(
109f"https://{fmc_ip}/api/fmc_config/v1/domain/{domains_uuid}/object/urls?bulk=true",
110headers=headers_json, data=json.dumps(domain_data), verify=False)
111# time.sleep(0.7)
112# logging.info(f'request_body:\n{resp.request.body}')
113# logging.info(f'Headers: {str(resp.headers)}\n')
114# logging.info(f'Text: {str(resp.text)}\n')
115logging.info(
116f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Url objects add operation in domain {get_domain_name_by_uuid(domains_uuid)} status Code: {str(resp.status_code)}\n')
117if not str(resp.status_code).startswith('2'):
118errors_filename = 'outputs/errors.txt'
119with open(errors_filename, "a") as f:
120f.write(f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Status code: {resp.status_code}\n Request.body: {resp.request.body}\n Request text: {resp.text}\n')
121except requests.exceptions.HTTPError as errh:
122logging.info(f'{errh}')
123raise SystemExit(errh)
124except requests.exceptions.RequestException as err:
125logging.info(f'{err}')
126raise SystemExit(err)
127logging.info(f'\n{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Finished adding url objects in domain {get_domain_name_by_uuid(domains_uuid)}\n')
128
129
130def post_groups_objects(domain_data, domain_uuid):
131fmc_ip = cfg.fmc_ip
132headers_json = cfg.headers_json
133
134logging.info(f'\n {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Starting to add POST group in domain {get_domain_name_by_uuid(domain_uuid)}\n')
135try:
136check_token_status()
137api_call_counter()
138resp = requests.post(
139f"https://{fmc_ip}/api/fmc_config/v1/domain/{domain_uuid}/object/networkgroups?bulk=true",
140headers=headers_json, data=json.dumps(domain_data), verify=False)
141# time.sleep(0.7)
142logging.info(
143f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} POST group ADD status Code: {str(resp.status_code)}\n')
144if not str(resp.status_code).startswith('2'):
145errors_filename = 'outputs/errors.txt'
146with open(errors_filename, "a") as f:
147f.write(f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Status code: {resp.status_code}\n Request.body: {resp.request.body}\n Request text: {resp.text}\n')
148except requests.exceptions.HTTPError as errh:
149logging.info(f'{errh}')
150raise SystemExit(errh)
151except requests.exceptions.RequestException as err:
152logging.info(f'{err}')
153raise SystemExit(err)
154logging.info(f'\n{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Finished adding POST group in domain {get_domain_name_by_uuid(domain_uuid)}"\n')
155
156
157def post_urlgroups_objects(domain_data, domain_uuid):
158fmc_ip = cfg.fmc_ip
159headers_json = cfg.headers_json
160
161logging.info(
162f'\n {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Starting to add POST group in domain {get_domain_name_by_uuid(domain_uuid)}\n')
163try:
164check_token_status()
165api_call_counter()
166resp = requests.post(
167f"https://{fmc_ip}/api/fmc_config/v1/domain/{domain_uuid}/object/urlgroups?bulk=true",
168headers=headers_json, data=json.dumps(domain_data), verify=False)
169# time.sleep(0.7)
170logging.info(
171f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} POST urlgroup ADD status Code: {str(resp.status_code)}\n')
172if not str(resp.status_code).startswith('2'):
173errors_filename = 'outputs/errors.txt'
174with open(errors_filename, "a") as f:
175f.write(f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Status code: {resp.status_code}\n Request.body: {resp.request.body}\n Request text: {resp.text}\n')
176except requests.exceptions.HTTPError as errh:
177logging.info(f'{errh}')
178raise SystemExit(errh)
179except requests.exceptions.RequestException as err:
180logging.info(f'{err}')
181raise SystemExit(err)
182logging.info(
183f'\n{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Finished adding POST urlgroup in domain {get_domain_name_by_uuid(domain_uuid)}\n')
184
185
186def del_groups(group_data, domain_name):
187fmc_ip = cfg.fmc_ip
188headers_json = cfg.headers_json
189
190logging.info(f'\n {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Starting to DEL groups in domain {domain_name}\n')
191object_type = group_data['type']
192group_id = group_data['id']
193domain_uuid = get_domain_uuid(domain_name)['uuid']
194try:
195check_token_status()
196api_call_counter()
197resp = requests.delete(
198f"https://{fmc_ip}/api/fmc_config/v1/domain/{domain_uuid}/object/networkgroups/{group_id}",
199headers=headers_json, verify=False)
200# time.sleep(0.7)
201# logging.info(f'Text: {str(resp.text)}\n')
202logging.info(
203f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} DEL group {group_data["name"]} status Code: {str(resp.status_code)}\n')
204if not str(resp.status_code).startswith('2'):
205errors_filename = 'outputs/errors.txt'
206with open(errors_filename, "a") as f:
207f.write(
208f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Status code: {resp.status_code}\n Request.body: {resp.request.body}\n Request text: {resp.text}\n')
209elif str(resp.status_code).startswith('2'):
210del cfg.all_obj_domain[domain_name][object_type][group_data['name']]
211del cfg.all_detailed_networkgroups[domain_name][group_data['name']]
212except requests.exceptions.HTTPError as errh:
213logging.info(f'{errh}')
214raise SystemExit(errh)
215except requests.exceptions.RequestException as err:
216logging.info(f'{err}')
217raise SystemExit(err)
218logging.info(f'\n {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Finished to DEL groups in domain {domain_name}\n')
219
220
221def del_urlgroups(group_data, domain_name):
222fmc_ip = cfg.fmc_ip
223headers_json = cfg.headers_json
224
225logging.info(
226f'\n {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Starting to DEL urlgroups in domain {domain_name}\n')
227object_type = group_data['type']
228group_id = group_data['id']
229domain_uuid = get_domain_uuid(domain_name)['uuid']
230try:
231check_token_status()
232api_call_counter()
233resp = requests.delete(
234f"https://{fmc_ip}/api/fmc_config/v1/domain/{domain_uuid}/object/{object_type.lower()}s/{group_id}",
235headers=headers_json, verify=False)
236# time.sleep(0.7)
237# logging.info(f'Text: {str(resp.text)}\n')
238logging.info(
239f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} DEL urlgroup {group_data["name"]} status Code: {str(resp.status_code)}\n')
240if not str(resp.status_code).startswith('2'):
241errors_filename = 'outputs/errors.txt'
242with open(errors_filename, "a") as f:
243f.write(
244f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Status code: {resp.status_code}\n Request.body: {resp.request.body}\n Request text: {resp.text}\n')
245elif str(resp.status_code).startswith('2'):
246del cfg.all_obj_domain[domain_name][f'{object_type.lower()}s'][group_data['name']]
247except requests.exceptions.HTTPError as errh:
248logging.info(f'{errh}')
249raise SystemExit(errh)
250except requests.exceptions.RequestException as err:
251logging.info(f'{err}')
252raise SystemExit(err)
253logging.info(
254f'\n {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Finished to DEL urlgroups in domain {domain_name}\n')
255
256
257def put_networkgroups(group_data, domain_name):
258fmc_ip = cfg.fmc_ip
259headers_json = cfg.headers_json
260
261logging.info(f'\n {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Starting to modify PUT group in domain {domain_name}\n')
262group_id = group_data['id']
263domain_uuid = get_domain_uuid(domain_name)['uuid']
264try:
265check_token_status()
266api_call_counter()
267resp = requests.put(
268f"https://{fmc_ip}/api/fmc_config/v1/domain/{domain_uuid}/object/networkgroups/{group_id}",
269headers=headers_json, data=json.dumps(group_data), verify=False)
270# time.sleep(0.7)
271# logging.info(f'Text: {str(resp.text)}\n')
272logging.info(
273f' {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} PUT group {group_data["name"]} modify operation status Code: {str(resp.status_code)}\n')
274if not str(resp.status_code).startswith('2'):
275errors_filename = 'outputs/errors.txt'
276with open(errors_filename, "a") as f:
277f.write(f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Status code: {resp.status_code}\n Request.body: {resp.request.body}\n Request text: {resp.text}\n')
278except requests.exceptions.HTTPError as errh:
279logging.info(f'{errh}')
280raise SystemExit(errh)
281except requests.exceptions.RequestException as err:
282logging.info(f'{err}')
283raise SystemExit(err)
284logging.info(f'\n {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Finished to modify PUT group in domain {domain_name}\n')
285
286
287def put_urlgroups(group_data, domain_name):
288fmc_ip = cfg.fmc_ip
289headers_json = cfg.headers_json
290
291logging.info(
292f'\n {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Starting to modify PUT urlgroup in domain {domain_name}\n')
293group_id = group_data['id']
294domain_uuid = get_domain_uuid(domain_name)['uuid']
295try:
296check_token_status()
297api_call_counter()
298resp = requests.put(
299f"https://{fmc_ip}/api/fmc_config/v1/domain/{domain_uuid}/object/urlgroups/{group_id}",
300headers=headers_json, data=json.dumps(group_data), verify=False)
301# time.sleep(0.7)
302# logging.info(f'Text: {str(resp.text)}\n')
303logging.info(
304f' {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} PUT urlgroup {group_data["name"]} modify operation status Code: {str(resp.status_code)}\n')
305if not str(resp.status_code).startswith('2'):
306errors_filename = 'outputs/errors.txt'
307with open(errors_filename, "a") as f:
308f.write(f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Status code: {resp.status_code}\n Request.body: {resp.request.body}\n Request text: {resp.text}\n')
309
310except requests.exceptions.HTTPError as errh:
311logging.info(f'{errh}')
312raise SystemExit(errh)
313except requests.exceptions.RequestException as err:
314logging.info(f'{err}')
315raise SystemExit(err)
316logging.info(
317f'\n {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Finished to modify PUT urlgroup in domain {domain_name}\n')
318
319
320def put_object(object_data, domain_uuid):
321fmc_ip = cfg.fmc_ip
322headers_json = cfg.headers_json
323all_obj_domain = cfg.all_obj_domain
324
325logging.info(f'\n {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Starting to modify PUT object in domain {get_domain_name_by_uuid(domain_uuid)}\n')
326object_id = object_data['id']
327
328# object_type = check_object_type(object_data.get('name'))
329object_type = object_data.get('type')
330# domain_uuid = get_domain_uuid(domain_name)['uuid']
331try:
332check_token_status()
333api_call_counter()
334resp = requests.put(
335f"https://{fmc_ip}/api/fmc_config/v1/domain/{domain_uuid}/object/{object_type}s/{object_id}",
336headers=headers_json, data=json.dumps(object_data), verify=False)
337# time.sleep(0.7)
338# logging.info(f'Text: {str(resp.text)}\n')
339logging.info(
340f' {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} PUT object {object_data["name"]} modify operation status Code: {str(resp.status_code)}\n')
341if not str(resp.status_code).startswith('2'):
342errors_filename = 'outputs/errors.txt'
343with open(errors_filename, "a") as f:
344f.write(
345f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Status code: {resp.status_code}\n Request.body: {resp.request.body}\n Request text: {resp.text}\n')
346elif str(resp.status_code).startswith('2'):
347tmp_object_data = all_obj_domain[get_domain_name_by_uuid(domain_uuid)][f'{object_type}s'][object_data.get('name')]
348if object_data.get('value'):
349tmp_object_data.update({'value': object_data.get('value')})
350elif object_data.get('url'):
351tmp_object_data.update({'url': object_data.get('url')})
352cfg.all_obj_domain[get_domain_name_by_uuid(domain_uuid)][f'{object_type}s'][object_data.get('name')].update({object_data.get('name'): tmp_object_data})
353except requests.exceptions.HTTPError as errh:
354logging.info(f'{errh}')
355raise SystemExit(errh)
356except requests.exceptions.RequestException as err:
357logging.info(f'{err}')
358raise SystemExit(err)
359logging.info(f'\n {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Finished to modify PUT object in domain {get_domain_name_by_uuid(domain_uuid)}\n')
360
361
362def del_objects(object_data, domain_name):
363fmc_ip = cfg.fmc_ip
364headers_json = cfg.headers_json
365
366logging.info(f'\n {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Starting to DEL objects in domain {domain_name}\n')
367# obj_id = all_devices[object_data['name']]['id']
368obj_id = object_data['id']
369api_obj_type = f"{object_data['type'].lower()}s"
370try:
371uuid = get_domain_uuid(domain_name)['uuid']
372except TypeError as error:
373logging.info(
374f'Domain {domain_name} do not exist either on FMC, either in Excel sheet')
375errors_filename = 'outputs/errors.txt'
376with open(errors_filename, "a") as f:
377f.write(
378f'Domain {domain_name} do not exist either on FMC, either in Excel sheet\n Error: {error}\n')
379
380try:
381check_token_status()
382api_call_counter()
383resp = requests.delete(
384f"https://{fmc_ip}/api/fmc_config/v1/domain/{uuid}/object/{api_obj_type}/{obj_id}",
385headers=headers_json,
386# data=json.dumps(domain_data),
387verify=False)
388# time.sleep(0.7)
389# logging.info(f'resp.request.body')
390# logging.info(f'Headers: {str(resp.headers)}')
391# logging.info(f'Text: {str(resp.text)}')
392logging.info(
393f' {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} DEL object {object_data["name"]} in domain {domain_name} status code: {str(resp.status_code)}')
394if not str(resp.status_code).startswith('2'):
395errors_filename = 'outputs/errors.txt'
396with open(errors_filename, "a") as f:
397f.write(
398f'Status code: {resp.status_code}\n Request.body: {resp.request.body}\n Request text: {resp.text}\n')
399elif str(resp.status_code).startswith('2'):
400del cfg.all_obj_domain[domain_name][api_obj_type][object_data['name']]
401# elif str(resp.status_code).startswith('4'):
402# del cfg.all_obj_domain[domain_name][api_obj_type][object_data['name']]
403except requests.exceptions.HTTPError as errh:
404logging.info(f'{errh}')
405raise SystemExit(errh)
406except requests.exceptions.RequestException as err:
407logging.info(f'{err}')
408raise SystemExit(err)
409logging.info(f'\n {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Finished to DEL objects in domain {domain_name}\n')
410