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 = [] want = remove_empties(want) delete_commands = [] for have_afi in have.get('access_groups', []): want_afi = search_obj_in_list(have_afi['afi'], want.get('access_groups', []), key='afi') or {} afi = have_afi.get('afi') for acl in have_afi.get('acls', []): if acl not in want_afi.get('acls', []): delete_commands.extend( self._compute_commands(afi, [acl], remove=True)) if delete_commands: pad_commands(delete_commands, want['name']) commands.extend(delete_commands) merged_commands = self._state_merged(want, have) if merged_commands and delete_commands: del merged_commands[0] commands.extend(merged_commands) return commands
def _purge_attribs(self, intf): """ The command generator for purging attributes :rtype: A list :returns: the commands necessary to purge attributes """ commands = [] have_copy = deepcopy(intf) members = have_copy.pop('members', []) to_delete = dict_delete(have_copy, remove_empties({'name': have_copy['name']})) if to_delete: for key, value in iteritems(flatten_dict( remove_empties(to_delete))): commands.append( self._compute_commands(key=key, value=value, remove=True)) if commands: pad_commands(commands, intf['name']) if members: members = param_list_to_dict(deepcopy(members), unique_key='member') for key in members: member_cmd = ['no bundle id'] pad_commands(member_cmd, key) commands.extend(member_cmd) return commands
def _render_interface_del_commands(self, want, have): """ The command generator for delete commands w.r.t member interfaces :rtype: A list :returns: the commands necessary to update member interfaces """ commands = [] if not want: want = {} have_members = have.get('members') if have_members: have_members = param_list_to_dict(deepcopy(have_members), unique_key='member') want_members = param_list_to_dict(deepcopy(want).get( 'members', []), unique_key='member') for key in have_members: if key not in want_members: member_cmd = ['no bundle id'] pad_commands(member_cmd, key) commands.extend(member_cmd) return commands
def _render_interface_updates(self, want, have): """ The command generator for updates to member interfaces :rtype: A list :returns: the commands necessary to update member interfaces """ commands = [] if not have: have = {'name': want['name']} member_diff = diff_list_of_dicts(want['members'], have.get('members', [])) for diff in member_diff: diff_cmd = [] bundle_cmd = 'bundle id {0}'.format( want['name'].split('Bundle-Ether')[1]) if diff.get('mode'): bundle_cmd += ' mode {0}'.format(diff.get('mode')) diff_cmd.append(bundle_cmd) pad_commands(diff_cmd, diff['member']) commands.extend(diff_cmd) return commands
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 """ commands = [] for key, value in iteritems(flatten_dict(dict_delete(have, remove_empties(want)))): commands.append(self._compute_commands(key, value, remove=True)) if commands: pad_commands(commands, have['name']) return commands
def _state_merged(self, 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 = [] commands.extend(self._render_bundle_updates(want, have)) if commands or have == {}: pad_commands(commands, want['name']) commands.extend(self._render_interface_updates(want, have)) return commands
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 """ commands = [] # This handles deletion for both empty/no config # and just interface name provided. if 'access_groups' not in want: for x in have.get('access_groups', []): afi = x.get('afi') for have_acl in x.get('acls', []): commands.extend( self._compute_commands(afi, [have_acl], remove=True)) else: for want_afi in want['access_groups']: have_afi = search_obj_in_list(want_afi['afi'], have.get('access_groups', []), key='afi') or {} afi = have_afi.get('afi') # If only the AFI has be specified, we # delete all the access-groups for that AFI if 'acls' not in want_afi: for have_acl in have_afi.get('acls', []): commands.extend( self._compute_commands(afi, [have_acl], remove=True)) # If one or more acl has been explicitly specified, we # delete that and leave the rest untouched else: for acl in want_afi['acls']: if acl in have_afi.get('acls', []): commands.extend( self._compute_commands(afi, [acl], remove=True)) if commands: pad_commands(commands, have['name']) return commands
def _state_merged(self, 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 = [] if not have: have = {'name': want['name']} for key, value in iteritems(flatten_dict(remove_empties(dict_diff(have, want)))): commands.append(self._compute_commands(key, value)) if commands: pad_commands(commands, want['name']) 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 = [] if have: commands.extend(self._render_bundle_del_commands(want, have)) commands.extend(self._render_bundle_updates(want, have)) if commands or have == {}: pad_commands(commands, want['name']) if have: commands.extend(self._render_interface_del_commands(want, have)) commands.extend(self._render_interface_updates(want, have)) return commands
def _state_merged(self, 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 = [] want = remove_empties(want) for want_afi in want.get('access_groups', []): have_afi = search_obj_in_list(want_afi['afi'], have.get('access_groups', []), key='afi') or {} delta = diff_list_of_dicts(want_afi['acls'], have_afi.get('acls', []), key='direction') commands.extend(self._compute_commands(want_afi['afi'], delta)) if commands: pad_commands(commands, want['name']) return commands