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

        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        cmds = []
        for h in have:
            # Clean up bfd attrs for any interfaces not listed in the play
            h = flatten_dict(h)
            obj_in_want = flatten_dict(
                search_obj_in_list(h["name"], want, "name")
            )
            if obj_in_want:
                # Let the 'want' loop handle all vals for this interface
                continue
            cmds.extend(self.del_attribs(h))
        for w in want:
            # Update any want attrs if needed. The overridden state considers
            # the play as the source of truth for the entire device, therefore
            # set any unspecified attrs to their default state.
            w = self.set_none_vals_to_defaults(flatten_dict(w))
            cmds.extend(self.set_commands(w, have))
        return cmds
Beispiel #2
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']
        if state in ('overridden', 'merged', 'replaced') and not want:
            self._module.fail_json(
                msg='config is required for state {0}'.format(state))

        cmds = list()
        if state == 'overridden':
            cmds.extend(self._state_overridden(want, have))
        elif state == 'deleted':
            cmds.extend(self._state_deleted(want, have))
        else:
            for w in want:
                if state == 'merged':
                    cmds.extend(self._state_merged(flatten_dict(w), have))
                elif state == 'replaced':
                    cmds.extend(self._state_replaced(flatten_dict(w), have))
        return cmds
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"]
        if (
            state in ("overridden", "merged", "replaced", "rendered")
            and not want
        ):
            self._module.fail_json(
                msg="value of config parameter must not be empty for state {0}".format(
                    state
                )
            )

        cmds = list()
        if state == "overridden":
            cmds.extend(self._state_overridden(want, have))
        elif state == "deleted":
            cmds.extend(self._state_deleted(want, have))
        else:
            for w in want:
                if state in ["merged", "rendered"]:
                    cmds.extend(self._state_merged(flatten_dict(w), have))
                elif state == "replaced":
                    cmds.extend(self._state_replaced(flatten_dict(w), have))
        return cmds
Beispiel #4
0
    def _state_overridden(self, want, have):
        """ The command generator when state is overridden

        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        commands = []
        for h in have:
            h = flatten_dict(h)
            obj_in_want = flatten_dict(
                search_obj_in_list(h['name'], want, 'name'))
            if h == obj_in_want:
                continue
            for w in want:
                w = flatten_dict(w)
                if h['name'] == w['name']:
                    wkeys = w.keys()
                    hkeys = h.keys()
                    for k in wkeys:
                        if k in self.exclude_params and k in hkeys:
                            del h[k]
            commands.extend(self.del_attribs(h))
        for w in want:
            commands.extend(self.set_commands(flatten_dict(w), have, True))
        return commands
    def _state_overridden(self, want, have):
        """ The command generator when state is overridden

        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        commands = []
        want_intfs = [w["name"] for w in want]
        for h in have:
            h = flatten_dict(h)
            delete_dict = {}
            if h["name"] in want_intfs:
                for w in want:
                    if w["name"] == h["name"]:
                        delete_keys = list(set(h) - set(flatten_dict(w)))
                        for k in delete_keys:
                            delete_dict.update({k: h[k]})
                        delete_dict.update({"name": h["name"]})
                        break
            else:
                delete_dict.update(h)
            commands.extend(self.del_commands(delete_dict))
        for w in want:
            commands.extend(self.set_commands(flatten_dict(w), have))
        return commands
    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 = []
        state = self._module.params["state"]
        if state == "overridden":
            commands = self._state_overridden(want, have)
        elif state == "deleted":
            commands = self._state_deleted(want, have)
        elif state == "rendered":
            commands = self._state_rendered(want)
        elif state == "parsed":
            want = self._module.params["running_config"]
            commands = self._state_parsed(want)
        else:
            for w in want:
                if state == "merged":
                    commands.extend(self._state_merged(flatten_dict(w), have))
                elif state == "replaced":
                    commands.extend(self._state_replaced(
                        flatten_dict(w), have))
        return commands
 def set_commands(self, want, have):
     commands = []
     obj_in_have = flatten_dict(
         search_obj_in_list(want["name"], have, "name"))
     if not obj_in_have:
         commands = self.add_commands(flatten_dict(want))
     else:
         diff = dict_diff(obj_in_have, want)
         if diff:
             diff.update({"name": want["name"]})
             commands = self.add_commands(diff)
     return commands
