Mnet

 view release on metacpan or  search on metacpan

lib/Mnet/Opts/Cli/Cache.pm  view on Meta::CPAN

    # return undef if Mnet::Opts::Cli was not used for cli option parsing
    return undef if not $input and not $Mnet::Opts::Cli::Cache::opts;

    # init output opts from Mnet::Opts::Set pragmas, if any are loaded
    my $opts = Mnet::Opts::Set::pragmas();

    # init output extra cli args, from ARGV if Mnet::Opts::Cli is not loaded
    my @extras = @Mnet::Opts::Cli::Cache::extras;
    @extras = @ARGV if not $INC{"Mnet/Opts/Cli.pm"};

    # next overlay output opts with Mnet opts read from Mnet::Opts::Cli->new
    #   opts with dashes would be a pain, because of need to xlate underscores
    if ($INC{"Mnet/Opts/Cli.pm"}) {
        foreach my $opt (keys %$Mnet::Opts::Cli::defined) {
            if ($opt =~ /^(batch|debug|quiet|record|replay|silent|tee|test)$/) {
                $opts->{$opt} = $Mnet::Opts::Cli::Cache::opts->{$opt};
            }
        }
    }

    # finally overlay input options on top of any Mnet pragma and Mnet options
    $opts->{$_} = $input->{$_} foreach keys %$input;

    # finished new method, return opts hash, and extra args in list context
    return wantarray ? ($opts, @extras) : $opts
}



# normal package return
1;

lib/Mnet/Stanza.pm  view on Meta::CPAN




sub ios {

=head2 ios

    $output = Mnet::Stanza::ios($template, $config)

The Mnet::Stanza::ios fucntion uses a template to check a config for needed
config changes, outputting a generated list of overlay config commands that can
be applied to the device to bring it into compliance.

The script dies with an error if the template argument is missing. The second
config argument is optional and can be set to the current device config.

The output from this function will be commands that need to be added and/or
removed from the input config, generated by comparing the input template to the
input config. The output will be null if the input config does not need to be
updated.

lib/Mnet/Stanza.pm  view on Meta::CPAN

    # define ios feature remove template string
    #   used to remove any old config before applying update
    my $remove_template = "

        ! acl automatically removed from interface
        -access-list 1
        -ip access-list test

    ";

    # output overlay config if update is needed
    #   overlay will remove old config before updating with new config
    if (Mnet::Stanza::ios($update_template, $sh_run)) {
        print Mnet::Stanza::ios($remove_template, $sh_run);
        print Mnet::Stanza::ios($update_template);
    }

Note that extra spaces are removed from the template and config inputs using
the Mnet::Stanza::trim function. Refer to that function for more info.

=cut

lib/Mnet/Stanza.pm  view on Meta::CPAN

        $template = Mnet::Stanza::trim($template);
        sub _ios_error_in_template {
            my $template = shift // croak "undefined arg";
            $template =~ /^(\s*\S.*)/m;
            die "ios error in template line '$1'".Carp::shortmess()."\n";
        }
    } elsif (caller ne "Mnet::Stanza::_ios_find") {
        croak("Mnet::Stanza::ios called with too many args")
    }

    # init output config overlay
    my $output = "";

    # track lines checked in current stanza
    #   key is set for each line checked with add/find/match/remove operation
    #   used by _ios_remove sub to ensure lines already checked are not removed
    #   example, adds test 1-2, removes all others: +test 1, +test 2, -test *!*
    my $checked = {};

    # clear lines starting with the ios comment character from template
    $template =~ s/^\s*!.*//mg;

lib/Mnet/Stanza.pm  view on Meta::CPAN

            $output .= _ios_remove($template_stanza, $config, $checked,$indent);

        # otherwise abort with template error
        } else {
            _ios_error_in_template($template_stanza);
        }

    # continue processing parsed template stanzas
    }

    # finished ios function, return output config overlay
    return $output;
}



sub _ios_add {

# $output = _ios_add($template, $config, \%checked, $indent)
# purpose: ios template '+' add line if missing
# $output: missing config commands that need to be added
# $template: current template line to add, expected to be on left margin
# $config: ios config to work on, expected to be on left margin
# \%checked: keys for each add/find/match/remove in current stanza
# $indent: current template indent, zero or more spaces

    # read input args and initialize output config overlay
    my ($template, $config, $checked, $indent) = (shift, shift, shift, shift);
    my $output = "";

    # parse single line to add
    _ios_error_in_template($template) if $template !~ /^\+(\S.*)$/;
    my $add_line = $1;

    # append line to output if not already present with same indent
    $output .= $indent.$add_line."\n" if $config !~ /^$indent\Q$add_line\E$/m;

    # note that this line was checked
    $checked->{$indent.$add_line}++;

    # finished _ios_add, return output config overlay
    return $output;
}



