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