########################################################################### # CLOUD SERVER API DOCUMENTATION # # Name: createLoadBalancer # Language: Python # # Description: this function creates a load balancer ########################################################################### from urllib.request import Request, urlopen import urllib import json #ID access to API TOKEN = "{YOUR_API_TOKEN}" # e.g.: "f03c3c76cc853ee690b879909c9c6f2a" url = "https://cloudpanel-api.ionos.com/v1" def _createLoadBalancer(content): #Configure the request _command = url + "/load_balancers" _method = 'POST' request = Request(_command, data=content.encode(encoding='utf_8'), headers={'X-TOKEN':TOKEN, 'content-type':'application/json'}, method=_method) #Try to get the response try: response = urlopen(request) content = response.read() return (content.decode()) #Fetch error except urllib.error.URLError as e: return("Error " + str(e.code) + ":" + e.reason) #PARAMETERS name = 'My load balancer api test' description = 'My load balancer api test description' health_check_test = 'TCP' health_check_interval = 40 persistence = True persistence_time = 1200 method = 'ROUND_ROBIN' rules = [{"protocol":"TCP", "port_balancer":22, 'port_server':22, 'source':'0.0.0.0'}, {"protocol":"TCP", "port_balancer":8080, 'port_server':8080, 'source':'0.0.0.0'}] data = json.dumps({'name':name, 'description':description, 'health_check_test': health_check_test, 'health_check_interval':health_check_interval, 'persistence':persistence, 'persistence_time':persistence_time, 'method':method, 'rules':rules}) #Create load balancer print(_createLoadBalancer(data))