Provisioning API Sample code

Provisioning API Sample code

You are here:

Below you can find sample code for the Provisioning API. Click here to download it.

 #!/usr/bin/python3
import json
from flask import Flask, request
app = Flask(__name__)
def network_cfg(network, enable_presence):
    network["presence_analytics"] = {}
    if enable_presence == True:
        network["presence_analytics"]["mode"] = "active"
        network["presence_analytics"]["transport"] = {}
        network["presence_analytics"]["transport"]["type"] = "cmx"
        network["presence_analytics"]["transport"]["secret"] = "mysecret"
        network["presence_analytics"]["transport"]["validator"] = "myvalidator"
        network["presence_analytics"]["transport"]["destination_url"] = "https://mydomain.com/presence/receive.php"
        network["presence_analytics"]["transport"]["triangulation"] = False
    else:
        network["presence_analytics"]["mode"] = "inactive"
def ssid_cfg(ssid, enable_cp):
    ssid["remote_splashpage"] = {}
    if enable_cp == True:
        ssid["remote_splashpage"]["mode"] = "active"
        ssid["remote_splashpage"]["remote"] = {}
        ssid["remote_splashpage"]["remote"]["url"] = "https://splashpageserver.com"
        ssid["remote_splashpage"]["remote"]["secret"] = "splashpage server secret"
        ssid["remote_splashpage"]["backend"] = {}
        ssid["remote_splashpage"]["backend"]["type"] = "RADIUS"
        ssid["remote_splashpage"]["backend"]["primary"] = {}
        ssid["remote_splashpage"]["backend"]["primary"]["authentication"] = {}
        ssid["remote_splashpage"]["backend"]["primary"]["authentication"]["server"] = "1.2.3.4"
        ssid["remote_splashpage"]["backend"]["primary"]["authentication"]["secret"] = "radiussecret"
        ssid["remote_splashpage"]["backend"]["primary"]["authentication"]["port"] = 1812
        ssid["remote_splashpage"]["backend"]["nas_id"] = "mynasid"
        ssid["dashboard_url"] = "https://dashboard.mydomain.com/"
    else:
        ssid["remote_splashpage"]["mode"] = "inactive"
        ssid["dashboard_url"] = None

@app.route("/provision/subscriptions", methods=["POST"])
def subscription_add():
    # subscription create - handle provisioning & return subscription id
    subscription = request.get_json()
    # handle subscription create
    print("SUBSCRIPTION: CREATE => tier = %s, name = %s, email = %s" % (subscription["service_tier"], subscription["name"], subscription["email"]))
    # return subscription id to be used in subsequent API calls
    subscription["id"] = 42
    # return 201 to confirm successful provisioning of subscription
    # return 500 to reject subscription - provisioning is aborted
    return subscription, 201
@app.route("/provision/subscriptions/<int:subscription_id>", methods=["POST"])
def subscription_update(subscription_id):
    # subscription update - user name / email / etc might have changed
    subscription = request.get_json()
    # handle subscription update
    print("SUBSCRIPTION: UPDATE => tier = %s, new name = %s, new email = %s" % (subscription["service_tier"], subscription["name"], subscription["email"]))
    subscription["id"] = subscription_id
    return subscription, 201
@app.route("/provision/subscriptions/<int:subscription_id>", methods=["DELETE"])
def subscription_del(subscription_id):
    # subscription termination - confirm by returning HTTP 204
    # handle subscription delete
    print("SUBSCRIPTION: DELETE => subscription id = %s" % (subscription_id))
    return "", 204
@app.route("/provision/subscriptions/<int:subscription_id>/networks", methods=["POST"])
def network_add(subscription_id):
    # network service activation - handle provisioning & return network cfg + network id
    network = request.get_json()
    # handle network create
    print("NETWORK: CREATE => subscription id = %d, network name = %s" % (subscription_id, network["name"]))
    # return network id to be used in subsequent API calls
    network["id"] = 4242
    # return network configuration - presence analytics enabled
    network_cfg(network, True)
    # return 201 to confirm successful provisioning of network
    # return 500 to reject network - provisioning is aborted
    return network, 201
