Beispiel #1
0
    def _state_overridden(self, want, have, diff_members, diff_portchannels):
        """ The command generator when state is overridden

        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        requests = list()
        commands = list()
        delete_list = list()
        delete_list = get_diff(have, want, ["name", "member"])
        delete_members, delete_portchannels = self.diff_list_for_member_creation(delete_list)
        replaced_list = list()
        for i in want:
            list_obj = search_obj_in_list(i['name'], delete_members, "name")
            if list_obj:
                replaced_list.append(list_obj)
        requests = self.get_delete_lag_interfaces_requests(replaced_list)
        commands.extend(update_states(replaced_list, "overridden"))
        delete_members = get_diff(delete_members, replaced_list, ["name", "member"])
        commands_overridden, requests_overridden = self.template_for_lag_deletion(have, delete_members, delete_portchannels, "overridden")
        requests.extend(requests_overridden)
        commands.extend(commands_overridden)
        override_commands, override_requests = self.template_for_lag_creation(have, diff_members, diff_portchannels, "overridden")
        commands.extend(override_commands)
        requests.extend(override_requests)
        return commands, requests
    def _state_overridden(self, want, have, diff):
        """ The command generator when state is overridden

        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        commands = []
        requests = []

        commands_del = get_diff(have, want, ["name", "vlan"])
        requests_del = self.get_delete_all_switchport_requests(commands_del)
        if len(requests_del):
            requests.extend(requests_del)
            commands_del = update_states(commands_del, "deleted")
            commands.extend(commands_del)

        commands_over = diff
        requests_over = self.get_create_l2_interface_request(commands_over)
        if requests_over:
            requests.extend(requests_over)
            commands_over = update_states(commands_over, "overridden")
            commands.extend(commands_over)

        return commands, requests
Beispiel #3
0
    def set_state(self, want, have):
        """ Select the appropriate function based on the state provided

        :param want: the desired configuration as a dictionary
        :param have: the current configuration as a dictionary
        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        state = self._module.params['state']
        # diff method works on dict, so creating temp dict
        diff = get_diff(want, have)
        # removing the dict in case diff found

        if state == 'overridden':
            have = [
                each_intf for each_intf in have
                if each_intf['name'].startswith('Ethernet')
            ]
            commands, requests = self._state_overridden(want, have, diff)
        elif state == 'deleted':
            commands, requests = self._state_deleted(want, have, diff)
        elif state == 'merged':
            commands, requests = self._state_merged(want, have, diff)
        elif state == 'replaced':
            commands, requests = self._state_replaced(want, have, diff)

        return commands, requests
Beispiel #4
0
    def set_state(self, want, have):
        """ Select the appropriate function based on the state provided

        :param want: the desired configuration as a dictionary
        :param have: the current configuration as a dictionary
        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        commands = []
        diff = get_diff(want, have, ["name", "member"])
        if diff:
            diff_members, diff_portchannels = self.diff_list_for_member_creation(diff)
        else:
            diff_members = []
            diff_portchannels = []

        state = self._module.params['state']
        if state in ('overridden', 'merged', 'replaced') and not want:
            self._module.fail_json(msg='value of config parameter must not be empty for state {0}'.format(state))

        if state == 'overridden':
            commands, requests = self._state_overridden(want, have, diff_members, diff_portchannels)
        elif state == 'deleted':
            commands, requests = self._state_deleted(want, have, diff)
        elif state == 'merged':
            commands, requests = self._state_merged(want, have, diff_members, diff_portchannels)
        elif state == 'replaced':
            commands, requests = self._state_replaced(want, have, diff_members, diff_portchannels)

        return commands, requests
