def test_slb_g01(self): """ slb_g01 """ if "json" == ml_system.CFG_TYPE: shutil.copyfile(os.path.join("unittest", "slb-g01.json"), os.path.join("running", "slb.txt")) if "jcfg" == ml_system.CFG_TYPE: shutil.copyfile(os.path.join("unittest", "slb-g01.jcfg"), os.path.join("running", "slb.txt")) if "pickle" == ml_system.CFG_TYPE: return self.maxDiff = None e = ml_w_slb.get(None, threading.RLock()) self.assertEqual(e, (True, {'service_group': {'ipv4': [{'protocol': 'TCP', 'application_port': [80, 443], 'label': 'group1_v4'}], 'ipv6': [{'protocol': 'TCP', 'application_port': [80, 443], 'label': 'group1_v6'}]}, 'policy': {'ipv4': [{'service_group': 'group1_v4', 'destination_ip': 'many', 'fallback_server': 'fall1_v4', 'action': 'VIP', 'property': 'proper1', 'source_ip': 'one', 'real_server_group': 'real1_v4'}], 'ipv6': [{'service_group': 'group1_v6', 'destination_ip': 'many', 'fallback_server': '', 'action': 'Accept', 'property': '', 'source_ip': 'one', 'real_server_group': ''}]}, 'ip': {'ipv4': [{'ip_address': ['10.1.1.4'], 'label': 'one'}, {'ip_address': ['10.1.1.1', '10.1.1.2', '10.1.1.3', '10.1.1.4'], 'label': 'many'}], 'ipv6': [{'ip_address': ['2001::1'], 'label': 'one'}, {'ip_address': ['2001::1', '2001::2', '2001::3', '2001::4'], 'label': 'many'}]}, 'real_server_group': {'ipv4': [{'health_check': 'NA', 'weight': 1, 'maintenance_mode': False, 'label': 'real1_v4', 'tcp_check': {'to_remote_ip': '0.0.0.0', 'from_local_ip': '0.0.0.0', 'connection_timeout': 10}, 'https_get': {'delay_before_retry': 10, 'from_local_ip': '0.0.0.0', 'url': '', 'status_code': 200, 'connection_timeout': 10, 'to_remote_port': 80}, 'pattern_check': {'to_remote_ip': '0.0.0.0', 'send': '', 'to_remote_port': 80, 'expect': '', 'timeout': 10}, 'smtp_check': {'to_remote_ip': '0.0.0.0', 'from_local_ip': '0.0.0.0', 'connection_timeout': 10, 'to_remote_port': 80, 'helo_name': ''}, 'icmp_check': {'timeout': 10}, 'ip_address': '10.1.1.1', 'http_get': {'delay_before_retry': 10, 'from_local_ip': '0.0.0.0', 'url': '', 'status_code': 200, 'connection_timeout': 10, 'to_remote_port': 80}}, {'health_check': 'HTTP_GET', 'weight': 1, 'maintenance_mode': False, 'label': 'real2_v4', 'tcp_check': {'to_remote_ip': '0.0.0.0', 'from_local_ip': '0.0.0.0', 'connection_timeout': 10}, 'https_get': {'delay_before_retry': 10, 'from_local_ip': '0.0.0.0', 'url': '', 'status_code': 200, 'connection_timeout': 10, 'to_remote_port': 80}, 'pattern_check': {'to_remote_ip': '0.0.0.0', 'send': '', 'to_remote_port': 80, 'expect': '', 'timeout': 10}, 'smtp_check': {'to_remote_ip': '0.0.0.0', 'from_local_ip': '0.0.0.0', 'connection_timeout': 10, 'to_remote_port': 80, 'helo_name': ''}, 'icmp_check': {'timeout': 10}, 'ip_address': '10.1.1.1', 'http_get': {'delay_before_retry': 10, 'from_local_ip': '10.1.1.1', 'url': '/', 'status_code': 200, 'connection_timeout': 10, 'to_remote_port': 80}}, {'health_check': 'HTTPS_GET', 'weight': 1, 'maintenance_mode': False, 'label': 'real3_v4', 'tcp_check': {'to_remote_ip': '0.0.0.0', 'from_local_ip': '0.0.0.0', 'connection_timeout': 10}, 'https_get': {'delay_before_retry': 10, 'from_local_ip': '10.1.1.1', 'url': '/', 'status_code': 200, 'connection_timeout': 10, 'to_remote_port': 80}, 'pattern_check': {'to_remote_ip': '0.0.0.0', 'send': '', 'to_remote_port': 80, 'expect': '', 'timeout': 10}, 'smtp_check': {'to_remote_ip': '0.0.0.0', 'from_local_ip': '0.0.0.0', 'connection_timeout': 10, 'to_remote_port': 80, 'helo_name': ''}, 'icmp_check': {'timeout': 10}, 'ip_address': '10.1.1.1', 'http_get': {'delay_before_retry': 10, 'from_local_ip': '0.0.0.0', 'url': '', 'status_code': 200, 'connection_timeout': 10, 'to_remote_port': 80}}, {'health_check': 'ICMP_CHECK', 'weight': 1, 'maintenance_mode': False, 'label': 'real4_v4', 'tcp_check': {'to_remote_ip': '0.0.0.0', 'from_local_ip': '0.0.0.0', 'connection_timeout': 10}, 'https_get': {'delay_before_retry': 10, 'from_local_ip': '0.0.0.0', 'url': '', 'status_code': 200, 'connection_timeout': 10, 'to_remote_port': 80}, 'pattern_check': {'to_remote_ip': '0.0.0.0', 'send': '', 'to_remote_port': 80, 'expect': '', 'timeout': 10}, 'smtp_check': {'to_remote_ip': '0.0.0.0', 'from_local_ip': '0.0.0.0', 'connection_timeout': 10, 'to_remote_port': 80, 'helo_name': ''}, 'icmp_check': {'timeout': 10}, 'ip_address': '10.1.1.1', 'http_get': {'delay_before_retry': 10, 'from_local_ip': '0.0.0.0', 'url': '', 'status_code': 200, 'connection_timeout': 10, 'to_remote_port': 80}}], 'ipv6': [{'health_check': 'NA', 'weight': 1, 'maintenance_mode': False, 'label': 'real1_v6', 'tcp_check': {'to_remote_ip': '::', 'from_local_ip': '2001::2', 'connection_timeout': 10}, 'https_get': {'delay_before_retry': 10, 'from_local_ip': '::', 'url': '', 'status_code': 200, 'connection_timeout': 10, 'to_remote_port': 80}, 'pattern_check': {'to_remote_ip': '::', 'send': '', 'to_remote_port': 80, 'expect': '', 'timeout': 10}, 'smtp_check': {'to_remote_ip': '::', 'from_local_ip': '::', 'connection_timeout': 10, 'to_remote_port': 80, 'helo_name': ''}, 'icmp_check': {'timeout': 10}, 'ip_address': '2001::1', 'http_get': {'delay_before_retry': 10, 'from_local_ip': '::', 'url': '', 'status_code': 200, 'connection_timeout': 10, 'to_remote_port': 80}}, {'health_check': 'NA', 'weight': 1, 'maintenance_mode': False, 'label': 'real2_v6', 'tcp_check': {'to_remote_ip': '::', 'from_local_ip': '::', 'connection_timeout': 10}, 'https_get': {'delay_before_retry': 10, 'from_local_ip': '::', 'url': '', 'status_code': 200, 'connection_timeout': 10, 'to_remote_port': 80}, 'pattern_check': {'to_remote_ip': '::', 'send': '', 'to_remote_port': 80, 'expect': '', 'timeout': 10}, 'smtp_check': {'to_remote_ip': '::', 'from_local_ip': '2001::2', 'connection_timeout': 10, 'to_remote_port': 25, 'helo_name': ''}, 'icmp_check': {'timeout': 10}, 'ip_address': '2001::1', 'http_get': {'delay_before_retry': 10, 'from_local_ip': '::', 'url': '', 'status_code': 200, 'connection_timeout': 10, 'to_remote_port': 80}}, {'health_check': 'NA', 'weight': 1, 'maintenance_mode': False, 'label': 'real3_v6', 'tcp_check': {'to_remote_ip': '::', 'from_local_ip': '::', 'connection_timeout': 10}, 'https_get': {'delay_before_retry': 10, 'from_local_ip': '::', 'url': '', 'status_code': 200, 'connection_timeout': 10, 'to_remote_port': 80}, 'pattern_check': {'to_remote_ip': '::', 'send': '', 'to_remote_port': 80, 'expect': 'HTTP', 'timeout': 10}, 'smtp_check': {'to_remote_ip': '::', 'from_local_ip': '::', 'connection_timeout': 10, 'to_remote_port': 80, 'helo_name': ''}, 'icmp_check': {'timeout': 10}, 'ip_address': '2001::1', 'http_get': {'delay_before_retry': 10, 'from_local_ip': '::', 'url': '', 'status_code': 200, 'connection_timeout': 10, 'to_remote_port': 80}}]}, 'fallback_server': {'ipv4': [{'ip_address': '10.1.1.1', 'label': 'fall1_v4'}], 'ipv6': [{'ip_address': '2001::1', 'label': 'fall1_v6'}]}}))
def slb(self, **kwargs): ''' slb getter and setter format: {"ip": {"ipv4": [{"label": "one", "ip_address": ["10.1.1.4"] }, {"label": "many", "ip_address": ["10.1.1.1", "10.1.1.2", "10.1.1.3", "10.1.1.4"] }, {"label": "ANY", "ip_address": ["ANY"] }], "ipv6": [{"label": "one", "ip_address": ["2001::1"] }, {"label": "many", "ip_address": ["2001::1", "2001::2", "2001::3", "2001::4"] }, {"label": "ANY", "ip_address": ["ANY"] }] }, "service_group": {"ipv4": [{"label": "group1_v4", "protocol": "TCP", "application_port": [80, 443] }], "ipv6": [{"label": "group1_v6", "protocol": "TCP", "application_port": [80, 443] }] }, "real_server_group": {"ipv4": [{"label": "real1_v4", "ip_address": "10.1.1.1", "weight": 1, "health_check": "NA", "maintenance_mode": False }, {"label": "real2_v4", "ip_address": "10.1.1.1", "weight": 1, "health_check": "HTTP_GET", "http_get": {"url": "/", "status_code": 200, "from_local_ip": "10.1.1.1", "to_remote_port": 80, "connection_timeout": 10, "delay_before_retry": 10 }, "maintenance_mode": False }, {"label": "real3_v4", "ip_address": "10.1.1.1", "weight": 1, "health_check": "HTTPS_GET", "https_get": {"url": "/", "status_code": 200, "from_local_ip": "10.1.1.1", "to_remote_port": 80, "connection_timeout": 10, "delay_before_retry": 10 }, "maintenance_mode": False }, {"label": "real4_v4", "ip_address": "10.1.1.1", "weight": 1, "health_check": "ICMP_CHECK", "icmp_check": {"timeout": 10}, "maintenance_mode": False }] "ipv6": [{"label": "real1_v6", "ip_address": "2001::1", "weight": 1, "health_check": "TCP_CHECK", "tcp_check": {"from_local_ip": "2001::2", "to_remote_ip": "2001::3", "connection_timeout": 10 }, "maintenance_mode": False }, {"label": "real2_v6", "ip_address": "2001::1", "weight": 1, "health_check": "SMTP_CHECK", "smtp_check": {"helo_name": "", "from_local_ip": "2001::2", "to_remote_ip": "2001::3", "to_remote_port": 25, "connection_timeout": 10 }, "maintenance_mode": False }, {"label": "real3_v6", "ip_address": "2001::1", "weight": 1, "health_check": "PATTERN_CHECK", "pattern_check": {"send": "GET / HTTP/1.0\r\n\r\n", "expect": "HTTP", "to_remote_ip": "2001::3", "to_remote_port": 80, "timeout": 10 }, "maintenance_mode": False }] }, "fallback_server": {"ipv4": [{"label": "fall1_v4", "ip_address": "10.1.1.1" }, {"label": "NA", "ip_address": "NA" }], "ipv6": [{"label": "fall1_v6", "ip_address": "2001::1" }, {"label": "NA", "ip_address": "NA" }] }, "property": [{"label": "proper1", "forward_method": "NAT", "balance_mode": "Round-robin", "health_check_interval": 10, "persistence": 10, "ipv4_netmask": "255.255.255.0", "ipv6_prefix": 64 }] "policy": {"ipv4": [{"source_ip": "one", "destination_ip": "many", "service_group": "group1_v4", "action": "VIP", "real_server_group": "real1_v4", "fallback_server": "fall1_v4", "property": "proper1" }], "ipv6": [{"source_ip": "one", "destination_ip": "many", "service_group": "group1_v6", "action": "Accept", "real_server_group": "", "fallback_server": "", "property": "" }] } } ''' import libs.login if False == libs.login.cklogin(): raise _.HTTPRedirect('/') import ml_w_slb as slb import json import libs.tools if "slb" in kwargs: dat = kwargs res = {} opt = [] goptTmp = [] nopt = {} for k in dat: if "@@" in k: kk = k.split("@@") if "list" == type(kk).__name__ and len(kk) > 1: goptTmp = self._unstructDict(dat, "@@") gopt = {} for k in goptTmp: if not "slb" == k: gopt[k] = goptTmp[k] for k in gopt: if "property" == k: po = [] da = self._unstructDict(gopt["property"], "@@") for kk in da: po.append(da[kk]) nopt["property"] = po elif not "real_server_group" == k: da = self._unstructDict(gopt[k], "@@") db = {} dc = [] nopt[k] = {} for kk in da: db = self._unstructDict(da[kk], "@@") for ka in db: dc.append(db[ka]) nopt[k][kk] = dc else: da = self._unstructDict(gopt[k], "@@") db = {} dc = [] nopt[k] = {} for kk in da: if not kk in nopt[k]: nopt[k][kk] = [] db = self._unstructDict(da[kk], "@@") for ka in db: dc = self._unstructDict(db[ka], "@@") for kb in dc: if "label" == kb: nopt[k][kk].append({kb : dc[kb]}) else: de = self._unstructDict(dc[kb], "@@") for kc in de: df = {} dg = de[kc] df = self._unstructDict(dg, "@@") nopt[k][kk].append({kb: df}) libs.tools.v(nopt); res = slb.set(user = self.getUser(), cfg = nopt) if False == res[0]: return json.dumps([res[0], libs.tools.translateMessage(res[1])]) else: return json.dumps(res) else: # getter return json.dumps(slb.get())