update acl (#57268)

pull/57450/head
xuxiaowei0512 6 years ago committed by Nathaniel Case
parent 6d21cebd7d
commit e009936f87

@ -427,7 +427,7 @@ class BaseAcl(object):
if self.acl_type:
conf_str += "<aclType></aclType>"
if self.acl_num:
if self.acl_num or self.acl_name.isdigit():
conf_str += "<aclNumber></aclNumber>"
if self.acl_step:
conf_str += "<aclStep></aclStep>"
@ -444,12 +444,11 @@ class BaseAcl(object):
xml_str = recv_xml.replace('\r', '').replace('\n', '').\
replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\
replace('xmlns="http://www.huawei.com/netconf/vrp"', "")
root = ElementTree.fromstring(xml_str)
# parse acl
acl_info = root.findall(
"data/acl/aclGroups/aclGroup")
"acl/aclGroups/aclGroup")
if acl_info:
for tmp in acl_info:
tmp_dict = dict()
@ -460,25 +459,46 @@ class BaseAcl(object):
self.cur_acl_cfg["acl_info"].append(tmp_dict)
if self.cur_acl_cfg["acl_info"]:
find_list = list()
for tmp in self.cur_acl_cfg["acl_info"]:
find_flag = True
cur_cfg_dict = dict()
exist_cfg_dict = dict()
if self.acl_name:
if self.acl_name.isdigit() and tmp.get("aclNumber"):
cur_cfg_dict["aclNumber"] = self.acl_name
exist_cfg_dict["aclNumber"] = tmp.get("aclNumber")
else:
cur_cfg_dict["aclNumOrName"] = self.acl_name
exist_cfg_dict["aclNumOrName"] = tmp.get("aclNumOrName")
if self.acl_type:
cur_cfg_dict["aclType"] = self.acl_type
exist_cfg_dict["aclType"] = tmp.get("aclType")
if self.acl_num:
cur_cfg_dict["aclNumber"] = self.acl_num
exist_cfg_dict["aclNumber"] = tmp.get("aclNumber")
if self.acl_step:
cur_cfg_dict["aclStep"] = self.acl_step
exist_cfg_dict["aclStep"] = tmp.get("aclStep")
if self.acl_description:
cur_cfg_dict["aclDescription"] = self.acl_description
exist_cfg_dict["aclDescription"] = tmp.get("aclDescription")
if self.acl_name and tmp.get("aclNumOrName") != self.acl_name:
find_flag = False
if self.acl_type and tmp.get("aclType") != self.acl_type:
find_flag = False
if self.acl_num and tmp.get("aclNumber") != self.acl_num:
find_flag = False
if self.acl_step and tmp.get("aclStep") != self.acl_step:
find_flag = False
if self.acl_description and tmp.get("aclDescription") != self.acl_description:
find_flag = False
if cur_cfg_dict == exist_cfg_dict:
find_bool = True
else:
find_bool = False
find_list.append(find_bool)
if find_flag:
for mem in find_list:
if mem:
find_flag = True
break
else:
find_flag = False
else:
find_flag = False
if self.state == "present":
need_cfg = bool(not find_flag)
elif self.state == "delete_acl":
@ -593,7 +613,7 @@ class BaseAcl(object):
# parse base rule
base_rule_info = root.findall(
"data/acl/aclGroups/aclGroup/aclRuleBas4s/aclRuleBas4")
"acl/aclGroups/aclGroup/aclRuleBas4s/aclRuleBas4")
if base_rule_info:
for tmp in base_rule_info:
tmp_dict = dict()

