def _state_overridden(self, want, have): """ The request generator when state is overridden :rtype: A list :returns: the requests necessary to migrate the current configuration to the desired configuration """ requests = [] have_copy = [] for w in want: for h in have: if w["name"] == h["name"]: if dict_diff(w, h): l2_request = self._update_patch_request(w, h) l2_request["data"] = json.dumps(l2_request["data"]) requests.append(l2_request) have_copy.append(h) break for h in have: if h not in have_copy: l2_delete = self._update_delete_request(h) if l2_delete["path"]: l2_delete["data"] = json.dumps(l2_delete["data"]) requests.append(l2_delete) return requests
def _state_merged(self, want, have): """ The requests generator when state is merged :rtype: A list :returns: the requests necessary to merge the provided into the current configuration """ requests = [] request_patch = deepcopy(self.VLAN_PATCH) for w in want: if w.get('vlan_id'): h = search_obj_in_list(w['vlan_id'], have, 'vlan_id') if h: if dict_diff(w, h): request_body = self._update_patch_request(w) request_patch["data"]["openconfig-vlan:vlans"][ "vlan"].append(request_body) else: request_post = self._update_post_request(w) requests.append(request_post) if len(request_patch["data"]["openconfig-vlan:vlans"]["vlan"]): request_patch["data"] = json.dumps(request_patch["data"]) requests.append(request_patch) return requests
def _state_overridden(self, want, have): """ The request generator when state is overridden :rtype: A list :returns: the requests necessary to migrate the current configuration to the desired configuration """ requests = [] request_patch = deepcopy(self.VLAN_PATCH) have_copy = [] for w in want: if w.get('vlan_id'): h = search_obj_in_list(w['vlan_id'], have, 'vlan_id') if h: if dict_diff(w, h): request_body = self._update_patch_request(w) request_patch["data"]["openconfig-vlan:vlans"][ "vlan"].append(request_body) have_copy.append(h) else: request_post = self._update_post_request(w) requests.append(request_post) for h in have: if h not in have_copy and h['vlan_id'] != 1: request_delete = self._update_delete_request(h) requests.append(request_delete) if len(request_patch["data"]["openconfig-vlan:vlans"]["vlan"]): request_patch["data"] = json.dumps(request_patch["data"]) requests.append(request_patch) return requests
def _state_overridden(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 key, extant in have.items(): if key in want: desired = want[key] else: desired = dict(name=key) add_config = dict_diff(extant, desired) del_config = dict_diff(desired, extant) commands.extend(generate_commands(key, add_config, del_config)) return commands
def set_config(want, have): commands = [] to_set = dict_diff(have, want) for member in to_set.get("members", []): channel_id = want["name"][12:] commands.extend([ "interface {0}".format(member["member"]), "channel-group {0} mode {1}".format(channel_id, member["mode"]), ]) return commands
def _state_replaced(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 = [] for key, desired in want.items(): interface_name = normalize_interface(key) if interface_name in have: extant = have[interface_name] else: extant = dict() add_config = dict_diff(extant, desired) del_config = dict_diff(desired, extant) commands.extend(generate_commands(key, add_config, del_config)) return commands
def remove_config(want, have): commands = [] if not want.get("members"): return ["no interface {0}".format(want["name"])] to_remove = dict_diff(want, have) for member in to_remove.get("members", []): commands.extend([ "interface {0}".format(member["member"]), "no channel-group", ]) return commands
def _state_replaced(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 = [] to_set = dict_diff(have, want) if 'system' in to_set: system = to_set['system'] if 'priority' in system: commands.append('lacp system-priority {0}'.format( system['priority'])) to_del = dict_diff(want, have) if 'system' in to_del: system = to_del['system'] system_set = to_set.get('system', {}) if 'priority' in system and 'priority' not in system_set: commands.append('no lacp system-priority') return commands
def _state_deleted(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 """ commands = [] to_del = dict_diff(want, have) if 'system' in to_del: system = to_del['system'] if 'priority' in system: commands.append('no lacp system-priority') return commands
def _state_merged(want, have): """ The command generator when state is merged :rtype: A list :returns: the commands necessary to merge the provided into the current configuration """ commands = [] to_set = dict_diff(have, want) if 'system' in to_set: system = to_set['system'] if 'priority' in system: commands.append('lacp system-priority {0}'.format( system['priority'])) return commands
def _state_merged(self, want, have): """ The request generator when state is merged :rtype: A list :returns: the requests necessary to merge the provided into the current configuration """ requests = [] for w in want: for h in have: if w["name"] == h["name"]: if dict_diff(h, w): l2_request = self._update_patch_request(w, h) l2_request["data"] = json.dumps(l2_request["data"]) requests.append(l2_request) break return requests
def _state_merged(want, have): """ The command generator when state is merged :rtype: A list :returns: the commands necessary to merge the provided into the current configuration """ commands = [] for key, desired in want.items(): interface_name = normalize_interface(key) if interface_name in have: extant = have[interface_name] else: extant = dict(name=interface_name) add_config = dict_diff(extant, desired) commands.extend(generate_commands(interface_name, add_config, {})) return commands
def state_deleted(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 """ commands = [] to_remove = dict_diff(want, have) tlv_options = to_remove.pop("tlv_select", {}) for key in to_remove: commands.append("no lldp {0}".format(key)) for key, value in tlv_options.items(): device_option = key.replace("_", "-") if value is False: commands.append("lldp tlv-select {0}".format(device_option)) elif value is True: commands.append("no lldp tlv-select {0}".format(device_option)) return commands
def state_merged(want, have): """ The command generator when state is merged :rtype: A list :returns: the commands necessary to merge the provided into the current configuration """ commands = [] to_set = dict_diff(have, want) tlv_options = to_set.pop("tlv_select", {}) for key, value in to_set.items(): commands.append("lldp {0} {1}".format(key, value)) for key, value in tlv_options.items(): device_option = key.replace("_", "-") if value is True: commands.append("lldp tlv-select {0}".format(device_option)) elif value is False: commands.append("no lldp tlv-select {0}".format(device_option)) return commands
def _state_deleted(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 """ commands = [] for key in want: desired = dict() if key in have: extant = have[key] else: continue del_config = dict_diff(desired, extant) commands.extend(generate_commands(key, {}, del_config)) return commands
def _state_deleted(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 """ commands = [] for key in want.keys(): interface_name = normalize_interface(key) desired = dict(name=interface_name) if interface_name in have: extant = have[interface_name] else: continue del_config = dict_diff(desired, extant) commands.extend(generate_commands(interface_name, {}, del_config)) return commands