#!/usr/bin/python3

import requests
import json
import sys
from time import gmtime, strftime

requests.packages.urllib3.disable_warnings()

def x2_login(board_addr, username, password):
    # Create request
    url = "https://" + board_addr + "/api2/tokens"
    data = {}
    data["username"] = username
    data["password"] = password

    # Send Request
    headers = {"content-type": "application/json"}
    r = requests.post(url, headers=headers, json=data, verify=False)
    # print(r.status_code)
    # print(r.content)
    return json.loads(r.content)


def x2_create_ruleset(board_addr, token, set_name):
    # Prepare request
    url = "https://" + board_addr + "/api2/tm/rulesets"
    data = {}
    data["name"] = set_name
    data["description"] = "A simple RuleSet crated by Python using Profitap API"
    headers = {}
    headers["content-type"] = "application/json"
    headers["Authorization"] = token

    # Check result
    r = requests.post(url, headers=headers, json=data, verify=False)
    print(r.status_code)
    print(r.content)

    # Return set id
    resp = json.loads(r.content)
    return resp["data"]


def x2_export_complete_config(addr, token, dest_filename, passphrase):
    # Prepare request
    url = "https://" + addr + "/api2/system/export"
    # Define which part of the configuration to export
    export_option = {}
    export_option["port_configuration"] = True
    export_option["rulesets"] = True
    export_option["local_users"] = True
    export_option["tacacs_servers"] = True
    export_option["radius_servers"] = True
    export_option["authentication_configuration"] = True
    export_option["supervisor_authentication"] = True
    export_option["time_settings"] = True
    export_option["snmp_settings"] = True
    export_option["syslog_remote_servers"] = True
    export_option["firewall_settings"] = True
    export_option["passphrase"] = passphrase
    headers = {}
    headers["content-type"] = "application/json"
    headers["Authorization"] = token
    headers["data"] = json.dumps(export_option)

    # Send request and save output
    r = requests.get(url, headers=headers, verify=False)
    files = open(dest_filename, "wb")
    files.write(r.content)
    files.close()
    print("Export Done")


def x2_import_complete_config(addr, token, src_filename, passphrase):
    # Prepare request
    url = "https://" + addr + "/api2/system/import"
    # Define which part of the configuration to import
    export_option = {}
    export_option["port_configuration"] = True
    export_option["rulesets"] = True
    export_option["local_users"] = True
    export_option["tacacs_servers"] = True
    export_option["radius_servers"] = True
    export_option["authentication_configuration"] = True
    export_option["supervisor_authentication"] = True
    export_option["time_settings"] = True
    export_option["snmp_settings"] = True
    export_option["syslog_remote_servers"] = True
    export_option["firewall_settings"] = True
    export_option["passphrase"] = passphrase
    headers = {}
    headers["content-type"] = "application/json"
    headers["Authorization"] = token
    headers["data"] = json.dumps(export_option)

    # Send request with configuration file
    files = open(src_filename, "rb")
    r = requests.post(url, headers=headers, data=files, verify=False)
    files.close()
    print("Import Done")


if __name__ == "__main__":
    board_url = "x2-device-ip"
    username = "admin"
    password = "Adminadmin1"
    conf_passphrase = "configuration_encryption_passphrase"
    conf_filename = "configuration_file.conf"

    auth_reply = x2_login(board_url, username, password)
    x2_export_complete_config(
        board_url, auth_reply["token"], conf_filename, conf_passphrase
    )
    x2_import_complete_config(
        board_url, auth_reply["token"], conf_filename, conf_passphrase
    )