@ -602,7 +602,7 @@ class AdvanceAcl(object):
if self.acl_type:
conf_str += "<aclType></aclType>"
if self.acl_num:
if self.acl_num or self.acl_name.isdigit():
conf_str += "<aclNumber></aclNumber>"
if self.acl_step:
conf_str += "<aclStep></aclStep>"
@ -624,7 +624,7 @@ class AdvanceAcl(object):
# parse acl
acl_info = root.findall(
"data/acl/aclGroups/aclGroup")
"acl/aclGroups/aclGroup")
if acl_info:
for tmp in acl_info:
tmp_dict = dict()
@ -635,24 +635,44 @@ class AdvanceAcl(object):
self.cur_acl_cfg["acl_info"].append(tmp_dict)
if self.cur_acl_cfg["acl_info"]:
find_list = list()
for tmp in self.cur_acl_cfg["acl_info"]:
find_flag = True
cur_cfg_dict = dict()
exist_cfg_dict = dict()
if self.acl_name and tmp.get("aclNumOrName") != self.acl_name:
find_flag = False
if self.acl_type and tmp.get("aclType") != self.acl_type:
find_flag = False
if self.acl_num and tmp.get("aclNumber") != self.acl_num:
find_flag = False
if self.acl_step and tmp.get("aclStep") != self.acl_step:
find_flag = False
if self.acl_description and tmp.get("aclDescription") != self.acl_description:
find_flag = False
if self.acl_name:
if self.acl_name.isdigit() and tmp.get("aclNumber"):
cur_cfg_dict["aclNumber"] = self.acl_name
exist_cfg_dict["aclNumber"] = tmp.get("aclNumber")
else:
cur_cfg_dict["aclNumOrName"] = self.acl_name
exist_cfg_dict["aclNumOrName"] = tmp.get("aclNumOrName")
if self.acl_type:
cur_cfg_dict["aclType"] = self.acl_type
exist_cfg_dict["aclType"] = tmp.get("aclType")
if self.acl_num:
cur_cfg_dict["aclNumber"] = self.acl_num
exist_cfg_dict["aclNumber"] = tmp.get("aclNumber")
if self.acl_step:
cur_cfg_dict["aclStep"] = self.acl_step
exist_cfg_dict["aclStep"] = tmp.get("aclStep")
if self.acl_description:
cur_cfg_dict["aclDescription"] = self.acl_description
exist_cfg_dict["aclDescription"] = tmp.get("aclDescription")
if find_flag:
if cur_cfg_dict == exist_cfg_dict:
find_bool = True
else:
find_bool = False
find_list.append(find_bool)
for mem in find_list:
if mem:
find_flag = True
break
else:
find_flag = False
else:
find_flag = False
if self.state == "present":
need_cfg = bool(not find_flag)
@ -1001,7 +1021,7 @@ class AdvanceAcl(object):
# parse advance rule
adv_rule_info = root.findall(
"data/acl/aclGroups/aclGroup/aclRuleAdv4s/aclRuleAdv4")
"acl/aclGroups/aclGroup/aclRuleAdv4s/aclRuleAdv4")
if adv_rule_info:
for tmp in adv_rule_info:
tmp_dict = dict()

@ -122,7 +122,7 @@ updates:
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network.cloudengine.ce import get_config, load_config
from ansible.module_utils.network.cloudengine.ce import get_config, load_config, exec_command
from ansible.module_utils.network.cloudengine.ce import ce_argument_spec
@ -169,9 +169,16 @@ class AclInterface(object):
msg='Error: The len of acl_name is out of [1 - 32].')
if self.interface:
regular = "| ignore-case section include ^interface %s$" % self.interface
result = self.cli_get_config(regular)
if not result:
cmd = "display current-configuration | ignore-case section include ^interface %s$" % self.interface
rc, out, err = exec_command(self.module, cmd)
if rc != 0:
self.module.fail_json(msg=err)
result = str(out).strip()
if result:
tmp = result.split('\n')
if "display" in tmp[0]:
tmp.pop(0)
if not tmp:
self.module.fail_json(
msg='Error: The interface %s is not in the device.' % self.interface)
@ -192,28 +199,36 @@ class AclInterface(object):
def get_existing(self):
""" Get existing config """
regular = "| ignore-case section include ^interface %s$ | include traffic-filter" % self.interface
result = self.cli_get_config(regular)
cmd = "display current-configuration | ignore-case section include ^interface %s$ | include traffic-filter" % self.interface
rc, out, err = exec_command(self.module, cmd)
if rc != 0:
self.module.fail_json(msg=err)
result = str(out).strip()
end = []
if result:
tmp = result.split('\n')
if "display" in tmp[0]:
tmp.pop(0)
for item in tmp:
end.append(item)
end.append(item.strip())
self.cur_cfg["acl interface"] = end
self.existing["acl interface"] = end
def get_end_state(self):
""" Get config end state """
regular = "| ignore-case section include ^interface %s$ | include traffic-filter" % self.interface
result = self.cli_get_config(regular)
cmd = "display current-configuration | ignore-case section include ^interface %s$ | include traffic-filter" % self.interface
rc, out, err = exec_command(self.module, cmd)
if rc != 0:
self.module.fail_json(msg=err)
result = str(out).strip()
end = []
if result:
tmp = result.split('\n')
if "display" in tmp[0]:
tmp.pop(0)
for item in tmp:
item = item[1:-1]
end.append(item)
end.append(item.strip())
self.end_state["acl interface"] = end
def cli_load_config(self, commands):
@ -222,15 +237,6 @@ class AclInterface(object):
if not self.module.check_mode:
load_config(self.module, commands)
def cli_get_config(self, regular):
""" Cli method to get config """
flags = list()
flags.append(regular)
tmp_cfg = get_config(self.module, flags)
return tmp_cfg
def work(self):
""" Work function """

Loading…
Cancel
Save