-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateTag.py
More file actions
52 lines (43 loc) · 1.46 KB
/
CreateTag.py
File metadata and controls
52 lines (43 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import requests
import csv
import json
# Your API key and firewall IP
api_key = 'Basic cmVzdHVzZXI6QWExMjM0NTY='
FIREWALL_IP = '10.0.4.253'
# CSV file containing tag information
CSV_FILE = 'tag_mappings.csv'
# URL for adding tags
TAG_URL = f'https://{FIREWALL_IP}/restapi/v10.1/Objects/Tags?location=vsys&vsys=vsys1'
# Set up headers with basic auth and specify Content-Type as JSON
headers = {
'Authorization': api_key,
'Content-Type': 'application/json' # Set the Content-Type to JSON
}
# Function to send a POST request to add a tag
def add_tag(tag_name, color):
# Updated URL with the tag name
tag_url = f'{TAG_URL}&name={tag_name}'
payload = {
"entry": {
"@name": tag_name,
"color": color
}
}
print(payload)
response = requests.post(tag_url, json=payload, headers=headers, verify=False)
if response.status_code == 200:
print(f"Tag '{tag_name}' added successfully.")
else:
print(f"Failed to add tag '{tag_name}'. Status code: {response.status_code}")
# Load tag data from CSV and add tags
with open(CSV_FILE) as csvfile:
reader = csv.DictReader(csvfile)
print(reader)
for row in reader:
# Check for 'Tag' column with or without leading/trailing whitespaces
tag_name = row.get('Tag', '').strip()
color = row.get('Color', '').strip()
print(tag_name, color)
if tag_name:
add_tag(tag_name, color)
print("Tag addition complete!")