« blib/lib/Lemonldap/NG/Common/PSGI/Cli/Lib.pm
↑ Summary
blib/lib/Lemonldap/NG/Common/Conf/AccessLib.pm »
| 1 | package Lemonldap::NG::Common::Conf::Backends::MongoDB; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use utf8; | ||
| 5 | use strict; | ||
| 6 | use Lemonldap::NG::Common::Conf::Serializer; | ||
| 7 | |||
| 8 | our $VERSION = '2.22.0'; | ||
| 9 | our $initDone; | ||
| 10 | |||
| 11 | sub prereq { | ||
| 12 | my $self = shift; | ||
| 13 | unless ($initDone) { | ||
| 14 | eval "use MongoDB"; | ||
| 15 | if ($@) { | ||
| 16 | $Lemonldap::NG::Common::Conf::msg .= "Unable to load MongoDB: $@\n"; | ||
| 17 | return 0; | ||
| 18 | } | ||
| 19 | $self->{dbName} ||= 'llConfDB'; | ||
| 20 | $self->{collectionName} ||= 'configuration'; | ||
| 21 | $initDone++; | ||
| 22 | } | ||
| 23 | 1; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub Lemonldap::NG::Common::Conf::_mongoDB { | ||
| 27 | my $self = shift; | ||
| 28 | return $self->{_mongoDB} if ( $self->{_mongoDB} ); | ||
| 29 | my $conn_args = {}; | ||
| 30 | foreach my $w ( | ||
| 31 | qw(host auth_mechanism auth_mechanism_properties bson_codec connect_timeout_ms db_name heartbeat_frequency_ms j local_threshold_ms max_time_ms password port read_pref_mode read_pref_tag_sets replica_set_name server_selection_timeout_ms server_selection_try_once socket_check_interval_ms socket_timeout_ms ssl username w wtimeout read_concern_level) | ||
| 32 | ) | ||
| 33 | { | ||
| 34 | $conn_args->{$w} = $self->{$w} if ( defined $self->{$w} ); | ||
| 35 | } | ||
| 36 | return $self->{_mongoDB} = | ||
| 37 | MongoDB::MongoClient->new($conn_args)->get_database( $self->{dbName} ); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub Lemonldap::NG::Common::Conf::_mongoColl { | ||
| 41 | my $self = shift; | ||
| 42 | return $self->{_coll} if ( $self->{_coll} ); | ||
| 43 | return $self->{_coll} = | ||
| 44 | $self->_mongoDB->get_collection( $self->{collectionName} ); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub Lemonldap::NG::Common::Conf::run_command { | ||
| 48 | my $self = shift; | ||
| 49 | $self->_mongoDB->run_command(@_); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub available { | ||
| 53 | my $self = shift; | ||
| 54 | my $c = $self->_mongoColl or return 0; | ||
| 55 | my $res = $self->run_command( | ||
| 56 | [ distinct => $self->{collectionName}, key => '_id' ] ); | ||
| 57 | unless ( ref($res) ) { | ||
| 58 | $Lemonldap::NG::Common::Conf::msg .= $res; | ||
| 59 | return (); | ||
| 60 | } | ||
| 61 | return sort { $a <=> $b } @{ $res->{values} } if ( $res->{ok} ); | ||
| 62 | return (); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub lastCfg { | ||
| 66 | my $self = shift; | ||
| 67 | my @avail = $self->available; | ||
| 68 | return $avail[$#avail]; | ||
| 69 | } | ||
| 70 | sub lock { 1 } | ||
| 71 | sub isLocked { 0 } | ||
| 72 | sub unlock { 1 } | ||
| 73 | |||
| 74 | sub store { | ||
| 75 | my ( $self, $fields ) = @_; | ||
| 76 | $fields = $self->serialize($fields); | ||
| 77 | $fields->{_id} = $fields->{cfgNum}; | ||
| 78 | my $res = eval { $self->_mongoColl->insert_one($fields) }; | ||
| 79 | if ($@) { | ||
| 80 | $Lemonldap::NG::Common::Conf::msg .= "Unable to store conf: $@\n"; | ||
| 81 | return 0; | ||
| 82 | } | ||
| 83 | return $fields->{cfgNum}; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub load { | ||
| 87 | my ( $self, $cfgNum, $fields ) = @_; | ||
| 88 | my $res = | ||
| 89 | $self->unserialize( $self->_mongoColl->find_one( { _id => $cfgNum } ) ); | ||
| 90 | return $res; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub delete { | ||
| 94 | my ( $self, $cfgNum ) = @_; | ||
| 95 | die "cfgNum required" unless ($cfgNum); | ||
| 96 | $self->_mongoColl->delete_one( { _id => $cfgNum } ); | ||
| 97 | } | ||
| 98 | |||
| 99 | 1; |