summaryrefslogtreecommitdiffabout
path: root/lib/Config/HAProxy/Node/Section.pm
blob: 3ef1cf0323b9703cf942e78832a103552abc2a35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package Config::HAProxy::Node::Section;
use strict;
use warnings;
use parent 'Config::HAProxy::Node';
use Carp;

=head1 NAME

Config::HAProxy::Node::Section - HAProxy configuration section

=head1 DESCRIPTION

Objects of this class represent a C<section> in the HAProxy configuration file.
A section is a statement that can contain sub-statements. The following
statements form sections: B<global>, B<defaults>, B<frontend>, and B<backend>.

=cut

sub new {
    my $class = shift;
    my $self = $class->SUPER::new(@_);
    $self->{_tree} = [];
    return $self;
}

=head1 ATTRIBUTES

=head2 is_section

Always true.

=cut

sub is_section { 1 }

=head1 METHODS

=head2 kw

Returns the configuration keyword.

=head2 argv

Returns the list of arguments to the configuration keyword.

=head2 arg

    $s = $node->arg($n)

Returns the B<$n>th argument.

=head2 orig

Returns original line as it appeared in the configuration file.

=head2 locus

Returns the location of this statement in the configuration file (the
B<Text::Locus> object).

=head2 append_node

    $section->append_node(@nodes);

Takes a list of objects of B<Config::HAProxy::Node> derived classes as
arguments. Adds these objects after the last node in the subtree in this
section.

=cut

sub append_node {
    my $self = shift;
    my $n = @{$self->{_tree}};
    push @{$self->{_tree}},
         map {
	     $_->parent($self);
	     $_->index($n++);
	     $_
	 }  @_;
}

=head2 append_node_nonempty

    $section->append_node_nonempty(@nodes);

Same as B<append_node>, but adds new nodes after the last non-empty
node in the subtree.

=cut

sub append_node_nonempty {
    my $self = shift;
    my $n = $#{$self->{_tree}};
    while ($n >= 0 && $self->{_tree}[$n]->is_empty) {
	$n--;
    }
    $self->insert_node($n+1, @_);
}

=head2 insert_node

    $section->insert_node($idx, @nodes);

Inserts B<@nodes> after subnode in position B<$idx> (0-based). 

=cut

sub insert_node {
    my $self = shift;
    my $n = shift;
    my $i = $n;
    splice @{$self->{_tree}}, $n, 0,
         map {
	     $_->parent($self);
	     $_->index($i++);
	     $_
	 }  @_;
    for (; $i < @{$self->{_tree}}; $i++) {
	$self->{_tree}[$i]->index($i);
    }
}

=head2 delete_node

    $section->delete_node($i);

Deletes B<$i>th subnode from the B<$section>.

=cut

sub delete_node {
    my ($self, $n) = @_;
    splice @{$self->{_tree}}, $n, 1;
    for (; $n < @{$self->{_tree}}; $n++) {
	$self->{_tree}[$n]->index($n);
    }
    $self->root->mark_dirty;
}

=head2 tree

    @nodes = $section->tree;

Returns subnodes as a list of B<Config::HAProxy::Node> derived objects.

    $node = $section->tree($i);

Returns B<$i>th subnode from the B<$section>.

=cut

sub tree {
    my ($self, $n) = @_;
    if ($n) {
	return undef if $n >= @{$self->{_tree}};
	return $self->{_tree}[$n];
    }
    return @{shift->{_tree}}
};

=head2 ends_in_empty

    $bool = $section->ends_in_empty

Returns true if the last node in the list of sub-nodes in B<$section> is
an empty node.

=cut

sub ends_in_empty {
    my $self = shift;
    while ($self->is_section) {
	$self = $self->tree(-1);
    }
    return $self->is_empty;
}

my %match = (
    name => {
	wantarg => 1,
	matcher => sub {
	    my ($node, $value) = @_;
	    return $node->kw && $node->kw eq $value;
	}
    },
    arg => {
	wantarg => 1,
	matcher => sub {
	    my ($node, $value) = @_;
	    my $arg = $node->arg($value->{n});
	    return $arg && $arg eq $value->{v};
	}
    },
    section => {
	matcher => sub {
	    my $node = shift;
	    return $node->is_section;
	}
    },
    statement => {
	matcher => sub {
	    my $node = shift;
	    return $node->is_statement;
	}
    },
    comment => {
	matcher => sub {
	    my $node = shift;
	    return $node->is_comment;
	}
    }
);
		
=head2 select

    @nodes = $section->select(%cond);

Returns nodes from B<$section> that match conditions in B<%cond>. Valid
conditions are:

=over 4

=item B<name =E<gt>> I<$s>

Node matches if its keyword (B<kw>) equals I<$s>.

=item B<arg =E<gt>> B<{ n =E<gt>> I<$n>, B<v> =E<gt> I<$s> B<}>

Node mathches if its I<$n>th argument equals I<$s>.

=item B<section =E<gt>> I<$bool>

Node matches if it is (or is not, if I<$bool> is false) a section.

=item B<statement =E<gt>> I<$bool>

Node matches if it is (not) a simple statement.

=item B<comment =E<gt>> I<$bool>

Node matches if it is (not) a comment.

=back

Multiple conditions are checked in the order of their appearance in the
argument list and are joined by the short-circuit logical C<and>. 

For example, to return all B<frontend> statements:

  @fe = $section->select(name => 'frontend');

To return the frontend named C<in>:

  ($fe) = $section->select( name => 'frontend',
                            arg => { n => 0, v => 'in' } );

=cut

sub select {
    my $self = shift;
    my @prog;
    while (my $p = shift) {
	my $arg = shift or croak "missing argument";
	my $m = $match{$p} or croak "unknown matcher: $p";
	if ($m->{wantarg}) {
	    push @prog, [ $m->{matcher}, $arg ];
	} elsif ($arg) {
	    push @prog, $m->{matcher};
	}
    }
    grep { _test_node($_, @prog) } $self->tree;
}

sub _test_node {
    my $node = shift;
    foreach my $f (@_) {
	if (ref($f) eq 'ARRAY') {
	    return 0 unless &{$f->[0]}($node, $f->[1]);
	} else {
	    return 0 unless &{$f}($node);
	}
    }
    return 1;
}

=head1 SEE ALSO

B<Config::HAProxy::Node>, B<Config::HAProxy>, B<Text::Locus>.

=cut

1;


Return to:

Send suggestions and report system problems to the System administrator.