enh(meraki/restapi): hardened discovery by using defined snake case attributs

This commit is contained in:
Colin Gagnaire 2020-08-12 17:27:07 +02:00
parent 4b06a5cac5
commit 7b1126b2fe
2 changed files with 59 additions and 38 deletions

View File

@ -165,7 +165,7 @@ sub request_api {
my $content; my $content;
eval { eval {
$content = JSON::XS->new->allow_nonref(1)->utf8->decode($response); $content = JSON::XS->new->allow_nonref(1)->decode($response);
}; };
if ($@) { if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");

View File

@ -47,78 +47,99 @@ sub check_options {
sub discovery_devices { sub discovery_devices {
my ($self, %options) = @_; my ($self, %options) = @_;
my $disco_data = []; my $devices = $options{custom}->get_devices(
foreach (values %{$options{devices}}) { organizations => [keys %{$options{organizations}}],
disable_cache => 1
);
my $devices_statuses = $options{custom}->get_organization_device_statuses();
my @results;
foreach (values %{$devices}) {
my $node = { my $node = {
%$_, name => $_->{name},
%{$options{devices_statuses}->{$_->{serial}}}, status => $devices_statuses->{$_->{serial}}->{status},
networkName => $options{networks}->{ $options{devices_statuses}->{$_->{serial}}->{networkId} }->{name}, address => $_->{address},
organizationName => $options{organizations}->{ $options{networks}->{ $options{devices_statuses}->{$_->{serial}}->{networkId} }->{organizationId} }->{name}, latitude => $_->{lat},
type => 'device' longitude => $_->{lng},
mac => $_->{mac},
url => $_->{url},
notes => $_->{notes},
tags => $_->{tags},
model => $_->{model},
firmware => $_->{firmware},
serial => $_->{serial},
public_ip => $devices_statuses->{$_->{serial}}->{publicIp},
lan_ip => $_->{lanIp},
network_id => $_->{networkId},
network_name => $options{networks}->{ $devices_statuses->{$_->{serial}}->{networkId} }->{name},
organization_name => $options{organizations}->{ $options{networks}->{ $devices_statuses->{$_->{serial}}->{networkId} }->{organizationId} }->{name},
configuration_updated_at => $_->{configurationUpdatedAt},
last_reported_at => $devices_statuses->{$_->{serial}}->{lastReportedAt},
}; };
push @$disco_data, $node; push @results, $node;
} }
return $disco_data; return @results;
} }
sub discovery_networks { sub discovery_networks {
my ($self, %options) = @_; my ($self, %options) = @_;
my $disco_data = []; my @results;
foreach (values %{$options{networks}}) { foreach (values %{$options{networks}}) {
my $node = { my $node = {
%$_, name => $_->{name},
organizationName => $options{organizations}->{ $_->{organizationId} }->{name}, id => $_->{id},
type => 'network' type => $_->{type},
timezone => $_->{timeZone},
tags => $_->{tags},
product_types => $_->{productTypes},
organization_id => $_->{organizationId},
organization_name => $options{organizations}->{ $_->{organizationId} }->{name},
disable_remote_status_page => $_->{disableRemoteStatusPage},
disable_my_meraki_com => $_->{disableMyMerakiCom}
}; };
push @$disco_data, $node; push @results, $node;
} }
return $disco_data; return @results;
} }
sub run { sub run {
my ($self, %options) = @_; my ($self, %options) = @_;
my (@disco_data, $disco_stats); my @disco_data;
my $disco_stats;
$disco_stats->{start_time} = time(); $disco_stats->{start_time} = time();
my $organizations = $options{custom}->get_organizations(disable_cache => 1); my $organizations = $options{custom}->get_organizations(disable_cache => 1);
my $networks = $options{custom}->get_networks( my $networks = $options{custom}->get_networks(
organizations => [keys %{$self->{organizations}}], organizations => [keys %{$organizations}],
disable_cache => 1 disable_cache => 1
); );
my $devices = $options{custom}->get_devices(
organizations => [keys %{$self->{organizations}}],
disable_cache => 1
);
my $devices_statuses = $options{custom}->get_organization_device_statuses();
$disco_stats->{end_time} = time();
$disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time};
my $results = [];
if ($self->{option_results}->{resource_type} eq 'network') { if ($self->{option_results}->{resource_type} eq 'network') {
$results = $self->discovery_networks( @disco_data = $self->discovery_networks(
networks => $networks, networks => $networks,
organizations => $organizations organizations => $organizations
); );
} else { } else {
$results = $self->discovery_devices( @disco_data = $self->discovery_devices(
devices => $devices,
networks => $networks, networks => $networks,
devices_statuses => $devices_statuses, organizations => $organizations,
organizations => $organizations %options
); );
} }
$disco_stats->{discovered_items} = scalar(@$results);
$disco_stats->{results} = $results; $disco_stats->{end_time} = time();
$disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time};
$disco_stats->{discovered_items} = @disco_data;
$disco_stats->{results} = \@disco_data;
my $encoded_data; my $encoded_data;
eval { eval {