def get_acl(self, acl, value): """Lookup the value in the ACL. :param acl: acl id or a file. :type acl: ``integer`` or a file path passed as ``string`` :param value: value to lookup :type value: ``string`` :return: matching patterns associated with ACL. :rtype: ``string`` Usage:: >>> from haproxyadmin import haproxy >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy') >>> hap.show_acl(acl=4) ['0x2318120 /static/js/', '0x23181c0 /static/css/'] >>> hap.get_acl(acl=4, value='/foo') 'type=beg, case=sensitive, match=no' >>> hap.get_acl(acl=4, value='/static/js/') 'type=beg, case=sensitive, match=yes, idx=tree, pattern="/static/js/"' """ if isint(acl): cmd = "get acl #{} {}".format(acl, value) else: cmd = "get acl {} {}".format(acl, value) get_results = cmd_across_all_procs(self._hap_processes, 'command', cmd) get_info_proc1 = get_results[0][1] if not check_output(get_info_proc1): raise ValueError(get_info_proc1) return get_info_proc1
def get_map(self, mapid, value): """Lookup the value in the map. :param mapid: map id or a file. :type mapid: ``integer`` or a file path passed as ``string`` :param value: value to lookup. :type value: ``string`` :return: matching patterns associated with map. :rtype: ``string`` Usage:: >>> from haproxyadmin import haproxy >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy') >>> hap.show_map(0) ['0x1a78980 11 new2', '0x1b15c00 22 0'] >>> hap.get_map(0, '11') 'type=str, case=sensitive, found=yes, idx=tree, key="11", value="new2", type="str"' >>> hap.get_map(0, '10') 'type=str, case=sensitive, found=no' """ if isint(mapid): cmd = "get map #{} {}".format(mapid, value) elif os.path.isfile(mapid): cmd = "get map {} {}".format(mapid, value) else: raise ValueError("Invalid input") get_results = cmd_across_all_procs(self._hap_processes, 'command', cmd) get_info_proc1 = get_results[0][1] if not check_output(get_info_proc1): raise CommandFailed(get_info_proc1[0]) return get_info_proc1
def clear_acl(self, acl): """Remove all entries from a acl. :param acl: acl id or a file. :type acl: ``integer`` or a file path passed as ``string`` :return: True if command succeeds otherwise False :rtype: bool Usage:: >>> from haproxyadmin import haproxy >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy') >>> hap.clear_acl(acl=4) True >>> hap.clear_acl(acl='/etc/haproxy/bl_frontend') True """ if isint(acl): cmd = "clear acl #{}".format(acl) elif os.path.isfile(acl): cmd = "clear acl {}".format(acl) else: raise ValueError("Invalid input") results = cmd_across_all_procs(self._hap_processes, 'command', cmd) return check_command(results)
def clear_map(self, mapid): """Remove all entries from a mapid. :param mapid: map id or a file :type mapid: ``integer`` or a file path passed as ``string`` :return: ``True`` if command succeeds otherwise ``False`` :rtype: ``bool`` Usage:: >>> from haproxyadmin import haproxy >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy') >>> hap.clear_map(0) True >>> hap.clear_map(mapid='/etc/haproxy/bl_frontend') True """ if isint(mapid): cmd = "clear map #{}".format(mapid) elif os.path.isfile(mapid): cmd = "clear map {}".format(mapid) else: raise ValueError("Invalid input") results = cmd_across_all_procs(self._hap_processes, 'command', cmd) return check_command(results)
def add_acl(self, acl, pattern): """Add an entry into the acl. :param acl: acl id or a file. :type acl: ``integer`` or a file path passed as ``string`` :param pattern: entry to add. :type pattern: ``string`` :return: ``True`` if command succeeds otherwise ``False`` :rtype: ``bool`` Usage:: >>> from haproxyadmin import haproxy >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy') >>> hap.show_acl(acl=4) ['0x23181c0 /static/css/'] >>> hap.add_acl(acl=4, pattern='/foo/' ) True >>> hap.show_acl(acl=4) ['0x23181c0 /static/css/', '0x238f790 /foo/'] """ if isint(acl): cmd = "add acl #{} {}".format(acl, pattern) elif os.path.isfile(acl): cmd = "add acl {} {}".format(acl, pattern) else: raise ValueError("Invalid input") results = cmd_across_all_procs(self._hap_processes, 'command', cmd) return check_command(results)
def add_map(self, mapid, key, value): """Add an entry into the map. :param mapid: map id or a file. :type mapid: ``integer`` or a file path passed as ``string`` :param key: key to add. :type key: ``string`` :param value: Value assciated to the key. :type value: ``string`` :return: ``True`` if command succeeds otherwise ``False``. :rtype: ``bool`` Usage:: >>> from haproxyadmin import haproxy >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy') >>> hap.show_map(0) ['0x1a78b20 1 www.foo.com-1'] >>> hap.add_map(0, '9', 'foo') True >>> hap.show_map(0) ['0x1a78b20 1 www.foo.com-1', '0x1b15c80 9 foo'] """ if isint(mapid): cmd = "add map #{} {} {}".format(mapid, key, value) elif os.path.isfile(mapid): cmd = "add map {} {} {}".format(mapid, key, value) else: raise ValueError("Invalid input") results = cmd_across_all_procs(self._hap_processes, 'command', cmd) return check_command(results)
def get_acl(self, acl, value): """Lookup the value in the ACL. :param acl: acl id or a file. :type acl: ``integer`` or a file path passed as ``string`` :param value: value to lookup :type value: ``string`` :return: matching patterns associated with ACL. :rtype: ``string`` Usage:: >>> from haproxyadmin import haproxy >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy') >>> hap.show_acl(acl=4) ['0x2318120 /static/js/', '0x23181c0 /static/css/'] >>> hap.get_acl(acl=4, value='/foo') 'type=beg, case=sensitive, match=no' >>> hap.get_acl(acl=4, value='/static/js/') 'type=beg, case=sensitive, match=yes, idx=tree, pattern="/static/js/"' """ if isint(acl): cmd = "get acl #{} {}".format(acl, value) elif os.path.isfile(acl): cmd = "get acl {} {}".format(acl, value) else: raise ValueError("Invalid input") get_results = cmd_across_all_procs(self._hap_processes, 'command', cmd) get_info_proc1 = get_results[0][1] if not check_output(get_info_proc1): raise ValueError(get_info_proc1) return get_info_proc1
def get_map(self, mapid, value): """Lookup the value in the map. :param mapid: map id or a file. :type mapid: ``integer`` or a file path passed as ``string`` :param value: value to lookup. :type value: ``string`` :return: matching patterns associated with map. :rtype: ``string`` Usage:: >>> from haproxyadmin import haproxy >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy') >>> hap.show_map(0) ['0x1a78980 11 new2', '0x1b15c00 22 0'] >>> hap.get_map(0, '11') 'type=str, case=sensitive, found=yes, idx=tree, key="11", value="new2", type="str"' >>> hap.get_map(0, '10') 'type=str, case=sensitive, found=no' """ if isint(mapid): cmd = "get map #{} {}".format(mapid, value) else: cmd = "get map {} {}".format(mapid, value) get_results = cmd_across_all_procs(self._hap_processes, 'command', cmd) get_info_proc1 = get_results[0][1] if not check_output(get_info_proc1): raise CommandFailed(get_info_proc1[0]) return get_info_proc1
def clear_acl(self, acl): """Remove all entries from a acl. :param acl: acl id or a file. :type acl: ``integer`` or a file path passed as ``string`` :return: True if command succeeds otherwise False :rtype: bool Usage:: >>> from haproxyadmin import haproxy >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy') >>> hap.clear_acl(acl=4) True >>> hap.clear_acl(acl='/etc/haproxy/bl_frontend') True """ if isint(acl): cmd = "clear acl #{}".format(acl) else: cmd = "clear acl {}".format(acl) results = cmd_across_all_procs(self._hap_processes, 'command', cmd) return check_command(results)
def clear_map(self, mapid): """Remove all entries from a mapid. :param mapid: map id or a file :type mapid: ``integer`` or a file path passed as ``string`` :return: ``True`` if command succeeds otherwise ``False`` :rtype: ``bool`` Usage:: >>> from haproxyadmin import haproxy >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy') >>> hap.clear_map(0) True >>> hap.clear_map(mapid='/etc/haproxy/bl_frontend') True """ if isint(mapid): cmd = "clear map #{}".format(mapid) else: cmd = "clear map {}".format(mapid) results = cmd_across_all_procs(self._hap_processes, 'command', cmd) return check_command(results)
def add_map(self, mapid, key, value): """Add an entry into the map. :param mapid: map id or a file. :type mapid: ``integer`` or a file path passed as ``string`` :param key: key to add. :type key: ``string`` :param value: Value assciated to the key. :type value: ``string`` :return: ``True`` if command succeeds otherwise ``False``. :rtype: ``bool`` Usage:: >>> from haproxyadmin import haproxy >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy') >>> hap.show_map(0) ['0x1a78b20 1 www.foo.com-1'] >>> hap.add_map(0, '9', 'foo') True >>> hap.show_map(0) ['0x1a78b20 1 www.foo.com-1', '0x1b15c80 9 foo'] """ if isint(mapid): cmd = "add map #{} {} {}".format(mapid, key, value) else: cmd = "add map {} {} {}".format(mapid, key, value) results = cmd_across_all_procs(self._hap_processes, 'command', cmd) return check_command(results)
def add_acl(self, acl, pattern): """Add an entry into the acl. :param acl: acl id or a file. :type acl: ``integer`` or a file path passed as ``string`` :param pattern: entry to add. :type pattern: ``string`` :return: ``True`` if command succeeds otherwise ``False`` :rtype: ``bool`` Usage:: >>> from haproxyadmin import haproxy >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy') >>> hap.show_acl(acl=4) ['0x23181c0 /static/css/'] >>> hap.add_acl(acl=4, pattern='/foo/' ) True >>> hap.show_acl(acl=4) ['0x23181c0 /static/css/', '0x238f790 /foo/'] """ if isint(acl): cmd = "add acl #{} {}".format(acl, pattern) else: cmd = "add acl {} {}".format(acl, pattern) results = cmd_across_all_procs(self._hap_processes, 'command', cmd) return check_command(results)
def show_acl(self, aclid=None): """Dump info about acls. Without argument, the list of all available acls is returned. If a aclid is specified, its contents are dumped. :param aclid: (optional) acl id or a file :type aclid: ``integer`` or a file path passed as ``string`` :return: a list with the acls :rtype: ``list`` Usage:: >>> from haproxyadmin import haproxy >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy') >>> hap.show_acl(aclid=6) ['0x1d09730 ver%3A27%3Bvar%3A0'] >>> hap.show_acl() ['# id (file) description', "1 () acl 'ssl_fc' file '/etc/haproxy/haproxy.cfg' line 83", "2 () acl 'src' file '/etc/haproxy/haproxy.cfg' line 95", "3 () acl 'path_beg' file '/etc/haproxy/haproxy.cfg' line 97", ] """ if aclid is not None: if isint(aclid): cmd = "show acl #{}".format(aclid) else: cmd = "show acl {}".format(aclid) else: cmd = "show acl" acl_info = cmd_across_all_procs(self._hap_processes, 'command', cmd, full_output=True) # ACL can't be different per process thus we only return the acl # content found in 1st process. acl_info_proc1 = acl_info[0][1] if not check_output(acl_info_proc1): raise CommandFailed(acl_info_proc1[0]) if len(acl_info_proc1) == 1 and not acl_info_proc1[0]: return [] else: return acl_info_proc1
def show_map(self, mapid=None): """Dump info about maps. Without argument, the list of all available maps is returned. If a mapid is specified, its contents are dumped. :param mapid: (optional) map id or a file. :type mapid: ``integer`` or a file path passed as ``string`` :return: a list with the maps. :rtype: ``list`` Usage:: >>> from haproxyadmin import haproxy >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy') >>> hap.show_map() ['# id (file) description', "0 (/etc/haproxy/v-m1-bk) pattern loaded ...... line 82", ] >>> hap.show_map(mapid=0) ['0x1a78ab0 0 www.foo.com-0', '0x1a78b20 1 www.foo.com-1'] """ if mapid is not None: if isint(mapid): cmd = "show map #{}".format(mapid) else: cmd = "show map {}".format(mapid) else: cmd = "show map" map_info = cmd_across_all_procs(self._hap_processes, 'command', cmd, full_output=True) # map can't be different per process thus we only return the map # content found in 1st process. map_info_proc1 = map_info[0][1] if not check_output(map_info_proc1): raise CommandFailed(map_info_proc1[0]) if len(map_info_proc1) == 1 and not map_info_proc1[0]: return [] else: return map_info_proc1
def del_map(self, mapid, key): """Delete all the map entries from the map corresponding to the key. :param mapid: map id or a file. :type mapid: ``integer`` or a file path passed as ``string``. :param key: key to delete :type key: ``string`` :return: ``True`` if command succeeds otherwise ``False``. :rtype: ``bool`` Usage:: >>> from haproxyadmin import haproxy >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy') >>> hap.show_map(0) ['0x1b15cd0 9 foo', '0x1a78980 11 bar'] >>> hap.del_map(0, '0x1b15cd0') True >>> hap.show_map(0) ['0x1a78980 11 bar'] >>> hap.add_map(0, '22', 'bar22') True >>> hap.show_map(0) ['0x1a78980 11 bar', '0x1b15c00 22 bar22'] >>> hap.del_map(0, '22') True >>> hap.show_map(0) ['0x1a78980 11 bar'] """ if key.startswith('0x'): key = "#{}".format(key) if isint(mapid): cmd = "del map #{} {}".format(mapid, key) elif os.path.isfile(mapid): cmd = "del map {} {}".format(mapid, key) else: raise ValueError("Invalid input") results = cmd_across_all_procs(self._hap_processes, 'command', cmd) return check_command(results)
def set_map(self, mapid, key, value): """Modify the value corresponding to each key in a map. mapid is the #<id> or <file> returned by :func:`show_map <haproxyadmin.haproxy.HAProxy.show_map>`. :param mapid: map id or a file. :type mapid: ``integer`` or a file path passed as ``string`` :param key: key id :type key: ``string`` :param value: value to set for the key. :type value: ``string`` :return: ``True`` if command succeeds otherwise ``False``. :rtype: ``bool`` Usage:: >>> from haproxyadmin import haproxy >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy') >>> hap.show_map(0) ['0x1a78980 11 9', '0x1b15c00 22 0'] >>> hap.set_map(0, '11', 'new') True >>> hap.show_map(0) ['0x1a78980 11 new', '0x1b15c00 22 0'] >>> hap.set_map(0, '0x1a78980', 'new2') True >>> hap.show_map(0) ['0x1a78980 11 new2', '0x1b15c00 22 0'] """ if key.startswith('0x'): key = "#{}".format(key) if isint(mapid): cmd = "set map #{} {} {}".format(mapid, key, value) elif os.path.isfile(mapid): cmd = "set map {} {} {}".format(mapid, key, value) else: raise ValueError("Invalid input") results = cmd_across_all_procs(self._hap_processes, 'command', cmd) return check_command(results)
def del_acl(self, acl, key): """Delete all the acl entries from the acl corresponding to the key. :param acl: acl id or a file :type acl: ``integer`` or a file path passed as ``string`` :param key: key to delete. :type key: ``string`` :return: ``True`` if command succeeds otherwise ``False``. :rtype: ``bool`` Usage:: >>> from haproxyadmin import haproxy >>> hap = haproxy.HAProxy(socket_dir='/run/haproxy') >>> hap.show_acl(acl=4) ['0x23181c0 /static/css/', '0x238f790 /foo/', '0x238f810 /bar/'] >>> hap.del_acl(acl=4, key='/static/css/') True >>> hap.show_acl(acl=4) ['0x238f790 /foo/', '0x238f810 /bar/'] >>> hap.del_acl(acl=4, key='0x238f790') True >>> hap.show_acl(acl=4) ['0x238f810 /bar/'] """ if key.startswith('0x'): key = "#{}".format(key) if isint(acl): cmd = "del acl #{} {}".format(acl, key) elif os.path.isfile(acl): cmd = "del acl {} {}".format(acl, key) else: raise ValueError("Invalid input") results = cmd_across_all_procs(self._hap_processes, 'command', cmd) return check_command(results)