@app.route("/provision/subscriptions/<int:subscription_id>/networks/<int:network_id>", methods=["POST"])
def network_update(subscription_id, network_id):
    # network update - network name might have changed
    network = request.get_json()
    # handle network update
    print("NETWORK: UPDATE => subscription id = %d, network id = %d, new network name = %s" % (subscription_id, network_id, network["name"]))
    network["id"] = network_id
    # return network configuration - presence analytics enabled
    network_cfg(network, True)
    return network, 201
@app.route("/provision/subscriptions/<int:subscription_id>/networks/<int:network_id>", methods=["DELETE"])
def network_del(subscription_id, network_id):
    # network service deactivation - confirm by returning HTTP 204
    # handle network delete
    print("NETWORK: DELETE => subscription id = %d, network id = %d" % (subscription_id, network_id))
    return "", 204
@app.route("/provision/subscriptions/<int:subscription_id>/networks/<int:network_id>/ssids", methods=["POST"])
def ssid_add(subscription_id, network_id):
    # ssid service activation - handle provisioning & return ssid cfg + ssid id
    ssid = request.get_json()
    # handle ssid create
    print("SSID: CREATE => subscription id = %d, network id = %d, ssid name = %s" % (subscription_id, network_id, ssid["name"]))
    # return ssid id to be used in subsequent API calls
    ssid["id"] = 424242
    # return ssid configuration - captive portal enabled
    ssid_cfg(ssid, True)
    # return 201 to confirm successful provisioning of ssid
    # return 500 to reject ssid - provisioning is aborted
    return ssid, 201
@app.route("/provision/subscriptions/<int:subscription_id>/networks/<int:network_id>/ssids/<int:ssid_id>", methods=["POST"])
def ssid_update(subscription_id, network_id, ssid_id):
    # ssid update - ssid name might have changed
    ssid = request.get_json()
    # handle ssid update
    print("SSID: UPDATE => subscription id = %d, network id = %d, ssid id = %d, new ssid name = %s" % (subscription_id, network_id, ssid_id, ssid["name"]))
    ssid["id"] = ssid_id
    # return ssid configuration - captive portal enabled
    ssid_cfg(ssid, True)
    return ssid, 201
@app.route("/provision/subscriptions/<int:subscription_id>/networks/<int:network_id>/ssids/<int:ssid_id>", methods=["DELETE"])
def ssid_del(subscription_id, network_id, ssid_id):
    # ssid service deactivation - confirm by returning HTTP 204
    # handle ssid delete
    print("NETWORK: DELETE => subscription id = %d, network id = %d, ssid id = %d" % (subscription_id, network_id, ssid_id))
    return "", 204
@app.route("/provision/subscriptions/<int:subscription_id>/networks/<int:network_id>/devices", methods=["POST"])
def device_add(subscription_id, network_id):
    # device added to network - handle provisioning
    device = request.get_json()
    # handle ssid update
    print("DEVICE: CREATE => subscription id = %d, network id = %d, device name = %s, lat = %f, long = %f" % (subscription_id, network_id, device["name"], device["location"]["lat"], device["location"]["long"]))
    device["id"] = 2323
    return device, 201
@app.route("/provision/subscriptions/<int:subscription_id>/networks/<int:network_id>/devices/<int:device_id>", methods=["POST"])
def device_update(subscription_id, network_id, device_id):
    # device update - device name or location might have changed
    device = request.get_json()
    # handle ssid update
    print("DEVICE: UPDATE => subscription id = %d, network id = %d, device id = %d, new device name = %s, new lat = %f, new long = %f" % (subscription_id, network_id, device_id, device["name"], device["location"]["lat"], device["location"]["long"]))
    device["id"] = device_id
    return device, 201
@app.route("/provision/subscriptions/<int:subscription_id>/networks/<int:network_id>/devices/<int:device_id>", methods=["DELETE"])
def device_del(subscription_id, network_id, device_id):
    # device removal from network - confirm by returning HTTP 204
    # handle device delete
    print("DEVICE: DELETE => subscription id = %d, network id = %d, device id = %d" % (subscription_id, network_id, device_id))
    return "", 204
Previous Devices Endpoint
Table of Contents