| 1 | package Net::FreeIPA::API; | ||
| 2 | $Net::FreeIPA::API::VERSION = '3.0.3'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Net::FreeIPA::API::Convert qw(process_args); | ||
| 7 | use Net::FreeIPA::API::Magic qw(retrieve); | ||
| 8 | |||
| 9 | our $AUTOLOAD; | ||
| 10 | |||
| 11 | use Readonly; | ||
| 12 | |||
| 13 | Readonly our $API_METHOD_PREFIX => 'api_'; | ||
| 14 | |||
| 15 | # This will add all AUTOLOADable functions as methods calls | ||
| 16 | # So only AUTOLOAD method with command name prefixed | ||
| 17 | # with api_, returns a C<$api_method> call | ||
| 18 | |||
| 19 | sub AUTOLOAD | ||
| 20 | { | ||
| 21 | my $called = $AUTOLOAD; | ||
| 22 | |||
| 23 | # Don't mess with garbage collection! | ||
| 24 | return if $called =~ m{DESTROY}; | ||
| 25 | |||
| 26 | my $called_orig = $called; | ||
| 27 | $called =~ s{^.*::}{}; | ||
| 28 | |||
| 29 | my ($cmd, $fail); | ||
| 30 | my $api_pattern = "^$API_METHOD_PREFIX"; | ||
| 31 | if ($called =~ m/$api_pattern/) { | ||
| 32 | $called =~ s{$api_pattern}{}; | ||
| 33 | |||
| 34 | ($cmd, $fail) = retrieve($called); | ||
| 35 | } else { | ||
| 36 | $fail = "only $API_METHOD_PREFIX methods supported"; | ||
| 37 | } | ||
| 38 | |||
| 39 | if ($fail) { | ||
| 40 | die "Unknown Net::FreeIPA::API method: $called failed $fail (from original $called_orig)"; | ||
| 41 | } else { | ||
| 42 | # Run the expected method. | ||
| 43 | # AUTOLOAD with glob assignment and goto defines the autoloaded method | ||
| 44 | # (so they are only autoloaded once when they are first called), | ||
| 45 | # but that breaks inheritance. | ||
| 46 | |||
| 47 | # The method name is in the name attribute | ||
| 48 | my ($self, @args) = @_; | ||
| 49 | return $self->rpc(process_args($cmd, @args)); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | 1; |