sub _ios_find {

# $output = _ios_find($template, $config, \%checked, $indent)
# purpose: ios template '>' find stanza
# $output: missing config commands that need to be added
# $template: current template line to find, expected to be on left margin
# $config: ios config to work on, expected to be on left margin
# \%checked: keys for each add/find/match/remove in current stanza
# $indent: current template indent, zero or more spaces

    # read input args and initialize output config overlay
    my ($template, $config, $checked, $indent) = (shift, shift, shift, shift);
    my $output = "";

    # parse template first line to find
    _ios_error_in_template($template) if $template !~ /^\>(\S.*)$/m;
    my $find_line = $1;

    # parse template subcommands under first line to find
    my $find_subcommands = undef;
    foreach my $line (split(/\n/, $template)) {

lib/Mnet/Stanza.pm  view on Meta::CPAN

                $output .= $indent_subcommands.$line."\n";
            }
        }

    # finished processing find template stanza
    }

    # note that template find stanza first line was checked
    $checked->{$indent.$find_line}++;

    # finished _ios_find, return output config overlay
    return $output;
}



sub _ios_match {

# $output = _ios_match($template, $config, \%checked, $indent)
# purpose: ios template '=' match stanza
# $output: missing config commands that need to be added
# $template: current template line to match, expected to be on left margin
# $config: ios config to work on, expected to be on left margin
# \%checked: keys for each add/find/match/remove in current stanza
# $indent: current template indent, zero or more spaces

    # read input args and initialize output config overlay
    my ($template, $config, $checked, $indent) = (shift, shift, shift, shift);
    my $output = "";

    # parse first line to match
    _ios_error_in_template($template) if $template !~ /^\=(\S.*)$/m;
    my $match_line = $1;

    # parse entire stanza to match
    my $match_stanza = "";
    foreach my $line (split(/\n/, $template)) {

lib/Mnet/Stanza.pm  view on Meta::CPAN


    # look for matching stanza in config
    my $config_stanza = Mnet::Stanza::parse($config,qr/^\Q$match_line\E$/)."\n";

    # append match stanza to output if not already present with same indent
    $output .= $indent.$match_stanza if $config_stanza ne $match_stanza;

    # note that this line was checked
    $checked->{$indent.$match_line}++;

    # finished _ios_stanza, return output config overlay
    return $output;
}



sub _ios_remove {

# $output = _ios_remove($template, $config, \%checked, $indent)
# purpose: ios template '-' remove line or stanza
# $output: extra config commands that need to be removed
# $template: current template line to remove, expected to be on left margin
# $config: ios config to work on, expected to be on left margin
# \%checked: keys for each add/find/match/remove in current stanza
# $indent: current template indent, zero or more spaces

    # read input args and initialize output config overlay
    my ($template, $config, $checked, $indent) = (shift, shift, shift, shift);
    my $output = "";

    # parse single line to remove
    _ios_error_in_template($template) if $template !~ /^\-(\S.*)$/;
    my $remove_line = $1;

    # set regex to match line, with optional '*!*' wildcard at end
    my ($regex_line, $regex_wildcard) = ($remove_line, "");
    ($regex_line, $regex_wildcard) = ($1, ".*")

lib/Mnet/Stanza.pm  view on Meta::CPAN

            my $match_line = $1;
            if (not $checked->{$indent.$match_line}) {
                $checked->{$indent.$match_line}++;
                my $no_line = "no $match_line";
                $no_line =~ s/^no no //;
                $output .= $indent.$no_line."\n";
            }
        }
    }

    # finished _ios_remove, return output config overlay
    return $output;
}



=head1 SEE ALSO

L<Mnet>

L<Mnet::Log>

t/Opts_Cli_Cache.t  view on Meta::CPAN

        use Mnet::Opts::Cli::Cache;
        Mnet::Opts::Cli::define({ getopt => "sample" });
        Mnet::Opts::Cli->new;
        my $opts = Mnet::Opts::Cli::Cache::get({});
        warn "defined" if defined $opts->{sample};
    perl-eof
    expect  => '',
});

# get with input hash override of cli opt
#   check cache get overlays input opts over parsed cli opts
Mnet::T::test_perl({
    name    => 'get with input hash override of cli opt',
    perl    => <<'    perl-eof',
        use warnings;
        use strict;
        use Mnet::Opts::Cli;
        use Mnet::Opts::Cli::Cache;
        Mnet::Opts::Cli::define({ getopt => "sample" });
        Mnet::Opts::Cli->new;
        my $opts = Mnet::Opts::Cli::Cache::get({ sample => "2" });



( run in 2.534 seconds using v1.01-cache-2.11-cpan-49f99fa48dc )