Debian Codegraph @

src/cloud/azure/management/resource/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::management::resource::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-type:s" => { name => 'filter_type' },
38 "filter-name:s" => { name => 'filter_name' },
39 });
40
41 return $self;
42 }
43
44 sub check_options {
45 my ($self, %options) = @_;
46 $self->SUPER::init(%options);
47 }
48
49 sub manage_selection {
50 my ($self, %options) = @_;
51
52 $self->{resources} = $options{custom}->azure_list_resources(
53 namespace => '',
54 resource_type => '',
55 location => $self->{option_results}->{location},
56 resource_group => $self->{option_results}->{resource_group}
57 );
58 }
59
60 sub run {
61 my ($self, %options) = @_;
62
63 $self->manage_selection(%options);
64 foreach my $resource (@{$self->{resources}}) {
65 next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
66 && $resource->{name} !~ /$self->{option_results}->{filter_name}/);
67 next if (defined($self->{option_results}->{filter_type}) && $self->{option_results}->{filter_type} ne ''
68 && $resource->{type} !~ /$self->{option_results}->{filter_type}/);
69 my $resource_group = '-';
70 $resource_group = $resource->{resourceGroup} if (defined($resource->{resourceGroup}));
71 $resource_group = $1 if (defined($resource->{id}) && $resource->{id} =~ /resourceGroups\/(.*)\/providers/);
72
73 my @tags;
74 foreach my $tag (keys %{$resource->{tags}}) {
75 push @tags, $tag . ':' . $resource->{tags}->{$tag};
76 }
77
78 $self->{output}->output_add(long_msg => sprintf("[name = %s][resourcegroup = %s][location = %s][id = %s][type = %s][tags = %s]",
79 $resource->{name},
80 $resource_group,
81 $resource->{location},
82 $resource->{id},
83 $resource->{type},
84 join(',', @tags),
85 ));
86 }
87
88 $self->{output}->output_add(severity => 'OK',
89 short_msg => 'List resources:');
90 $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
91 $self->{output}->exit();
92 }
93
94 sub disco_format {
95 my ($self, %options) = @_;
96
97 $self->{output}->add_disco_format(elements => ['name', 'resourcegroup', 'location', 'id', 'type', 'tags']);
98 }
99
100 sub disco_show {
101 my ($self, %options) = @_;
102
103 $self->manage_selection(%options);
104 foreach my $resource (@{$self->{resources}}) {
105 my $resource_group = '-';
106 $resource_group = $resource->{resourceGroup} if (defined($resource->{resourceGroup}));
107 $resource_group = $1 if (defined($resource->{id}) && $resource->{id} =~ /resourceGroups\/(.*)\/providers/);
108
109 my @tags;
110 foreach my $tag (keys %{$resource->{tags}}) {
111 push @tags, $tag . ':' . $resource->{tags}->{$tag};
112 }
113
114 $self->{output}->add_disco_entry(
115 name => $resource->{name},
116 resourcegroup => $resource_group,
117 location => $resource->{location},
118 id => $resource->{id},
119 type => $resource->{type},
120 tags => join(',', @tags),
121 );
122 }
123 }
124
125 1;
126
127 __END__
128
129 =head1 MODE
130
131 List resources.
132
133 =over 8
134
135 =item B<--resource-group>
136
137 Set resource group.
138
139 =item B<--location>
140
141 Set resource location.
142
143 =item B<--filter-type>
144
145 Filter resource type (can be a regexp).
146
147 =item B<--filter-name>
148
149 Filter resource name (can be a regexp).
150
151 =back
152
153 =cut