Debian Codegraph @

src/cloud/azure/storage/storageaccount/mode/listresources.pm

raw
1 #
2 # Copyright 2024 Centreon (http://www.centreon.com/)
3 #
4 # Centreon is a full-fledged industry-strength solution that meets
5 # the needs in IT infrastructure and application monitoring for
6 # service performance.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20
21 package cloud::azure::storage::storageaccount::mode::listresources;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27
28 sub new {
29 my ($class, %options) = @_;
30 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
31 bless $self, $class;
32
33 $options{options}->add_options(arguments =>
34 {
35 "resource-group:s" => { name => 'resource_group' },
36 "location:s" => { name => 'location' },
37 "filter-name:s" => { name => 'filter_name' },
38 });
39
40 return $self;
41 }
42
43 sub check_options {
44 my ($self, %options) = @_;
45 $self->SUPER::init(%options);
46 }
47
48 sub manage_selection {
49 my ($self, %options) = @_;
50
51 $self->{resources} = $options{custom}->azure_list_resources(
52 namespace => 'Microsoft.Storage',
53 resource_type => 'storageAccounts',
54 location => $self->{option_results}->{location},
55 resource_group => $self->{option_results}->{resource_group}
56 );
57 }
58
59 sub run {
60 my ($self, %options) = @_;
61
62 $self->manage_selection(%options);
63 foreach my $resource (@{$self->{resources}}) {
64 next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
65 && $resource->{name} !~ /$self->{option_results}->{filter_name}/);
66 my $resource_group = '-';
67 $resource_group = $resource->{resourceGroup} if (defined($resource->{resourceGroup}));
68 $resource_group = $1 if (defined($resource->{id}) && $resource->{id} =~ /resourceGroups\/(.*)\/providers/);
69
70 my @tags;
71 foreach my $tag (keys %{$resource->{tags}}) {
72 push @tags, $tag . ':' . $resource->{tags}->{$tag};
73 }
74
75 $self->{output}->output_add(long_msg => sprintf("[name = %s][resourcegroup = %s][location = %s][id = %s][tags = %s]",
76 $resource->{name},
77 $resource_group,
78 $resource->{location},
79 $resource->{id},
80 join(',', @tags),
81 ));
82 }
83
84 $self->{output}->output_add(severity => 'OK',
85 short_msg => 'List storage accounts:');
86 $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
87 $self->{output}->exit();
88 }
89
90 sub disco_format {
91 my ($self, %options) = @_;
92
93 $self->{output}->add_disco_format(elements => ['name', 'resourcegroup', 'location', 'id', 'tags']);
94 }
95
96 sub disco_show {
97 my ($self, %options) = @_;
98
99 $self->manage_selection(%options);
100 foreach my $resource (@{$self->{resources}}) {
101 my $resource_group = '-';
102 $resource_group = $resource->{resourceGroup} if (defined($resource->{resourceGroup}));
103 $resource_group = $1 if (defined($resource->{id}) && $resource->{id} =~ /resourceGroups\/(.*)\/providers/);
104
105 my @tags;
106 foreach my $tag (keys %{$resource->{tags}}) {
107 push @tags, $tag . ':' . $resource->{tags}->{$tag};
108 }
109
110 $self->{output}->add_disco_entry(
111 name => $resource->{name},
112 resourcegroup => $resource_group,
113 location => $resource->{location},
114 id => $resource->{id},
115 tags => join(',', @tags),
116 );
117 }
118 }
119
120 1;
121
122 __END__
123
124 =head1 MODE
125
126 List storage account resources.
127
128 =over 8
129
130 =item B<--resource-group>
131
132 Set resource group.
133
134 =item B<--location>
135
136 Set resource location.
137
138 =item B<--filter-name>
139
140 Filter resource name (can be a regexp).
141
142 =back
143
144 =cut