import requests
import time
import json
from random import seed
import random, string
import enum


seed(1)

class IpMode(enum.Enum):
    static = 1,
    dynamic = 2

DEFAULT_MODE = IpMode.static

def login(client_id, secret_key):
    url = "https://dev-cloudinfra-gw.kube1.iaas.checkpoint.com/auth/external"

    payload = "{\n  \"clientId\": \"" + client_id + "\",\n  \"accessKey\": \"" + secret_key + "\"\n}"
    headers = {
        'Content-Type': "application/json",
        'User-Agent': "PostmanRuntime/7.16.3",
        'Accept': "*/*",
        'Cache-Control': "no-cache",
        'Postman-Token': "56421816-ad70-4713-b764-996dc091ae15,bea47a68-1367-431c-9ce3-aacbea652628",
        'Host': "cloudinfra-gw.portal.checkpoint.com",
        'Accept-Encoding': "gzip, deflate",
        'Content-Length': "132",
        'Connection': "keep-alive",
        'cache-control': "no-cache"
    }

    response = requests.request("POST", url, data=payload, headers=headers)
    response_json = response.json()

    return response_json["data"]["token"]


def random_string(string_length=32):
    return ''.join(random.choices(string.ascii_letters + string.digits, k=string_length))


def random_site(i, mode = DEFAULT_MODE):
    random_ip = str(random.randint(0, 255)) + "." + \
                str(random.randint(0, 255)) + "." + \
                str(random.randint(0, 255)) + "." + \
                str(random.randint(0, 255))

    random_subnet = str(random.randint(0, 255)) + "." + \
                    str(random.randint(0, 255)) + "." + \
                    str(random.randint(0, 255)) + ".0/24"

    random_fqdns = [random_string(8) + "." + random_string(8) + ".com" for i in range(4)]
    preSharedKey = # set key

    if mode == IpMode.static:
        ipsec_connection = {
            "isDynamicIp": False,
            "staticIpConnection": {
                "externalIps": [random_ip],
                "preSharedKey": preSharedKey
            }
        }
    else: # IpMode.Dynamic
        ipsec_connection = {
            "isDynamicIp": True,
            "dynamicIpConnection": {
                "interfaceIdentifiers": random_fqdns,
                "preSharedKey": preSharedKey
            }
        }

    return {
        "name": "site" + str(i),
        "description": "Site number " + str(i),
        "subnets": [random_subnet],
        "tunnelType": "IPSEC",
        # Cloud service location in USA North East
        "regionId": "671b0bc6-62c3-4007-9dbc-dd8c1aa56234",
        "ipsecConnection": ipsec_connection
    }


def add_sites(token, totals, mode = DEFAULT_MODE):

    random.seed(time.perf_counter())

    url = "https://cloudinfra-gw.portal.checkpoint.com/app/cgc/v1/"

    headers = {
        "Authorization": "Bearer " + token,
        "Content-Type": "application/json",
        'Accept': "*/*",
        'Cache-Control': "no-cache",
    }

    for i in range(1, totals + 1):

        success = False
        while not success:

            site = random_site(i)
            print(site)
            res = requests.post(url + "sites", data=json.dumps(site), headers=headers)
            # put some sleep here because status doesn't change immediately
            time.sleep(5)

            response = res.json()
            print(response)
            if "errors" not in response:
                success = True


# To start, replace the 2 parameters with an API Key from Infinity Portal.
target_account = login("Asfas", "fsfga")

add_sites(target_account, 50, IpMode.dynamic)
