@@ -17,6 +17,8 @@ package util
17
17
18
18
import (
19
19
"context"
20
+ "fmt"
21
+ "os"
20
22
21
23
"go.mongodb.org/mongo-driver/bson"
22
24
"go.mongodb.org/mongo-driver/mongo"
@@ -29,6 +31,7 @@ const (
29
31
ErrNotYetInitialized = int32 (94 )
30
32
ErrNoReplicationEnabled = int32 (76 )
31
33
ErrNotPrimaryOrSecondary = int32 (13436 )
34
+ ErrNotUnauthorized = int32 (13 )
32
35
)
33
36
34
37
// MyState returns the replica set and the instance's state if available.
@@ -37,6 +40,12 @@ func MyState(ctx context.Context, client *mongo.Client) (string, int, error) {
37
40
38
41
err := client .Database ("admin" ).RunCommand (ctx , bson.M {"replSetGetStatus" : 1 }).Decode (& status )
39
42
if err != nil {
43
+ if e , ok := err .(mongo.CommandError ); ok {
44
+ if e .Code == ErrNotUnauthorized {
45
+ fmt .Fprintf (os .Stderr , "unauthorized to run command replSetGetStatus: %v\n " , err )
46
+ os .Exit (1 )
47
+ }
48
+ }
40
49
return "" , 0 , err
41
50
}
42
51
@@ -48,6 +57,12 @@ func MyRole(ctx context.Context, client *mongo.Client) (*proto.HelloResponse, er
48
57
var role proto.HelloResponse
49
58
err := client .Database ("admin" ).RunCommand (ctx , bson.M {"isMaster" : 1 }).Decode (& role )
50
59
if err != nil {
60
+ if e , ok := err .(mongo.CommandError ); ok {
61
+ if e .Code == ErrNotUnauthorized {
62
+ fmt .Fprintf (os .Stderr , "unauthorized to run command isMaster: %v\n " , err )
63
+ os .Exit (1 )
64
+ }
65
+ }
51
66
return nil , err
52
67
}
53
68
@@ -57,6 +72,12 @@ func MyRole(ctx context.Context, client *mongo.Client) (*proto.HelloResponse, er
57
72
func ReplicasetConfig (ctx context.Context , client * mongo.Client ) (* proto.ReplicasetConfig , error ) {
58
73
var rs proto.ReplicasetConfig
59
74
if err := client .Database ("admin" ).RunCommand (ctx , bson.M {"replSetGetConfig" : 1 }).Decode (& rs ); err != nil {
75
+ if e , ok := err .(mongo.CommandError ); ok {
76
+ if e .Code == ErrNotUnauthorized {
77
+ fmt .Fprintf (os .Stderr , "unauthorized to run command replSetGetConfig: %v\n " , err )
78
+ os .Exit (1 )
79
+ }
80
+ }
60
81
return nil , err
61
82
}
62
83
0 commit comments