summaryrefslogtreecommitdiff
path: root/support/api_utils/delete_rules.py
blob: 7e162c0ab0118b14be7f8d8be1ac0ba340d6c76e (plain)
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
# -*- coding: UTF-8 -*-
import requests
from datetime import datetime

class DeleteRules:
    def __init__(self, parameter, headers):
        self.parameter = parameter
        self.headers = headers
    
    def delete_rules(self, rule_uuids_tuple):
        api_server = self.parameter["api_server"]
        vsys = self.parameter["vsys"]
        try:
            rule_type_dict = {
                "security": "security-rules",
                "proxy_intercept": "proxy-intercept-rules",
                "proxy_manipulation": "proxy-manipulation-rules",
                "traffic_shaping": "traffic-shaping-rules",
                "service_chaining": "service-chaining-rules",
                "statistics": "statistics-rules",
                "monitor": "monitor-rules",
                "dos_protection": "dos-protection-rules",
            }
            rule_uuids_list = list(rule_uuids_tuple)
            for i in range(len(rule_uuids_list)):
                type = rule_uuids_list[i]["type"]
                deleted_rule_dict = {"uuids": [], "vsys": 1}
                deleted_rule_dict["uuids"].append(rule_uuids_list[i]["uuid"])
                deleted_rule_dict["vsys"] = vsys
                url = api_server + "/v1/policies/{}".format(rule_type_dict[type])
                response = requests.delete(url, headers=self.headers, json=deleted_rule_dict, verify=False)
                assert response.status_code == 200
                if response.status_code == 200:
                    print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Delete {} rule successfully.".format(type.replace("_", " ")), flush=True)
                else:
                    print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Delete {} rule failed.".format(type.replace("_", " ")), flush=True)
        except Exception as e:
            print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "When deleting rule, the exception error: ", str(e), flush=True)
            return "" ,"When deleting rule, the exception error: " + str(e)