1package Netdot::Model::RRSRV;
2
3use base 'Netdot::Model';
4use warnings;
5use strict;
6
7my $logger = Netdot->log->get_logger('Netdot::Model::DNS');
8
9=head1 Netdot::Model::RRSRV - DNS SRV record Class
10
11=head1 CLASS METHODS
12=cut
13
14############################################################################
15
16=head2 insert - Insert new RRSRV object
17
18    We override the base method to:
19    - Check for conflicting record types
20    - Validate TTL
21
22
23  Arguments:
24    See schema
25  Returns:
26    RRSRV object
27  Example:
28    my $record = RRSRV->insert(\%args)
29
30=cut
31
32sub insert {
33    my($class, $argv) = @_;
34    $class->isa_class_method('insert');
35
36    $class->throw_fatal('Missing required arguments: rr')
37	unless ( $argv->{rr} );
38
39    foreach my $field ( qw/port priority target weight/ ){
40	$class->throw_user("Missing required argument: $field")
41	    unless (defined $argv->{$field});
42    }
43
44    my $rr = (ref $argv->{rr})? $argv->{rr} : RR->retrieve($argv->{rr});
45    $class->throw_fatal("Invalid rr argument") unless $rr;
46
47    # TTL needs to be set and converted into integer
48    $argv->{ttl} = (defined($argv->{ttl}) && length($argv->{ttl}))? $argv->{ttl} : $rr->zone->default_ttl;
49    $argv->{ttl} = $class->ttl_from_text($argv->{ttl});
50
51
52    # Make sure name is valid
53    unless ( $rr->name =~ /^_.+\._.+/ ){
54	$class->throw_user("Owner name must be of the form _Service._Proto");
55    }
56
57    # Avoid the "CNAME and other records" error condition
58    if ( $rr->cnames ){
59	$class->throw_user("Cannot add any other record to an alias");
60    }
61    if ( $rr->ptr_records ){
62	$class->throw_user($rr->name.": Cannot add SRV records when PTR records exist");
63    }
64    if ( $rr->naptr_records ){
65	$class->throw_user($rr->name.": Cannot add SRV records when NAPTR records exist");
66    }
67
68    return $class->SUPER::insert($argv);
69
70}
71
72=head1 INSTANCE METHODS
73=cut
74
75############################################################################
76
77=head2 update
78
79    We override the base method to:
80     - Validate TTL
81
82  Arguments:
83    Hash with field/value pairs
84  Returns:
85    Number of rows updated or -1
86  Example:
87    $record->update(\%args)
88
89=cut
90
91sub update {
92    my($self, $argv) = @_;
93    $self->isa_object_method('update');
94
95    if ( defined $argv->{ttl} ){
96	$argv->{ttl} = $self->ttl_from_text($argv->{ttl});
97    }
98
99    return $self->SUPER::update($argv);
100}
101
102##################################################################
103
104=head2 as_text
105
106    Returns the text representation of this record
107
108  Arguments:
109    None
110  Returns:
111    string
112  Examples:
113    print $rr->as_text();
114
115=cut
116
117sub as_text {
118    my $self = shift;
119    $self->isa_object_method('as_text');
120
121    return $self->_net_dns->string();
122}
123
124
125##################################################################
126# Private methods
127##################################################################
128
129
130##################################################################
131sub _net_dns {
132    my $self = shift;
133
134    my $ndo = Net::DNS::RR->new(
135	name        => $self->rr->get_label,
136	ttl         => $self->ttl,
137	class       => 'IN',
138	type        => 'SRV',
139	priority    => $self->priority,
140	weight      => $self->weight,
141	port        => $self->port,
142	target      => $self->target,
143	);
144
145    return $ndo;
146}
147
148=head1 AUTHOR
149
150Carlos Vicente, C<< <cvicente at ns.uoregon.edu> >>
151
152=head1 COPYRIGHT & LICENSE
153
154Copyright 2012 University of Oregon, all rights reserved.
155
156This program is free software; you can redistribute it and/or modify
157it under the terms of the GNU General Public License as published by
158the Free Software Foundation; either version 2 of the License, or
159(at your option) any later version.
160
161This program is distributed in the hope that it will be useful, but
162WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
163or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
164License for more details.
165
166You should have received a copy of the GNU General Public License
167along with this program; if not, write to the Free Software Foundation,
168Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
169
170=cut
171
172#Be sure to return 1
1731;
174
175