From 0a30502e9187904d225ee92835fb6aa9e19a769e Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Wed, 30 Sep 2020 15:01:30 +0200 Subject: [PATCH] enhance cisco meraki --- .../cloudcontroller/restapi/custom/api.pm | 15 ++++++++++ .../cloudcontroller/restapi/mode/devices.pm | 30 +++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/network/cisco/meraki/cloudcontroller/restapi/custom/api.pm b/network/cisco/meraki/cloudcontroller/restapi/custom/api.pm index 12b6085bf..d117a2eb9 100644 --- a/network/cisco/meraki/cloudcontroller/restapi/custom/api.pm +++ b/network/cisco/meraki/cloudcontroller/restapi/custom/api.pm @@ -137,6 +137,21 @@ sub get_cache_networks { return $self->{cache_networks}; } +sub get_organization { + my ($self, %options) = @_; + + $self->cache_meraki_entities(); + my $organization_id; + if (defined($options{network_id})) { + $organization_id = $self->{cache_networks}->{ $options{network_id} }->{organizationId}; + } + my $organization; + $organization = $self->{cache_organizations}->{$organization_id} + if (defined($organization_id) && defined($self->{cache_organizations}->{$organization_id})); + + return $organization; +} + sub get_organization_id { my ($self, %options) = @_; diff --git a/network/cisco/meraki/cloudcontroller/restapi/mode/devices.pm b/network/cisco/meraki/cloudcontroller/restapi/mode/devices.pm index 898febe24..45bfd5e52 100644 --- a/network/cisco/meraki/cloudcontroller/restapi/mode/devices.pm +++ b/network/cisco/meraki/cloudcontroller/restapi/mode/devices.pm @@ -236,9 +236,11 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - 'filter-device-name:s' => { name => 'filter_device_name' }, - 'filter-network-id:s' => { name => 'filter_network_id' }, - 'filter-tags:s' => { name => 'filter_tags' } + 'filter-device-name:s' => { name => 'filter_device_name' }, + 'filter-network-id:s' => { name => 'filter_network_id' }, + 'filter-organization-name:s' => { name => 'filter_organization_name' }, + 'filter-organization-id:s' => { name => 'filter_organization_id' }, + 'filter-tags:s' => { name => 'filter_tags' } }); return $self; @@ -359,6 +361,8 @@ sub manage_selection { (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' . (defined($self->{option_results}->{filter_device_name}) ? md5_hex($self->{option_results}->{filter_device_name}) : md5_hex('all')) . '_' . (defined($self->{option_results}->{filter_network_id}) ? md5_hex($self->{option_results}->{filter_network_id}) : md5_hex('all')) . '_' . + (defined($self->{option_results}->{filter_organization_id}) ? md5_hex($self->{option_results}->{filter_organization_id}) : md5_hex('all')) . '_' . + (defined($self->{option_results}->{filter_organization_name}) ? md5_hex($self->{option_results}->{filter_organization_name}) : md5_hex('all')) . '_' . (defined($self->{option_results}->{filter_tags}) ? md5_hex($self->{option_results}->{filter_tags}) : md5_hex('all')); my $last_timestamp = $self->read_statefile_key(key => 'last_timestamp'); my $timespan = 300; @@ -382,6 +386,18 @@ sub manage_selection { $self->{output}->output_add(long_msg => "skipping device '" . $_->{name} . "': no matching filter.", debug => 1); next; } + + my $organization = $options{custom}->get_organization(network_id => $_->{networkId}); + if (defined($self->{option_results}->{filter_organization_id}) && $self->{option_results}->{filter_organization_id} ne '' && + $organization->{id} !~ /$self->{option_results}->{filter_organization_id}/) { + $self->{output}->output_add(long_msg => "skipping device '" . $_->{name} . "': no matching filter.", debug => 1); + next; + } + if (defined($self->{option_results}->{filter_organization_name}) && $self->{option_results}->{filter_organization_name} ne '' && + $organization->{name} !~ /$self->{option_results}->{filter_organization_name}/) { + $self->{output}->output_add(long_msg => "skipping device '" . $_->{name} . "': no matching filter.", debug => 1); + next; + } $devices->{ $_->{serial} } = $_->{model}; } @@ -476,6 +492,14 @@ Filter devices by name (Can be a regexp). Filter devices by network id (Can be a regexp). +=item B<--filter-organization-id> + +Filter devices by organization id (Can be a regexp). + +=item B<--filter-organization-name> + +Filter devices by organization name (Can be a regexp). + =item B<--filter-tags> Filter devices by tags (Can be a regexp).