enh: meraki cloud discovery add resource-type option (#2146)

This commit is contained in:
pkriko 2020-08-12 12:23:40 +02:00 committed by GitHub
parent 4fedaa208e
commit 4b06a5cac5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 60 additions and 24 deletions

View File

@ -32,7 +32,8 @@ sub new {
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
'prettify' => { name => 'prettify' } 'prettify' => { name => 'prettify' },
'resource-type:s' => { name => 'resource_type'},
}); });
return $self; return $self;
@ -43,6 +44,45 @@ sub check_options {
$self->SUPER::init(%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 { sub run {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -63,30 +103,22 @@ sub run {
$disco_stats->{end_time} = time(); $disco_stats->{end_time} = time();
$disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time}; $disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time};
foreach (values %$devices) { my $results = [];
my $node = { if ($self->{option_results}->{resource_type} eq 'network') {
%$_, $results = $self->discovery_networks(
%{$devices_statuses->{$_->{serial}}}, networks => $networks,
networkName => $networks->{ $devices_statuses->{$_->{serial}}->{networkId} }->{name}, organizations => $organizations
organizationName => $organizations->{ $networks->{ $devices_statuses->{$_->{serial}}->{networkId} }->{organizationId} }->{name}, );
type => 'device' } else {
}; $results = $self->discovery_devices(
devices => $devices,
push @disco_data, $node; networks => $networks,
devices_statuses => $devices_statuses,
organizations => $organizations
);
} }
$disco_stats->{discovered_items} = scalar(@$results);
foreach (values %$networks) { $disco_stats->{results} = $results;
my $node = {
%$_,
organizationName => $organizations->{ $_->{organizationId} }->{name},
type => 'network'
};
push @disco_data, $node;
}
$disco_stats->{discovered_items} = @disco_data;
$disco_stats->{results} = \@disco_data;
my $encoded_data; my $encoded_data;
eval { eval {
@ -119,6 +151,10 @@ Resources discovery.
Prettify JSON output. Prettify JSON output.
=item B<--resource-type>
Choose the type of resources to discover (Can be: 'device', 'network').
=item B<--ignore-permission-errors> =item B<--ignore-permission-errors>
Continue the discovery and ignore permission errors (403 status code). Continue the discovery and ignore permission errors (403 status code).