Bind8/Conf/Server.pm

filter: Statementstmt 29.1% 28 / 96 Branchbran 3.7% 2 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 6 / 12 Podpod 100% 3 / 3 totaltotal 23.6% 39 / 165 CC 29 SCAR 39.1
CC
29
Cov
20%
new
110.0
keys
56.0
__render
42.0
CRAP 49.7
« Bind8/Conf/Parser.pm ↑ Summary Bind8/DB/MX.pm »
1 # Bind8 server handling
2 #
3 # Copyright Karthik Krishnamurthy <karthik.k@extremix.net>
4  
5 =head1 NAME
6  
7 Unix::Conf::Bind8::Conf::Server - Class for handling Bind8 configuration
8 directive `server'
9  
10 =head1 SYNOPSIS
11  
12 use Unix::Conf::Bind8;
13  
14 my ($conf, $server, $ret);
15  
16 $conf = Unix::Conf::Bind8->new_conf (
17 FILE => '/etc/named.conf',
18 SECURE_OPEN => 1,
19 ) or $conf->die ("couldn't open `named.conf'");
20  
21 #
22 # Ways to get a server object.
23 #
24  
25 $server = $conf->new_server (
26 NAME => '192.168.1.1',
27 BOGUS => 'yes',
28 ) or $server->die ("couldn't create server `192.168.1.1'");
29  
30 # OR
31  
32 $server = $conf->get_server ('10.0.0.1')
33 or $server->die ("couldn't get server `10.0.0.1'");
34  
35 #
36 # Operations that can be performed on a server object.
37 #
38  
39 $ret = $server->bogus ('no')
40 or $ret->die ("couldn't set attribute");
41  
42 $ret = $server->keys (qw (extremix-slaves.key sample_key));
43
44 # get attributes
45 $ret = $server->keys ()
46 or $ret->die ("couldn't get keys");
47 local $" = "\n";
48 printf "Keys defined:\n@$ret\n";
49  
50 # delete attribute
51 $ret = $server->delete_transfer_format ()
52 or $ret->die ("couldn't delete attribute");
53  
54 =head1 METHODS
55  
56 =cut
57  
58  
59 package Unix::Conf::Bind8::Conf::Server;
60  
61 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 10 ▶use Unix::Conf;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 10 ▶use Unix::Conf::Bind8::Conf::Directive;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 10 our (@ISA) = qw (Unix::Conf::Bind8::Conf::Directive);
66 10 ▶use Unix::Conf::Bind8::Conf::Lib;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 10 my %ServerDirectives = (
69 'bogus' => \&__valid_yesno,
70 # the man page doesn't mention this, but the
71 # sample conf file has it
72 'support-ixfr' => \&__valid_yesno,
73 'transfers' => \&__valid_number,
74 'transfer-format' => \&__valid_transfer_format,
75  
76 'keys' => 0,
77 );
78  
79 =over 4
80  
81 =item new ()
82  
83 Arguments
84 NAME => scalar,
85 BOGUS => scalar, # Optional
86 TRANSFERS => scalar, # Optional
87 SUPPORT-IXFR => scalar,
88 TRANSFER-FORMAT
89 => scalar, # Optional
90 KEYS => [elements ], # Optional
91 WHERE => 'FIRST'|'LAST'|'BEFORE'|'AFTER'
92 WARG => Unix::Conf::Bind8::Conf::Directive subclass object
93 # WARG is to be provided only in case WHERE eq 'BEFORE
94 # or WHERE eq 'AFTER'
95 PARENT => reference,
96 # to the Conf object datastructure.
97  
98 Class constructor.
99 Creates a new Unix::Conf::Bind8::Conf::Server object, and returns it,
100 on success, an Err object otherwise. Do not call this constructor
101 directly. Use Unix::Conf::Bind8::Conf::new_server () instead.
102  
103 =cut
104  
105 sub new
106 0 ▶{
Subroutine new: not called CC=10 SCAR=47.0
Pod documented
107 0 shift ();
108 0 my $new = bless ({});
109 0 my %args = @_;
110 0 my $ret;
111  
112 0 ▶ $args{PARENT} || return (Unix::Conf->_err ('new', "PARENT not defined"));
Branch: 1 branch
BranchTrueFalseTotal
$args{"PARENT"} or return "Unix::Conf"->_err("new", "PARENT not defined") 0 0 0
113 0 ▶ $args{NAME} || return (Unix::Conf->_err ('new', "NAME not defined"));
Branch: 1 branch
BranchTrueFalseTotal
$args{"NAME"} or return "Unix::Conf"->_err("new", "NAME not defined") 0 0 0
114  
115 0 ▶ $ret = $new->_parent ($args{PARENT}) or return ($ret);
Branch: 1 branch
BranchTrueFalseTotal
$ret = $new->_parent($args{"PARENT"}) or return $ret 0 0 0
116 0 ▶ $ret = $new->name ($args{NAME}) or return ($ret);
Branch: 1 branch
BranchTrueFalseTotal
$ret = $new->name($args{"NAME"}) or return $ret 0 0 0
117 0 ▶ my $where = $args{WHERE} ? $args{WHERE} : 'LAST';
Branch: 1 branch
BranchTrueFalseTotal
$args{'WHERE'} ? : 0 0 0
118 0 my $warg = $args{WARG};
119 0 delete (@args{'PARENT','NAME','WHERE','WARG'});
120  
121 0 for (keys (%args)) {
122 0 my $meth = $_;
123 0 $meth =~ tr/A-Z/a-z/;
124 return (Unix::Conf->_err ("new", "attribute `$meth' not supported"))
125 0 ▶ unless (defined ($ServerDirectives{$meth}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ServerDirectives{$meth} 0 0 0
126 0 $meth =~ tr/-/_/;
127 0 ▶ $ret = $new->$meth ($args{$_}) or return ($ret);
Branch: 1 branch
BranchTrueFalseTotal
$ret = $new->$meth($args{$_}) or return $ret 0 0 0
128 }
129  
130 0 ▶ $ret = Unix::Conf::Bind8::Conf::_insert_in_list ($new, $where, $warg)
Branch: 1 branch
BranchTrueFalseTotal
$ret = Unix::Conf::Bind8::Conf::_insert_in_list($new, $where, $warg) or return $ret 0 0 0
131 or return ($ret);
132  
133 0 return ($new);
134 }
135  
136  
137 =item name ()
138  
139 Arguments
140 value # optional
141  
142 Object method.
143 Get/set the name attribute in the invocant. Returns the attribute value
144 or true on success, an Err object otherwise.
145  
146 =cut
147  
148 sub name
149 0 ▶{
Subroutine name: not called CC=5 SCAR=34.0
Pod documented
150 0 my ($self, $name) = @_;
151  
152 0 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 0 0 0
153 0 my $ret;
154 0 ▶ return (Unix::Conf->_err ('name', "illegal name `$name'"))
Branch: 1 branch
BranchTrueFalseTotal
unless &__valid_ipaddress($name) 0 0 0
155 unless (__valid_ipaddress ($name));
156 0 $self->{name} = $name;
157 0 ▶ $ret = Unix::Conf::Bind8::Conf::_add_server ($self)
Branch: 1 branch
BranchTrueFalseTotal
$ret = Unix::Conf::Bind8::Conf::_add_server($self) or return $ret 0 0 0
158 or return ($ret);
159 0 $ret = $self->dirty (1);
160 0 return (1);
161 }
162 return (
163 defined ($self->{name}) ? $self->{name} :
164 0 ▶ Unix::Conf->_err ('name', "name not defined for server")
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'name'} ? : 0 0 0
165 );
166 }
167  
168 =item bogus ()
169  
170 Arguments
171 value # optional
172  
173 Object method.
174 Get/set the name attribute in the invocant. Returns the attribute value
175 or true on success, an Err object otherwise.
176  
177 =cut
178  
179 =item delete_bogus ()
180  
181 Object method.
182 Deletes the corresponding attribute, if defined, and returns true,
183 an Err object otherwise.
184  
185 =cut
186  
187 =item support_ixfr ()
188  
189 Arguments
190 value # optional
191  
192 Object method.
193 Get/set the name attribute in the invocant. Returns the attribute value
194 or true on success, an Err object otherwise.
195  
196 =cut
197  
198 =item delete_support_ixfr ()
199  
200 Object method.
201 Deletes the corresponding attribute, if defined, and returns true,
202 an Err object otherwise.
203  
204 =cut
205  
206 =item transfers ()
207  
208 Arguments
209 value # number, optional
210  
211 Object method.
212 Get/set the name attribute in the invocant. Returns the attribute value
213 or true on success, an Err object otherwise.
214  
215 =cut
216  
217 =item delete_transfers ()
218  
219 Object method.
220 Deletes the corresponding attribute, if defined, and returns true,
221 an Err object otherwise.
222  
223 =cut
224  
225 =item transfer_format ()
226  
227 Arguments
228 value # 'one-answer'|'many-answers', optional
229  
230 Object method.
231 Get/set the name attribute in the invocant. Returns the attribute value
232 or true on success, an Err object otherwise.
233  
234 =cut
235  
236 =item delete_transfer_format ()
237  
238 Object method.
239 Deletes the corresponding attribute, if defined, and returns true,
240 an Err object otherwise.
241  
242 =cut
243  
244 10 for my $dir (keys (%ServerDirectives)) {
245 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
246
247 50 my $meth = $dir;
248 50 $meth =~ tr/-/_/;
249  
250 50 ▶ ($ServerDirectives{$dir} =~ /^CODE/) && do {
Branch: 1 branch
BranchTrueFalseTotal
$ServerDirectives{$dir} =~ /^CODE/ and do { *$meth = sub { my($self, $arg) = @_; if (defined $arg) { return "Unix::Conf"->_err("$meth", "invalid argument `${arg}'") unless &{$ServerDirectives{$dir};}($arg); $self->{$dir} = $arg; $self->dirty(1); return 1; } ; return defined $self->{$dir} ? $self->{$dir} : "Unix::Conf"->_err("$dir", "`${dir}' not defined"); } } 40 10 50
251 0 ▶ *$meth = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
252 0 my ($self, $arg) = @_;
253  
254 0 ▶ if (defined ($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 0 0 0
255 return (Unix::Conf->_err ("$meth", "invalid argument `$arg'"))
256 0 ▶ unless (&{$ServerDirectives{$dir}}($arg));
Branch: 1 branch
BranchTrueFalseTotal
unless &{$ServerDirectives{$dir};}($arg) 0 0 0
257 0 $self->{$dir} = $arg;
258 0 $self->dirty (1);
259 0 return (1);
260 }
261 return (
262 0 ▶ defined ($self->{$dir}) ? $self->{$dir} :
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$dir} ? : 0 0 0
263 Unix::Conf->_err ("$dir", "`$dir' not defined")
264 );
265 40 };
266 };
267 50 ▶ *{"delete_$meth"} = sub {
Subroutine __ANON__: not called CC=2 SCAR=15.4
268 0 my $self = $_[0];
269  
270 return (Unix::Conf->_err ("delete_$meth", "`$dir' not defined"))
271 0 ▶ unless (defined ($self->{$dir}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$dir} 0 0 0
272 0 delete ($self->{$dir});
273 0 $self->dirty (1);
274 0 return (1);
275 50 };
276 }
277  
278 =item keys ()
279  
280 Arguments
281 LIST # name, optional
282 or
283 [ LIST ]
284  
285 Object method.
286 Get/set the name attribute in the invocant. Returns the attribute value
287 or true on success, an Err object otherwise.
288  
289 =cut
290  
291 =item delete_keys ()
292  
293 Object method.
294 Deletes the corresponding attribute, if defined, and returns true,
295 an Err object otherwise.
296  
297 =cut
298  
299 sub keys
300 0 ▶{
Subroutine keys: not called CC=7 SCAR=40.3
Pod documented
301 0 my $self = shift ();
302  
303 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
304 0 my $args;
305 0 ▶ if (ref ($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
306 0 ▶ return (Unix::Conf->_err ('keys', "expected argument LIST or [ LIST ]"))
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($_[0], "ARRAY") 0 0 0
307 unless (UNIVERSAL::isa ($_[0], 'ARRAY'));
308 0 $args = $_[0];
309 }
310 else {
311 # assume a list
312 0 $args = \@_;
313 }
314  
315 0 for (@$args) {
316 0 ▶ return (Unix::Conf->_err ('keys', "`$_' not a valid key"))
Branch: 1 branch
BranchTrueFalseTotal
unless Unix::Conf::Bind8::Conf::_get_key($self->_parent, $_) 0 0 0
317 unless (Unix::Conf::Bind8::Conf::_get_key ($self->_parent (), $_));
318 }
319 0 @{$self->{keys}}{@$args} = (1) x @$args;
320 0 $self->dirty (1);
321 0 return (1);
322 }
323 return (Unix::Conf->_err ('keys', "keys not defined"))
324 0 ▶ unless ($self->{keys});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"keys"} 0 0 0
325 0 return ([ keys (%{$self->{keys}}) ]);
326 }
327  
328 # no add_to_keys, delete_from_keys as there are not likely to be
329 # many keys in for one server.
330  
331 sub __render
332 0 ▶{
Subroutine __render: not called CC=6 SCAR=37.4
333 0 my $self = $_[0];
334 0 my ($rendered, $tmp);
335  
336 0 $tmp = $self->name ();
337 0 $rendered = "server $tmp {\n";
338 0 ▶ $rendered .= "\tbogus $tmp;\n"
Branch: 1 branch
BranchTrueFalseTotal
if $tmp = $self->bogus 0 0 0
339 if ($tmp = $self->bogus ());
340 0 ▶ $rendered .= "\tsupport-ixfr $tmp;\n"
Branch: 1 branch
BranchTrueFalseTotal
if $tmp = $self->support_ixfr 0 0 0
341 if ($tmp = $self->support_ixfr ());
342 0 ▶ $rendered .= "\ttransfers $tmp;\n"
Branch: 1 branch
BranchTrueFalseTotal
if $tmp = $self->transfers 0 0 0
343 if ($tmp = $self->transfers ());
344 0 ▶ $rendered .= "\ttransfer-format $tmp;\n"
Branch: 1 branch
BranchTrueFalseTotal
if $tmp = $self->transfer_format 0 0 0
345 if ($tmp = $self->transfer_format ());
346 0 ▶ $rendered .= "\tkeys { @$tmp };\n"
Branch: 1 branch
BranchTrueFalseTotal
if $tmp = $self->keys 0 0 0
347 if ($tmp = $self->keys ());
348 0 $rendered .= "};";
349 0 return ($self->_rstring (\$rendered));
350 }
351  
352 10 1;
« Bind8/Conf/Parser.pm ↑ Summary Bind8/DB/MX.pm »