Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 1 | // Copyright (C) MongoDB, Inc. 2017-present. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | // not use this file except in compliance with the License. You may obtain |
| 5 | // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | |
| 7 | package command |
| 8 | |
| 9 | import ( |
| 10 | "context" |
| 11 | |
| 12 | "github.com/mongodb/mongo-go-driver/bson" |
| 13 | "github.com/mongodb/mongo-go-driver/x/bsonx" |
| 14 | "github.com/mongodb/mongo-go-driver/x/mongo/driver/session" |
| 15 | "github.com/mongodb/mongo-go-driver/x/network/description" |
| 16 | "github.com/mongodb/mongo-go-driver/x/network/result" |
| 17 | "github.com/mongodb/mongo-go-driver/x/network/wiremessage" |
| 18 | ) |
| 19 | |
| 20 | // ListDatabases represents the listDatabases command. |
| 21 | // |
| 22 | // The listDatabases command lists the databases in a MongoDB deployment. |
| 23 | type ListDatabases struct { |
| 24 | Clock *session.ClusterClock |
| 25 | Filter bsonx.Doc |
| 26 | Opts []bsonx.Elem |
| 27 | Session *session.Client |
| 28 | |
| 29 | result result.ListDatabases |
| 30 | err error |
| 31 | } |
| 32 | |
| 33 | // Encode will encode this command into a wire message for the given server description. |
| 34 | func (ld *ListDatabases) Encode(desc description.SelectedServer) (wiremessage.WireMessage, error) { |
| 35 | encoded, err := ld.encode(desc) |
| 36 | if err != nil { |
| 37 | return nil, err |
| 38 | } |
| 39 | return encoded.Encode(desc) |
| 40 | } |
| 41 | |
| 42 | func (ld *ListDatabases) encode(desc description.SelectedServer) (*Read, error) { |
| 43 | cmd := bsonx.Doc{{"listDatabases", bsonx.Int32(1)}} |
| 44 | |
| 45 | if ld.Filter != nil { |
| 46 | cmd = append(cmd, bsonx.Elem{"filter", bsonx.Document(ld.Filter)}) |
| 47 | } |
| 48 | cmd = append(cmd, ld.Opts...) |
| 49 | |
| 50 | return &Read{ |
| 51 | Clock: ld.Clock, |
| 52 | DB: "admin", |
| 53 | Command: cmd, |
| 54 | Session: ld.Session, |
| 55 | }, nil |
| 56 | } |
| 57 | |
| 58 | // Decode will decode the wire message using the provided server description. Errors during decoding |
| 59 | // are deferred until either the Result or Err methods are called. |
| 60 | func (ld *ListDatabases) Decode(desc description.SelectedServer, wm wiremessage.WireMessage) *ListDatabases { |
| 61 | rdr, err := (&Read{}).Decode(desc, wm).Result() |
| 62 | if err != nil { |
| 63 | ld.err = err |
| 64 | return ld |
| 65 | } |
| 66 | return ld.decode(desc, rdr) |
| 67 | } |
| 68 | |
| 69 | func (ld *ListDatabases) decode(desc description.SelectedServer, rdr bson.Raw) *ListDatabases { |
| 70 | ld.err = bson.Unmarshal(rdr, &ld.result) |
| 71 | return ld |
| 72 | } |
| 73 | |
| 74 | // Result returns the result of a decoded wire message and server description. |
| 75 | func (ld *ListDatabases) Result() (result.ListDatabases, error) { |
| 76 | if ld.err != nil { |
| 77 | return result.ListDatabases{}, ld.err |
| 78 | } |
| 79 | return ld.result, nil |
| 80 | } |
| 81 | |
| 82 | // Err returns the error set on this command. |
| 83 | func (ld *ListDatabases) Err() error { return ld.err } |
| 84 | |
| 85 | // RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter. |
| 86 | func (ld *ListDatabases) RoundTrip(ctx context.Context, desc description.SelectedServer, rw wiremessage.ReadWriter) (result.ListDatabases, error) { |
| 87 | cmd, err := ld.encode(desc) |
| 88 | if err != nil { |
| 89 | return result.ListDatabases{}, err |
| 90 | } |
| 91 | |
| 92 | rdr, err := cmd.RoundTrip(ctx, desc, rw) |
| 93 | if err != nil { |
| 94 | return result.ListDatabases{}, err |
| 95 | } |
| 96 | |
| 97 | return ld.decode(desc, rdr).Result() |
| 98 | } |