8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-21 19:03:11 +00:00

Merge branch 'new_freeradius_failible_client_creation' into 'new_radius_api'

Allow the Re2oAPIClient creation to fail

See merge request re2o/re2o!611
This commit is contained in:
chapeau 2021-02-24 21:41:14 +00:00
commit 0857577c4e

View file

@ -123,9 +123,16 @@ def instantiate(*_):
api_password = config.get('Re2o', 'password') api_password = config.get('Re2o', 'password')
api_username = config.get('Re2o', 'username') api_username = config.get('Re2o', 'username')
def get_api_client():
"""Gets a Re2o, or tries to initialize one"""
if get_api_client.client is None:
get_api_client.client = Re2oAPIClient(
api_hostname, api_username, api_password, use_tls=True)
return get_api_client.client
get_api_client.client = None
global api_client global api_client
api_client = Re2oAPIClient( api_client = get_api_client
api_hostname, api_username, api_password, use_tls=True)
@radius_event @radius_event
@ -143,7 +150,7 @@ def authorize(data):
mac = data.get("Calling-Station-Id", "") mac = data.get("Calling-Station-Id", "")
# Get all required objects from API # Get all required objects from API
data_from_api = api_client.view( data_from_api = api_client().view(
"radius/authorize/{0}/{1}/{2}".format( "radius/authorize/{0}/{1}/{2}".format(
urllib.parse.quote(nas or "None", safe=""), urllib.parse.quote(nas or "None", safe=""),
urllib.parse.quote(username or "None", safe=""), urllib.parse.quote(username or "None", safe=""),
@ -183,7 +190,7 @@ def post_auth(data):
mac = data.get("Calling-Station-Id", None) mac = data.get("Calling-Station-Id", None)
# Get all required objects from API # Get all required objects from API
data_from_api = api_client.view( data_from_api = api_client().view(
"radius/post_auth/{0}/{1}/{2}".format( "radius/post_auth/{0}/{1}/{2}".format(
urllib.parse.quote(nas or "None", safe=""), urllib.parse.quote(nas or "None", safe=""),
urllib.parse.quote(nas_port or "None", safe=""), urllib.parse.quote(nas_port or "None", safe=""),
@ -270,7 +277,7 @@ def check_user_machine_and_register(nas_type, user, user_interface, nas_id, user
elif not user_interface["ipv4"]: elif not user_interface["ipv4"]:
# Try to autoassign ip # Try to autoassign ip
try: try:
api_client.view( api_client().view(
"radius/assign_ip/{0}".format( "radius/assign_ip/{0}".format(
urllib.parse.quote(mac_address or "None", safe="") urllib.parse.quote(mac_address or "None", safe="")
)) ))
@ -284,7 +291,7 @@ def check_user_machine_and_register(nas_type, user, user_interface, nas_id, user
# The interface is not yet registred, try to autoregister if enabled # The interface is not yet registred, try to autoregister if enabled
if nas_type["autocapture_mac"]: if nas_type["autocapture_mac"]:
try: try:
api_client.view( api_client().view(
"radius/autoregister/{0}/{1}/{2}".format( "radius/autoregister/{0}/{1}/{2}".format(
urllib.parse.quote(nas_id or "None", safe=""), urllib.parse.quote(nas_id or "None", safe=""),
urllib.parse.quote(username or "None", safe=""), urllib.parse.quote(username or "None", safe=""),
@ -504,7 +511,7 @@ def decide_vlan_switch(data_from_api, user_mac, nas_port):
DECISION_VLAN = user_interface["vlan_id"] DECISION_VLAN = user_interface["vlan_id"]
if not user_interface["ipv4"]: if not user_interface["ipv4"]:
try: try:
api_client.view( api_client().view(
"radius/assign_ip/{0}".format( "radius/assign_ip/{0}".format(
urllib.parse.quote(user_mac or "None", safe="") urllib.parse.quote(user_mac or "None", safe="")
)) ))