enh(commvault/commserve): add cache files system (#2735)

This commit is contained in:
qgarnier 2021-04-23 15:54:30 +02:00 committed by GitHub
parent 0f80b9ea03
commit 693cf702e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 105 additions and 4 deletions

View File

@ -52,7 +52,9 @@ sub new {
'api-username:s' => { name => 'api_username' }, 'api-username:s' => { name => 'api_username' },
'api-password:s' => { name => 'api_password' }, 'api-password:s' => { name => 'api_password' },
'user-domain:s' => { name => 'user_domain' }, 'user-domain:s' => { name => 'user_domain' },
'timeout:s' => { name => 'timeout' } 'timeout:s' => { name => 'timeout' },
'cache-create' => { name => 'cache_create' },
'cache-use' => { name => 'cache_use' }
}); });
} }
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
@ -83,6 +85,8 @@ sub check_options {
$self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : ''; $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : '';
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 30; $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 30;
$self->{user_domain} = (defined($self->{option_results}->{user_domain})) ? $self->{option_results}->{user_domain} : ''; $self->{user_domain} = (defined($self->{option_results}->{user_domain})) ? $self->{option_results}->{user_domain} : '';
$self->{cache_create} = $self->{option_results}->{cache_create};
$self->{cache_use} = $self->{option_results}->{cache_use};
if ($self->{hostname} eq '') { if ($self->{hostname} eq '') {
$self->{output}->add_option_msg(short_msg => 'Need to specify hostname option.'); $self->{output}->add_option_msg(short_msg => 'Need to specify hostname option.');
@ -119,6 +123,12 @@ sub get_port {
return $self->{port}; return $self->{port};
} }
sub is_use_cache {
my ($self, %options) = @_;
return defined($self->{cache_use}) ? 1 : 0;
}
sub json_decode { sub json_decode {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -259,17 +269,88 @@ sub request_internal {
return $decoded; return $decoded;
} }
sub get_cache_file_response {
my ($self, %options) = @_;
$self->{cache}->read(statefile => 'cache_commvault_commserve_' . $options{type} . '_' . md5_hex($self->{option_results}->{hostname}) . '_' . md5_hex($self->{option_results}->{api_username}));
my $response = $self->{cache}->get(name => 'response');
my $update_time = $self->{cache}->get(name => 'update_time');
if (!defined($response)) {
$self->{output}->add_option_msg(short_msg => 'Cache file missing');
$self->{output}->option_exit();
}
return $response;
}
sub get_cache_file_update {
my ($self, %options) = @_;
$self->{cache}->read(statefile => 'cache_commvault_commserve_' . $options{type} . '_' . md5_hex($self->{option_results}->{hostname}) . '_' . md5_hex($self->{option_results}->{api_username}));
my $update_time = $self->{cache}->get(name => 'update_time');
return $update_time;
}
sub create_cache_file {
my ($self, %options) = @_;
$self->{cache}->read(statefile => 'cache_commvault_commserve_' . $options{type} . '_' . md5_hex($self->{option_results}->{hostname}) . '_' . md5_hex($self->{option_results}->{api_username}));
$self->{cache}->write(data => { response => $options{response}, update_time => time() });
$self->{output}->output_add(
severity => 'ok',
short_msg => 'Cache file created successfully'
);
$self->{output}->display();
$self->{output}->exit();
}
sub request { sub request {
my ($self, %options) = @_; my ($self, %options) = @_;
return $self->request_internal( return $self->get_cache_file_response(type => $options{type})
if (defined($self->{cache_use}));
my $response = $self->request_internal(
endpoint => $options{endpoint} endpoint => $options{endpoint}
); );
$self->create_cache_file(type => $options{type}, response => $response)
if (defined($self->{cache_create}));
return $response;
}
sub request_jobs {
my ($self, %options) = @_;
return $self->get_cache_file_response(type => 'jobs')
if (defined($self->{cache_use}));
my $lookup_time = $options{completed_job_lookup_time};
if (defined($self->{cache_create})) {
my $update_time = $self->get_cache_file_update(type => 'jobs');
$lookup_time = 3600;
if (defined($update_time)) {
$lookup_time = time() - $update_time;
}
}
my $response = $self->request_internal(
endpoint => $options{endpoint},
get_param => ['completedJobLookupTime=' . $lookup_time]
);
$self->create_cache_file(type => 'jobs', response => $response)
if (defined($self->{cache_create}));
return $response;
} }
sub request_paging { sub request_paging {
my ($self, %options) = @_; my ($self, %options) = @_;
return $self->get_cache_file_response(type => $options{type})
if (defined($self->{cache_use}));
my ($page_num, $page_count) = (1, 200); my ($page_num, $page_count) = (1, 200);
my $alerts = []; my $alerts = [];
while (1) { while (1) {
@ -283,6 +364,9 @@ sub request_paging {
$page_num++; $page_num++;
} }
$self->create_cache_file(type => $options{type}, response => $alerts)
if (defined($self->{cache_create}));
return $alerts; return $alerts;
} }
@ -330,6 +414,14 @@ Set API password
Set HTTP timeout Set HTTP timeout
=item B<--cache-create>
Create a cache file and quit.
=item B<--cache-use>
Use the cache file (created with --cache-create).
=back =back
=head1 DESCRIPTION =head1 DESCRIPTION

View File

@ -138,6 +138,7 @@ sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
my $alarms = $options{custom}->request_paging( my $alarms = $options{custom}->request_paging(
type => 'alert',
endpoint => '/Alert' endpoint => '/Alert'
); );

View File

@ -159,8 +159,9 @@ sub manage_selection {
} }
# Also we get Pending/Waiting/Running jobs with that # Also we get Pending/Waiting/Running jobs with that
my $results = $options{custom}->request( my $results = $options{custom}->request_jobs(
endpoint => '/Job?completedJobLookupTime=' . $lookup_time endpoint => '/Job',
completed_job_lookup_time => $lookup_time
); );
$self->{global} = { total => 0 }; $self->{global} = { total => 0 };
@ -214,6 +215,9 @@ sub manage_selection {
$self->{policy}->{$policy_name} = { job => {}, display => $policy_name } if (!defined($self->{policy}->{$policy_name})); $self->{policy}->{$policy_name} = { job => {}, display => $policy_name } if (!defined($self->{policy}->{$policy_name}));
my $elapsed_time = $current_time - $job->{jobStartTime}; my $elapsed_time = $current_time - $job->{jobStartTime};
if ($options{custom}->is_use_cache()) {
$elapsed_time = $job->{jobElapsedTime};
}
$self->{policy}->{$policy_name}->{job}->{ $job->{jobId} } = { $self->{policy}->{$policy_name}->{job}->{ $job->{jobId} } = {
display => $job->{jobId}, display => $job->{jobId},
elapsed => $elapsed_time, elapsed => $elapsed_time,

View File

@ -46,6 +46,7 @@ sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
my $agents = $options{custom}->request( my $agents = $options{custom}->request(
type => 'mediaagent',
endpoint => '/v2/MediaAgents' endpoint => '/v2/MediaAgents'
); );

View File

@ -46,6 +46,7 @@ sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
my $policies = $options{custom}->request( my $policies = $options{custom}->request(
type => 'storagepolicy',
endpoint => '/V2/StoragePolicy' endpoint => '/V2/StoragePolicy'
); );

View File

@ -105,6 +105,7 @@ sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
my $results = $options{custom}->request( my $results = $options{custom}->request(
type => 'mediaagent',
endpoint => '/v2/MediaAgents' endpoint => '/v2/MediaAgents'
); );

View File

@ -115,6 +115,7 @@ sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
my $results = $options{custom}->request( my $results = $options{custom}->request(
type => 'storagepool',
endpoint => '/StoragePool' endpoint => '/StoragePool'
); );