MongoDB.pm 2.69 KB
Newer Older
Yadd's avatar
Yadd committed
1
package Lemonldap::NG::Common::Conf::Backends::MongoDB;
Yadd's avatar
Yadd committed
2
3

use 5.010;
Yadd's avatar
Yadd committed
4
use utf8;
Yadd's avatar
Yadd committed
5
use strict;
Yadd's avatar
Yadd committed
6
use Lemonldap::NG::Common::Conf::Serializer;
Yadd's avatar
Yadd committed
7

Yadd's avatar
Yadd committed
8
our $VERSION = '2.0.0';
Yadd's avatar
Yadd committed
9
10
11
12
13
14
15
16
17
18
our $initDone;

sub prereq {
    my $self = shift;
    unless ($initDone) {
        eval "use MongoDB";
        if ($@) {
            $Lemonldap::NG::Common::Conf::msg .= "Unable to load MongoDB: $@\n";
            return 0;
        }
Clément OUDOT's avatar
Clément OUDOT committed
19
        $self->{dbName}         ||= 'llConfDB';
Yadd's avatar
Yadd committed
20
21
22
23
24
25
26
27
28
29
        $self->{collectionName} ||= 'configuration';
        $initDone++;
    }
    1;
}

sub Lemonldap::NG::Common::Conf::_mongoDB {
    my $self = shift;
    return $self->{_mongoDB} if ( $self->{_mongoDB} );
    my $conn_args = {};
Yadd's avatar
Yadd committed
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)
Yadd's avatar
Yadd committed
32
33
34
      )
    {
        $conn_args->{$w} = $self->{$w} if ( defined $self->{$w} );
Yadd's avatar
Yadd committed
35
    }
Clément OUDOT's avatar
Clément OUDOT committed
36
37
    return $self->{_mongoDB} =
      MongoDB::MongoClient->new($conn_args)->get_database( $self->{dbName} );
Yadd's avatar
Yadd committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
}

sub Lemonldap::NG::Common::Conf::_mongoColl {
    my $self = shift;
    return $self->{_coll} if ( $self->{_coll} );
    return $self->{_coll} =
      $self->_mongoDB->get_collection( $self->{collectionName} );
}

sub Lemonldap::NG::Common::Conf::run_command {
    my $self = shift;
    $self->_mongoDB->run_command(@_);
}

sub available {
    my $self = shift;
    my $c    = $self->_mongoColl or return 0;
Clément OUDOT's avatar
Clément OUDOT committed
55
56
57
    my $res  = $self->run_command(
        [ distinct => $self->{collectionName}, key => '_id' ] );
    unless ( ref($res) ) {
Yadd's avatar
Yadd committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
        $Lemonldap::NG::Common::Conf::msg .= $res;
        return ();
    }
    return sort { $a <=> $b } @{ $res->{values} } if ( $res->{ok} );
    return ();
}

sub lastCfg {
    my $self  = shift;
    my @avail = $self->available;
    return $avail[$#avail];
}
sub lock     { 1 }
sub isLocked { 0 }
sub unlock   { 1 }

sub store {
    my ( $self, $fields ) = @_;
Yadd's avatar
Yadd committed
76
77
    $fields = $self->serialize($fields);
    $fields->{_id} = $fields->{cfgNum};
Yadd's avatar
Yadd committed
78
    my $res = eval { $self->_mongoColl->insert_one($fields) };
Clément OUDOT's avatar
Clément OUDOT committed
79
    if ($@) {
Yadd's avatar
Yadd committed
80
81
82
        $Lemonldap::NG::Common::Conf::msg .= "Unable to store conf: $@\n";
        return 0;
    }
83
    return $fields->{cfgNum};
Yadd's avatar
Yadd committed
84
85
86
87
}

sub load {
    my ( $self, $cfgNum, $fields ) = @_;
Yadd's avatar
Yadd committed
88
89
90
    my $res =
      $self->unserialize( $self->_mongoColl->find_one( { _id => $cfgNum } ) );
    return $res;
Yadd's avatar
Yadd committed
91
92
93
94
}

sub delete {
    my ( $self, $cfgNum ) = @_;
Clément OUDOT's avatar
Clément OUDOT committed
95
    die "cfgNum required" unless ($cfgNum);
Yadd's avatar
Yadd committed
96
97
98
99
    $self->_mongoColl->remove( { _id => $cfgNum } );
}

1;