def validate_ips(self, afi, want=None, have=None): if afi == "ipv4" and want: v4_addr = validate_n_expand_ipv4(self._module, want) if want.get("address") else {} if v4_addr: want["address"] = v4_addr elif afi == "ipv6" and want: if want.get("address"): validate_ipv6(want["address"], self._module) if afi == "ipv4" and have: v4_addr_h = validate_n_expand_ipv4(self._module, have) if have.get("address") else {} if v4_addr_h: have["address"] = v4_addr_h elif afi == "ipv6" and have: if have.get("address"): validate_ipv6(have["address"], self._module)
def delete_l3_attributes(self, have): if have.get("ipv4"): for k, v in iteritems(have["ipv4"]): if v.get("address"): v["address"] = validate_n_expand_ipv4(self._module, v) self.compare( parsers=self.parsers, want=dict(), have={"ipv4": v} ) if have.get("ipv6"): for k, v in iteritems(have["ipv6"]): if v.get("address"): if "/" in v["address"]: validate_ipv6(v["address"], self._module) self.compare( parsers=self.parsers, want=dict(), have={"ipv6": v} )
def _compare(self, want, have): if want != have and self.state != "deleted": self.addcmd(want or have, "name", False) if want.get("ipv4"): for k, v in iteritems(want["ipv4"]): if have.get("ipv4") and have["ipv4"].get(k): h_val = have["ipv4"].pop(k) if v.get("address"): v["address"] = validate_n_expand_ipv4( self._module, v ) h_val["address"] = validate_n_expand_ipv4( self._module, h_val ) if v != h_val: h_val = {} self.compare( self.parsers, want={"ipv4": v}, have={"ipv4": h_val}, ) else: if v.get("address"): v["address"] = validate_n_expand_ipv4( self._module, v ) self.compare( self.parsers, want={"ipv4": v}, have=dict() ) if want.get("ipv6"): for k, v in iteritems(want["ipv6"]): if have.get("ipv6") and have["ipv6"].get(k): h_val = have["ipv6"].pop(k) if v.get("address"): if "/" in v["address"] and "/" in h_val["address"]: validate_ipv6(v["address"], self._module) validate_ipv6(h_val["address"], self._module) if v != h_val: h_val = {} self.compare( self.parsers, want={"ipv6": v}, have={"ipv6": h_val}, ) else: if v.get("address"): if "/" in v["address"]: validate_ipv6(v["address"], self._module) self.compare( self.parsers, want={"ipv6": v}, have=dict() ) if self.state == "replaced" or self.state == "overridden": self.delete_l3_attributes(have)
def _set_config(self, want, have, module): # Set the interface config based on the want and have config commands = [] interface = "interface " + want["name"] # To handle L3 IPV4 configuration if want.get("ipv4"): for each in want.get("ipv4"): if each.get("address") != "dhcp": ip_addr_want = validate_n_expand_ipv4(module, each) each["address"] = ip_addr_want # Convert the want and have dict to set want_dict = dict_to_set(want) have_dict = dict_to_set(have) # To handle L3 IPV4 configuration if want.get("ipv4"): # Get the diff b/w want and have IPV4 if have.get("ipv4"): ipv4 = tuple( set(dict(want_dict).get("ipv4")) - set(dict(have_dict).get("ipv4"))) if ipv4: ipv4 = (ipv4 if self.verify_diff_again( dict(want_dict).get("ipv4"), dict(have_dict).get("ipv4"), ) else ()) else: diff = want_dict - have_dict ipv4 = dict(diff).get("ipv4") if ipv4: for each in ipv4: ipv4_dict = dict(each) if ipv4_dict.get("address") != "dhcp": cmd = "ip address {0}".format(ipv4_dict["address"]) if ipv4_dict.get("secondary"): cmd += " secondary" elif ipv4_dict.get("address") == "dhcp": cmd = "ip address dhcp" if ipv4_dict.get( "dhcp_client") is not None and ipv4_dict.get( "dhcp_hostname"): cmd = "ip address dhcp client-id GigabitEthernet 0/{0} hostname {1}".format( ipv4_dict.get("dhcp_client"), ipv4_dict.get("dhcp_hostname"), ) elif ipv4_dict.get( "dhcp_client" ) and not ipv4_dict.get("dhcp_hostname"): cmd = "ip address dhcp client-id GigabitEthernet 0/{0}".format( ipv4_dict.get("dhcp_client")) elif not ipv4_dict.get( "dhcp_client") and ipv4_dict.get( "dhcp_hostname"): cmd = "ip address dhcp hostname {0}".format( ipv4_dict.get("dhcp_client")) add_command_to_config_list(interface, cmd, commands) # To handle L3 IPV6 configuration if want.get("ipv6"): # Get the diff b/w want and have IPV6 if have.get("ipv6"): ipv6 = tuple( set(dict(want_dict).get("ipv6")) - set(dict(have_dict).get("ipv6"))) else: diff = want_dict - have_dict ipv6 = dict(diff).get("ipv6") if ipv6: for each in ipv6: ipv6_dict = dict(each) validate_ipv6(ipv6_dict.get("address"), module) cmd = "ipv6 address {0}".format(ipv6_dict.get("address")) add_command_to_config_list(interface, cmd, commands) return commands