Beispiel #5
0
    def set_state(self, want, have):
        """ Select the appropriate function based on the state provided

        :param want: the desired configuration as a dictionary
        :param have: the current configuration as a dictionary
        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        state = self._module.params['state']
        diff = get_diff(want, have)
        with open('/root/ansible_log.log', 'a+') as fp:
            fp.write('set_state want : ' + str(want) + '\n')
            fp.write('set_state diff : ' + str(diff) + '\n')
            fp.write('set_state have : ' + str(have) + '\n')
        if state == 'overridden':
            commands, requests = self._state_overridden(want, have, diff)
        elif state == 'deleted':
            commands, requests = self._state_deleted(want, have, diff)
        elif state == 'merged':
            commands, requests = self._state_merged(want, have, diff)
        elif state == 'replaced':
            commands, requests = self._state_replaced(want, have, diff)
        ret_commands = commands
        return ret_commands, requests
Beispiel #6
0
    def _state_replaced(self, want, have, diff):
        """ The command generator when state is replaced

        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        ret_requests = list()
        commands = list()
        l3_interfaces_to_delete = get_diff(have, want)
        obj = self.get_object(l3_interfaces_to_delete, want)
        diff = get_diff(obj, want)
        if diff:
            delete_l3_interfaces_requests = self.get_delete_all_requests(want)
            ret_requests.extend(delete_l3_interfaces_requests)
            commands.extend(update_states(want, "deleted"))
            l3_interfaces_to_create_requests = self.get_create_l3_interfaces_requests(want, have, want)
            ret_requests.extend(l3_interfaces_to_create_requests)
            commands.extend(update_states(want, "merged"))
        return commands, ret_requests
Beispiel #7
0
    def _state_deleted(self, want, have, diff):
        """ The command generator when state is deleted

        :rtype: A list
        :returns: the commands necessary to remove the current configuration
                  of the provided objects
        """
        commands = list()
        # if want is none, then delete all the vlans
        if not want:
            commands = have
        else:  # delete specific vlans
            commands = get_diff(want, diff)

        requests = self.get_delete_vlans_requests(commands)
        commands = update_states(commands, "deleted")
        return commands, requests
Beispiel #8
0
    def _state_overridden(self, want, have, diff):
        """ The command generator when state is overridden

        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        ret_requests = list()
        commands = list()
        interfaces_to_delete = get_diff(have, want)
        if interfaces_to_delete:
            delete_interfaces_requests = self.get_delete_l3_interfaces_requests(want, have)
            ret_requests.extend(delete_interfaces_requests)
            commands.extend(update_states(interfaces_to_delete, "deleted"))

        if diff:
            interfaces_to_create_requests = self.get_create_l3_interfaces_requests(diff, have, want)
            ret_requests.extend(interfaces_to_create_requests)
            commands.extend(update_states(diff, "merged"))

        return commands, ret_requests
Beispiel #9
0
    def _state_deleted(self, want, have, diff):
        """ The command generator when state is deleted

        :rtype: A list
        :returns: the commands necessary to remove the current configuration
                  of the provided objects
        """
        commands = list()
        requests = list()
        portchannel_requests = list()
        # if want is none, then delete all the lag interfaces and all portchannels
        if not want:
            requests = self.get_delete_all_lag_interfaces_requests()
            portchannel_requests = self.get_delete_all_portchannel_requests()
            requests.extend(portchannel_requests)
            commands.extend(update_states(have, "Deleted"))
        else:  # delete specific lag interfaces and specific portchannels
            commands = get_diff(want, diff, ["name", "member"])
            commands = remove_empties_from_list(commands)
            want_members, want_portchannels = self.diff_list_for_member_creation(commands)
            commands, requests = self.template_for_lag_deletion(have, want_members, want_portchannels, "deleted")
        return commands, requests
    def set_state(self, want, have):
        """ Select the appropriate function based on the state provided

        :param want: the desired configuration as a dictionary
        :param have: the current configuration as a dictionary
        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        state = self._module.params['state']

        diff = get_diff(want, have, ["name", "vlan"])

        if state == 'overridden':
            commands, requests = self._state_overridden(want, have, diff)
        elif state == 'deleted':
            commands, requests = self._state_deleted(want, have, diff)
        elif state == 'merged':
            commands, requests = self._state_merged(want, have, diff)
        elif state == 'replaced':
            commands, requests = self._state_replaced(want, have, diff)

        return commands, requests
Beispiel #11
0
    def set_state(self, want, have):
        """ Select the appropriate function based on the state provided

        :param want: the desired configuration as a dictionary
        :param have: the current configuration as a dictionary
        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        state = self._module.params['state']
        # diff method works on dict, so creating temp dict
        diff = get_diff(want, have)

        if state == 'overridden':
            commands, requests = self._state_overridden(want, have, diff)
        elif state == 'deleted':
            commands, requests = self._state_deleted(want, have, diff)
        elif state == 'merged':
            commands, requests = self._state_merged(want, have, diff)
        elif state == 'replaced':
            commands, requests = self._state_replaced(want, have, diff)

        ret_commands = remove_empties_from_list(commands)
        return ret_commands, requests