(plugin) network::cisco::meraki::cloudcontroller::restapi - add options --ignore-orgs-api-disabled and --api-filter-orgs (#3591)

This commit is contained in:
qgarnier 2022-04-12 10:12:25 +02:00 committed by GitHub
parent 2160014b90
commit f7945affba

View File

@ -50,6 +50,8 @@ sub new {
'timeout:s' => { name => 'timeout' },
'reload-cache-time:s' => { name => 'reload_cache_time' },
'ignore-permission-errors' => { name => 'ignore_permission_errors' },
'ignore-orgs-api-disabled' => { name => 'ignore_orgs_api_disabled' },
'api-filter-orgs:s' => { name => 'api_filter_orgs' },
'timespan:s' => { name => 'timespan' },
'cache-use' => { name => 'cache_use' }
});
@ -88,13 +90,14 @@ sub check_options {
$self->{reload_cache_time} = (defined($self->{option_results}->{reload_cache_time})) ? $self->{option_results}->{reload_cache_time} : 180;
$self->{reload_extra_cache_time} = (defined($self->{option_results}->{reload_extra_cache_time})) ? $self->{option_results}->{reload_extra_cache_time} : 10;
$self->{ignore_permission_errors} = (defined($self->{option_results}->{ignore_permission_errors})) ? 1 : 0;
$self->{ignore_orgs_api_disabled} = (defined($self->{option_results}->{ignore_orgs_api_disabled})) ? 1 : 0;
$self->{timespan} = (defined($self->{option_results}->{timespan})) ? $self->{option_results}->{timespan} : 300;
if (!defined($self->{hostname}) || $self->{hostname} eq '') {
if ($self->{hostname} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --hostname option.");
$self->{output}->option_exit();
}
if (!defined($self->{api_token}) || $self->{api_token} eq '') {
if ($self->{api_token} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --api-token option.");
$self->{output}->option_exit();
}
@ -221,7 +224,12 @@ sub request_api {
sub write_cache_file {
my ($self, %options) = @_;
$self->{cache}->read(statefile => 'cache_meraki_' . md5_hex($self->{api_token}));
$self->{cache}->read(
statefile => 'cache_meraki_' . md5_hex(
$self->{api_token} . '_' .
(defined($self->{option_results}->{api_filter_orgs}) ? $self->{option_results}->{api_filter_orgs} : '')
)
);
$self->{cache}->write(data => {
update_time => time(),
response => $self->{datas}
@ -231,7 +239,12 @@ sub write_cache_file {
sub get_cache_file_response {
my ($self, %options) = @_;
$self->{cache}->read(statefile => 'cache_meraki_' . md5_hex($self->{api_token}));
$self->{cache}->read(
statefile => 'cache_meraki_' . md5_hex(
$self->{api_token} . '_' .
(defined($self->{option_results}->{api_filter_orgs}) ? $self->{option_results}->{api_filter_orgs} : '')
)
);
$self->{datas} = $self->{cache}->get(name => 'response');
if (!defined($self->{datas})) {
$self->{output}->add_option_msg(short_msg => 'Cache file missing');
@ -288,6 +301,9 @@ sub get_organizations {
$self->{datas}->{orgs} = {};
if (defined($datas)) {
foreach (@$datas) {
next if (defined($self->{option_results}->{api_filter_orgs}) && $self->{option_results}->{api_filter_orgs} ne '' &&
$_->{name} !~ /$self->{option_results}->{api_filter_orgs}/);
$self->{datas}->{orgs}->{ $_->{id} } = {
name => $_->{name},
url => $_->{url}
@ -301,12 +317,22 @@ sub get_organizations {
sub get_networks {
my ($self, %options) = @_;
my $ignore_codes = {};
$ignore_codes = { 404 => 1 } if ($self->{ignore_orgs_api_disabled} == 1);
$self->{datas}->{networks} = {};
foreach my $id (@{$options{orgs}}) {
my $datas = $self->request_api(
endpoint => '/organizations/' . $id . '/networks',
hostname => $self->get_shard_hostname(organization_id => $id)
hostname => $self->get_shard_hostname(organization_id => $id),
ignore_codes => $ignore_codes
);
if (!defined($datas) && $self->{ignore_orgs_api_disabled} == 1) {
delete $self->{datas}->{orgs}->{$id};
next;
}
if (defined($datas)) {
foreach (@$datas) {
if (defined($options{extended})) {
@ -542,6 +568,14 @@ Set HTTP timeout
Ignore permission errors (403 status code).
=item B<--ignore-orgs-api-disabled>
Ignore organizations with api disabled.
=item B<--api-filter-orgs>
Filter organizations (regexp).
=item B<--cache-use>
Use the cache file (created with cache mode).