add filter for meraki discovery
This commit is contained in:
parent
9eead752ad
commit
9c9a540cfa
|
@ -32,8 +32,12 @@ 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'},
|
'resource-type:s' => { name => 'resource_type'},
|
||||||
|
'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;
|
return $self;
|
||||||
|
@ -62,10 +66,19 @@ sub discovery_devices {
|
||||||
my $devices_statuses = $options{custom}->get_organization_device_statuses();
|
my $devices_statuses = $options{custom}->get_organization_device_statuses();
|
||||||
|
|
||||||
my @results;
|
my @results;
|
||||||
foreach (values %{$devices}) {
|
foreach (values %$devices) {
|
||||||
|
next if (defined($self->{option_results}->{filter_network_id}) && $self->{option_results}->{filter_network_id} ne '' &&
|
||||||
|
$_->{networkId} !~ /$self->{option_results}->{filter_network_id}/);
|
||||||
|
next if (defined($self->{option_results}->{filter_tags}) && $self->{option_results}->{filter_tags} ne '' &&
|
||||||
|
(!defined($_->{tags}) || $_->{tags} !~ /$self->{option_results}->{filter_tags}/));
|
||||||
|
next if (defined($self->{option_results}->{filter_organization_id}) && $self->{option_results}->{filter_organization_id} ne '' &&
|
||||||
|
$options{networks}->{ $_->{networkId} }->{organizationId} !~ /$self->{option_results}->{filter_organization_id}/);
|
||||||
|
next if (defined($self->{option_results}->{filter_organization_name}) && $self->{option_results}->{filter_organization_name} ne '' &&
|
||||||
|
$options{organizations}->{ $options{networks}->{ $_->{networkId} }->{organizationId} }->{name} !~ /$self->{option_results}->{filter_organization_name}/);
|
||||||
|
|
||||||
my $node = {
|
my $node = {
|
||||||
name => $_->{name},
|
name => $_->{name},
|
||||||
status => $devices_statuses->{$_->{serial}}->{status},
|
status => $devices_statuses->{ $_->{serial} }->{status},
|
||||||
address => $_->{address},
|
address => $_->{address},
|
||||||
latitude => $_->{lat},
|
latitude => $_->{lat},
|
||||||
longitude => $_->{lng},
|
longitude => $_->{lng},
|
||||||
|
@ -76,13 +89,13 @@ sub discovery_devices {
|
||||||
model => $_->{model},
|
model => $_->{model},
|
||||||
firmware => $_->{firmware},
|
firmware => $_->{firmware},
|
||||||
serial => $_->{serial},
|
serial => $_->{serial},
|
||||||
public_ip => $devices_statuses->{$_->{serial}}->{publicIp},
|
public_ip => $devices_statuses->{ $_->{serial} }->{publicIp},
|
||||||
lan_ip => $_->{lanIp},
|
lan_ip => $_->{lanIp},
|
||||||
network_id => $_->{networkId},
|
network_id => $_->{networkId},
|
||||||
network_name => $options{networks}->{ $devices_statuses->{$_->{serial}}->{networkId} }->{name},
|
network_name => $options{networks}->{ $_->{networkId} }->{name},
|
||||||
organization_name => $options{organizations}->{ $options{networks}->{ $devices_statuses->{$_->{serial}}->{networkId} }->{organizationId} }->{name},
|
organization_name => $options{organizations}->{ $options{networks}->{ $_->{networkId} }->{organizationId} }->{name},
|
||||||
configuration_updated_at => $_->{configurationUpdatedAt},
|
configuration_updated_at => $_->{configurationUpdatedAt},
|
||||||
last_reported_at => $devices_statuses->{$_->{serial}}->{lastReportedAt},
|
last_reported_at => $devices_statuses->{ $_->{serial} }->{lastReportedAt}
|
||||||
};
|
};
|
||||||
|
|
||||||
push @results, $node;
|
push @results, $node;
|
||||||
|
@ -97,6 +110,13 @@ sub discovery_networks {
|
||||||
|
|
||||||
my @results;
|
my @results;
|
||||||
foreach (values %{$options{networks}}) {
|
foreach (values %{$options{networks}}) {
|
||||||
|
next if (defined($self->{option_results}->{filter_tags}) && $self->{option_results}->{filter_tags} ne '' &&
|
||||||
|
(!defined($_->{tags}) || $_->{tags} !~ /$self->{option_results}->{filter_tags}/));
|
||||||
|
next if (defined($self->{option_results}->{filter_organization_id}) && $self->{option_results}->{filter_organization_id} ne '' &&
|
||||||
|
$_->{organizationId} !~ /$self->{option_results}->{filter_organization_id}/);
|
||||||
|
next if (defined($self->{option_results}->{filter_organization_name}) && $self->{option_results}->{filter_organization_name} ne '' &&
|
||||||
|
$options{organizations}->{ $_->{organizationId} }->{name} !~ /$self->{option_results}->{filter_organization_name}/);
|
||||||
|
|
||||||
my $node = {
|
my $node = {
|
||||||
name => $_->{name},
|
name => $_->{name},
|
||||||
id => $_->{id},
|
id => $_->{id},
|
||||||
|
@ -184,9 +204,21 @@ Prettify JSON output.
|
||||||
|
|
||||||
Choose the type of resources to discover (Can be: 'device', 'network').
|
Choose the type of resources to discover (Can be: 'device', 'network').
|
||||||
|
|
||||||
=item B<--ignore-permission-errors>
|
=item B<--filter-network-id>
|
||||||
|
|
||||||
Continue the discovery and ignore permission errors (403 status code).
|
Filter by network id (Can be a regexp).
|
||||||
|
|
||||||
|
=item B<--filter-organization-id>
|
||||||
|
|
||||||
|
Filter by organization id (Can be a regexp).
|
||||||
|
|
||||||
|
=item B<--filter-organization-name>
|
||||||
|
|
||||||
|
Filter by organization name (Can be a regexp).
|
||||||
|
|
||||||
|
=item B<--filter-tags>
|
||||||
|
|
||||||
|
Filter by tags (Can be a regexp).
|
||||||
|
|
||||||
=back
|
=back
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,10 @@ sub new {
|
||||||
bless $self, $class;
|
bless $self, $class;
|
||||||
|
|
||||||
$options{options}->add_options(arguments => {
|
$options{options}->add_options(arguments => {
|
||||||
|
'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;
|
return $self;
|
||||||
|
@ -56,10 +60,33 @@ sub manage_selection {
|
||||||
|
|
||||||
my $devices_statuses = $options{custom}->get_organization_device_statuses();
|
my $devices_statuses = $options{custom}->get_organization_device_statuses();
|
||||||
foreach (keys %$devices) {
|
foreach (keys %$devices) {
|
||||||
|
if (defined($self->{option_results}->{filter_network_id}) && $self->{option_results}->{filter_network_id} ne '' &&
|
||||||
|
$devices->{$_}->{networkId} !~ /$self->{option_results}->{filter_network_id}/) {
|
||||||
|
delete $devices->{$_};
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
if (defined($self->{option_results}->{filter_tags}) && $self->{option_results}->{filter_tags} ne '' &&
|
||||||
|
(!defined($devices->{$_}->{tags}) || $devices->{$_}->{tags} !~ /$self->{option_results}->{filter_tags}/)) {
|
||||||
|
delete $devices->{$_};
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
if (defined($self->{option_results}->{filter_organization_id}) && $self->{option_results}->{filter_organization_id} ne '' &&
|
||||||
|
$networks->{ $devices->{$_}->{networkId} }->{organizationId} !~ /$self->{option_results}->{filter_organization_id}/) {
|
||||||
|
delete $devices->{$_};
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
|
||||||
|
my $organization_name = $organizations->{ $networks->{ $devices->{$_}->{networkId} }->{organizationId} }->{name};
|
||||||
|
if (defined($self->{option_results}->{filter_organization_name}) && $self->{option_results}->{filter_organization_name} ne '' &&
|
||||||
|
$organization_name !~ /$self->{option_results}->{filter_organization_name}/) {
|
||||||
|
delete $devices->{$_};
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
|
||||||
$devices->{$_}->{status} = $devices_statuses->{ $devices->{$_}->{serial} }->{status};
|
$devices->{$_}->{status} = $devices_statuses->{ $devices->{$_}->{serial} }->{status};
|
||||||
$devices->{$_}->{public_ip} = $devices_statuses->{ $devices->{$_}->{serial} }->{publicIp};
|
$devices->{$_}->{public_ip} = $devices_statuses->{ $devices->{$_}->{serial} }->{publicIp};
|
||||||
$devices->{$_}->{network_name} = $networks->{ $devices_statuses->{ $devices->{$_}->{serial} }->{networkId} }->{name};
|
$devices->{$_}->{network_name} = $networks->{ $devices->{$_}->{networkId} }->{name};
|
||||||
$devices->{$_}->{organization_name} = $organizations->{ $networks->{ $devices_statuses->{ $devices->{$_}->{serial} }->{networkId} }->{organizationId} }->{name};
|
$devices->{$_}->{organization_name} = $organization_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $devices;
|
return $devices;
|
||||||
|
@ -122,6 +149,22 @@ List devices.
|
||||||
|
|
||||||
=over 8
|
=over 8
|
||||||
|
|
||||||
|
=item B<--filter-network-id>
|
||||||
|
|
||||||
|
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).
|
||||||
|
|
||||||
=back
|
=back
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
|
Loading…
Reference in New Issue