Skip to content

Instantly share code, notes, and snippets.

@Pobek
Last active August 20, 2019 12:10
Show Gist options
  • Save Pobek/5b4a0cd980a104a58943ada712d815d0 to your computer and use it in GitHub Desktop.
Save Pobek/5b4a0cd980a104a58943ada712d815d0 to your computer and use it in GitHub Desktop.
This python script will return data about a rule from policy if the schema name matchs
import requests
import urllib3
import json
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def build_json(mpgws):
output = {
"mpgws" : mpgws
}
return output
def get_mpgw_details():
url = "https://localhost:5554"
creds = ("admin", "admin")
#mpgw_response = requests.get(url="{}/mgmt/config/Sandbox/MultiProtocolGateway/{}".format(url, mpgw_name), auth=creds, verify=False)
mpgw_response = requests.get(url="{}/mgmt/config/Sandbox/MultiProtocolGateway".format(url), auth=creds, verify=False)
if mpgw_response.status_code == 200:
if type(mpgw_response.json()["MultiProtocolGateway"]) is dict:
mpgw = {}
mpgw_name = mpgw_response.json()["MultiProtocolGateway"]["name"]
mpgw[mpgw_name] = {}
policy = mpgw_response.json()["MultiProtocolGateway"]["StylePolicy"]
policy_response = requests.get(url="{}{}".format(url, policy["href"]), auth=creds, verify=False)
if policy_response.status_code == 200:
mpgw[mpgw_name][policy["value"]] = {}
match = policy_response.json()["StylePolicy"]["PolicyMaps"]["Match"]
match_response = requests.get(url="{}{}".format(url, match["href"]), auth=creds, verify=False)
if match_response.status_code == 200:
match_rules = match_response.json()["Matching"]["MatchRules"]
mpgw[mpgw_name][policy["value"]]["Match"] = {}
mpgw[mpgw_name][policy["value"]]["Match"][match["value"]] = match_rules
else:
raise Exception("Match '{}' Not found".format(match["value"]))
rule = policy_response.json()["StylePolicy"]["PolicyMaps"]["Rule"]
rule_response = requests.get(url="{}{}".format(url, rule["href"]), auth=creds, verify=False)
if rule_response.status_code == 200:
mpgw[mpgw_name][policy["value"]]["Rule"] = {}
mpgw[mpgw_name][policy["value"]]["Rule"][rule["value"]] = rule_response.json()["StylePolicyRule"]
rule_actions = rule_response.json()["StylePolicyRule"]["Actions"]
actions = {}
mpgw[mpgw_name][policy["value"]]["Rule"][rule["value"]]["Actions"] = []
for action in rule_actions:
action_response = requests.get(url="{}{}".format(url, action["href"]), auth=creds, verify=False)
if action_response.status_code == 200:
action_value = action_response.json()["StylePolicyAction"]
actions[action["value"]] = action_value
else:
raise Exception("Action '{}' Not found".format(action["value"]))
mpgw[mpgw_name][policy["value"]]["Rule"][rule["value"]]["Actions"].append(action_value)
else:
raise Exception("Rule '{}' Not found".format(rule["value"]))
else:
raise Exception("Policy '{}' Not found".format(policy["value"]))
return build_json(mpgw)
else:
mpgws = {}
for mpgw in mpgw_response.json()["MultiProtocolGateway"]:
mpgws[mpgw["name"]] = {}
policy = mpgw["StylePolicy"]
policy_response = requests.get(url="{}{}".format(url, policy["href"]), auth=creds, verify=False)
if policy_response.status_code == 200:
mpgws[mpgw["name"]][policy["value"]] = {}
match = policy_response.json()["StylePolicy"]["PolicyMaps"]["Match"]
match_response = requests.get(url="{}{}".format(url, match["href"]), auth=creds, verify=False)
if match_response.status_code == 200:
match_rules = match_response.json()["Matching"]["MatchRules"]
mpgws[mpgw["name"]][policy["value"]]["Match"] = {}
mpgws[mpgw["name"]][policy["value"]]["Match"][match["value"]] = match_rules
else:
raise Exception("Match '{}' Not found".format(match["value"]))
rule = policy_response.json()["StylePolicy"]["PolicyMaps"]["Rule"]
rule_response = requests.get(url="{}{}".format(url, rule["href"]), auth=creds, verify=False)
if rule_response.status_code == 200:
mpgws[mpgw["name"]][policy["value"]]["Rule"] = {}
mpgws[mpgw["name"]][policy["value"]]["Rule"][rule["value"]] = rule_response.json()["StylePolicyRule"]
rule_actions = rule_response.json()["StylePolicyRule"]["Actions"]
actions = {}
mpgws[mpgw["name"]][policy["value"]]["Rule"][rule["value"]]["Actions"] = []
for action in rule_actions:
action_response = requests.get(url="{}{}".format(url, action["href"]), auth=creds, verify=False)
if action_response.status_code == 200:
action_value = action_response.json()["StylePolicyAction"]
actions[action["value"]] = action_value
else:
raise Exception("Action '{}' Not found".format(action["value"]))
mpgws[mpgw["name"]][policy["value"]]["Rule"][rule["value"]]["Actions"].append(action_value)
else:
raise Exception("Rule '{}' Not found".format(rule["value"]))
else:
raise Exception("Policy '{}' Not found".format(policy["value"]))
return build_json(mpgws)
else:
raise Exception("MultiProtocolGateway Not Found.")
def main(schema_name):
result = get_mpgw_details()
for mpgw in result["mpgws"]:
for policy in result["mpgws"][mpgw]:
for rule in result["mpgws"][mpgw][policy]["Rule"]:
for action in result["mpgws"][mpgw][policy]["Rule"][rule]["Actions"]:
if "validate" in action["name"]:
if schema_name in action["SchemaURL"]:
print(result["mpgws"][mpgw][policy]["Rule"][rule])
return
print("No MPGW Found")
if __name__ == "__main__":
main("test.xsd")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment