enhance token for cisco aci apic

This commit is contained in:
garnier-quentin 2019-12-04 15:29:28 +01:00
parent a85fc16ea4
commit f491fd765c
2 changed files with 22 additions and 11 deletions

View File

@ -129,12 +129,18 @@ sub settings {
$self->build_options_for_httplib(); $self->build_options_for_httplib();
$self->{http}->add_header(key => 'Accept', value => 'application/json'); $self->{http}->add_header(key => 'Accept', value => 'application/json');
$self->{http}->add_header(key => 'Content-Type', value => 'application/json'); $self->{http}->add_header(key => 'Content-Type', value => 'application/json');
if (defined($self->{access_token})) {
$self->{http}->add_header(key => 'Cookie', value => 'APIC-Cookie=' . $self->{access_token});
}
$self->{http}->set_options(%{$self->{option_results}}); $self->{http}->set_options(%{$self->{option_results}});
} }
sub clean_access_token {
my ($self, %options) = @_;
my $datas = { last_timestamp => time() };
$options{statefile}->write(data => $datas);
$self->{access_token} = undef;
$self->{http}->add_header(key => 'Cookie', value => undef);
}
sub get_access_token { sub get_access_token {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -173,31 +179,38 @@ sub get_access_token {
$access_token = $decoded->{imdata}->[0]->{aaaLogin}->{attributes}->{token}; $access_token = $decoded->{imdata}->[0]->{aaaLogin}->{attributes}->{token};
my $datas = { my $datas = {
last_timestamp => time(), last_timestamp => time(),
access_token => $decoded->{imdata}->[0]->{aaaLogin}->{attributes}->{token}, access_token => $access_token,
expires_on => time() + $decoded->{imdata}->[0]->{aaaLogin}->{attributes}->{refreshTimeoutSeconds} expires_on => time() + $decoded->{imdata}->[0]->{aaaLogin}->{attributes}->{refreshTimeoutSeconds}
}; };
$options{statefile}->write(data => $datas); $options{statefile}->write(data => $datas);
} }
return $access_token; $self->{access_token} = $access_token;
$self->{http}->add_header(key => 'Cookie', value => 'APIC-Cookie=' . $self->{access_token});
} }
sub request_api { sub request_api {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->settings();
if (!defined($self->{access_token})) { if (!defined($self->{access_token})) {
$self->{access_token} = $self->get_access_token(statefile => $self->{cache}); $self->{access_token} = $self->get_access_token(statefile => $self->{cache});
} }
$self->settings(); my $content = $self->{http}->request(%options, warning_status => '', unknown_status => '', critical_status => '');
my $content = $self->{http}->request(%options);
# Maybe there is an issue with the access_token. So we retry.
if ($self->{http}->get_code() != 200) {
$self->clean_access_token(statefile => $self->{cache});
$self->get_access_token(statefile => $self->{cache});
$content = $self->{http}->request(%options);
}
my $decoded; my $decoded;
eval { eval {
$decoded = JSON::XS->new->utf8->decode($content); $decoded = JSON::XS->new->utf8->decode($content);
}; };
if ($@) { if ($@) {
$self->{output}->output_add(long_msg => $content, debug => 1);
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }

View File

@ -84,11 +84,9 @@ sub new {
sub manage_selection { sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{nodes} = {};
my $result_nodes = $options{custom}->get_node_health_5m(); my $result_nodes = $options{custom}->get_node_health_5m();
$self->{nodes} = {};
foreach my $node (@{$result_nodes->{imdata}}) { foreach my $node (@{$result_nodes->{imdata}}) {
$node->{fabricNodeHealth5min}->{attributes}->{dn} =~ /^topology\/(.*)\/sys\/CDfabricNodeHealth5min$/; $node->{fabricNodeHealth5min}->{attributes}->{dn} =~ /^topology\/(.*)\/sys\/CDfabricNodeHealth5min$/;
my $node_dn = $1; my $node_dn = $1;