Beispiel #8
0
    def set_commands(self, w, have, replace=False):
        commands = []

        obj_in_have = flatten_dict(search_obj_in_list(w['name'], have, 'name'))
        if not obj_in_have:
            commands = self.add_commands(w)
        else:
            diff = self.diff_of_dicts(w, obj_in_have)
            if diff and not replace:
                if 'mode' in diff.keys() and diff['mode']:
                    commands = self.add_commands(diff)
                if "allowed_vlans" in diff.keys() and diff["allowed_vlans"]:
                    vlan_tobe_added = diff["allowed_vlans"].split(',')
                    vlan_list = vlan_tobe_added[:]
                    if obj_in_have.get("allowed_vlans"):
                        have_vlans = obj_in_have["allowed_vlans"].split(',')
                    else:
                        have_vlans = []
                    for w_vlans in vlan_list:
                        if w_vlans in have_vlans:
                            vlan_tobe_added.pop(vlan_tobe_added.index(w_vlans))
                    if vlan_tobe_added:
                        diff.update(
                            {"allowed_vlans": ','.join(vlan_tobe_added)})
                        if have_vlans:
                            commands = self.add_commands(diff, True)
                        else:
                            commands = self.add_commands(diff)
                    return commands
            commands = self.add_commands(diff)
        return commands
Beispiel #9
0
    def _state_replaced(self, w, have):
        """ The command generator when state is replaced

        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        commands = []
        obj_in_have = flatten_dict(search_obj_in_list(w['name'], have, 'name'))
        if obj_in_have:
            diff = dict_diff(w, obj_in_have)
        else:
            diff = w
        merged_commands = self.set_commands(w, have, True)
        if 'name' not in diff:
            diff['name'] = w['name']

        dkeys = diff.keys()
        for k in w.copy():
            if k in self.exclude_params and k in dkeys:
                del diff[k]
        replaced_commands = self.del_attribs(diff)

        if merged_commands:
            cmds = set(replaced_commands).intersection(set(merged_commands))
            for cmd in cmds:
                merged_commands.remove(cmd)
            commands.extend(replaced_commands)
            commands.extend(merged_commands)
        return commands
    def _state_replaced(self, want, have):
        """ The command generator when state is replaced

        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        commands = []
        del_commands = []
        delete_dict = {}
        obj_in_have = flatten_dict(
            search_obj_in_list(want["name"], have, "name"))
        for k1 in obj_in_have.keys():
            if k1 not in want.keys():
                delete_dict.update({k1: obj_in_have[k1]})

        if delete_dict:
            delete_dict.update({"name": want["name"]})
            del_commands = self.del_commands(delete_dict)
        merged_commands = self.set_commands(want, have)

        if merged_commands:
            cmds = set(del_commands).intersection(set(merged_commands))
            for cmd in cmds:
                merged_commands.remove(cmd)

        commands.extend(del_commands)
        commands.extend(merged_commands)
        return commands
Beispiel #11
0
 def set_commands(self, want, have):
     cmds = []
     obj_in_have = flatten_dict(search_obj_in_list(want['name'], have, 'name'))
     if not obj_in_have:
         cmds = self.add_commands(want)
     else:
         diff = self.diff_of_dicts(want, obj_in_have)
         cmds = self.add_commands(diff)
     return cmds
Beispiel #12
0
    def _state_deleted(self, want, have):
        """ The command generator when state is deleted

        :rtype: A list
        :returns: the commands necessary to remove the current configuration
                  of the provided objects
        """
        if not (want or have):
            return []
        cmds = []
        if want:
            for w in want:
                obj_in_have = flatten_dict(search_obj_in_list(w['name'], have, 'name'))
                cmds.extend(self.del_attribs(obj_in_have))
        else:
            for h in have:
                cmds.extend(self.del_attribs(flatten_dict(h)))
        return cmds
Beispiel #13
0
 def set_commands(self, w, have):
     commands = []
     obj_in_have = flatten_dict(search_obj_in_list(w["name"], have, "name"))
     if not obj_in_have:
         commands = self.add_commands(w)
     else:
         diff = self.diff_of_dicts(w, obj_in_have)
         commands = self.add_commands(diff)
     return commands
Beispiel #14
0
 def set_commands(self, w, have, replace=False):
     commands = []
     vlan_tobe_added = []
     obj_in_have = flatten_dict(search_obj_in_list(w["name"], have, "name"))
     if not obj_in_have:
         commands = self.add_commands(w)
     else:
         diff = self.diff_of_dicts(w, obj_in_have)
         if diff and not replace:
             if "allowed_vlans" in diff.keys() and diff["allowed_vlans"]:
                 vlan_tobe_added = diff["allowed_vlans"].split(",")
                 vlan_list = vlan_tobe_added[:]
                 have_vlans = obj_in_have["allowed_vlans"].split(",")
                 for w_vlans in vlan_list:
                     if w_vlans in have_vlans:
                         vlan_tobe_added.pop(vlan_tobe_added.index(w_vlans))
                 if vlan_tobe_added:
                     diff.update(
                         {"allowed_vlans": ",".join(vlan_tobe_added)}
                     )
                     commands = self.add_commands(diff, True)
                 return commands
         commands = self.add_commands(diff)
     return commands