| 1 | package Net::ISC::DHCPd::OMAPI::Actions; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Net::ISC::DHCPd::OMAPI::Actions - Common actions on OMAPI objects | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | This module contains methods which can be called on each of the | ||
| 10 | L<Net::ISC::DHCPd::OMAPI> subclasses. | ||
| 11 | |||
| 12 | Changing object attributes will not alter the attributes on server. To do | ||
| 13 | so use L</write> to update the server. | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | use Moose::Role; | ||
| 18 | |||
| 19 | my $ATTR_ROLE = "Net::ISC::DHCPd::OMAPI::Meta::Attribute"; | ||
| 20 | |||
| 21 | =head1 ATTRIBUTES | ||
| 22 | |||
| 23 | =head2 parent | ||
| 24 | |||
| 25 | $omapi_obj = $self->parent; | ||
| 26 | |||
| 27 | Returns the parent L<Net::ISC::DHCPd::OMAPI> object. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | has parent => ( | ||
| 32 | is => 'ro', | ||
| 33 | isa => 'Net::ISC::DHCPd::OMAPI', | ||
| 34 | required => 1, | ||
| 35 | ); | ||
| 36 | |||
| 37 | =head2 errstr | ||
| 38 | |||
| 39 | $str = $self->errstr; | ||
| 40 | |||
| 41 | Holds the latest error. Check this if a method returns empty list. | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | has errstr => ( | ||
| 46 | is => 'rw', | ||
| 47 | isa => 'Str', | ||
| 48 | default => '', | ||
| 49 | ); | ||
| 50 | |||
| 51 | =head2 extra_attributes | ||
| 52 | |||
| 53 | $hash_ref = $self->extra_attributes; | ||
| 54 | |||
| 55 | Contains all attributes, which is not predefined by the OMAPI object. | ||
| 56 | |||
| 57 | Note: If you ever need to use this - send me a bug report, since it | ||
| 58 | means something is missing. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | has extra_attributes => ( | ||
| 63 | is => 'ro', | ||
| 64 | isa => 'HashRef', | ||
| 65 | default => sub { {} }, | ||
| 66 | ); | ||
| 67 | |||
| 68 | =head1 METHODS | ||
| 69 | |||
| 70 | =head2 read | ||
| 71 | |||
| 72 | $int = $self->read; | ||
| 73 | |||
| 74 | Open an object. Returns the number of attributes read. 0 = not in server. | ||
| 75 | |||
| 76 | It looks up an object on server, by all the attributes that has action | ||
| 77 | C<lookup>. Will update all attributes in the local object, and setting | ||
| 78 | all unknown objects in L</extra_attributes>. | ||
| 79 | |||
| 80 | |||
| 81 | This is subject for change, but: | ||
| 82 | |||
| 83 | C<read()> will also do a post check which checks if the retrieved values | ||
| 84 | actually match the one used to lookup. If they do not match all retrieved | ||
| 85 | data will be stored in L</extra_attributes> and this method will return | ||
| 86 | zero (0). | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | sub read { | ||
| 91 | my $self = shift; | ||
| 92 | my $post_check_failed = 0; | ||
| 93 | my $n = 0; | ||
| 94 | my(@out, %out); | ||
| 95 | |||
| 96 | @out = $self->_open; | ||
| 97 | |||
| 98 | %{ $self->extra_attributes } = (); # clear all extra attributes | ||
| 99 | |||
| 100 | while($out[-1] =~ /(\S+)\s=\s(\S+)/g) { | ||
| 101 | my($name, $value) = ($1, $2); | ||
| 102 | $name =~ s/-/_/g; | ||
| 103 | $value =~ s/^"(.*)"$/$1/; | ||
| 104 | $n++; | ||
| 105 | |||
| 106 | if(my $attr = $self->meta->get_attribute($name)) { | ||
| 107 | |||
| 108 | if( #_ugly___________________________ | ||
| 109 | $attr->does($ATTR_ROLE) | ||
| 110 | and $self->${ \"has_$name" } | ||
| 111 | and $attr->has_action('lookup') | ||
| 112 | ) { #-------------------------------- | ||
| 113 | |||
| 114 | if($attr->should_coerce) { | ||
| 115 | $value = $attr->type_constraint->coerce($value); | ||
| 116 | } | ||
| 117 | |||
| 118 | if($self->$name ne $value) { | ||
| 119 | $post_check_failed = 1; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | $out{$name} = $value; | ||
| 124 | } | ||
| 125 | else { | ||
| 126 | $self->extra_attributes->{$name} = $value; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | for my $name (keys %out) { | ||
| 131 | if($post_check_failed) { | ||
| 132 | $self->extra_attributes->{$name} = $out{$name}; | ||
| 133 | } | ||
| 134 | else { | ||
| 135 | $self->$name($out{$name}); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | return $post_check_failed ? 0 : $n; | ||
| 140 | } | ||
| 141 | |||
| 142 | around read => \&_around; | ||
| 143 | |||
| 144 | =head2 write | ||
| 145 | |||
| 146 | $bool = $self->write; | ||
| 147 | $bool = $self->write(@attributes); | ||
| 148 | |||
| 149 | Will set attributes on server object. | ||
| 150 | |||
| 151 | C<@attributes> is by default every attribute on create, or every | ||
| 152 | attribute with action "modify" on update. | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | sub write { | ||
| 157 | my $self = shift; | ||
| 158 | my @attr = @_; | ||
| 159 | my $new = 0; | ||
| 160 | my(@cmd, @out); | ||
| 161 | |||
| 162 | # check for existence | ||
| 163 | @out = $self->_open; | ||
| 164 | |||
| 165 | if(grep { /not found/i } @out) { | ||
| 166 | $new = 1; | ||
| 167 | } | ||
| 168 | |||
| 169 | if(@attr == 0) { | ||
| 170 | for my $attr ($self->meta->get_all_attributes) { | ||
| 171 | my $name = $attr->name; | ||
| 172 | |||
| 173 | next if(!$attr->does($ATTR_ROLE)); | ||
| 174 | next if(!$self->${ \"has_$name" }); | ||
| 175 | next if(!$attr->has_action('modify')); | ||
| 176 | |||
| 177 | push @attr, $attr; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | @cmd = map { $self->_set_cmd($_) } @attr or return; | ||
| 182 | |||
| 183 | # set attributes | ||
| 184 | @out = $self->_cmd(@cmd); | ||
| 185 | |||
| 186 | # update or create | ||
| 187 | @out = $self->_cmd( $new ? "create" : "update" ) or return; | ||
| 188 | |||
| 189 | if(grep { /not found/ } @out) { | ||
| 190 | $self->errstr("not found"); | ||
| 191 | return; | ||
| 192 | } | ||
| 193 | |||
| 194 | return $new ? +1 : -1; | ||
| 195 | } | ||
| 196 | |||
| 197 | around write => \&_around; | ||
| 198 | |||
| 199 | =head2 unset | ||
| 200 | |||
| 201 | $bool = $self->unset(@attributes); | ||
| 202 | |||
| 203 | Will unset values for an object in DHCP server. | ||
| 204 | |||
| 205 | =cut | ||
| 206 | |||
| 207 | sub unset { | ||
| 208 | my $self = shift; | ||
| 209 | my @attr = @_; | ||
| 210 | my(@out, $success); | ||
| 211 | |||
| 212 | @out = $self->_cmd(map { local $_ = $_; s/_/-/g; "unset $_" } @attr); | ||
| 213 | |||
| 214 | # read @out: | ||
| 215 | # ip-address = <null> | ||
| 216 | # key = value | ||
| 217 | # ... | ||
| 218 | |||
| 219 | if($success) { | ||
| 220 | $self->${ \"clear_$_" } for(@attr); | ||
| 221 | } | ||
| 222 | |||
| 223 | return 1; | ||
| 224 | } | ||
| 225 | |||
| 226 | around unset => \&_around; | ||
| 227 | |||
| 228 | =head2 remove | ||
| 229 | |||
| 230 | $bool = $self->remove; | ||
| 231 | |||
| 232 | This method will remove the object from the server. | ||
| 233 | |||
| 234 | =cut | ||
| 235 | |||
| 236 | sub remove { | ||
| 237 | my $self = shift; | ||
| 238 | my @out; | ||
| 239 | |||
| 240 | @out = $self->_open; | ||
| 241 | @out = $self->_cmd('remove'); | ||
| 242 | |||
| 243 | if(grep { /not implemented/i } @out) { | ||
| 244 | $self->errstr('not implemented'); | ||
| 245 | return; | ||
| 246 | } | ||
| 247 | if(grep { /not found/i } @out) { | ||
| 248 | $self->errstr('not found'); | ||
| 249 | return; | ||
| 250 | } | ||
| 251 | |||
| 252 | for my $attr ($self->meta->get_all_attributes) { | ||
| 253 | next unless($attr->does($ATTR_ROLE)); | ||
| 254 | my $clearer = 'clear_' .$attr->name; | ||
| 255 | $self->$clearer; | ||
| 256 | } | ||
| 257 | |||
| 258 | return 1; | ||
| 259 | } | ||
| 260 | |||
| 261 | around remove => \&_around; | ||
| 262 | |||
| 263 | # @out = $self->_open; | ||
| 264 | sub _open { | ||
| 265 | my $self = shift; | ||
| 266 | my @cmd; | ||
| 267 | |||
| 268 | for my $name ($self->meta->get_attribute_list) { | ||
| 269 | my $attr = $self->meta->get_attribute($name); | ||
| 270 | |||
| 271 | next unless($attr->does("Net::ISC::DHCPd::OMAPI::Meta::Attribute")); | ||
| 272 | next unless($attr->has_action("lookup")); | ||
| 273 | next unless($self->${ \"has_$name" }); | ||
| 274 | |||
| 275 | push @cmd, $self->_set_cmd($attr); | ||
| 276 | } | ||
| 277 | |||
| 278 | return $self->_cmd(@cmd, "open"); | ||
| 279 | } | ||
| 280 | |||
| 281 | sub _set_cmd { | ||
| 282 | my $self = shift; | ||
| 283 | my $attr = shift; | ||
| 284 | my $name = $attr->name; | ||
| 285 | my $key = $name; | ||
| 286 | my $format; | ||
| 287 | |||
| 288 | $key =~ s/_/-/g; | ||
| 289 | $format = $attr->type_constraint->equals('Str') ? 'set %s = "%s"' | ||
| 290 | : 'set %s = %s'; | ||
| 291 | |||
| 292 | return sprintf $format, $key, $self->${ \"raw_$name" }; | ||
| 293 | } | ||
| 294 | |||
| 295 | sub _around { | ||
| 296 | my $next = shift; | ||
| 297 | my $self = shift; | ||
| 298 | my $type = lc +(ref($self) =~ /::(\w+)$/)[0]; | ||
| 299 | my(@out, @ret); | ||
| 300 | |||
| 301 | $self->errstr(""); | ||
| 302 | |||
| 303 | @out = $self->_cmd("new $type") or return 0; | ||
| 304 | @ret = $self->$next(@_); | ||
| 305 | @out = $self->_cmd('close') or return 0; | ||
| 306 | |||
| 307 | return @ret == 1 ? $ret[0] : @ret; | ||
| 308 | }; | ||
| 309 | |||
| 310 | # @buffer = $self->_cmd(@cmd) | ||
| 311 | # @buffer contains one-to-one output data from @cmd | ||
| 312 | # $self->errstr is reset each time empty errstr == success | ||
| 313 | sub _cmd { | ||
| 314 | my $self = shift; | ||
| 315 | my @cmd = @_; | ||
| 316 | my(@buffer, $head); | ||
| 317 | |||
| 318 | for my $cmd (@cmd) { | ||
| 319 | my $tmp = $self->parent->_cmd($cmd); | ||
| 320 | last unless(defined $tmp); | ||
| 321 | push @buffer, $tmp; | ||
| 322 | } | ||
| 323 | |||
| 324 | if($self->parent->errstr) { | ||
| 325 | $self->errstr($self->parent->errstr); | ||
| 326 | return; | ||
| 327 | } | ||
| 328 | |||
| 329 | return @buffer; | ||
| 330 | } | ||
| 331 | |||
| 332 | =head1 COPYRIGHT & LICENSE | ||
| 333 | |||
| 334 | =head1 AUTHOR | ||
| 335 | |||
| 336 | See L<Net::ISC::DHCPd>. | ||
| 337 | |||
| 338 | =cut | ||
| 339 | |||
| 340 | 1; |