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