From 4b06a5cac5786e0c561faf567fbe796209b494f2 Mon Sep 17 00:00:00 2001 From: pkriko <32265250+pkriko@users.noreply.github.com> Date: Wed, 12 Aug 2020 12:23:40 +0200 Subject: [PATCH] enh: meraki cloud discovery add resource-type option (#2146) --- .../cloudcontroller/restapi/mode/discovery.pm | 84 +++++++++++++------ 1 file changed, 60 insertions(+), 24 deletions(-) diff --git a/network/cisco/meraki/cloudcontroller/restapi/mode/discovery.pm b/network/cisco/meraki/cloudcontroller/restapi/mode/discovery.pm index fcc3047c3..94dd8fbb7 100644 --- a/network/cisco/meraki/cloudcontroller/restapi/mode/discovery.pm +++ b/network/cisco/meraki/cloudcontroller/restapi/mode/discovery.pm @@ -32,7 +32,8 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - 'prettify' => { name => 'prettify' } + 'prettify' => { name => 'prettify' }, + 'resource-type:s' => { name => 'resource_type'}, }); return $self; @@ -43,6 +44,45 @@ sub check_options { $self->SUPER::init(%options); } +sub discovery_devices { + my ($self, %options) = @_; + + my $disco_data = []; + foreach (values %{$options{devices}}) { + my $node = { + %$_, + %{$options{devices_statuses}->{$_->{serial}}}, + networkName => $options{networks}->{ $options{devices_statuses}->{$_->{serial}}->{networkId} }->{name}, + organizationName => $options{organizations}->{ $options{networks}->{ $options{devices_statuses}->{$_->{serial}}->{networkId} }->{organizationId} }->{name}, + type => 'device' + }; + + push @$disco_data, $node; + + } + + return $disco_data; + +} + +sub discovery_networks { + my ($self, %options) = @_; + + my $disco_data = []; + foreach (values %{$options{networks}}) { + my $node = { + %$_, + organizationName => $options{organizations}->{ $_->{organizationId} }->{name}, + type => 'network' + }; + + push @$disco_data, $node; + + } + + return $disco_data; +} + sub run { my ($self, %options) = @_; @@ -63,30 +103,22 @@ sub run { $disco_stats->{end_time} = time(); $disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time}; - foreach (values %$devices) { - my $node = { - %$_, - %{$devices_statuses->{$_->{serial}}}, - networkName => $networks->{ $devices_statuses->{$_->{serial}}->{networkId} }->{name}, - organizationName => $organizations->{ $networks->{ $devices_statuses->{$_->{serial}}->{networkId} }->{organizationId} }->{name}, - type => 'device' - }; - - push @disco_data, $node; + my $results = []; + if ($self->{option_results}->{resource_type} eq 'network') { + $results = $self->discovery_networks( + networks => $networks, + organizations => $organizations + ); + } else { + $results = $self->discovery_devices( + devices => $devices, + networks => $networks, + devices_statuses => $devices_statuses, + organizations => $organizations + ); } - - foreach (values %$networks) { - my $node = { - %$_, - organizationName => $organizations->{ $_->{organizationId} }->{name}, - type => 'network' - }; - - push @disco_data, $node; - } - - $disco_stats->{discovered_items} = @disco_data; - $disco_stats->{results} = \@disco_data; + $disco_stats->{discovered_items} = scalar(@$results); + $disco_stats->{results} = $results; my $encoded_data; eval { @@ -119,6 +151,10 @@ Resources discovery. Prettify JSON output. +=item B<--resource-type> + +Choose the type of resources to discover (Can be: 'device', 'network'). + =item B<--ignore-permission-errors> Continue the discovery and ignore permission errors (403 status code).