Пример #1
0
    def prepare_config_commands(self, config_dict, cmd):
        """
            function to parse the input dict and form the prepare the config commands
            :rtype: A str
            :returns: The command necessary to configure the static routes
        """

        dhcp = config_dict.get('dhcp')
        distance_metric = config_dict.get('distance_metric')
        forward_router_address = config_dict.get('forward_router_address')
        global_route_config = config_dict.get('global')
        interface = config_dict.get('interface')
        multicast = config_dict.get('multicast')
        name = config_dict.get('name')
        permanent = config_dict.get('permanent')
        tag = config_dict.get('tag')
        track = config_dict.get('track')
        dest = config_dict.get('dest')
        temp_dest = dest.split('/')
        if temp_dest and ':' not in dest:
            dest = validate_n_expand_ipv4(self._module, {'address': dest})

        cmd = cmd + dest
        if interface:
            cmd = cmd + ' {0}'.format(interface)
        if forward_router_address:
            cmd = cmd + ' {0}'.format(forward_router_address)
        if dhcp:
            cmd = cmd + ' DHCP'
        if distance_metric:
            cmd = cmd + ' {0}'.format(distance_metric)
        if global_route_config:
            cmd = cmd + ' global'
        if multicast:
            cmd = cmd + ' multicast'
        if name:
            cmd = cmd + ' name {0}'.format(name)
        if permanent:
            cmd = cmd + ' permanent'
        elif track:
            cmd = cmd + ' track {0}'.format(track)
        if tag:
            cmd = cmd + ' tag {0}'.format(tag)

        return cmd
Пример #2
0
    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