Skip to main content

Doc Store SDK Example

This tutorial demonstrates how to use Macrometa SDKs to work with document collections.

Prerequisites

Code Sample

Copy the code below, and your API key, and then run it in your favorite IDE.

from c8 import C8Client
import pprint
import time

if __name__ == '__main__':

# Define constants
API_KEY = "my API key" # Change this to your API key
GEO_FABRIC = "_system"
GLOBAL_URL = "play.paas.macrometa.io"
REGION_URLS = [
"play2-ap-southeast.paas.macrometa.io",
"play2-ap-northeast.paas.macrometa.io",
"play2-ap-south.paas.macrometa.io",
"play2-ap-west.paas.macrometa.io",
"play2-eu-central.paas.macrometa.io",
"play2-eu-west.paas.macrometa.io",
"play2-us-east.paas.macrometa.io",
"play2-ca-central.paas.macrometa.io",
"play2-us-southeast.paas.macrometa.io",
"play2-us-central.paas.macrometa.io",
"play2-us-west.paas.macrometa.io"
]
IP_ADDRESS = "20.1.1.9"

# Variables - Queries
READ_QUERY = f"FOR device in ddoslist FILTER device.ip == '{IP_ADDRESS}' RETURN" + "{IP:device.ip, IsAllowed:device.action}"
INSERT_QUERY = "INSERT { \"ip\" : \"" + IP_ADDRESS + "\", \"action\": \"block\", \"rule\":\"blocklistA\"} INTO ddoslist"

# Variables - Data
COLLECTION_NAME = "ddoslist"
DATA = [
{"ip": "10.1.1.1", "action": "block", "rule": "blocklistA"},
{"ip": "20.1.1.2", "action": "block", "rule": "blocklistA"},
{"ip": "30.1.1.3", "action": "block", "rule": "blocklistB"},
{"ip": "40.1.1.4", "action": "block", "rule": "blocklistA"},
{"ip": "50.1.1.5", "action": "block", "rule": "blocklistB"},
{"ip": "20.1.1.3", "action": "allow", "rule": "allowlistA"},
{"ip": "20.1.1.4", "action": "allow", "rule": "allowlistA"},
{"ip": "30.1.1.4", "action": "allow", "rule": "allowlistB"},
{"ip": "30.1.1.5", "action": "allow", "rule": "allowlistB"}
]

pp = pprint.PrettyPrinter(indent=4)

# Step 1: Open connection to GDN. You will be routed to closest region.
print(f"1. CONNECT: server: {GLOBAL_URL}")
print("--- Connecting to GDN")
# Choose one of the following methods to access the GDN. API key is recommended.

# Authenticate with API key
client = C8Client(protocol='https', host=GLOBAL_URL, port=443, apikey=API_KEY, geofabric=GEO_FABRIC)

# Authenticate with JWT
# client = C8Client(protocol='https', host=GLOBAL_URL, port=443, token=<your token>, geofabric=GEO_FABRIC)

# Authenticate with email and password
# client = C8Client(protocol='https', host=GLOBAL_URL, port=443, email=<your email id>, password=<your password>, geofabric=GEO_FABRIC)

# Step 2: Create the collection if it does not exist.
print(f"2. CREATE_COLLECTION: region: {GLOBAL_URL}, collection: {COLLECTION_NAME}")
if client.has_collection(COLLECTION_NAME):
collection = client.collection(COLLECTION_NAME)
else:
collection = client.create_collection(COLLECTION_NAME)

# Step 3: Insert data into collection.
print(f"3. INSERT_DDOS_DATA: in region: {GLOBAL_URL}")
client.insert_document(COLLECTION_NAME, document = DATA)

# Step 4: Read data.
print(f"4. IS_IP_ALLOWED...from region: {GLOBAL_URL}")
cursor = client.execute_query(READ_QUERY)
docs = [document for document in cursor]
if docs == []:
print(f"IP: {IP_ADDRESS}" + "IsAllowed: {"'allow'"}\n")
else:
pp.pprint(docs)

# Step 5: Blocklist IP address.
print(f"5. BLOCKLIST the IP...from region: {GLOBAL_URL}, ip: {IP_ADDRESS}")
cursor = client.execute_query(INSERT_QUERY)
time.sleep(0.3)

# Step 6: Read data from other regions.
print("6. Check again if IP is allowed globally")
for region_url in REGION_URLS:
print(f"\n IS_IP_ALLOWED...checking from region: {region_url}")
# Reuse API key for authentication
clientx = C8Client(protocol = 'https', host = region_url, port = 443, apikey = API_KEY, geofabric = GEO_FABRIC)
cursorx = clientx.execute_query(READ_QUERY)
docs = [document for document in cursorx]
pp.pprint(docs[0])


# Step 7: Delete data.
print(f"\n7. DELETE_DATA: region: {GLOBAL_URL}, collection: {COLLECTION_NAME}")
collection.truncate()
#client.delete_collection(COLLECTION_NAME)