(plugin)cloud::azure::policyinsights::policystates - fix (#4338)
* fix * update * update regex * add loop on nextlink * Add filter policy name * fix
This commit is contained in:
parent
1402f7cad5
commit
91707f0a3e
|
@ -1194,38 +1194,50 @@ sub azure_list_policystates {
|
||||||
'api-version', $self->{api_version},
|
'api-version', $self->{api_version},
|
||||||
'$select', 'ResourceId, ResourceType, ResourceLocation, ResourceGroup, PolicyDefinitionName, IsCompliant, ComplianceState'
|
'$select', 'ResourceId, ResourceType, ResourceLocation, ResourceGroup, PolicyDefinitionName, IsCompliant, ComplianceState'
|
||||||
];
|
];
|
||||||
my $resource_location;
|
my @filters = ();
|
||||||
if (length $options{resource_location}) {
|
if (length $options{resource_location}) {
|
||||||
$resource_location = ("ResourceLocation eq '" . $options{resource_location} . "'");
|
push(@filters, ("ResourceLocation eq '" . $options{resource_location} . "'"));
|
||||||
delete $options{$resource_location};
|
delete $options{resource_location};
|
||||||
}
|
}
|
||||||
my $resource_type;
|
|
||||||
if (length $options{resource_type}) {
|
if (length $options{resource_type}) {
|
||||||
$resource_type = ("ResourceType eq '" . $options{resource_type} . "'");
|
push(@filters, ("ResourceType eq '" . $options{resource_type} . "'"));
|
||||||
delete $options{$resource_type};
|
delete $options{resource_type};
|
||||||
}
|
}
|
||||||
my $filter;
|
if (length $options{policy_name}) {
|
||||||
if (length $resource_location && length $resource_type) {
|
push(@filters, ("PolicyDefinitionName eq '" . $options{policy_name} . "'"));
|
||||||
$filter = $resource_location . " and " . $resource_type;
|
delete $options{policy_name};
|
||||||
} elsif (length $resource_location) {
|
|
||||||
$filter = $resource_location
|
|
||||||
} elsif (length $resource_type) {
|
|
||||||
$filter = $resource_type
|
|
||||||
}
|
}
|
||||||
if (length($filter)) {
|
my $filterRequest;
|
||||||
push(@$get_params, '$filter', $filter);
|
foreach my $filter (@filters) {
|
||||||
|
if (!defined($filterRequest)) {
|
||||||
|
$filterRequest = $filter;
|
||||||
|
} else {
|
||||||
|
$filterRequest .= " and " . $filter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (length$filterRequest) {
|
||||||
|
push(@$get_params, '$filter', $filterRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
my ($url) = $self->azure_set_url(%options);
|
my ($url) = $self->azure_set_url(%options);
|
||||||
|
my $full_response = [];
|
||||||
|
while (1) {
|
||||||
my $response = $self->request_api(
|
my $response = $self->request_api(
|
||||||
method => 'POST',
|
method => 'POST',
|
||||||
full_url => $url,
|
full_url => $url,
|
||||||
hostname => '',
|
hostname => '',
|
||||||
header => ['Content-Type: application/json'],
|
get_params => $get_params,
|
||||||
get_params => $get_params
|
query_form_post => ''
|
||||||
);
|
);
|
||||||
|
foreach (@{$response->{value}}) {
|
||||||
|
push @$full_response, $_;
|
||||||
|
}
|
||||||
|
|
||||||
return $response->{value};
|
last if (!defined($response->{'@odata.nextLink'}));
|
||||||
|
$url = $response->{'@odata.nextLink'};
|
||||||
|
}
|
||||||
|
|
||||||
|
return $full_response;
|
||||||
}
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|
|
@ -71,6 +71,7 @@ sub new {
|
||||||
$options{options}->add_options(arguments => {
|
$options{options}->add_options(arguments => {
|
||||||
'api-version:s' => { name => 'api_version', default => '2019-10-01'},
|
'api-version:s' => { name => 'api_version', default => '2019-10-01'},
|
||||||
'policy-states:s' => { name => 'policy_states', default => 'default' },
|
'policy-states:s' => { name => 'policy_states', default => 'default' },
|
||||||
|
'policy-name:s' => { name => 'policy_name' },
|
||||||
'resource-group:s' => { name => 'resource_group' },
|
'resource-group:s' => { name => 'resource_group' },
|
||||||
'resource-location:s' => { name => 'resource_location' },
|
'resource-location:s' => { name => 'resource_location' },
|
||||||
'resource-type:s' => { name => 'resource_type' }
|
'resource-type:s' => { name => 'resource_type' }
|
||||||
|
@ -95,14 +96,15 @@ sub manage_selection {
|
||||||
resource_group => $self->{option_results}->{resource_group},
|
resource_group => $self->{option_results}->{resource_group},
|
||||||
query_name => 'queryResults',
|
query_name => 'queryResults',
|
||||||
resource_location => $self->{option_results}->{resource_location},
|
resource_location => $self->{option_results}->{resource_location},
|
||||||
resource_type => $self->{option_results}->{resource_type}
|
resource_type => $self->{option_results}->{resource_type},
|
||||||
|
policy_name => $self->{option_results}->{policy_name}
|
||||||
);
|
);
|
||||||
|
|
||||||
my $non_compliant_policies = 0;
|
my $non_compliant_policies = 0;
|
||||||
$self->{compliance_state} = {};
|
$self->{compliance_state} = {};
|
||||||
foreach my $policy_state (@{ $policy_states }) {
|
foreach my $policy_state (@{ $policy_states }) {
|
||||||
my $resource_name = $policy_state->{resourceId};
|
my $resource_name = $policy_state->{resourceId};
|
||||||
$resource_name =~ /.*\/(\w*)$/;
|
$resource_name =~ /(\w*)$/;
|
||||||
$resource_name = $1;
|
$resource_name = $1;
|
||||||
my $display = $policy_state->{policyDefinitionName} . "_" . $resource_name;
|
my $display = $policy_state->{policyDefinitionName} . "_" . $resource_name;
|
||||||
$self->{compliance_state}->{ $display } = {
|
$self->{compliance_state}->{ $display } = {
|
||||||
|
@ -148,6 +150,10 @@ Set resource location (Optional).
|
||||||
|
|
||||||
Set resource type (Optional).
|
Set resource type (Optional).
|
||||||
|
|
||||||
|
=item B<--policy-name>
|
||||||
|
|
||||||
|
Set policy name (Optional).
|
||||||
|
|
||||||
=item B<--warning-*> B<--critical-*>
|
=item B<--warning-*> B<--critical-*>
|
||||||
|
|
||||||
Thresholds.
|
Thresholds.
|
||||||
|
|
Loading…
Reference in New Issue