def test_slb_g02(self): """ slb_g02 """ slb = { "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"] } ], "ipv6": [ { "label": "one", "ip_address": ["2001::1"] }, { "label": "many", "ip_address": ["2001::1", "2001::2", "2001::3", "2001::4"] } ] }, "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": "", "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" } ], "ipv6": [ { "label": "fall1_v6", "ip_address": "2001::1" } ] }, "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": "" }, ] } } self.maxDiff = None e = ml_w_slb.set(None, slb) self.assertTrue(e[0], e[1]) f = open(os.path.join("running", "slb.txt"), "r") e = f.readlines() f.close() if "json" == ml_system.CFG_TYPE: self.assertEqual(e, ['{"slb-server-1": "", "slb-server-2": "9.8.7.6", "domain-name": "test.domain.org", "hostname": "test.host"}']) if "jcfg" == ml_system.CFG_TYPE: self.assertEqual(e, ['slb {\n', ' ip {\n', ' ipv4-array {\n', ' ipv4 { #1\n', ' label one\n', ' ip_address-array {\n', ' ip_address 10.1.1.4 #1\n', ' }\n', ' }\n', ' ipv4 { #2\n', ' label many\n', ' ip_address-array {\n', ' ip_address 10.1.1.1 #1\n', ' ip_address 10.1.1.2 #2\n', ' ip_address 10.1.1.3 #3\n', ' ip_address 10.1.1.4 #4\n', ' }\n', ' }\n', ' }\n', ' ipv6-array {\n', ' ipv6 { #1\n', ' label one\n', ' ip_address-array {\n', ' ip_address 2001::1 #1\n', ' }\n', ' }\n', ' ipv6 { #2\n', ' label many\n', ' ip_address-array {\n', ' ip_address 2001::1 #1\n', ' ip_address 2001::2 #2\n', ' ip_address 2001::3 #3\n', ' ip_address 2001::4 #4\n', ' }\n', ' }\n', ' }\n', ' }\n', ' service_group {\n', ' ipv4-array {\n', ' ipv4 { #1\n', ' label group1_v4\n', ' protocol TCP\n', ' application_port-array {\n', ' application_port 80 #1\n', ' application_port 443 #2\n', ' }\n', ' }\n', ' }\n', ' ipv6-array {\n', ' ipv6 { #1\n', ' label group1_v6\n', ' protocol TCP\n', ' application_port-array {\n', ' application_port 80 #1\n', ' application_port 443 #2\n', ' }\n', ' }\n', ' }\n', ' }\n', ' real_server_group {\n', ' ipv4-array {\n', ' ipv4 { #1\n', ' label real1_v4\n', ' ip_address 10.1.1.1\n', ' }\n', ' ipv4 { #2\n', ' label real2_v4\n', ' ip_address 10.1.1.1\n', ' health_check HTTP_GET\n', ' http_get {\n', ' url /\n', ' from_local_ip 10.1.1.1\n', ' }\n', ' }\n', ' ipv4 { #3\n', ' label real3_v4\n', ' ip_address 10.1.1.1\n', ' health_check HTTPS_GET\n', ' https_get {\n', ' url /\n', ' from_local_ip 10.1.1.1\n', ' }\n', ' }\n', ' ipv4 { #4\n', ' label real4_v4\n', ' ip_address 10.1.1.1\n', ' health_check ICMP_CHECK\n', ' }\n', ' }\n', ' ipv6-array {\n', ' ipv6 { #1\n', ' label real1_v6\n', ' ip_address 2001::1\n', ' tcp_check {\n', ' from_local_ip 2001::2\n', ' }\n', ' }\n', ' ipv6 { #2\n', ' label real2_v6\n', ' ip_address 2001::1\n', ' smtp_check {\n', ' from_local_ip 2001::2\n', ' to_remote_port 25\n', ' }\n', ' }\n', ' ipv6 { #3\n', ' label real3_v6\n', ' ip_address 2001::1\n', ' pattern_check {\n', ' expect HTTP\n', ' }\n', ' }\n', ' }\n', ' }\n', ' fallback_server {\n', ' ipv4-array {\n', ' ipv4 { #1\n', ' label fall1_v4\n', ' ip_address 10.1.1.1\n', ' }\n', ' }\n', ' ipv6-array {\n', ' ipv6 { #1\n', ' label fall1_v6\n', ' ip_address 2001::1\n', ' }\n', ' }\n', ' }\n', ' policy {\n', ' ipv4-array {\n', ' ipv4 { #1\n', ' source_ip one\n', ' destination_ip many\n', ' service_group group1_v4\n', ' real_server_group real1_v4\n', ' fallback_server fall1_v4\n', ' property proper1\n', ' }\n', ' }\n', ' ipv6-array {\n', ' ipv6 { #1\n', ' source_ip one\n', ' destination_ip many\n', ' service_group group1_v6\n', ' action Accept\n', ' }\n', ' }\n', ' }\n', '}\n'])
def run(self): e = ml_w_slb.set(None, slb, threading.RLock())
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())